JS Interview #6 — Introduction to JavaScript NaN

Mighty Ghost Hack
Mighty ghost hack
Published in
2 min readFeb 4, 2023
JavaScript NaN

In this blog, you’ll learn about JavasScript NaN, how to verify whether a value is NaN, and how to handle NaN effectively.

JavaScript NaN

In JavaScript, we have a number type that allows you to represent numbers including integer and floating-point numbers.

JavaScript number has a unique value called NaN, which stands for Not–a–Number. It represents a value that is not a valid number.

The NaN has the type number

console.log(typeof NaN); // number

JavaScript isNaN() & Number.isNaN() Function

JavaScript has a built-in method, which is isNaN() & Number.isNaN() Functions and those used to check whether a given value is an illegal number or not.

Both isNaN() & Number.isNaN() works differently.

isNaN() method returns true if a value is Not-a-Number.

Number.isNaN() method returns true if the value is Not-a-Number, and a type is a Number.

isNaN() Example

console.log(isNaN(12));               // false
console.log(isNaN(0 / 0)); // true
console.log(isNaN(12.3)); // false
console.log(isNaN("Geeks")); // true
console.log(isNaN("13/12/2020")); // true
console.log(isNaN(-46)); // false
console.log(isNaN(NaN)); // true

Number.isNaN() Example

console.log(Number.isNaN(12));               // false
console.log(Number.isNaN(0 / 0)); // true
console.log(Number.isNaN(12.3)); // false
console.log(Number.isNaN("Geeks")); // false
console.log(Number.isNaN("13/12/2020")); // false
console.log(Number.isNaN(-46)); // false
console.log(Number.isNaN(NaN)); // true

Key Points to remember

  • isNaN() & Number.isNaN() are javascript build in methods
  • NaN is NEVER equal to itself
  • Output of NaN === NaN & NaN == NaN always returns false
  • Object.is(NaN, NaN) will return true, it treats NaN differently.
  • To check the NaN value, you can use a predefined function Object.is(NaN, NaN), Number.isNaN(), isNaN() and isFinite() methods.

To understand in a much better way here is the video link

--

--

Mighty Ghost Hack
Mighty ghost hack

A passionate full-stack developer, Ethical Hacker, Blogger, Youtuber and many more