JavaScript isNaN() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Global function

Definition

isNaN() answers one question: "Is this value NaN after it is turned into a number?" It returns true or false.

NaN means "not a number". It is the result of things like 0 / 0 or Number("hello"). The tricky part: NaN is not equal to anything, not even itself, so value === NaN never works. That is why isNaN() exists.

Remember
isNaN(value);

value   -> anything; it is converted with Number(value) first
returns -> true if the result is NaN, otherwise false

NaN === NaN is false, so use isNaN() to test for NaN.
Number.isNaN() does the same without the conversion.

Syntax

Call isNaN() by name and give it any value.

JavaScript
isNaN(value);
CallWhat it does
isNaN(NaN)The value is NaN. Result: true.
isNaN("42")The text "42" becomes the number 42. Result: false.
isNaN("hello")The text cannot become a number, so it becomes NaN. Result: true.
isNaN("")Empty text becomes 0. Result: false.

Built into JavaScript (ES1). It is a global function, so you call it by name with no import. Number.isNaN() is a different, stricter function (ES2015). See also: isFinite(), parseInt() and parseFloat().

Library declarations
JavaScript
// Global function (ES1)
isNaN(value)

// returns true or false
// isNaN(x) is the same as Number.isNaN(Number(x))

Parameters

ParameterDescription
valueThe value to test. It can be a number, text, undefined, null, a boolean, an object or an array. It is converted to a number with Number(value) before the test. A BigInt or a Symbol throws a TypeError during that conversion.

Return Value

Return ValueDescription
trueThe value is NaN, or it becomes NaN when converted to a number.
falseThe value is a number, or it converts to a number (including Infinity).

JavaScript Examples

Example 1 — Check Different Values

Pass numbers, text and undefined to isNaN().

JavaScript
console.log("NaN: " + isNaN(NaN));
console.log("42: " + isNaN(42));
console.log("'42': " + isNaN("42"));
console.log("'3.5': " + isNaN("3.5"));
console.log("'hello': " + isNaN("hello"));
console.log("'12px': " + isNaN("12px"));
console.log("undefined: " + isNaN(undefined));
console.log("0 / 0: " + isNaN(0 / 0));
console.log("NaN === NaN: " + (NaN === NaN));
Try It Yourself

How It Works

  1. Numbers and numeric text. 42 and "42" are both fine, so isNaN() returns false.
  2. Text that is not a number. "hello" and "12px" cannot be converted, so they become NaN and the answer is true. Note that "12px" fails here even though parseInt("12px", 10) would read 12.
  3. Maths that fails. 0 / 0 has no answer, so JavaScript gives NaN.
  4. Why you need isNaN(). The last line shows that NaN === NaN is false, so you cannot test for NaN with ===.

Example 2 — Meet the Surprising Results

These values are not numbers, but isNaN() says false. Each one converts to a normal number first.

JavaScript
console.log("Empty text: " + isNaN(""));
console.log("Spaces only: " + isNaN("   "));
console.log("null: " + isNaN(null));
console.log("true: " + isNaN(true));
console.log("Empty array: " + isNaN([]));
console.log("Object: " + isNaN({}));

console.log("Number(''): " + Number(""));
console.log("Number(null): " + Number(null));
console.log("Number(true): " + Number(true));
Try It Yourself

How It Works

  1. Convert, then test. isNaN(value) first runs Number(value) and then checks the result.
  2. Values that become 0 or 1. Empty text, spaces, null and an empty array become 0. true becomes 1. None of these is NaN.
  3. An object does not convert. {} becomes NaN, so isNaN({}) is true.
  4. Lesson. Do not use isNaN() alone to check user input. Check for empty text first, or use Number.isNaN() after converting.

Example 3 — Compare isNaN() and Number.isNaN()

The two functions agree on numbers but not on other values.

JavaScript
const values = ["hello", undefined, 5, NaN];

values.forEach(function (value) {
  const line = String(value) + " -> isNaN: " + isNaN(value) +
    ", Number.isNaN: " + Number.isNaN(value);
  console.log(line);
});

console.log("Same function? " + (isNaN === Number.isNaN));
Try It Yourself

How It Works

  1. Global isNaN() converts. "hello" and undefined become NaN during the conversion, so it returns true.
  2. Number.isNaN() does not convert. It returns true only when the value is really the number NaN. Text and undefined give false.
  3. Same for real numbers. Both functions say false for 5 and true for NaN.
  4. Two functions. Unlike parseInt and parseFloat, isNaN and Number.isNaN are not the same function, so the last line prints false.

Browser & Runtime Support

isNaN() is one of the oldest functions in JavaScript. Every browser and every Node.js version has it. Number.isNaN() arrived later, with ES2015.

✓ ES1 · Works everywhere

isNaN()

Supported in all browsers, including old ones, and in Node.js, Deno and Bun.

ES1 Standard since
Google Chrome All versions · Desktop & Mobile
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Opera All versions
Full support
Samsung Internet All versions · Android
Full support
Android WebView All versions
Full support
Node.js All versions · Server runtime
Supported
Deno All versions · JavaScript runtime
Supported
Bun All versions · JavaScript runtime
Supported
Internet Explorer All versions
Full support
isNaN() Excellent

Bottom line: Safe to use anywhere. The stricter Number.isNaN() is missing only in Internet Explorer.

Frequently Asked Questions

It converts the value to a number and returns true if the result is NaN, otherwise false. For example isNaN("hello") is true and isNaN("42") is false.
NaN is the only value in JavaScript that is not equal to itself, so NaN === NaN is false. Use isNaN(value) or Number.isNaN(value) instead.
isNaN() converts the value to a number first, so isNaN("hello") and isNaN(undefined) are true. Number.isNaN() does no conversion and returns true only for the value NaN itself. Prefer Number.isNaN() when you want a strict check.
Empty text converts to 0, and 0 is a number. The same happens with spaces, null and an empty array. If an empty field should be an error, check for empty text before you convert it.
Trim the text, make sure it is not empty, convert it with Number(), and then test the result with Number.isNaN(). For example: const n = Number(text); if (text.trim() === "" || Number.isNaN(n)) { show an error }.

Next: parseFloat()

Learn how to read numbers with a decimal part from text such as "3.14abc".

parseFloat() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful