Addition

suggest change

The addition operator (+) adds numbers.

const a = 9; const b = 3; const c = a + b;

c will now be 12

The operator can be used more than once in a single expression:

const a = 9;
const b = 3;
const c = 8;
const d = a + b + c;

d will now be 20.

Both operands are converted to primitive types. Then, if either one is a string, they’re both converted to strings and concatenated. Otherwise, they’re both converted to numbers and added.

null + null;      // 0
null + undefined; // NaN
null + {};        // "null[object Object]"
null + '';        // "null"

If the operands are a string and a number, the number is converted to a string and then they’re concatenated, which may lead to unexpected results when working with strings that look numeric.

"123" + 1;        // "1231" (not 124)

If a boolean is used where a number is expected, false converts to 0 and true converts to 1:

true + 1;         // 2
false + 5;        // 5

null converts to 0 in numeric addition. undefined converts to NaN:

null + 1;         // 1
undefined + 1;    // NaN

If a boolean value is given alongside a string value, the boolean value is converted to a string instead:

true + "1";        // "true1"
false + "bar";     // "falsebar"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents