JS Operators
JavaScript operators are used to assign values, compare values, perform arithmetic operations, etc.
For the JavaScript Operators tutorial, check out JavaScript Operators Tutorial .
JavaScript arithmetic operators
Arithmetic operators are used to perform operations on two variables or values.
Assigning y = 5 , the following table will show you the use of arithmetic operators:
Operator | Description | Example | y value | x value | Try It |
---|---|---|---|---|---|
+ | addition | x = y + 2 | y = 5 | x = 7 | Try It! |
- | subtraction | x = y - 2 | y = 5 | x = 3 | Try It! |
* | multiplication | x = y * 2 | y = 5 | x = 10 | Try It! |
/ | division | x = y / 2 | y = 5 | x = 2.5 | Try It! |
% | remainder | x = y% 2 | y = 5 | x = 1 | Try It! |
++ | auto increment | x = ++ y | y = 6 | x = 6 | Try It! |
x = y ++ | y = 6 | x = 5 | Try It! | ||
-- | Decrement | x = --y | y = 4 | x = 4 | Try It! |
x = y-- | y = 4 | x = 5 | Try It! |
For arithmetic operators, you can read our JavaScript Operators tutorial .
JavaScript assignment operator
The assignment operator is used to assign values to JavaScript variables.
Given x=10 and y=5 , the following table explains the assignment operators:
Operator | Example1 | Example2 | x value | Try It |
---|---|---|---|---|
= | 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! |
For assignment operators, you can read our JavaScript Operators tutorial .
JavaScript string operators
+ operator, += operator can be used to concatenate strings.
Given text1 = "Good " , text2 = "Morning" , and text3 = "" , the following table explains the use of string operators:
Operator | Example | Text1 | Text2 | Text3 | Try It |
---|---|---|---|---|---|
+ | text3 = text1 + text2 | "Good " | "Morning" | "Good Morning" | Try It! |
+= | text1 += text2 | "Good Morning" | "Morning" | "" | Try It! |
Comparison operator
Comparison operators are used in logical statements to determine whether two given values or variables are equal.
Given x=5 , the following table shows the use of comparison operators:
Operator | Description | Compare | Result | Try It |
---|---|---|---|---|
== | equal | x == 8 | false | Try It! |
x == 5 | true | Try It! | ||
=== | Both value and type are equal (constant equal) | x === "5" | false | Try It! |
x === 5 | true | Try It! | ||
!= | not equal to | x != 8 | true | Try It! |
!== | Both value and type are not equal (not equal) | x !== "5" | true | Try It! |
x !== 5 | false | Try It! | ||
> | more than the | x > 8 | false | Try It! |
< | less than | x < 8 | true | Try It! |
>= | greater than or equal to | x >= 8 | false | Try It! |
<= | less than or equal to | x <= 8 | true | Try it! |
For comparison operators, you can read our JavaScript Comparison Operators tutorial .
Conditional operator
Conditional operators are used for conditional assignment operations.
Given x=6 and y=3 , the following table demonstrates the operation of the conditional operator:
Grammar | Example | Try It |
---|---|---|
variable = ( condition ) ? value1 : value2 | voteable = (age < 18) ? "too young": "age is ok"; | Try It! |
Logical Operators
Logical operators are used to determine logical relationships between variables or values.
Given x=6 and y=3 , the following example demonstrates the use of logical operators:
operator | describe | example |
---|---|---|
&& | and | (x < 10 && y > 1) is true |
|| | or | (x == 5 || y == 5) is false |
! | No | !(x == y) is true |
JavaScript bitwise operators
Bitwise operators work on 32-bit numbers. Any numeric operations will be converted to 32 bits. The result is converted to a JavaScript number.
Operator | Description | example | similar to | result | decimal |
---|---|---|---|---|---|
& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | Take the opposite | x = ~ 5 | ~0101 | 1010 | -6 |
^ | XOR | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | Shift left | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Shift right | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |