Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
++ | Increment |
-- | Decrement |
<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>
a + b = 43
a - b = 23
a / b = 3.3
a % b = 3
a + b + c = 43Sample
++a = 35
--b = 8
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 |
<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>
(a == b) : false
(a < b) : true
(a > b) : false
(a != b) : true
(a >= b) : false
(a <= b) : true
Operator | Description |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
<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>
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Bitwise Left Shift |
>> | Bitwise Right Shift |
>>> | Right shift with Zero |
<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>
(a & b) => 2
(a | b) => 3
(a ^ b) => 1
(~b) => -4
(a << b) => 16
(a >> b) => 0
Operator | Description |
---|---|
= | Assign |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
<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>
(a = b) => 25
(a += b) => 50
(a -= b) => 25
(a *= b) => 625
(a /= b) => 25
(a %= b) => 0