Arithmetic Expressions

An expression is any mathematical expression that evaluates to some value, the result of which may be assigned to a variable or just used directly. ex: 1+2, 3-4

The Mathematical Operators are:

+ Addition, adds two numbers or concatenates two strings together. ex:
1+23
"a" + "b""ab"
- Subtraction: a-b = subtraction of b from a.
* Multiply: a*b = multiplies a by b
/ Divide: a/b = divides a by b
% Modulus: Remainder from integer division: a%b = remainder from a/b
(expr) Evaluates the expression outside of the rest of the expression, forcing order of evaluation:
5 * 2 + 3 == 13; 5 * ( 2 + 3 ) == 25;

If either side of an addition operator (+) is a string, then the side that is not a string is converted into one and string concatenation is performed, so be careful to convert strings into numbers with parseInt or parseFloat before attempting to do numeric addition.

Incrementing and decrementing a numeric value by one happens so often in programming there is a special operation for it.

++   Increment a variable by one.
--   decrement a variable by one.

The ++ and -- operations must be associated with a variable:

var a = 10; // a is assigned the value 10. a++; // Increments a by one, a now is 11. ++a; // Also increments a by one, a is now 12.

There is a difference in which side of the variable we use ++ / -- on if we want the value of the variable.

var x = ++a; // Pre-increment: a is incremented, then the new value is given to x. var x = a++; // The value of a is given to x first, then a is incremented.