Operators in JavaScript Revealed - Addition (Part 1)

by:

Share:  

A quick tour on how addition operators work in the JavaScript Language (Part 1)



Operators in JavaScript (Part 1)

In JavaScript, operators are paramount to your daily development. In the first installment in this series, we will look at how the addition operator behaves in JavaScript and the quirks you should be aware of.

So, let's start adding!

Addition Operator

The Addition operator + is useful for numeric calculations but also quite adept at string concatenation. Let's see a few examples. In JavaScript, it's essential to understand how the operator works as many programming languages have different behaviour.

In this first example, we will look into how to add two whole integers:

var first = 10;
var second = 20;
var sum = first + second;
console.log(total); // 30

When two floating point numbers are added, JavaScript is less predictable as we start noticing rounding errors.

One way to solve this is to make use of the Number.toFixed() method. The argument to the toFixed method dictates the decimal precision of the number.

var first = 1.1;
var second = 2.2;
var sum = first + second;
console.log(sum); // 3.3000000000000003
console.log(sum.toFixed(2)); // 3.30

We can also use the + addition operator as an overloaded operator to concatenate strings together:

var first = "Hello";
var second = " World";
var third = " Again!";
var sentence = first + second + third;
console.log(sentence); // Hello World Again!

If we add a string to number together using the addition operator, we see some interesting behaviour in that the number gets implicitly converted to a string:

var first = "The meaning of life is ";
var second = 42;
var sentence = first + second;
console.log(second); // The meaning of life is 42

If we add two strings, both containing numbers in them, we get string concatenation behaviour rather than mathematical behaviour:

var first = "99";
var second = "1";
var sentence = first + second;
console.log(sentence); // 991

If we add a number to an undefined value, we notice a very peculiar behaviour:

var first = 400;
var second = undefined;
var sum = first + second;
console.log(sum); // NaN

The above has an important fact, if we add any number to an undefined value, the result will be a NaN or Not a Number.

If we add a string to an undefined value, we will get a string concatenation again:

var first = "Game";
var second = undefined;
var sum = first + second;
console.log(sum); // Gameundefined

If we add a number to a null value, the null being an empty object, is converted to a 0 as it's a falsy value.

var first = 99;
var second = null;
var sum = first + second;
console.log(sum); // 99

If we add a string to a null value, again string concatenation takes precedence:

var first = "Game";
var second = null;
var sum = first + second;
console.log(sum): // Gamenull

If we add any number to a NaN, the result will always be a NaN:

var first = 42;
var second = NaN;
var sum = first + second;
console.log(sum); // NaN

Finally if we add any string to a NaN, string concatenation will occur:

var first = "Game";
var second = NaN;
var sum = first + second;
console.log(sum); // GameNaN

Do you use the addition operator in JavaScript or want to suggest an improvement to this article? Please share it with us by leaving a reply below.

Stay tuned for Part 2 where we will talk about the subtraction operator.

Continue to


About the Author

Alvin Luchmun
Alvin Luchmun is a freelance developer, trainer and co-founder at and currently lives in London. Alvin is passionate about anything tech/biz related but is also a firm believer in agile clean code, he loves preaching about code to fellow humanoids!.

Leave a comment