Comparison Equal-value and Equal-type
comparison equal-value and equal-type operator takes two operands, returns true if left and right operands are equal in both value and data type, or false otherwise
===
symbol is used for comparison equal-value and equal-type operator
a === 5 //returns true if a has a value of 5 and type of a is numeric
a === b //returns true if a and b has same value and type
since equal-value and equal-type operator does check both type and value, the following example illustrates the case
a = 5 //a has a value of 5 (numeric)
a === '5' //returns false
a === 5 //returns true
Examples
1. check if value of a
is equal to 5
var a = 5;
if (a === 5) {
console.log('a is 5');
} else {
console.log('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');
} else {
console.log('a and b are not equal');
}
3. check if value in a
is equal to 5
var a = '5';
if (a === 5) {
console.log('a is \'5\'');
} else {
console.log('a is not \'5\'');
}
a
is assigned with a string type value. since comparison equal-value and equal-type operator checks both the type and value, it returns false