Hello, readers today we are going to see what is javascript operators and types of the operator in detail. So let's gets started.
Operators in javascript:
An operator is a symbol that tells the computer to perform certain mathematical and logical operations. Operators are used in a program to manipulate data and variables.
Types of the operator in JavaScript:
The JavaScript language following type of operators:
- Arithmetic operator
- Comparison operator
- Logical or relational operator
- Assignment operator
- Conditional or ternary operator
- Bitwise operator
Let’s have a look on all operators one by one.
Arithmetical operator:
There are following arithmetical operator supported by JavaScript language:
Assume variable A holds 10 and variable B holds 20 then:
Operator | Description | Example |
+ | Adds two operands | A+B will give 30 |
- | Subtract the second operand from the first | A-B will give -10 |
* | Multiply both operands | A*B will give 200 |
/ | Divide numerator by denominator | B/A will give 2 |
% | Modulus operator and a reminder of after an integer division | B%A will give 0 |
++ | Increment operator, increases integer value by one. | A++ will give 11 |
-- | The decrement operator, decreases the integer value by one. | A—will give 9 |
Example:
<html>
<head>
<title> Javascript</title>
</head>
<body>
<H2>
<script type=”text/javascript”>
var a=8;
var b=1;
document.write(“<h2>”+a+”+“+b+”=”+(a+b)+”<br></h2>”);
document.write(“<h2>”+a+”-“+b+”=”+(a-b)+”<br></h2>”);
document.write(“<h2>”+a+”*“+b+”=”+(a*b)+”<br></h2>”);
document.write(“<h2>”+a+”/“+b+”=”+(a/b)+”<br></h2>”);
</script>
</h2>
</body>
</html>
The output of the above program:
8+1=9
8-1=7
8*1=8
8/1=8
Javascript comparison operator:
There are following comparison operator supported by JavaScript language:
Assume variable A hold 10 and variable B holds 20 then:
Operator | Description | Example |
== | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (A==B) is not true |
!= | Checks if the value of two operands are equal or not, if the value is not equal then the condition becomes true. | (A!=B) is true |
> | Checks if the value of the left operands is greater than the value of right operand, if yes then condition becomes true. | (A>B) is not true |
< | Checks if the value of the left operand is less than the value of right operand, if yes then the condition becomes true. | (A<B) is true |
>= | Checks if the value of the left operand is greater than or equal to the left operand, if yes then the condition is true. | (A>=B) is not true |
<= | Checks if the value of the right operand is less than or equal to the value of right operand, if yes then the condition is true. | (A<=B) is true |
Example:
<html>
<head><title>Comparison operator</title></head>
<body>
<h2>
<script type=”text/javascript”>
var a=200;
var b=300;
if(a>b)
document.write(“A is greater than B”);
else
document.write(“B is greater than A”);
</script>
</h2>
</body>
</html>
Logical operator:
These are the following logical operators supported by javascript.
Assume that variable A holds 10 and variable B holds 20 then:
Operators | Description | Example |
&& | && is called a logical AND operator. If both the operands are non-zero then the condition becomes true. | (A&&B) is true |
|| | || is called a logical OR operator. If any of the two operands are non zero then the condition becomes true | (A||B) is true |
! | ! is called logical NOT. Use to reverse the logical state of its operands. If a condition is true then the logical NOT operator will make false. | !(A&&B) is false |
Example:
<html>
<head><title>Logical operator</title></head>
<body>
<h2>
<script type=”text/javascript”>
var marks1=24;
var marks2=35;
if(mark1>40 && marks2=>40)
document.write(“ pass”);
else
document.write(“fail”);
</script>
</h2>
</body>
</html>
The output is:
fail
Bitwise operator:
These are the following bitwise operator supported by the javascript language.
Assume that the variable A holds 2 and variable B holds 3 then:
Operator | Description | Example |
& | Called bitwise AND operator. It performs Boolean AND operation on each bit of its integer argument. | (A&B) is 2 |
| | Called bitwise OR operator. It performs Boolean OR operation on each bit of its argument. | (A|B) is 3 |
^ | Called bitwise XOR operator. It performs a Boolean exclusive OR operation on each biut of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. | (A^B) is 1 |
~ | Called bitwise NOT operator. It is a unary operator and operates by reversing all bits in the operands | (~B) is -4 |
<< | Called bitwise shift left operator.it moves all bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zero. shifting a value left by one position is equivalently multiplying by 2, shifting 2 positions is equivalent to multiplying by 4, etc., | (A<<1) is 4 |
>> | Called bitwise shift right with sign operator.it moves all the operands to the right by the number of places specified in the second operand. The bits filled in on the left depends on the sign bit of the original operand, in order to preserve the sign of the result. If the first operand is positive, the result has zeroes placed in the high bits; if the first operands are negative, the result has ones placed in the high bit. Shifting a value right one place is equivalent to dividing by 2(discarding the reminder), shift right two places is equivalent to integer division by 4, and so on | (A>>1) is 1 |
>>> | This operator is called a bitwise shift right with zero operators. This operator is just like the >> operator, except that the bits shifted in on the left are always zero, | (A>>>1) is 1 |
Assignment operator:
These are the following assignment operators supported by the javascript language:
Operator | Description | Example |
= | A simple assignment operator assigns a value from right side operands to left side operand | C=A+B will assign the value of A+B into C |
+= | Add AND assignment operator, it adds right operands to left operands and assigns the result to the left operand. | C+=A is equivalent to C=C+A |
-= | Subtract AND assignment operator, it subtracts right operands to left operands and assigns the result to the left operand. | C-=A is equivalent to C=C-A |
*= | Multiply AND assignment operator, it multiplies right operands to left operands and assigns the result to the left operand. | C*=A is equivalent to C=C*A |
/= | Divide AND assignment operator, it divides right operands to left operands and assigns the result to the left operand. | C/=A is equivalent to C=C/A |
%= | Modulus AND assignment operator takes modulus using two operands and assigns the result to the left operand. | C%=A is equivalent to C=C%A |
Example:
<html>
<head><title>Assignment operator</title></head>
<body>
<script type=”text/javascript”>
var a=1980;
var b=1985;
document.write(a+”++”+b+”=”+(a==b)+”<br>”);
document.write(a+”>=”+b+”=”+(a>=b)+”<br>”);
document.write(a+”<=”+b+”=”+(a<=b)+”<br>”);
</script>
</body>
</html>
The output is:
1980==1985=false
1980>=1985=false
1980<=1985=true
Ternary operator javascript/conditional operator:
There is an operator called a conditional operator. It is also called a ternary operator. this operator first evaluates an expression for a true or false value and then execute one of the two given statement depending upon the result of the evaluation. The conditional operator has this syntax:
Operator | description | Example |
?: | Conditional expression | If the condition is true? Then value X: Otherwise value Y |
Example:
<html>
<head><title>Logical operator</title></head>
<body>
<h2>
<script type=”text/javascript”>
var a=24;
var b=20;
document.write(a>b ? ”true”: “false”);
</script>
</h2>
</body>
</html>
The output is:
true
I hope you guyz understand what is javascript operators and types of operators in detail. If you have any queries or you guys find any mistake then please comment down below. Thank you for reading.
No comments:
Post a Comment