Google News
logo
Javascript Operators
JavaScript operators are used to assign values, compare values, arithmetic operations, and more. For example 1 + 2, where + sign is an operator and 1 is left operand and 2 is right operand. + operator adds two numeric values and produces a result which is 3 in this case.

1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment  Operators

Arithmetic Operators
Arithmetic operators are used to perform mathematical operations between numeric operands.
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
Example :
<html>
<head>
<title>Arithmetic Operators</title>
</head>
 
<body>

<script type="text/javascript">

var a = 33;
var b = 10;
var c = "Sample";
var linebreak = "<br />";
 
document.write("a + b = ");
result = a + b;
document.write(result);
document.write(linebreak);
 
document.write("a - b = ");
result = a - b;
document.write(result);
document.write(linebreak);
 
document.write("a / b = ");
result = a / b;
document.write(result);
document.write(linebreak);
 
document.write("a % b = ");
result = a % b;
document.write(result);
document.write(linebreak);
 
document.write("a + b + c = ");
result = a + b + c;
document.write(result);
document.write(linebreak);
 
a = ++a;
document.write("++a = ");
result = ++a;
document.write(result);
document.write(linebreak);
 
b = --b;
document.write("--b = ");
result = --b;
document.write(result);
document.write(linebreak);

</script>

</body>
</html>
Output :

a + b = 43

a - b = 23

a / b = 3.3

a % b = 3

a + b + c = 43Sample

++a = 35

--b = 8

Comparison Operators
JavaScript language includes operators that compare two operands.
Operator Description
== Is equal to
=== Identical (equal and of same type)
!= Not Equal
!== Not Identical
> Greater than
>= Greater than or Equal to
< Less than
<= Less than or Equal to
Example :
<html>
<head>
<title>Comparison  Operators</title>
</head>
 
<body>
 
<script type="text/javascript">

var a = 10;
var b = 20;
var linebreak = "<br />";
 
document.write("(a == b) : ");
result = (a == b);
document.write(result);
document.write(linebreak);
 
document.write("(a < b) : ");
result = (a < b);
document.write(result);
document.write(linebreak);
 
document.write("(a > b) : ");
result = (a > b);
document.write(result);
document.write(linebreak);
 
document.write("(a != b) : ");
result = (a != b);
document.write(result);
document.write(linebreak);
 
document.write("(a >= b) : ");
result = (a >= b);
document.write(result);
document.write(linebreak);
 
document.write("(a <= b) : ");
result = (a <= b);
document.write(result);
document.write(linebreak);

</script>
 
</body>
</html>
Output :

(a == b) : false

(a < b) : true

(a > b) : false

(a != b) : true

(a >= b) : false

(a <= b) : true

Logical Operators
Logical operators are used to combine two or more conditions. JavaScript includes following logical operators.
Operator Description
&& Logical AND
|| Logical OR
! Logical NOT
Example :
<html>
<head>
<title>Logical Operators</title>
</head>
 
<body>
    
<p id="p1"></p>
<p id="p2"></p>
<p id="p3"></p>
<p id="p4"></p>
<p id="p5"></p>
 
<script type="text/javascript">

var a = 25, b = 50;
document.getElementById("p1").innerHTML = (a != b) && (a < b); 
document.getElementById("p2").innerHTML = (a > b) || (a == b);
document.getElementById("p3").innerHTML = (a < b) || (a == b);
document.getElementById("p4").innerHTML = !(a < b);
document.getElementById("p5").innerHTML = !(a > b);

</script>
 
</body>
</html>
Output :

Bitwise Operators
The bitwise operators perform bitwise operations on operands. The bitwise operators are as follows:
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Bitwise Left Shift
>> Bitwise Right Shift
>>> Right shift with Zero
Example :
<html>
<head>
<title>Bitwise Operators</title>
</head>
 
<body>
 
<script type="text/javascript">

var a = 2;
var b = 3;
var linebreak = "<br />";
 
document.write("(a & b) => ");
result = (a & b);
document.write(result);
document.write(linebreak);
 
document.write("(a | b) => ");
result = (a | b);
document.write(result);
document.write(linebreak);
 
document.write("(a ^ b) => ");
result = (a ^ b);
document.write(result);
document.write(linebreak);
 
document.write("(~b) => ");
result = (~b);
document.write(result);
document.write(linebreak);
 
document.write("(a << b) => ");
result = (a << b);
document.write(result);
document.write(linebreak);
 
document.write("(a >> b) => ");
result = (a >> b);
document.write(result);
document.write(linebreak);

</script>

</body>
</html>
Output :

(a & b) => 2

(a | b) => 3

(a ^ b) => 1

(~b) => -4

(a << b) => 16

(a >> b) => 0

Assignment Operators
JavaScript includes assignment operators to assign values to variables with less key strokes.
Operator Description
= Assign
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign
Example :
<html>
<head>
<title>Assignment Operators</title>
</head>
 
<body>
 
<script type="text/javascript">

var a = 50;
var b = 25;
var linebreak = "<br />";
 
document.write("(a = b) => ");
result = (a = b);
document.write(result);
document.write(linebreak);
 
document.write("(a += b) => ");
result = (a += b);
document.write(result);
document.write(linebreak);
 
document.write("(a -= b) => ");
result = (a -= b);
document.write(result);
document.write(linebreak);
 
document.write("(a *= b) => ");
result = (a *= b);
document.write(result);
document.write(linebreak);
 
document.write("(a /= b) => ");
result = (a /= b);
document.write(result);
document.write(linebreak);
 
document.write("(a %= b) => ");
result = (a %= b);
document.write(result);
document.write(linebreak);

</script>

</body>
</html>
Output :

(a = b) => 25

(a += b) => 50

(a -= b) => 25

(a *= b) => 625

(a /= b) => 25

(a %= b) => 0