DEV Community

Discussion on: 10 Challenging JavaScript Quiz Questions and Answers

Collapse
 
objectmatrix profile image
Ahm A • Edited

the equality operator (==) will attempt to make the data types the same before proceeding

the identity operator (===) requires both data types to be the same, as a prerequisite.

Collapse
 
pentacular profile image
pentacular

We can tell that === is not an identity operator since it may return false when comparing a value with itself.

e.g. NaN === NaN.

Thread Thread
 
objectmatrix profile image
Ahm A • Edited

NaN === NaN // false, The identity evaluation algorithm (IEA) rule 4
Operands are the same type (numbers), but the IEA rule 4 indicates than nothing is equal with a NaN.
The result is false.

The identity evaluation algorithm (IEA) ===:

1. If both operands have different types, they are not strictly equal
2. If both operands are null, they are strictly equal
3. If both operands are undefined, they are strictly equal
4. If one or both operands are NaN, they are not strictly equal
5. If both operands are true or both false, they are strictly equal
6. If both operands are numbers and have the same value, they are strictly equal
7. If both operands are strings and have the same value, they are strictly equal
8. If both operands have reference to the same object or function, they are strictly equal
9. In all other cases operands are not strictly equal.
Enter fullscreen mode Exit fullscreen mode

Also,NaNs are the only non-reflexive value, i.e., if x !== x, then x is a NaN

more details:
dmitripavlutin.com/the-legend-of-j...

Thread Thread
 
pentacular profile image
pentacular

The page you've referenced is mostly correct, but they're a bit confused on terminology, and have made things rather more complicated that necessary.

See: ecma-international.org/publication...

7.2.16 Strict Equality Comparison
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Number or BigInt, then a. Return ! Type(x)::equal(x, y).
    1. Return ! SameValueNonNumeric(x, y).

Now, what this means is that some things with the same identity compare false with ===, and some things with different identities compare true with ===.

e.g.

NaN === NaN is false

-0 === 0 is true

Which means that it is not checking the identity of the operands -- it is computing a kind of equality (using the strict equality comparison).

So, === is not an identity operator, and for these reasons is not called an identity operator in the language specification.

It's just some confused people on the internet who are making that into a popular mistake. :)