JavaScript Code


JavaScript Operators


Operators

JavaScript has following operator categories

Arithmetic Operators

OperatorDescriptionJS Example
+Additiona + b
Subtractiona - b
*Multiplicationa * b
**Exponentiationa ** b
/Divisiona / b
%Modulusa % b
++Incrementa++
Decrementa--

Assignment Operators

OperatorDescriptionJS ExampleEquivalent to
=Assignmenta = 4
+=Addition Assignmenta += ba = a + b;
Subtraction Assignmenta -= ba = a - b;
*Multiplication Assignmenta *= ba = a * b;
**Exponentiation Assignmenta **= ba = a ** b;
/Division Assignmenta /= ba = a / b;
%Modulus Assignmenta %= ba = a % b;

Comparison Operators

OperatorDescriptionJS Example
==equal toa == b
===equal value and equal typea === b
!=not equala != b
!==not equal value or not equal typea !== b
>greater thana > b
<less thana < b
>=greater than or equal toa >= b
<=less than or equal toa <= b
?ternary operatorx = condition? a: b

Logical Operators

OperatorDescriptionJS Example
&&and(a == 2) && (b == 5)
||or(a == 2) || (b == 5)
!not!(a == 2)

Type Operators

OperatorDescriptionJS Example
typeofreturns type of variablea = 5;
console.log(typeof a);
instanceofreturns true if given object is an instance of specified typevar a = new String('apple');
console.log(a instanceof String);

Bitwise Operators

OperatorDescriptionJS Example
&ANDa & b
|ORa | b
~NOT~a
^XORa ^ b
<<left shifta << b
>>right shifta >> b
>>>unsigned right shifta >>> b