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

JS Puzzles · Const

NaN behavior

NaN represents an invalid or undefined numerical result. It is unequal to every value, usually propagates through arithmetic, and should be tested with Number.isNaN() rather than equality.

How it works

NaN is JavaScript's numeric value for an invalid or undefined numerical result. It has unusual comparison semantics: it is unequal to itself and makes relational comparisons false, while usually propagating through arithmetic. Number.isNaN() is the precise predicate because it does not coerce its argument.

Syntax

NaN; Number.isNaN(value)

Parameters

NameTypeDescription
valueanyThe value to inspect for the NaN numeric value.

Returns

A numeric value representing Not-A-Number; NaN-producing operations and comparison behavior are described on the page.

Key points

  • NaN is a special JavaScript numeric value used when a numerical result is not meaningful.
  • Failed conversions, non-real math, and indeterminate forms are among the documented ways to obtain NaN.
  • NaN normally contaminates subsequent arithmetic, though bitwise operations are excluded from this rule.
  • Number.isNaN() is the direct test for NaN; the global isNaN() also exists but applies coercion.
  • A self-comparison such as value !== value can detect NaN, although Number.isNaN() communicates intent better.

Common mistakes

  • Do not test NaN with equality; even NaN === NaN is false. Use Number.isNaN(value) instead.
  • Both directions of an ordering comparison fail: NaN < x and NaN > x are false, as are the corresponding inclusive comparisons.
  • Arithmetic generally propagates NaN, but NaN ** 0 evaluates to 1.

Example: Testing NaN and observing its comparison rules

const values = [Number("oops"), 0 / 0, NaN];

for (const value of values) {
  console.log(Number.isNaN(value), value !== value);
}

console.log(NaN === NaN); // false
console.log(NaN < 1, NaN > 1); // false false
console.log(NaN ** 0); // 1

Use Number.isNaN() to identify the actual NaN value without coercing other types. NaN is unequal to every value, including itself, propagates through most mathematical operations, and relational comparisons with it are false; exponentiation by zero is the notable exception.

When to use it

Detect failed numeric results

Validate parsed or calculated numeric input before using it in later arithmetic.

Check calculation pipelines

Perform a final validation after a calculation chain, because NaN usually propagates through arithmetic.

Use a non-coercing predicate

Avoid false positives from coercion when distinguishing actual NaN from non-number inputs.

Interview questions

QWhat is NaN, and when can it be produced?

NaN is a numeric value representing an invalid or undefined numerical result. It is returned by cases such as failed number conversion, non-real mathematical results, and indeterminate forms.

Follow-up: How can NaN arise from conversion? Number(undefined) and parseInt("blabla") both produce NaN when conversion fails.

QHow should code test whether a value is NaN?

NaN is not equal to any value, including itself, under ==, !=, ===, and !==. Use Number.isNaN(value), which tests for NaN without converting the argument.

Follow-up: Why is the global isNaN() less precise? It first converts non-number arguments, so values such as an empty string can produce misleading results.

QWhat happens when NaN participates in calculations and comparisons?

NaN usually propagates through mathematical operations, so a final check can detect an earlier numerical failure. Relational comparisons involving NaN always return false.

Follow-up: What exception avoids propagation? Exponentiation with a zero exponent returns 1, including NaN ** 0.

  • Number.isNaN()
  • isNaN()
  • Number.NaN
  • Infinity
  • parseInt()
  • Number()

Source: NaN. Checked 2026-10-15.