Java Script Arithmetic Operators
By Stephen Bucaro
Java Script arithmetic operations are represented by normal mathematical notation as shown below.
var n = 13 + 24;
var n = 24 - 13;
var n = 12 * 5;
var n = 12 / 4;
In each case above, the result of the arithmetic operation is assigned to the variable n.
There are several mathematical operators that you may not be familiar with.
The negation operator (-) returns the same result as multiplying the value by -1.
var m = 5;
var n = -n;
In the example above, a value of -5 is assigned to the variable n.
The modulus operator (%) returns the remainder of a division rather than the quotient.
var n = 13 % 5;
In the example above, a value of 3 is assigned to the variable n.
Increment and Decrement
The increment operator (++) increases a value by one.
var n = 3++;
In the example above, a value of 4 is assigned to the variable n.
The decrement operator (--) decreases a value by one.
var n = 3--;
In the example above, a value of 2 is assigned to the variable n.