JavaScript Operators


JavaScript operators are symbols used to perform operations on operands. After storing values in variables, we can also perform some operations between them like addition, multiplication, etc. To perform such operations we have to use some symbols (like +, -, *, /,%, etc.) which are called operators.

There are several types of operators in Javascript, which are given in detail below.

JavaScript Arithmetic Operators

Arithmetic operators are used to performing arithmetic operations between two variables or values. These are given below.

Operator Description Example
+ Addition 10+20=30
Subtraction 15-10=5
* Multiplication 5*2=10
/ Division 8/2=4
% Modulus (Remainder) 5%2=1

JavaScript Comparison Operators

The two operands are compared with comparison operators. The comparison operators are as follows:

Operator Description Example
== Is equal to 20==30 = false
!= Not equal to 20!=30 = true
=== Identical Equal to 5===2=false
!== Idential Not Equal to 5 !== 2 = false
5 !== ”5″=true
< Less than 10<20=true
> Greater than 10>20= false
<= Less than equal to 10<=20=true
10>=10=true
=> Greater than equal to 10>=20=false
10>=10=true

JavaScript Assignment Operators

The assignment operator (=) is used to assign a value to a variable. The assignment operators used in JS are given below.

Operators Description Examples Same as
= Assignment a = b a = b
+= Addition Assignment a += b a = a + b
-= Subtraction Assignment a -= b a = a - b
*= Multiplication Assignment a *= b a = a * b
/= Division Assignment a /= b a = a / b
%= Modulus Assignment a %= b a = a % b
&= Bitwise AND Assignment a &= b a = a & b
|= Bitwise OR Assignment a |= b a = a | b
^= Bitwise Exclusive OR Assignment a ^= b a = a ^ b
<<= Left Shift Assignment a <<= b a = a << b
>>= Right Shift Assignment a >>= b a = a >> b

JavaScript Logical Operators

The following logical operators are used to perform logical operations.

Operator Description Example
&& Logical AND (20<30 && 30>20)=true
|| Logical OR (10>30 || 30<40)=true
! Logical Not !(5>2)=false

JavaScript Bitwise Operators

The bitwise operator works on the basis of one bit, i.e. a binary number (0, 1). It calculates the given input by converting it into binary and returns the output again by converting it into a decimal number.

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Bitwise Left Shift
>> Bitwise Right Shift

JavaScript Conditional (Ternary) Operator

Sometimes we need to assign a value to a variable based on a condition. The conditional operator also called the ternary operator, by which we can perform this job easily.

Where do we put the question mark? And use a colon: We place a condition in square brackets () when this condition is true, then the first value returns from the colon, and when the condition is false, the value returns after a colon.

Syntax

variable_name = (condition) ? value1:value2;

Example

var a = 10;

b = ( a == 10 ? 2 : 0 );
document.write("Value of b is "+b);