Addition ( + )

Subtraction ( - )

Navigation Aids -- This Page | Navigation Aids -- This Topic | Navigation Aids -- This Site | ||
---|---|---|---|---|

- IEEE Standard for Binary Floating-Point Arithmetic (Std. 754-1985)
- addition operator
- subtraction operator
- number operand
- string operand
- precedence
- associativity
- binary operator
- NaN
- infinity
- Synonyms:
- Arithmetic Operators: Additive operators are a sub-set of arithmetic operators. Arithmetic Operators includes both the additive and multiplicative operators. Note that the additive operators have a lower precedence than the multiplicative operators.

This page will show the basic rules for JavaScript additive operator group. In addition, type conversions are discussed where applicable. Some special situation cases are explained. And finally, some scripting examples are provided with comments.

- The additive operators are binary operators. They have a "left to right" associatively and a precedence of 12.
- The addition operator is considered "overloaded" in OOP terms. It performs a different action depending on the content of its operands.
- The addition operator performs string concatenation when one or both operands are typeof "String".
- The addition operator performs addition when both operands are numeric and produces the sum of the two operands.
- The subtraction operator performs subtraction of two numeric operands producing the difference of the two numeric operands. The left operand is the minuend and the right operand is the subtrahend.
- When an expression contains multiple additive operators, the "left to right" associatively will handle each operator individually. Consider the following expression: (1 + 2 + 3 + "string"). The first two operators perform summations while the last operator performs the concatenation.
- With the subtraction operator, a - b is always the same as a + (-b). In other words, first you negate b and then add a and b.

- When one operand is a string and the other is a numeric, the additive operator performs the toString function on the numeric operand and then concatenates the two operands.

- Addition:
- The result is NaN when either number is NaN.
- The result is infinity when infinity is added to infinity.
- The result is -infinity when -infinity is added to -infinity.
- The result is NaN with two infinities of opposite signs.
- The result is infinity when one number is infinite and the other in finite.
- The result is +0 when +0 is added to +0.
- The result is +0 when -0 is added to +0.
- The result is -0 when -0 is added to -0.
- The sum of a zero and nonzero finite number is equal to the nonzero finite number.

- Subtraction:
- The result is NaN when either operand is not a number.
- The result is NaN when either number is NaN.
- The result is +0 when +0 is subtracted from +0.
- The result is -0 when -0 is subtracted from +0.
- The result is +0 when -0 is subtracted from -0.
- The result is NaN when infinity is subtracted from infinity.
- The result is NaN when -infinity is subtracted from -infinity.
- The result is -infinity when infinity is subtracted from -infinity.
- The result is infinity when -infinity is subtracted from infinity.

Expression | Result | Comment |
---|---|---|

10 + 10 | 20 | add two numbers and sum is 20 |

"to" + "tal" | "total" | concatenate two strings and result is "total" |

"to" + 2 | "to2" | convert 2 to string "2" and concatenates the two strings |

"5" + 2 | "52" | convert 2 to string "2" and concatenates the two strings |

10 - 8 | 2 | subtract two numbers and difference is 2 |

10 - (-8) | 18 | same as: 10 + (+8); negate the 2nd operand and add the two numbers |