JavaScript Operators
0 2462
Identical to other programming languages, JavaScript also comprises of operators to perform tasks on singles or multiple operands. For example, if we want to solve expression like 5*6 is equal to 11 in JavaScript, operator and operands are used. 5 and 6 acts as operands whereas + acts as an operator.
Function of an operator:
Function of an operator is similar in almost every programming language. Some of them are mentioned below:
a) It Permits to perform operations on data.
b) Value assignment
c) Value comparison
d) Arithmetic operations
e) Permits shortening of code
f) Performing simple to complex calculations.
Following are the type of operators supported by JavaScript:
a) Arithmetic Operators
b) Comparison Operators
c) Logical (or Relational) Operators
d) Assignment Operators
e) Conditional (or Ternary) Operators
1. Arithmetic Operators
The JavaScript comprises of Arithmetic Operators associated for performing various mathematical operations including Addition(+), Subtraction(-), Multiplication(*), etc. These operators take operand as a values/variables and return output as a value i.e. work on two operands.
Below mentioned table will display the Arithmetic Operators used in JavaScript along with examples.
JavaScript Arithmetic Operators | Syntax |
+ (Addition) | var p1 = 43+65 |
- (Subtraction) | var p1 = 56-34; |
* (Multiplication) | var p1 = 45*2; |
% (Modulus) | var p1 = 45%5; |
Program illustration arithmetic operators:
<html>Output:
<body>
<script>
var p1 = 28;
var p2 = 25;
var p3 = "verify";
var linebreak = "<br />";
document.write("p1 + p2 = ");
result = p1 + p2;
document.write(result);
document.write(linebreak);
document.write("p1 - p2 = ");
result = p1 - p2;
document.write(result);
document.write(linebreak);
document.write("p1 / p2 = ");
result = p1 / p2;
document.write(result);
document.write(linebreak);
document.write("p1 % p2 = ");
result = p1 % p2;
document.write(result);
document.write(linebreak);
</script>
</body>
</html>
p1 + p2 = 53
p1 - p2 = 3
p1 / p2 = 1.12
p1 % p2 = 3
2. Comparison Operators
= = (Equal)
!= (Not Equal)
> (Greater than)
< (Less than)
>= (Greater than or Equal to)
<= (Less than or Equal to)
Program illustrating comparison operators
<html>Output:
<body>
<script>
var p1 = 33;
var p2 = 23;
var linebreak = "<br />";
document.write("(p1 == p2) => ");
result = (p1 == p2);
document.write(result);
document.write(linebreak);
document.write("(a <= b) => ");
result = (p1 <= p2);
document.write(result);
document.write(linebreak);
document.write("(p1 < p2) => ");
result = (p1 < p2);
document.write(result);
document.write(linebreak);
document.write("(p1 > p2) => ");
result = (p1 > p2);
document.write(result);
document.write(linebreak);
document.write("(p1 >= p2) => ");
result = (p1 >= p2);
document.write(result);
document.write(linebreak);
document.write("(p1!= p2) => ");
result = (p1 != p2);
document.write(result);
document.write(linebreak);
</script>
</body>
</html>
(p1 == p2) => false
(a <= b) => false
(p1 < p2) => false
(p1 > p2) => true
(p1 >= p2) => true
(p1!= p2) => true
3. Logical Operators
Logical operators are usually used with logical values. Below mentioned table will display the logical Operators used in JavaScript along with examples.
Logical Operator | Example |
&& (Logical AND) | It verify two operands: If they both are non-zero, it returns true. P1=3 and P3=4 P1&&P2 is true |
|| (Logical OR) | It verify two operands: If they one among two are non-zero, it returns true. (P1 || P2) is true |
! (Logical NOT) | It converses the boolean output of the or condition. P1=3 and P2=0 !(P1 || P2) is false |
<html>Output:
<body>
<script >
<!--
var p1 = true;
var p2 = false;
var linebreak = "<br />";
document.write("(p1 && p2) => ");
result = (p1 && p2);
document.write(result);
document.write(linebreak);
document.write("(p1 || p2) => ");
result = (p1 || p2);
document.write(result);
document.write(linebreak);
document.write("!(p1 && p2) => ");
result = (!(p1 && p2));
document.write(result);
document.write(linebreak);
//-->
</script>
</body>
</html>
(p1 && p2) => false
(p1 || p2) => true
!(p1 && p2) => true
4. Assignment Operators
The assignment operator(=) is used to allot a particular value to any variable
Example:
var u=8;
var v=9;
var w=10;
Types of assignment operators in JavaScript
a) =
b) +=
c) -=
d) *=
e) /=
f) %=
Program:
<!DOCTYPE html>
<html>
<body>
<h1> Simple program illustrating assignment operators </h1>
<p id="a"></p>
<p id="b"></p>
<p id="c"></p>
<p id="d"></p>
<p id="e"></p>
<p id="f"></p>
<script>
var i = 17, j = 18;
i = j;
document.getElementById("a").innerHTML = i;
i += j;
document.getElementById("b").innerHTML = i;
i -= 2;
document.getElementById("c").innerHTML = i;
i *= 6;
document.getElementById("d").innerHTML = i;
i /= 7;
document.getElementById("e").innerHTML = i;
i %= 8;
document.getElementById("f").innerHTML = i;
</script>
</body>
</html>
Output:
Simple program illustrating assignment operators
18
36
34
204
29.142857142857142
5.142857142857142
5. Conditional / Ternary Operators
The JavaScript Ternary Operator is also referred as a conditional operator basically used in decision making operations. This operator comprises of three arguments where the 1st argument is for comparison and the result of second and third depends upon a true and false comparison respectively. In other words, we can also say that ternary operator is a shortened method of if-else statement.
Let's us know the syntax of the JavaScript conditional operator
cond? exprIfTrue : exprIfFalse
Example
<html>
<body>
<script type="text/javascript">
<!--
/*
****************************************
Example ternary conditional operator
****************************************
*/
var height = 156;
var weight = 89;
var size = 32;
var person;
person = (height < weight ) ? height : weight ;
person = (person < size ) ? person : size;
document.write("Minimum of three numbers is "+ person);
//-->
</script>
</body>
</html>
Output:
Minimum of three numbers is 32
Typeof Operators
In JavaScript, typeof operator refer as a unary operator sited before its single operand, used to retrieves the data type in the form of a string. This operator checks to "string","booleon", or "number" if it is string, Boolean or number value and as per evaluation it returns result.
Program
<html>
<body>
<script type = "text/javascript">
<!--
var p = 4;
var q = "monika";
var breakline = "<br />";
res = (typeof d == "monika" ? "q is monika" : "p is Numerical values ");
document.write("Res => ");
document.write(res);
document.write(breakline);
res1 = (typeof q == "monika" ? "q is monika" : "q is String values");
document.write("Res => ");
document.write(res1);
document.write(breakline);
//-->
</script>
</body>
</html>
Output
Res => p is Numerical values
Res => q is String values
Share:
Comments
Waiting for your comments