Comparison Equal-to
comparison equal-to operator takes two operands, returns true if left and right operands are equal in value, or false if not
==
symbol is used for comparison equal-to operator
a == 5 //returns true if a has a value of 5
a == b //returns true if a and b has same value
equal-to operator does not check if the operands are of same data type, but only checks if they have same value
a = 5 //a has a value of 5
a == '5' //returns true
a == 5 //returns true
Examples
1. check if value of a
is equal to 5
var a = 5;
if (a == 5) {
console.log('value of a is 5');
} else {
console.log('value of a is not 5');
}
2. check if values in a
and b
are equal to each other
var a = 5;
var b = 7;
if (a == b) {
console.log('a and b are equal in value');
} else {
console.log('a and b are not equal in value');
}
3. check if value in a
is equal to 5
var a = '5';
if (a == 5) {
console.log('value of a is 5');
} else {
console.log('value of a is not 5');
}
a
is assigned with a string type value. comparison equal-to operator does not check the type, but only value