JavaScript: Numbers
When you're working with numbers, it's important to understand addition versus concatenation. Addition gives you the sum of two numbers, whereas concatenation just joins or "sandwiches" the two values together.
For example, here's addition:
var x = 10;
var y = 5;
console.log (x + y)
The result would be 15.
However, if one of the values is a string rather than a regular number, the result will be concatenated instead of added. A string is identified by quotations; numbers do not have quotations.
var x = "10";
var y = 5;
console.log (x + y)
The result would be 105. The two values get joined together.
Not a Number (NaN)
If you try to multiply a string by a number, the result will be NaN, which means "Not a Number."
You would see NaN if you tried to run something like this:
Sponsored
var x = "Touchdown!";
var y = 5;
console.log (x * y);
The result would be NaN, because 5 times a touchdown doesn't yield a number.
The Math Object
JavaScript has a built-in Math object that you can use to do various things with numbers. For example,
var x = 5.8;
//round the value of x
var y = Math.round(x);
This will round the value of the x variable, resulting in 6.
Other methods you can apply to the Math object include .max, .min, .random.
About Tom Johnson
I'm an API technical writer based in the Seattle area. On this blog, I write about topics related to technical writing and communication — such as software documentation, API documentation, AI, information architecture, content strategy, writing processes, agent skills, tech comm careers, and more. Check out my AI courses for more on the latest in AI and tech comm. If you're looking for info about documenting APIs, see my API documentation course.
If you're a technical writer and want to keep on top of the latest trends in tech comm, be sure to subscribe to email updates below. You can also learn more about me or contact me.
Finally, I want to note a couple of things. I usually write the first drafts of my posts myself and then use AI to improve accuracy, clarity, and detail in places, so my writing is a mix of human and AI. (Hopefully that's not too off-putting.) Also, the opinions I express on my blog are my own perspectives, not those of my employer.