- 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 value in which the ‘==’ operator will return 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 value in which the ‘==’ operator will return false.
In the above 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 falsy group.
‘===’ operator: Also known as strict equality operator, it compares both the value and the type that is why the name “strict equality”.
Let’s see some code where ‘===’ operator will return true.
Simply checking the types and values at both the sides and then just printing out the boolean true or false.
Some example where it will return false.
Improved By : immukul