Javascript Equality Operators Identically Equal(===) and Not Identically Equal (!==)

Introduction

The identically equal and not identically equal operators do not convert operands before testing for equality.

The identically equal operator is represented by three equal signs (===).

It returns true only if the operands are equal without conversion, as in this example:

let result1 = ("55" == 55);   // true - equal because of conversion 
let result2 = ("55" === 55);  // false - not equal because different data types 

In this code, the first comparison uses the equal operator to compare the string "55" and the number 55, which returns true.

This happens because the string "55" is converted to the number 55 and then compared with the other number 55.

The second comparison uses the identically equal operator to compare the string and the number without conversion, and of course, a string isn't equal to a number, so this outputs false.

The not identically equal operator is represented by an exclamation point followed by two equal signs ( !==) and returns true only if the operands are not equal without conversion.

let result1 = ("55" != 55);  // false - equal because of conversion 
let result2 = ("55" !== 55);   // true - not equal because different data types 

Here, the first comparison uses the not-equal operator, which converts the string "55" to the number 55, making it equal to the second operand, also the number 55.

Therefore, this evaluates to false because the two are considered equal.

The second comparison uses the not identically equal operator.

null == undefined is true because they are similar values.

null === undefined is false because they are not the same type.




PreviousNext

Related