JavaScript Operators
JavaScript operators are used for assignment, comparison, performing arithmetic operations, and more.
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic between variables or values.
Given y = 5, the table below explains the arithmetic operators:
Operator | Description | Example | y Value | x Value | Demo |
---|---|---|---|---|---|
+ | Addition | x = y + 2 | y = 5 | x = 7 | Demo » |
- | Subtraction | x = y - 2 | y = 5 | x = 3 | Demo » |
* | Multiplication | x = y * 2 | y = 5 | x = 10 | Demo » |
/ | Division | x = y / 2 | y = 5 | x = 2.5 | Demo » |
% | Modulus | x = y % 2 | y = 5 | x = 1 | Demo » |
++ | Increment | x = ++y | y = 6 | x = 6 | Demo » |
x = y++ | y = 6 | x = 5 | Demo » | ||
-- | Decrement | x = --y | y = 4 | x = 4 | Demo » |
x = y-- | y = 4 | x = 5 | Demo » |
For more details on arithmetic operators, you can read our JavaScript Operators Tutorial.
JavaScript Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
Given x = 10 and y = 5, the table below explains the assignment operators:
Operator | Example | Same As | x Value | Demo |
---|---|---|---|---|
= | x = y | x = y | x = 5 | Demo » |
+= | x += y | x = x + y | x = 15 | Demo » |
-= | x -= y | x = x - y | x = 5 | Demo » |
*= | x *= y | x = x * y | x = 50 | Demo » |
/= | x /= y | x = x / y | x = 2 | Demo » |
%= | x %= y | x = x % y | x = 0 | Demo » |
For more details on assignment operators, you can read our JavaScript Operators Tutorial.
JavaScript String Operators
The + operator, and += operator can also be used to concatenate (add) strings.
Given text1 = "Good ", text2 = "Morning", and text3 = "", the table below explains the string operators:
Operator | Example | text1 | text2 | text3 | Demo |
---|---|---|---|---|---|
+ | text3 = text1 + text2 | "Good " | "Morning" | "Good Morning" | Demo » |
+= | text1 += text2 | "Good Morning" | "Morning" | "" | Demo » |
Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given x = 5, the table below explains the comparison operators:
Operator | Description | Comparison | Result | Demo |
---|---|---|---|---|
== | Equal to | x == 8 | false | Demo » |
x == 5 | true | Demo » | ||
=== | Equal value and type | x === "5" | false | Demo » |
x === 5 | true | Demo » | ||
!= | Not equal | x != 8 | true | Demo » |
!== | Not equal value or type | x !== "5" | true | Demo » |
x !== 5 | false | Demo » | ||
> | Greater than | x > 8 | false | Demo » |
< | Less than | x < 8 | true | Demo » |
>= | Greater than or equal to | x >= 8 | false | Demo » |
<= | Less than or equal to | x <= 8 | true | Demo » |
For more details on comparison operators, you can read our JavaScript Comparison Operators Tutorial.
Conditional (Ternary) Operator
The conditional operator assigns a value to a variable based on a condition.
Given x = 6 and y = 3, the table below demonstrates the conditional operator:
Syntax | Example | Demo |
---|---|---|
variable = (condition) ? value1 : value2 | voteable = (age < 18) ? "Too young":"Old enough"; | Demo » |
Logical Operators
Logical operators are used to determine the logic between variables or values.
Given x = 6 and y = 3, the table below demonstrates 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 Bitwise Operators
Bitwise operators work on 32 bits numbers. Any numeric operand in the operation is converted to a 32-bit number. The result is converted back to a JavaScript number.
Operator | Description | Example | Same As | Result | Decimal |
---|---|---|---|---|---|
& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | NOT | x = ~ 5 | ~0101 | 1010 | -6 |
^ | XOR | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | Left shift | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Right shift | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |