|addition||+||x + y||Sum of x and y|
|subtraction||–||x – y||Difference of x and y|
|multiplication||*||x * y||Product of x and y|
|division||/||x/y||Quotient of x and y|
|modulo||%||x%y||Remainder of x / y|
|exponentiation||**||x**y||x to the y power|
|increment||++||x++/++x||x plus one|
|decrement||—||x–/–x||x minus one|
const myInt = 15; const myFloat = 6,667; console.log(typeof myInt); //number console.log(typeof myFloat); // number
An operand is what operators are applied to. For instance, in the addition of 99 + 1 there are two operands: the left operand is 99 and the right operand is 1.
There are two types of operators, as follows:
- An operator is unary if it has a single operand. For example, the unary incrementor (++) adds 1 to a number.
- An operator is binary if it has two operands. In the 99 + 1 example above, the + is a binary operator because it goes between two values.
There’s a demo of the above script in codepen.
Operator precedence describes the order in which operations are performed in an arithmetic expression. Just as you learned in grade school math, multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (–), meaning those calculations get performed first. Hence, 10 + 4 / 2 would be equal to 12 and not 7. To override the default precedence, we can enclose the operations that we want performed first within parentheses, as in (10 + 4) / 2. Operations inside the parentheses are computed first, going from the innermost on outwards. Meanwhile, multiple operations with the same precedence (like addition and subtraction) are computed from left to right. Got all that? Now, here is a test:
(3 * (10 / (6 – 4))) + 2 = ?
(3 * (10 / 2)) + 2 (3 * 5) + 2 15 + 2 17