NaN === NaN is false in JavaScript

How to check for NaN when checking for a number in JavaScript

NaN is the only JavaScript value not equal to itself, so if you see a check for self-equality in JavaScript it is a NaN check, equivalent to Number.isNaN().

Dr. Derek Austin 🥳
1 min readDec 5, 2019

--

Hi Jakub Szlapański, thanks for stopping by with your response to my article on how to check for JavaScript numbers!

You’re right, it seems like the following code checking for self-equality should always return true in JavaScript, since values should equal themselves.

The reality is that NaN (Not-A-Number) doesn’t follow that rule:

Indeed, I wrote about checking for NaN in Coding in Simple English:

I also have written about using Object.is() in JavaScript in Coding at Dawn:

Thanks again for sharing your response and helping me clarify this point!

Photo by Rattap on Unsplash

--

--

Dr. Derek Austin 🥳
Dr. Derek Austin 🥳

Written by Dr. Derek Austin 🥳

Hi, I'm Doctor Derek! I've been a professional web developer since 2005, and I love writing about programming with JavaScript, TypeScript, React, Next.js & Git.

No responses yet