JavaScript Operators Reference
JavaScript Operators
JavaScript Operators are used to assign values, compare values, perform arithmetic operations, and much more.
Type | Common Use | Example |
---|---|---|
Assignment | Assigns values to variables | x = 5 |
Arithmetic | Performs arithmetic between variables | x = y + 2 |
Logical | Defines the logic between variables | (x>0 || x>0) |
Misc | Miscellaneous operators | void(0) |
JavaScript Assignment Operators
Assignment operators assign values to JavaScript variables.
Oper | Example | Same As | Result | |
---|---|---|---|---|
= | x = y | x = y | x = 5 | Try it » |
+= | x += y | x = x + y | x = 15 | Try it » |
-= | x -= y | x = x - y | x = 5 | Try it » |
*= | x *= y | x = x * y | x = 50 | Try it » |
/= | x /= y | x = x / y | x = 2 | Try it » |
%= | x %= y | x = x % y | x = 0 | Try it » |
: | x: 45 | size.x = 45 | x = 45 | Try it » |
Logical Assignment Operators
Oper | Example | Result | |
---|---|---|---|
&&= | true &&= 10 | x = 10 | Try it » |
||= | false ||= 10 | x = 10 | Try it » |
??= | null ??= 10 | x = 10 | Try it » |
The Conditional (Ternary) Operator
The conditional operator assigns a value to a variable based on a condition.
Syntax | Example | |
---|---|---|
(condition) ? x : y | (age < 18) ? false : true | Try it » |
The Spread (...) Operator
The ...
operator splits iterables into individual elements.
Syntax | Example | |
---|---|---|
const myArr = [1, 2, 3]; | let xMin = Math.min(...myArr); | Try it » |
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y = 5, the table below explains the arithmetic operators:
Oper | Name | Example | Results | Try it |
---|---|---|---|---|
+ | Addition | x = y + 2 | y=5, x=7 | Try it » |
++ | Increment | x = y++ | y=6, x=5 | Try it » |
++ | Increment | x = ++y | y=6, x=6 | Try it » |
- | Subtraction | x = y - 2 | y=5, x=3 | Try it » |
-- | Decrement | x = y-- | y=4, x=5 | Try it » |
-- | Decrement | x = --y | y=4, x=4 | Try it » |
* | Multiplication | x=y*2 | y=5, x=10 | Try it » |
** | Exponentiation | x = y ** 2 | y=5, x=25 | Try it » |
/ | Division | x = y / 2 | y=5, x=2.5 | Try it » |
% | Remainder | x = y % 2 | y=5, x=1 | Try it » |
Javascript Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x = 5, the table below explains the comparison operators:
Oper | Name | Comparing | Returns | Try it |
---|---|---|---|---|
== | equal to | x == 8 | false | Try it » |
== | equal to | x == 5 | true | Try it » |
=== | equal value and type | x === "5" | false | Try it » |
=== | equal value and type | x === 5 | true | Try it » |
!= | not equal | x != 8 | true | Try it » |
!== | not equal value or type | x !== "5" | true | Try it » |
!== | not equal value or type | x !== 5 | false | Try it » |
> | greater than | x > 8 | false | Try it » |
< | less than | x < 8 | true | Try it » |
>= | greater or equal to | x >= 8 | false | Try it » |
<= | less or equal to | x <= 8 | true | Try it » |
JavaScript 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:
Oper | Name | Example | Try it |
---|---|---|---|
&& | AND | (x < 10 && y > 1) is true | Try it » |
|| | OR | (x === 5 || y === 5) is false | Try it » |
! | NOT | !(x === y) is true | Try it » |
JavaScript Bitwise Operators
Bit operators work on 32 bits numbers. Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.
Oper | Name | Example | Same as | Result | Dec | Try it |
---|---|---|---|---|---|---|
& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 | Try it » |
| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 | Try it » |
~ | NOT | x = ~ 5 | ~0101 | 1010 | 10 | Try it » |
^ | XOR | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 | Try it » |
<< | Left shift | x = 5 << 1 | 0101 << 1 | 1010 | 10 | Try it » |
>> | Right shift | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 | Try it » |
>>> | Unsigned right | x = 5 >>> 1 | 0101 >>> 1 | 0010 | 2 | Try it » |