Operator in Java Script :
An operator performs operation between multiple or single data value to make a result out of them.
These are the types of operator :
Arithmetic Operator:
Arithmetic Operator use for mathematical operation between multiple numeric data or operands.
These are following Arithmetic operator :
Arithmetic Operator works in following way:
let a= 10;
let b= 2;
let Y= a+b;
document.write(Y)
The output of this code is:-
12
We can add operands with + operands like the example. And document.write just publishe the value. We can also write console.log to get the output. console.log show the output result in the console of your browser. It is a great way to test your code.
Example:-
let x= 45;
let y= 55;
let Q= x+y;
console.log(Q)
Output:
100
Firstly run the code and open console to see the result.
" - " Operand subtract right operand to left operand.
" * " Operand multiply numeric operand.
Example:
let a= 10;
let b= 20;
let Y= a*b;
document.write(Y)
Output:
200
The x variable is 10 and y variable is 20. And the Z variable was x*y. So x store the data 10 and y store the data 20. And Z multiply 10*20=200
Thats why document.write(Z) is 200
" / " Operator divide left operand by write operand.
Example:
let a= 10;
let b= 2;
let Y= a/b;
document.write(Y)
Output :
5
x variable is 10 and y variable is 2. Z variable store x/y. Thats mean 10/2. The document.write show the result by dividing 10 by 2 and that is 5.
++ Increment Operator:
Increment Operator increase operand value by One. Its mean +1 for every time.
-- Decrement Operator :
Decrement Operator decrease operand value by 1. Its mean -1 every time.
% Modulus Operator :
Modulus Operator is use for return remainder of two operand.
Code:
let F=10;
let Y=105;
Let Z= F % Y;
document.write(Z)
Result:
5
The result is 5 because the remainder in 5. If we set 10%100 thet the result will be 0.
Comparison Operator :
Comparison Operator compare two operator and returns boolean value among the operands. Boolean value means true or false.
== Operator(double equal)
This operator check the equality of to operands value but not type. If we set it between two operands that's value is same but the type is not same it will return true. If the value are not same it will return false.
=== Operator(Triple Equal)
This operator check the value of two operands as well as the type. If the type and value both are same then it will return true. Otherwise it will return false.
!= Operator (Not Equal)
This operator check that the operands are not same. If we set this operand between two operand and their value are not same it will return true. Otherwise it will return false.
> Operator :
If left side operand is greater than right side operand then it returns true. Otherwise it returns false.
< Operator:
If right side operand is greater than left side operand then it returns true. If not it returns false.
>= Operator:
If the left side operand is greater than right side or they are equal then it returns true. Otherwise it returns false.
<= Operator:
If the right side operand is greater than the left side operand or they are equal then it returns true. Otherwise it returns false.
Example:
let Z=10;
let X=20;
let Y= 10;
let A="10";
For the following value:
Z==A; // True
Z===A ; // False
X>Z; // True
X<Z // False
Z!=X // True
Z!=Y // False
X>=Z // True
Z>=Y // True
Z<=X \\ True
Logical Operator:
Logical operator are use for combine multiple condition. There are 3 types of Logical Operator in Java Script.
1: || (Or operator)
2: && (And operator)
3: ! (Not operator)
|| (Or)
Or operator use for manipulate only boolean values. If any of the given condition are true then it returns True.
If none of the given conditions are true it returns false.
As we can see:
If (truth || truth) // return truth
If (truth || false) // return truth
If (false || truth) // return truth
If (false || false) // return false
The truth is considered here 1 and the false is considered as 0.
Note: 0, "", null, undefined, false all is 0 here.
&& (And)
And && operator also use for manipulate boolean value only. If any of the conditions are false it returns false. If all of the conditions are true then and only then it returns truth.
As we can see:
If (truth || truth) // return truth
If (truth || false) // return false
If (false || truth) // return false
If (false || false) // return false
! ( Not)
Not operator ! convert operand in boolean. And it reverse the value.
Example:
If (!true); // return false
If (!0); // return true
Assignment Operator:
Assignment Operator use to assign value.
= Operator assign left operands value to right operands value.
+= Operator sums up the left operands value to the right operands value and assign it to the left operand value.
-= Operator subtract right operand value from the left operand value and assign it to the left operand value.
*= Operand multiply the right operand value to the left operand value and assign the value to the left operand value.
/= Operand divide the left operand value by the right operand value and assign it to the left operand value.
%= Operand take the remainder after divide the left operand value by the right operand value and assign the remainder to the left operand value.
Example :
Let T= 10;
Let W=3;
Let S= 5;
T = S ; // T will be 5
T += S ; // T will be 15
T -= W ; // T will be 7
T *= S ; // T will be 50
T %= W ; // T will be 1
Conditional Operator :
Conditional Operator is also known as tenary operator. This operator takes three operands.
Sometime this operator is use as the short form of if else statement.
Example :
Let age = 17;
Let drinks= (age >= 16) ? "Whiskey" : "Coke";
console.log(drinks); // "Whiskey"
Let age = 13;
Let drinks= (age >= 16) ? "Whiskey" : "Coke";
console.log(drinks); // "Coke"