DEV Community

Cover image for Why Does NaN === NaN Return False in JavaScript?
Agunechemba Ekene
Agunechemba Ekene

Posted on

2 1 1

Why Does NaN === NaN Return False in JavaScript?

In JavaScript, NaN === NaN evaluates to false, which might seem strange at first. To understand this behavior, we need to explore what NaN represents and why it behaves this way.

What is NaN?
NaN stands for “Not-a-Number” and is used to represent the result of invalid or undefined numerical operations. For example:

console.log(0 / 0);       // NaN
console.log(Math.sqrt(-1)); // NaN

Enter fullscreen mode Exit fullscreen mode

Why Does NaN === NaN Return False?

According to the IEEE 754 floating-point standard, NaN is not equal to anything, including itself. This ensures that invalid results aren’t mistakenly treated as valid. In simple terms, NaN signals an error, and JavaScript treats it as incomparable to anything.

How to Check for NaN
Since direct comparisons won’t work, use these methods to check for NaN:

  • Global isNaN() – checks if a value is NaN or can be converted to it:
console.log(isNaN("abc"));    // true
Enter fullscreen mode Exit fullscreen mode
  • Number.isNaN() – specifically checks for NaN without conversion:
console.log(Number.isNaN(NaN)); // true
Enter fullscreen mode Exit fullscreen mode

Conclusion
NaN === NaN returns false because NaN represents an error, not a valid number. To check for NaN, use isNaN() or Number.isNaN() to avoid confusion and bugs.

Source

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay