01 / 22
NaN → 42

Handling NaN &
Parsing Numbers

Learn how JavaScript represents non-numeric results, detect them with isNaN and Number.isNaN, and convert strings to numbers with parseInt and parseFloat.

JavaScript

What is NaN?

NaN stands for "Not-a-Number". It is a special value in JavaScript that signals a numeric operation failed to produce a meaningful number. Despite its name, typeof NaN returns "number".

const result = 0 / 0; console.log(result); // NaN console.log(typeof result); // "number"

Where Does NaN Come From?

Invalid Math
0 / 0, Math.sqrt(-1)
Failed Parsing
parseInt("hello") returns NaN
Coercion Gone Wrong
"abc" * 3 produces NaN

The NaN Paradox

NaN is the only value in JavaScript that is not equal to itself. This means you cannot use === to check for NaN.

console.log(NaN === NaN); // false (!!) console.log(NaN !== NaN); // true
NaN !== NaN always! NaN is never equal to anything, including itself

Detecting NaN: Two Tools

isNaN(value)
The global function. It coerces the argument to a number first, then checks. This can give surprising results.
Number.isNaN(value)
The strict version (ES6). It does not coerce: only returns true if the value is literally NaN.
isNaN("hello"); // true (coerces "hello" → NaN) Number.isNaN("hello"); // false ("hello" is a string, not NaN) isNaN(NaN); // true Number.isNaN(NaN); // true

isNaN vs Number.isNaN

Expression isNaN() Number.isNaN()
NaNtruetrue
"hello"truefalse
undefinedtruefalse
42falsefalse
"42"falsefalse

Introducing parseInt

parseInt(string, radix) reads a string from left to right and extracts an integer. It stops at the first character that is not a valid digit for the given radix (base).

parseInt("42"); // 42 parseInt("42px"); // 42 (stops at "p") parseInt(" 99 bottles"); // 99 (leading spaces ignored) parseInt("hello"); // NaN (no leading digits)

Introducing parseFloat

parseFloat(string) works similarly to parseInt, but it also recognizes decimal points. It always parses in base 10 and has no radix parameter.

parseFloat("3.14"); // 3.14 parseFloat("3.14.159"); // 3.14 (stops at second ".") parseFloat("0.5em"); // 0.5 parseFloat("width"); // NaN

The Parsing Pipeline

Input "42.5px" Skip leading whitespace Read valid digits Result 42.5 Input "hello" Skip leading whitespace No valid digits found Result NaN Both parseInt and parseFloat follow this left-to-right pattern

parseInt vs parseFloat

Feature parseInt parseFloat
Return typeIntegerFloating-point
Radix parameterYes (2nd arg)No (always base 10)
Handles decimalsTruncatesPreserves
"3.14"33.14
Hex string "0xFF"255 (with radix 16)0
"10abc"1010

Number() and the + Operator

Beyond parseInt and parseFloat, you can convert strings to numbers with Number() or the unary + operator. These are stricter: the entire string must be a valid number.

Number("42"); // 42 Number("42px"); // NaN (the whole string must be numeric) Number(""); // 0 (empty string → 0) Number(" 3.14 "); // 3.14 (trims whitespace) +"99"; // 99 (shorthand for Number()) +"nope"; // NaN

Step-by-Step: Safe Parsing

1
Choose your parser. Use parseInt for integers, parseFloat for decimals, or Number() for strict validation.
2
Always pass the radix to parseInt. Write parseInt(str, 10) for base-10.
3
Check the result. Use Number.isNaN(result) to see if parsing failed.
4
Provide a fallback. If NaN, use a default value: const val = Number.isNaN(parsed) ? 0 : parsed;

Example 1: Parsing User Input

A user types "25" into an age field. Convert it to a number and validate:

const input = "25"; const age = parseInt(input, 10); console.log(age); // 25 console.log(Number.isNaN(age)); // false ✓ valid console.log(typeof age); // "number"

Example 2: CSS Value Extraction

Extract the numeric value from a CSS property string "16.5px":

const cssVal = "16.5px"; const asInt = parseInt(cssVal, 10); // 16 (truncated!) const asFloat = parseFloat(cssVal); // 16.5 (preserved!) const asNum = Number(cssVal); // NaN (strict: "px" fails)

Example 3: Defensive Parsing

Build a function that safely parses a price string or returns a default:

function safePrice(str) { const parsed = parseFloat(str); if (Number.isNaN(parsed)) { return 0; // fallback } return parsed; } safePrice("9.99"); // 9.99 safePrice("free"); // 0 (NaN caught!) safePrice("12.50ea"); // 12.5

Common Mistakes

✕ Comparing with ===
x === NaN is always false. Use Number.isNaN(x) instead.
✕ Forgetting the Radix
parseInt("08") may behave oddly in old engines. Always write parseInt("08", 10).
✕ Using isNaN Loosely
isNaN("abc") returns true even though "abc" is not NaN: it is a string. Prefer Number.isNaN.

Summary: Conversion Methods

Method Decimals? Trailing text? Radix?
parseIntTruncatesIgnoresYes
parseFloatPreservesIgnoresNo
Number()PreservesNaNNo
+strPreservesNaNNo

Question 1 of 5

What does typeof NaN return in JavaScript?

Question 2 of 5

What does parseInt("3.9px", 10) return?

Question 3 of 5

Which expression correctly checks if a variable x is NaN?

Question 4 of 5

What is the difference between isNaN("hello") and Number.isNaN("hello")?

Question 5 of 5

What does Number("42px") return?