Why is NaN === NaN false? [duplicate]

Posted on

Why is NaN === NaN false? [duplicate] – Even if we have a good project plan and a logical concept, we will spend the majority of our time correcting errors abaout javascript and nan. Furthermore, our application can run without obvious errors with JavaScript, we must use various ways to ensure that everything is operating properly. In general, there are two types of errors that you’ll encounter while doing something wrong in code: Syntax Errors and Logic Errors. To make bug fixing easier, every JavaScript error is captured with a full stack trace and the specific line of source code marked. To assist you in resolving the JavaScript error, look at the discuss below to fix problem about Why is NaN === NaN false? [duplicate].

Problem :

Why does NaN === NaN return false in Javascript?

> undefined === undefined
> NaN === NaN
> a = NaN
> a === a

On the documentation page I see this:

Testing against NaN

Equality operator (== and ===) cannot be used to test a value against NaN. Use isNaN instead.

Is there any reference that answers to the question? It would be welcome.

Solution :

Strict answer: Because the JS spec says so:

  • If Type(x) is Number, then
    • If x is NaN, return false.
    • If y is NaN, return false.

Useful answer: The IEEE 754 spec for floating-point numbers (which is used by all languages for floating-point) says that NaNs are never equal.

This behaviour is specified by the IEEE-754 standard (which the JavaScript spec follows in this respect).

For an extended discussion, see What is the rationale for all comparisons returning false for IEEE754 NaN values?

Although either side of NaN===NaN contains the same value and their type is Number but they are not same. According to ECMA-262, either side of == or === contains NaN then it will result false value.

you may find a details rules in here-


Leave a Reply

Your email address will not be published. Required fields are marked *