JavaScript Tutorial JavaScript References

JavaScript - Operators



Operators are used to perform operation on a single operand or two operands. Operators in JavaScript can be categorized as follows:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • Bitwise operators
  • Miscellaneous operators

JavaScript Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

OperatorNameDescription
+AdditionAdd two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivide one value by another
**Exponent / PowerReturns first operand raised to the power of second operand
%ModuloReturns remainder of division operation

Example

JavaScript Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

Operator Expression Equivalent toExample
=a = 5a = 5Example
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
**=a **= ba = a ** b
%=a %= ba = a % b
&&=a &&= ba = a && b
||=a ||= ba = a || b
??=a ??= ba = a ?? b
&=a &= ba = a & bMore Info
|=a |= ba = a | bMore Info
^=a ^= ba = a ^ bMore Info
>>=a >>= ba = a >> bMore Info
>>>=a >>>= ba = a >>> b
<<=a <<= ba = a << bMore Info

Note: >>>= is just like the >>= operator, except that the bits shifted in on the left are always zero.

JavaScript Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

OperatorDescription
==Equal
===Equal value and equal type
!= Not equal
!== Not equal value or not equal type
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

JavaScript Increment/Decrement operators

Increment and decrement operators are used to increase and decrease the value of variable.

OperatorNameDescriptionExample
++xPre-incrementIncreases the value of x by 1, then returns x.Example
x++Post-incrementReturns x, then increases the value of x by 1.
--xPre-decrementDecreases the value of x by 1, then returns x.Example
x--Post-decrementReturns x, then decreases the value of x by 1.

JavaScript Logical operators

Logical operators are used to combine two or more conditions.

Operator NameDescription
&& Logical ANDReturns true when all conditions are true
||Logical ORReturns true when any of the conditions is true
!Logical NOTReturns true when given conditions is not true

More Info

JavaScript Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

OperatorNameDescriptionMore Info
&ANDReturns 1 if both bits at the same position in both operands are 1, else returns 0.More Info
|ORReturns 1 if one of two bits at the same position in both operands is 1, else returns 0.More Info
^XORReturns 1 if only one of two bits at the same position in both operands is 1, else returns 0.More Info
~NOTReverse all the bits.More Info
>>Right shiftThe left operand is moved right by the number of bits present in the right operand.More Info
>>>Right shift with ZeroThis operator is just like the >> operator, except that the bits shifted in on the left are always zero.
<<Left shiftThe left operand value is moved left by the number of bits present in the right operand.More Info

JavaScript Miscellaneous operators

The table below describes other operators supported by JavaScript:

OperatorDescription
comma operator (,)Evaluates each of its operands (from left to right) and returns the value of the last operand.
delete operatorDeletes the specified property of an object.
in operatorChecks if the specified property is in the specified object.
instanceof operatorReturns true if the object is an instance of a specific class, false otherwise.
Nullish coalescing operator (??)Returns right-hand side operand when left-hand side operand is null or undefined, otherwise returns left-hand side operand.
ternary operator (?:)Returns one of the two values based on value of boolean expression.
typeof operatorReturns the type of a JavaScript variable or expression.

JavaScript Operators Precedence

Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

The following table lists the precedence and associativity of JavaScript operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

PrecedenceOperatorDescriptionAssociativity
21( )GroupingNA
20newnew (with argument list)
.Member AccessLeft to Right
[ ]Computed Member Access
( )Function Call
?.Optional chaining
19newnew (without argument list)Right to Left
18a++  a--Postfix Increment, Postfix DecrementNA
17!Logical NOTRight to Left
~Bitwise NOT
+a  -aUnary plus, Unary negation
++a  --aPrefix Increment, Prefix Decrement
typeofType
voidvoid operator
deletedelete operator
awaitawait operator
16**Exponentiation
15*  /  %Multiplication, Division, RemainderLeft to Right
14+  -Addition, Subtraction
13<<  >>  >>>Bitwise left shift, right shift and unsigned right shift
12<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equal
inProperty in Object
instanceofInstance of Object
11==  !=Equality, Inequality
===  !==Strict Equality, Strict Inequality
10&Bitwise AND
9^Bitwise XOR
8|Bitwise OR
7&&Logical AND
6||Logical OR
5??Nullish coalescing operator
4?:Conditional (ternary) operatorRight to Left
3=Direct assignment
+=  -=  **=  *=  /=  %=Compound assignment by sum, difference, exponentiation, product, quotient and remainder
<<=  >>=  >>>=Compound assignment by Bitwise left shift, right shift and unsigned right shift
&=  ^=  |=Compound assignment by Bitwise AND, XOR and OR
&&=  ||=Compound assignment by Logical AND, OR
??=Compound assignment by nullish coalescing operator
2yield  yield*Pause Function
1,Comma / SequenceLeft to Right