Javascript Operators

What is Operators?

Operators are used to perform operation on single or multiple value (operand) and produce a result. Following are Operators that are available in javascript


Assignment operator

Assignment operator assign value to javascript variables. Following are assignment operators available in javascript

operator
example
shorthand for
=
a = 9
non
+=
a += 9
a = a + 9
-=
a -= 9
a = a - 9
*=
a *= 9
a = a * 9
/=
a /= 9
a = a / 9
%=
a %= 9
a = a % 9
<<=
a <<= 9
a = a << 9
>>>=
a >>>= 9
a = a >>> 9
&=
a &= 9
a = a & 9
^=
a ^= 9
a = a ^ 9
|=
a |= 9
a = a | 9
**=
a **= 9
a = a ** 9

Arithmetic operator

Arithmetic operator are used to perform arthmetic calculation on numbers(literals or variables). Following are arithmetic operators available in javascript

operator
description
example
+
for addition
a = 9 + 3
-
for subtraction
a = 10 - 9
/
division
a = a / 9
*
multiplication
a = a * 9
**
exponent
a = 2 ** 9
%
Modulus (remainder)
a = a % 9
++
increment
a = ++a
--
decrement
a = a--

Unary operator

Unary operator are used to perform operation on single operand. Following are unary operators available in javascript

operator
description
example
+
for converting string to number
a = +a
-
for subtraction
a = -b
++
increment
a = ++a
--
decrement
a = a--

Logical operator

Logical operator are used to perform operation on multi operand. Following are logical operators available in javascript

operator
description
example
||
OR
a = true || a
&&
AND
a = true && b
!
negate
a = !a

Comparison operator

Comparison operators are used in logical statements to determine equality or difference between variables or values. Following are Comparison operators available in javascript

operator
description
example
returns
==
Equall to
3 == 3
true
4 == 3
false
===
strict equal. checks value and type
3 === '3'
false
3 === 3
true
!=
negate
2 != 3
true
3 != 3
false
!==
strict not equal checks value and type
3 !== 3
false
3 !== '3'
true
>
greater than
6 > 3
true
1 > 3
false
<
lesser than
3 < 8
true
23 < 3
false
<=
less than or equal
3 <= 3
true
1 <= 3
false
>=
greater than or equal
3 >= 3
true
4 >= 3
true

Bitwise operator

Bitwise operator are used to perform operation on multi operand. Following are logical operators available in javascript

operator
description
example
|
OR
Sets each bit to 1 if both bits are 1
&
AND
Sets each bit to 1 if one of two bits is 1
^
XOR
Sets each bit to 1 if only one of two bits is 1
~
NOT
Inverts all the bits
<<
zero fill left shift
Shifts left by pushing zeros in from the right and let the leftmost bits fall off
>>
signed right
Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>>
Zero fill right shift
Shifts right by pushing zeros in from the left, and let the rightmost bits fall off

typeof operator

The typeof operator returns a string indicating the type of the unevaluated operand.

Usage: typeof operand

example
description
returns
typeof null
returns type of null
'object'
typeof 3
returns number
'number'
typeof '3'
returns string
"string"
typeof {}
object
'object'

Ternary (conditional) operator

Ternary operator shortcut for if else statement. The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy.

Usage: condition ? ifTrueStatement : ifFalseStatement