JavaScript Comparison Operators
Equality Operator(==)
The JavaScript language has the ability to perform type conversion at run time. With the equality operator, if the value on both sides of the equation is the same regardless of the type, the result is returned true, otherwise false.
1 == 1 // true
"1" == 1 //true
1 == '1'// true
0 == false // true
0 == null // false
Example:
var a=3;
if(a==3){
alert("Result is True");
}else{
alert("Result is False");
}
Example:
var a=3;
if(a==>"3"){
alert("Result is True");
}
Example:
var a=3;
if(a==7){
alert("Result is True");
}else{
alert("Result is False");
}
Inequality Operator(!=)
It returns true if the two values or variables being compared are different from each other, regardless of their type, and false if they are the same.
1 != 2 // true
1 != "1" // false
1 != '1' // false
1 != true // false
0 != false // false
Example:
var a=3;
var b=5;
if(a!=7){
alert("a is different from b.");
}
Exact Equality Operator(===)
If the two values or variables being compared are the same in terms of both type and value, the comparison result returns true; if one of the types or values is different, it returns false.
3 === 3 // true
3 === '3' // false
Example:
var a=3;
var b="3";
if(a!=7){
alert("two variables are equal in type and value.");
}
else
{
alert("the two variables are different in type and value.");
}
Exact Inequality Operator (!==)
It returns true if the compared data are different in terms of both type and value, and false if the two values are equal in both type and value.
3 !== '3' // true
4 !== 3 // true
Example:
var a=3;
var b=3;
if(a!=7){
alert("two variables are equal in type and value.");
}
else
{
alert("the two variables are different in type and value.");
}
Greater Than Operator ( > )
When comparing two values, the first value is greater than the second value.kse produces true, if the second value is greater than or equal to false.
4 > 3 //true
Greater Equal Operator ( >= )
If the first value of two compared values is greater than or equal to the second value, it produces true, and if the second value is greater than false.
4 >= 3 // true
3 >= 3 // true
Less Than Operator ( < )
When comparing two values, it produces true if the first value is less than the second value, and false if the second value is less than or equal to the second value.
3 < 4 // true
Less than Equal Operator ( <= )
If the first value is less than or equal to the second value, it produces true, and if the second value is less than false, it produces false.
3 <= 4 // true