DEV Community

Cover image for 🧩 Understanding `isNaN()` vs `Number.isNaN()` in JavaScript – Complete Guide
Yogesh Bamanier
Yogesh Bamanier

Posted on

🧩 Understanding `isNaN()` vs `Number.isNaN()` in JavaScript – Complete Guide

Short Description: Learn the key differences between isNaN() and Number.isNaN() in JavaScript. Understand type coercion, pitfalls, and best practices for handling NaN.

Keywords: JavaScript isNaN(), Number.isNaN(), type coercion, NaN handling, JavaScript numbers, ES6


πŸš€ Introduction

In JavaScript, handling NaN (Not-a-Number) values is a common source of confusion.

We have two different methods for checking NaN:

  • isNaN() – the older, global function.
  • Number.isNaN() – introduced in ES6 to fix isNaN() quirks.

Let’s break them down with examples and comparisons πŸ‘‡


πŸ“Œ What is isNaN()?

The isNaN() function checks whether a value is NaN. But before doing so, it tries to convert the value into a number.

βœ… Syntax:

isNaN(value)
Enter fullscreen mode Exit fullscreen mode

βœ… Behavior:

  • true β†’ if the converted value is NaN.
  • false β†’ if the converted value is a valid number.

πŸ‘‰ Example:

isNaN("2");   // false β†’ "2" β†’ 2
isNaN("abc"); // true  β†’ cannot convert β†’ NaN
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ What is Number.isNaN()?

The Number.isNaN() method was introduced in ES6. Unlike isNaN(), it does not coerce values.

βœ… Syntax:

Number.isNaN(value)
Enter fullscreen mode Exit fullscreen mode

βœ… Behavior:

  • Returns true only if the value is literally NaN.
  • No type conversion happens.

πŸ‘‰ Example:

Number.isNaN("2");   // false β†’ it's a string
Number.isNaN("abc"); // false β†’ still a string
Number.isNaN(NaN);   // true  β†’ exactly NaN
Enter fullscreen mode Exit fullscreen mode

βš–οΈ Comparison: isNaN() vs Number.isNaN()

Value isNaN(value) Number.isNaN(value) Why?
NaN βœ… true βœ… true Both detect actual NaN
"123" ❌ false ❌ false String β†’ number works
"ABC" βœ… true ❌ false Global isNaN coerces, Number.isNaN does not
undefined βœ… true ❌ false undefined β†’ NaN via coercion
null ❌ false ❌ false null β†’ 0 when coerced
{} βœ… true ❌ false Object β†’ NaN in coercion
[] ❌ false ❌ false Empty array β†’ 0
true ❌ false ❌ false true β†’ 1

πŸ“ Examples Side by Side

// Classic isNaN()
isNaN("Hello");      // true  β†’ coerced into NaN
isNaN(undefined);    // true  β†’ coerced into NaN

// Modern Number.isNaN()
Number.isNaN("Hello");   // false β†’ it's a string
Number.isNaN(undefined); // false β†’ it's undefined
Number.isNaN(NaN);       // true  β†’ only works for NaN
Enter fullscreen mode Exit fullscreen mode

🎯 Key Takeaways

  • isNaN() β†’ coerces values before checking β†’ can give unexpected results.
  • Number.isNaN() β†’ strict check β†’ only returns true for real NaN.
  • βœ… Best Practice: Always prefer Number.isNaN() in modern JavaScript for reliability.

✍️ Written by Yogesh Bamanier
πŸ”— Connect with me on LinkedIn

Top comments (1)

Collapse
 
cristea_theodora_6200140b profile image
Theodora Cristea

Verry well explained!πŸ‘πŸ»