Equal-value and Equal-type Operator in JavaScript



Equal-value and Equal-type Comparison Operator

In JavaScript, Equal-value and equal-type comparison operator takes two operands, returns true if left and right operands are equal in both value and data type, or false otherwise.

Syntax

=== 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.