Equality Operators in JavaScript
Equality operators in JavaScript are used to compare two values on both the sides and then return true or false.
Operator | Operation | Description |
---|---|---|
“==” | Equality | The equality operator (==) checks whether its two operands are equal, returning a Boolean result. |
“= = =” | Strict equality | The strict equality operator (===) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different. |
“!=” | Inequality | The inequality operator (!=) checks whether its two operands are not equal, returning a Boolean result. Unlike the strict inequality operator, it attempts to convert and compare operands that are of different types |
“!==” | Strict inequality | The strict inequality operator (!==) checks whether its two operands are not equal, returning a Boolean result. Unlike the inequality operator, the strict inequality operator always considers operands of different types to be different. |
Equality (==) : Checks that 'value' is same
The equality operator (==) checks whether its two operands are equal, returning a boolean result.
console.log(10 == 10);
// expected output: true
console.log('hello' == 'hello');
// expected output: true
console.log('1' == 1);
// expected output: true
console.log(0 == false);
// expected output: true
Strict Equality (===) : Checks that 'value and type' are same
The strict equality operator (===) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
console.log(10 === 10);
// expected output: true
console.log('hello' === 'hello');
// expected output: true
console.log('1' === 1);
// expected output: false
console.log(0 === false);
// expected output: false
Inequality (!=) : Checks that 'value' is not same
The inequality operator (!=) checks whether its two operands are not equal, returning a Boolean result. Unlike the strict inequality operator, it attempts to convert and compare operands that are of different types
console.log(10 != 10);
// expected output: false
console.log('hello' != 'hello');
// expected output: false
console.log('1' != 1);
// expected output: false
console.log(0 != false);
// expected output: false
Strict Inequality (!==) : Checks that 'value and type' are not same
The strict inequality operator (!==) checks whether its two operands are not equal, returning a Boolean result. Unlike the inequality operator, the strict inequality operator always considers operands of different types to be different.
console.log(10 !== 10);
// expected output: false
console.log('hello' !== 'hello');
// expected output: false
console.log('1' !== 1);
// expected output: true
console.log(0 !== false);
// expected output: true