JavaScript Operators
JavaScript Operators
JavaScript has the following types of operators. This section describes the javascript operators and operator precedence.
- Assignment operators
- Comparison operators
- Arithmetic operators
- Bitwise operators
- Logical operators
- String operators
- Conditional (ternary) operator
- Comma operator
- Unary operators
- Relational operator
JavaScript Arithmetic Operators
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.
Arithmetic operators are used to perform arithmetic on numbers
For more understanding let us assume that value of y = 10, the table below explains the arithmetic operators with the y value as 10:
Operator | Description | Example | Result | Try It Now |
---|---|---|---|---|
|
Addition operator. | x = y + 20 | x = 30 and y = 20 | Try It |
|
Subtraction operator. | x = y - 20 | x = -10 and y = 10 | Try It |
|
Division operator. | x = y / 2 | x = 5 and y = 10 | Try It |
|
Multiplication operator. | x = y * 2 | x = 20 and y = 10 | Try It |
|
Remainder operator. | x = y % 2 | x = 5 and y = 10 | Try It |
Assignment operators
An assignment operator assigns a value to its left operand based on the value of its right operand.The simple assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, a = b assigns the value of b to a.
Operator | Description | Shorthand Operator | Meaning | Example |
---|---|---|---|---|
= |
Assignment Operator | x = y | x = y | Try It |
*= |
Multiplication Assignment | x *= y | x = x * y | Try It |
/ = |
Division Assignment | x /= y | x = x / y | Try It |
+ = |
Addition Assignment | x += y | x = x + y | Try It |
- = |
Subtraction Assignment | x -= y | x = x - y | Try It |
% = |
Remainder Assignment | x %= y | x = x % y | Try It |
<< = |
Left shift Assignment | x <<= y | x = x >> y | |
>> = |
Right shift Assignment | x >>= y | x = x >> y | |
>>> = |
Unsigned Right shift Assignment | x >>>= y | x = x >>> y | |
&= |
Bitwise AND Assignment | x &= y | x = x & y | |
^= |
Bitwise XOR Assignment | x ^= y | x = x ^ y | |
| = |
Bitwise OR Assignment | x |= y | x = x | y |
JavaScript Equality operators
The equality operator always returns the boolean type as its output based on its comparison
Operator | Description |
---|---|
== |
Equality Operator |
!= |
Inequality operator. |
=== |
Identity Operator |
!== |
Nonidentity Operator |
Bitwise shift operators
Bitwise operators are used to shift all bits of operands
Operator | Description |
---|---|
<< |
Bitwise left shift Operator |
>> |
Bitwise right shift operator. |
>>> |
Bitwise unsigned right shift Operator |
JavaScript Binary bitwise operators
Bitwise operators treat their operands as a set of 32 bits (zeros and ones) and return standard JavaScript numerical values
Operator | Description |
---|---|
& |
Bitwise AND Operator |
| |
Bitwise OR operator. |
^ |
Bitwise XOR Operator |
Practice with our Interactive Editor and take your JavaScript Skills to the next level
Complete Beginner to JavaScript Developer - Udemy
Advanced JavaScript Development - Udemy
Learn javascript in one month from experts -OneMonth
Become a Certified Web Developer - Udemy