first
first
logical computations
There are different types of operators:
1. Arithmetic Operators
Addition(+) :
Addition ‘+’ operator performs addition on two operands. This ‘+’ operator can also be used
to concatenate (add) strings.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=90;
var b=99;
document.write("Arithmetic Operator: <br>");
document.write("Addition= "+ (a+b));
</script>
</body>
</html>
Output: Arithmetic Operator:
Addition= 189
Subtraction(-):
Subtraction ‘-‘ operator performs subtraction on two operands.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=100;
var b=4;
document.write("Arithmetic Operator: <br>");
document.write("Subtraction= "+ (a-b));
</script>
</body>
Output: Arithmetic Operator:
Subtraction= 96
Multiplication(*):
Multiplication ‘*’ operator performs multiplication on two operands.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=2;
var b=2;
document.write("Arithmetic Operator: <br>");
document.write("Multiplication= "+ (a*b));
</script>
</body>
</html>
Division(/):
Division ‘/’ operator performs division on two operands (divide the numerator by the
denominator).
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=125;
var b=5;
document.write("Arithmetic Operator: <br>");
document.write("Division= "+ (a/b));
</script>
</body>
</html>
Output: Arithmetic Operator:
Division= 25
Modulus(%):
Modulus ‘%’ operator gives a remainder of an integer division.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=125;
var b=25;
document.write("Arithmetic Operator: <br>");
document.write("Mod= "+ (a%b));
</script>
</body>
</html>
Output: Arithmetic Operator:
Mod= 0
2. Assignment Operators:
Assignment Operator(=):
This operator assigns the right operand value to the left operand.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=20;
document.write("a= "+ (a));
</script>
</body>
</html>
Output: a= 20
Addition Assignment(+=):
Sums up left and right operand values and then assigns the result to the left operand.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=20;
var b=10;
document.write("a="+ (a+=b));
</script>
</body>
</html>
Output: a=30
Subtraction Assignment(-=):
It subtracts the right side value from the left side value and then assigns the result to the left
operand.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=20;
var b=10;
document.write("a="+ (a-=b));
</script>
</body>
</html>
Output: a=10
Multiplication Assignment(*=):
It multiplies a variable by the value of the right operand and assigns the result to the
variable.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=5;
var b=4;
document.write("a="+ (a*=b));
</script>
</body>
</html>
Output: a=20
Division Assignment(/=):
It divides a variable by the value of the right operand and assigns the result to the variable.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=20;
var b=4;
document.write("a="+ (a/=b));
</script>
</body>
</html>
Output: a=5
Modulus Assignment(%=):
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=20;
var b=4;
document.write("a="+ (a%=b));
</script>
</body>
</html>
Output: a=0
3. Relational Operators:
Greater than(>):
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=100;
var b=5;
if(a>b)
{
document.write("a is greater");
}
</script>
</body>
</html>
Output: a is greater
Less than(<):
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=2;
var b=4;
if(a<b)
{
document.write("a is smaller");
}
</script>
</body>
</html>
Output: a is smaller
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=44;
var b=100;
if(a>=b)
{
document.write("a is greater or equal");
}
else{
document.write(“b is greater”);
}
</script>
</body>
</html>
Output: b is greater
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=1001;
var b=101;
if(a>=b)
{
document.write("a is less or equal to b");
}
else
{
document.write(“b is greater”);
}
</script>
</body>
</html>
Output: a is less or equal to b
Equal to(==):
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=4;
var b=4;
if(a==b)
{
document.write("a is equal to b");
}
</script>
</body>
</html>
Output: a is equal to b
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=2;
var b=4;
if(a!=b)
{
document.write("a is not equal to b");
}
</script>
</body>
</html>
Output: a is not equal to b
4. Conditional Operator :
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" language="javascript">
var a=8;
var b=4;
var s=((a>b)?"a is greater":"b is greater");
document.write(s);
</script>
</body>
</html>
Output: a is greater
5. Logical Operators:
Logical AND(&&):