JavaScript Operators


JavaScript operators are symbols that are used to perform operations on operands.

There are following types of operators in JavaScript.

  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Bitwise Operators
  • Logical Operators
  • Special Operators

JavaScript Arithmetic Operators

rithmetic operators are used to perform arithmetic between variables and/or values.

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
++Increment
--Decrement

Example

<script type="text/javascript">
var sum, subtr , mul , divid , mod , inc , dec;

var a= 5 ; 
var b =2 ;

sum = a + b; // The addition operator (+) adds a value	
document.write("Addition: "+sum);
document.write("<br/>");

subtr = a - b; // The subtraction operator (-) subtracts a value
document.write("Subtractin: "+subtr);
document.write("<br/>");

mul = a * b; // The multiplication operator (*) multiplies a value
document.write("Multiplication: "+mul);
document.write("<br/>");

divid = a ⁄ b; // The division operator (⁄) divides a value
document.write("Division: "+divid);
document.write("<br/>");

mod = a % b; // The modular operator (%) returns division remainder
document.write("Modular: "+mod);
document.write("<br/>");

inc = 5;//The increment operator (++) increments a value
inc++;
document.write("Increment: "+inc);
document.write("<br/>");

dec =5; // The decrement operator (--) decrements a value
dec--;
document.write("Decrement: "+dec);

</script>
Output
Addition: 7
Subtractin: 3
Multiplication: 10
Division: 2.5
Modular: 1
Increment: 6
Decrement: 4

JavaScript Assignment Operators

ssignment operators are used to assign values to JavaScript variables.

OperatorExampleSame As
=x = yx = y
+=x += yx = x + y
-=x -= yx = x - y
*=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y

Example

<script type="text/javascript">
var x;

x = 15; // The = assignment operator assigns a value to a variable	
document.write("= Assignment operator: "+x);
document.write("<br/>");

x = 15;
x += 5; // The += assignment operator adds a value to a variable
document.write("+= Assignment operator: "+x);
document.write("<br/>");

x = 15;
x -= 5; // The -= assignment operator subtracts a value from a variable
document.write("-= Assignment operator: "+x);
document.write("<br/>");

x = 15;
x *= 5; // The *= assignment operator multiplies a variable
document.write("*= Assignment operator: "+x);
document.write("<br/>");

x = 15;
x ⁄= 5; // The /= assignment divides a variable
document.write("/= Assignment operator: "+x);
document.write("<br/>");

x = 15;
x %= 5; // The %= assignment operator assigns a remainder to a variable
document.write("%= Assignment operator: "+x);
document.write("<br/>");

</script>
Output
= Assignment operator: 15
+= Assignment operator: 20
-= Assignment operator: 10
*= Assignment operator: 75
/= Assignment operator: 3
%= Assignment operator: 0

JavaScript Comparison Operators

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

Given that x=2 ;

OperatorDescriptionExampleOutput
==equal tox == 5false
x == 2true
===equal value and equal typex === "2"false
x === 2true
!=not equalx != 5true
!==not equal value or not equal typex !== "2"true
x !== 2false
> greater thanx > 5false
< less thanx < 5true
>= greater than or equal tox >= 5false
<= less than or equal tox <= 5true

JavaScript Bitwise Operators

Bit operators work on 32 bits numbers.

Any numeric operand in the operation is converted into a 32 bit number.

The bitwise operators perform bitwise operations on operands. The bitwise operators are as follows:

OperatorDescriptionExample
&Bitwise AND(10==20 & 20==33) = false
|Bitwise OR(10==20 | 20==33) = false
^Bitwise XOR(10==20 ^ 20==33) = false
~Bitwise NOT (~10) = -10
<<Bitwise Left Shift(10<<2) = 40
>>Bitwise Right Shift(10>>2) = 2
>>>Bitwise Right Shift with Zero(10>>>2) = 2

JavaScript Logical Operators

Logical operators are used to determine the logic between variables or values.

Given that a=5 and b=2.

OperatorDescriptionExample
&&Logical AND(a < 10 && y b 1) is true
||Logical OR(a == 5 || y == b) is false
!Logical Not!(a == b) is true

Example

// (&&) and Logical operators
<script type="text/javascript">
var a=5 , b =15 , c=10;
if((a < b) && (b > c))
 {
  document.write("True");
 }
 else
 {
 document.write("False");
 }
</script>

// (||) or Logical operators
<script type="text/javascript">
var a=5 , b =15 , c=10;
if((a == b) || ( b == c))
 {
  document.write("True");
 }
 else
 {
 document.write("False");
 }
</script>

// (!=) not Logical operators
<script type="text/javascript">
var a=5 , b =15 ;
if(a != b)
 {
  document.write("True");
 }
 else
 {
 document.write("False");
 }
</script>
Output
// (&&) and Logical operators
True 

// (||) or Logical operators
False 

// (!=) not Logical operators
True

JavaScript Special Operators

he following operators are known as JavaScript special operators.

OperatorDescription
(?:)Conditional Operator returns value based on the condition. It is like if-else.
,Comma Operator allows multiple expressions to be evaluated as single statement.
deleteDelete Operator deletes a property from the object.
inIn Operator checks if object has the given property
instanceofchecks if the object is an instance of given type
newcreates an instance (object)
typeofchecks the type of object.
voidit discards the expression's return value.
yieldchecks what is returned in a generator by the generator's iterator.

Share this article on