JavaScript Introduction | Operators | Arithmetic Operators
Arithmetic Operators
Arithmetic operators are the most basic and frequently used operators for handling the four arithmetic operations.
All arithmetic operators are binary operators with two operands, and their associativity is from left to right.
| Arithmetic Operator | Description |
|---|---|
| + | Adds the value of the right operand to the value of the left operand. |
| - | Subtracts the value of the right operand from the value of the left operand. |
| * | Multiplies the value of the left operand by the value of the right operand. |
| / | Divides the value of the left operand by the value of the right operand. |
| % | Divides the value of the left operand by the value of the right operand and returns the remainder. |
var x = 10, y = 4;
document.write(x + y + "<br>"); // 14
document.write(x - y + "<br>"); // 6
document.write(x * y + "<br>"); // 40
document.write(x / y + "<br>"); // 2.5
document.write(x % y); // 2
JavaScript Operator Precedence Table
In JavaScript, operator precedence and associativity are as follows.
| Precedence | Operator | Description | Associativity |
|---|---|---|---|
| 1 | () | Grouping (parentheses) | - |
| 2 | . | Member access | left to right |
| new | Object creation with arguments | - | |
| 3 | () | Function call | left to right |
| new | Object creation without arguments | right to left | |
| 4 | ++ | Postfix increment operator | - |
| – | Postfix decrement operator | - | |
| 5 | ! | Logical NOT operator | right to left |
| ~ | Bitwise NOT operator | right to left | |
| + | Positive sign (unary operator) | right to left | |
| - | Negative sign (unary operator) | right to left | |
| ++ | Prefix increment operator | right to left | |
| – | Prefix decrement operator | right to left | |
| typeof | Type return | right to left | |
| void | Return undefined | right to left | |
| delete | Remove a property | right to left | |
| 6 | ** | Exponentiation operator | right to left |
| * | Multiplication operator | left to right | |
| / | Division operator | left to right | |
| % | Remainder operator | left to right | |
| 7 | + | Addition operator (binary operator) | left to right |
| - | Subtraction operator (binary operator) | left to right | |
| 8 | « | Bitwise left shift operator | left to right |
| » | Bitwise right shift with sign extension | left to right | |
| »> | Bitwise right shift without sign extension | left to right | |
| 9 | < | Relational operator (less than) | left to right |
| <= | Relational operator (less than or equal to) | left to right | |
| > | Relational operator (greater than) | left to right | |
| >= | Relational operator (greater than or equal to) | left to right | |
| instanceof | Determines whether a value is an instance | left to right | |
| 10 | == | Equality operator | left to right |
| === | Strict equality operator | left to right | |
| != | Inequality operator | left to right | |
| !== | Strict inequality operator | left to right | |
| 11 | & | Bitwise AND operator | left to right |
| 12 | ^ | Bitwise XOR operator | left to right |
| 13 | | | Bitwise OR operator | left to right |
| 14 | && | Logical AND operator | left to right |
| 15 | || | Logical OR operator | left to right |
| 16 | ? : | Ternary operator | right to left |
| 17 | = | Assignment operator (=, +=, -=, *=, /=, %=, «=, »=, »>=, &=, ^=, |=) | right to left |
| 18 | … | Spread | - |
| 19 | , | Comma operator | left to right |
Operators with higher precedence in the table above are executed first.
When two or more operators have the same precedence, the execution order is determined by associativity.