Back to all posts

2026-04-28

NaN is still a number in JavaScript

A quick reminder that NaN is a sentinel numeric value, not a separate type.

NaN can be confusing the first time you meet it.
The name says "Not a Number", but in JavaScript it is still part of the number type.

const a = 2 / "foo"; // NaN
console.log(typeof a); // "number"

Why this matters:

  • You should treat NaN as an invalid numeric state, not a different data type.
  • Type checks alone (typeof x === "number") are not enough when your value may be invalid.
  • Use Number.isNaN(value) when you need a safe validation step.
const value = Number(input);
if (Number.isNaN(value)) {
  // handle invalid number input
}

Small semantic details like this prevent subtle bugs in forms, filters, and calculations.