MOCKSTACKS
EN

# Javascript Addition( + ) & Subtraction( - )

``````var a = 9,
b = 3,
c = a + b;``````

### Output

c will now be 12

This operand can also be used multiple times in a single assignment:

``````var a = 9,
b = 3,
c = 8,
d = a + b + c;``````

### Output

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; ``

### Output

"1231" (not 124)

If a boolean value is given in place of any of the number values, the boolean value is converted to a number (0 for
false, 1 for true) before the sum is calculated:

``````true + 1; // 2
false + 5; // 5
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"``````

### Subtraction (-)

The subtraction operator (-) subtracts numbers.

``````var a = 9;
var b = 3;
var c = a - b;``````

### Output

c will now be 6

If a string or boolean is provided in place of a number value, it gets converted to a number before the difference is calculated (0 for false, 1 for true):

``````"5" - 1; // 4
7 - "3"; // 4
"5" - true; // 4``````

If the string value cannot be converted into a Number, the result will be NaN:

``````"foo" - 1; // NaN
100 - "bar"; // NaN``````

## Conclusion

In this page (written and validated by ) you learned about Javascript Addition and Subtraction . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Multiplication and Division.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.