Our Javascript Code Expression:
Step-by-Step Breakdown
🔸 Step 1: What is NaN?
NaN stands for "Not a Number", and it represents a value that is not a valid number.
Examples that result in NaN:
It’s still a type of number, but it means "an invalid number".
🔸 Step 2: What Does == Do?
-
The
==operator checks equality, allowing type coercion if needed. - But even with coercion,
NaNis never equal to anything, not even itself.
🔸 Step 3: Why is NaN == NaN false?
This is defined in the IEEE 754 floating-point standard, which JavaScript follows.
According to this spec:
“NaN is not equal to any value, including itself.”
So even if you write:
It’s still false.
✔ Final Answer:
B) false
This question appeared in the HCL Tech 2nd round of interview, testing the candidate’s knowledge of NaN and loose equality (==) operator in JavaScript.
How to Properly Check for NaN?
Since you can't use == or === to check for NaN, JavaScript gives you a built-in method:
🔹 Use Number.isNaN()
Bonus Point: Object.is(NaN, NaN)
If you're looking for strict identity comparison, use:
Unlike == or ===, Object.is() considers NaN equal to NaN.
Summary
| Expression | Result | Why? |
|---|---|---|
NaN == NaN | false | NaN is not equal to itself |
NaN === NaN | false | Even strict equality fails |
Number.isNaN(NaN) | true | Proper way to check for NaN |
Object.is(NaN, NaN) | true | Another correct comparison tool |
