Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

NaN equality

NaN is unequal to every value, including itself, under == and ===. Detect it with Number.isNaN() rather than an equality comparison.

How it works

NaN is a numeric value produced by invalid or undefined numerical operations. Unlike ordinary values, it is unequal to every value, including itself, when tested with == or ===; use Number.isNaN() to check for it, while Object.is() considers NaN equal to itself.

Syntax

Number.isNaN(value)

Parameters

NameTypeDescription
valueanyThe value to test for the numeric NaN value.

Returns

A Boolean indicating whether the supplied value is NaN.

Key points

  • NaN is a Number value with special comparison behavior rather than a missing or undefined value.
  • Both loose and strict equality treat NaN as unequal to every value, including another NaN.
  • Use a NaN-detection function instead of comparing the value to NaN.
  • The strict equality operator preserves this special rule without coercing operands.
  • Same-value-zero comparisons, such as those used by some collection operations, can match NaN.

Common mistakes

  • Do not test NaN with == or ===; both comparisons return false even when both operands are NaN.
  • Relational operators also cannot establish ordering for NaN: every listed comparison evaluates to false.

Example: Detecting NaN safely

const value = Number("not a number");

console.log(value === value); // false
console.log(Number.isNaN(value)); // true
console.log(Object.is(value, value)); // true

The snippet demonstrates that strict equality does not identify NaN, while Number.isNaN does and Object.is treats the two NaN values as the same.

When to use it

Input validation

Validate parsed or calculated numeric input before further arithmetic.

Error detection

Identify invalid numeric results produced by conversions or mathematical operations.

Data cleaning

Filter or classify numeric data containing invalid values.

  • Number.isNaN()
  • isNaN()
  • Object.is()
  • strict equality (===)
  • loose equality (==)
  • SameValueZero

Source: NaN - JavaScript | MDN Web Docs. Checked 2026-10-15.