Arithmetic operatorsS2C Home « Operators « Arithmetic operators
The javascript arithmetic operators are used to return a single numerical value from other numerical literals or variables.
Description
Arithmetic operators allow us to apply basic maths to a set of operands.
- Operands can be numerical or string literals that evaluate to numeric.
- Division by zero produces
+Infinity
or-Infinity
dependant upon operand signage.
Basic Operators
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | var a = 10 + 5; var a = 10 + '5'; |
a = 15 a = 105 (string concatenation) |
- | Subtraction | var a = 10 - '5'; |
a = 5 |
* | Mutiplication | var a = 10 * '5'; |
a = 50 |
/ | Division | var a = 10 / '5'; |
a = 2 |
% | Modulus | var a = 10 % '5'; var a = 12 % '5'; |
a = 0 (integer remainder)a = 2 (integer remainder) |
Increment and Decrement Operators
Operator | Description | Example | Result |
---|---|---|---|
var++ | Post Increment | var a = 0, b = 5; var a = b++; |
a = 5 and b =6. |
++var | Pre Increment | var a = 0, b = 5; var a = ++b; |
a = 6 and b = 6. |
var-- | Post Decrement | var a = 0, b = 5; var a = b--; |
a = 5 and b = 4. |
--var | Pre Decrement | var a = 0, b = 5; var a = --b; |
a = 4 and b = 4. |
Unary Operators
Operator | Description | Example | Result |
---|---|---|---|
unary+ | Plus | var a = -5, b = 5; var a = +b; |
a = 5 and b = 5. |
unary- | Minus | var a = 5, b = 5; var a = -b; |
a = -5 and b = 5. |
Related Tutorials
JavaScript Basic Tutorials - Lesson 5 - Basic Maths Functions