JavaScript Operators
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic on numbers:
Operator Description + Addition - Subtraction * Multiplication ** Exponentiation (ES2016) / Division % Modulus (Division Remainder) ++ Increment -- Decrement
<script>
let a = 100; let b = 50; let x = a + b;
// let x = (100 + 50) * a;
document.write(x);
</script>
Comparison Operators
Comparison and Logical operators are used to test for true
or false
.
Given that x = 5
, the table below explains the comparison operators:
Operator | Description | Comparing | Returns | |
---|---|---|---|---|
== | equal to | x == 8 | false | |
x == 5 | true | |||
x == "5" | true | |||
=== | equal value and equal type | x === 5 | true | |
x === "5" | false | |||
!= | not equal | x != 8 | true | |
!== | not equal value or not equal type | x !== 5 | false | |
x !== "5" | true | |||
x !== 8 | true | |||
> | greater than | x > 8 | false | |
< | less than | x < 8 | true | |
>= | greater than or equal to | x >= 8 | false | |
<= | less than or equal to | x <= 8 | true |
JavaScript Assignment Operators
Assignment operators assign values to JavaScript variables.
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
**= | x **= y | x = x ** y |
The addition assignment operator (+=
) adds a value to a variable.
Logical Operators
Logical operators are used to determine the logic between variables or values.Given that x = 6
and y = 3
, the table below explains the logical operators:
Operator | Description | Example | |
---|---|---|---|
&& | and | (x < 10 && y > 1) is true | |
|| | or | (x == 5 || y == 5) is false | |
! | not | !(x == y) is true |
JavaScript Operator Precedence Values
Value | Operator | Description | Example |
---|---|---|---|
21 | ( ) | Expression grouping | (3 + 4) |
20 | . | Member | person.name |
20 | [] | Member | person["name"] |
20 | () | Function call | myFunction() |
20 | new | Create | new Date() |
18 | ++ | Postfix Increment | i++ |
18 | -- | Postfix Decrement | i-- |
17 | ++ | Prefix Increment | ++i |
17 | -- | Prefix Decrement | --i |
17 | ! | Logical not | !(x==y) |
17 | typeof | Type | typeof x |
16 | ** | Exponentiation (ES2016) | 10 ** 2 |
15 | * | Multiplication | 10 * 5 |
15 | / | Division | 10 / 5 |
15 | % | Division Remainder | 10 % 5 |
14 | + | Addition | 10 + 5 |
14 | - | Subtraction | 10 - 5 |
13 | << | Shift left | x << 2 |
13 | >> | Shift right | x >> 2 |
13 | >>> | Shift right (unsigned) | x >>> 2 |
12 | < | Less than | x < y |
12 | <= | Less than or equal | x <= y |
12 | > | Greater than | x > y |
12 | >= | Greater than or equal | x >= y |
12 | in | Property in Object | "PI" in Math |
12 | instanceof | Instance of Object | instanceof Array |
11 | == | Equal | x == y |
11 | === | Strict equal | x === y |
11 | != | Unequal | x != y |
11 | !== | Strict unequal | x !== y |
10 | & | Bitwise AND | x & y |
9 | ^ | Bitwise XOR | x ^ y |
8 | | | Bitwise OR | x | y |
7 | && | Logical AND | x && y |
6 | || | Logical OR | x || y |
5 | ?? | Nullish Coalescing | x ?? y |
4 | ? : | Condition | ? "Yes" : "No" |
3 | += | Assignment | x += y |
3 | /= | Assignment | x /= y |
3 | -= | Assignment | x -= y |
3 | *= | Assignment | x *= y |
3 | %= | Assignment | x %= y |
3 | <<= | Assignment | x <<= y |
3 | >>= | Assignment | x >>= y |
3 | >>>= | Assignment | x >>>= y |
3 | &= | Assignment | x &= y |
3 | ^= | Assignment | x ^= y |
3 | |= | Assignment | x |= y |
2 | yield | Pause Function | yield x |
1 | , | Comma | 5 , 6 |
Expressions in parentheses are fully computed before the value is used in the rest of the expression.