The symbols which are used to perform mathematical or logical operation are known as operator.
In the javascript there are different types of operator available which are given
1. Arithmetic operator
2. Relational operator
3. Logical operator
4. Assignment operator
5. String operator
6. conditional operator
1. Arithmetic operator
Arithmetic operator are used when any mathematical operation is to be done.
Operator | Description | Example | Result |
+ | Used for addition | x=2,y=2 x+y | 4 |
- | Used for subtraction | x=2,y=2 x-y | 0 |
* | Used for multiplication | X=2,y=2 x*y | 4 |
/ | Used for division | X=2,y=2 x/y | 1 |
% | Used for modules | X=2,y=2 x%y | 0 |
++ | Used for increment | X=2 x++ | 3 |
-- | Used for decrement | X=2 x-- | 1 |
There are different operators of arithmetic as given below.
Example
<html>
<title>Example of Arithmetic Operator</title>
<head>
<script language=”javascript”>
var x=20;
var y=10;
document.write("Addition : ",x+y,"<br>"); // 30
document.write("subtraction : ",x-y,"<br>"); // 10
document.write("Multiplication : ",x*y,"<br>"); // 200
document.write("Division : ",x/y,"<br>"); // 2
document.write("Modulus : ",x%y,"<br>"); // 0
document.write("Increment : ",++x,"<br>"); // 21
document.write("Decrement : ",--x,"<br>"); // 20
</script>
</head>
</html>
2. Relational/Comparison operator
When the variables are related to some value at that time the relational operator is used. There are different relational operators used as below.
Operator | Description |
== | This operator is used to compare the two variables or the value to check the equality. It checks only the value and not the data type. So even if the data types are different for the two variables it will return “true” if they have same value. |
=== | This operator is also used for comparing the equality of the variables or the values to check the equality. It will check not only the value but the data types are also checked. It will return true only if the values and data types are equal. |
!= | This operator is for not equality. It will return true if the values are not equal regardless the data. |
!== | This operator is for not quality but different from above. It will return true if the values are not equal and if the data types are not same. |
< | Used for less than. |
> | Used for greater than |
>= | Used for greater than equal to |
<= | Used for less than equal to |
Example
<html>
<title>Example of Relational Operator</title>
<head>
<script language=”javascript”>
var x=10;
var y=20;
document.write("x==y : ",x==y,"<br>"); // false
document.write("x===y : ",x===y,"<br>"); // false
document.write("x!=y : ",x!=y,"<br>"); // true
document.write("x!==y : ",x!==y,"<br>"); // true
document.write("x<y : ",x<y,"<br>"); // true
document.write("x>y : ",x>y,"<br>"); // false
document.write("x<=y : ",x<=y,"<br>"); // true
document.write("x>=y : ",x>=y,"<br>"); // false
</script>
</head>
</html>
3. Logical operator
When more than two conditions are to be checked at the same time, then logical operator can be helpful. There are different logical operators as given below.
Operator | Description |
&& | And operator. Returns true if all the condition are true. |
|| | Or operator. Returns true if any of the condition is true. |
! | Not operator. It reverses the condition. That means if the result of the condition is true then it converts it into false and vice versa. |
4. Assignment operator
There are different assignment operators as given below.
Operator | Description |
= | Assigning the value x=y |
*= | X*=y is similar to x=x*y |
/= | X/=y is similar to x=x/y |
+= | X+=y is similar to x=x+y |
-= | X-=y is similar to x=x-y |
%= | X%=y is similar to x=x%y |
Example
<html>
<title>Example of Assignment Operator</title>
<head>
<script language=”javascript”>
var x=10;
var y=20;
document.write("x=y : ",x=y,"<br>"); // 20
document.write("x*=y : ",x*=y,"<br>"); // 400
document.write("x/=y : ",x/=y,"<br>"); // 20
document.write("x+=y : ",x+=y,"<br>"); // 40
document.write("x-=y : ",x-=y,"<br>"); // 20
document.write("x%=y : ",x%=y,"<br>"); // 0
</script>
</head>
</html>
5. String operator
The operator + is used for concatenation of the two string and so + is also known as string operator.
Example
<html>
<title>Example of String Operator</title>
<head>
<script language=”javascript”>
var str=”hello”;
document.write(str+” “+”world”); // Hello World
</script>
</head>
</html>
6. Conditional operator
Javascript also contains a conditional operator that assign a value to a variable based on some condition.
Syntax
Variablename = (condition)?value1:value2;
Example
<html>
<title>Example of Conditional Operator</title>
<head>
<script language=”javascript”>
var result="pass";
document.write((result=="pass")?" Congrats..":"Try again..");
</script>
</head>
</html>
Output
Congrats…
Posted By : Hemangi Zala
No comments:
Post a Comment