- Using ‘==’ operator
- Using ‘===’ operator
- SameValueZero: used mainly in sets, maps and arrays.
- SameValue: used elsewhere
Note: Type coercion means that the two values are compared only after attempting to convert them into the same type. Let’s look at all the values in which the ‘==’ operator will return true.
true true true true true true
- ‘false’ // false wrapped in string.
- ” or “” // empty string
- NaN // not-a-number
So both null and undefined are false values and they represent an ’empty’ value or undefined in js, hence the comparison with ‘==’ operator returns true. Now, let’s look at all the values in which the ‘==’ operator will return false.
Example 2: In this code when we compare null with false we get false, as null being a primitive data type it can never be equal to a boolean value, even though they belong to the same false group.
false false false false
Example 1: Let’s see some code where ‘===’ operator will return true.
true true true
Example 2: Simply check the types and values at both sides and then just print out the boolean true or false. Some examples where it will return false.
false false false