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.
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".
Where Does NaN Come From?
0 / 0, Math.sqrt(-1)parseInt("hello") returns NaN"abc" * 3 produces NaNThe NaN Paradox
NaN is the only value in JavaScript that is not equal to itself. This means you cannot use === to check for NaN.
Detecting NaN: Two Tools
true if the value is literally NaN.isNaN vs Number.isNaN
| Expression | isNaN() | Number.isNaN() |
|---|---|---|
NaN | true | true |
"hello" | true | false |
undefined | true | false |
42 | false | false |
"42" | false | false |
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).
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.
The Parsing Pipeline
parseInt vs parseFloat
| Feature | parseInt | parseFloat |
|---|---|---|
| Return type | Integer | Floating-point |
| Radix parameter | Yes (2nd arg) | No (always base 10) |
| Handles decimals | Truncates | Preserves |
"3.14" | 3 | 3.14 |
Hex string "0xFF" | 255 (with radix 16) | 0 |
"10abc" | 10 | 10 |
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.
Step-by-Step: Safe Parsing
parseInt for integers, parseFloat for decimals, or Number() for strict validation.parseInt. Write parseInt(str, 10) for base-10.Number.isNaN(result) to see if parsing failed.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:
Example 2: CSS Value Extraction
Extract the numeric value from a CSS property string "16.5px":
Example 3: Defensive Parsing
Build a function that safely parses a price string or returns a default:
Common Mistakes
x === NaN is always false. Use Number.isNaN(x) instead.parseInt("08") may behave oddly in old engines. Always write parseInt("08", 10).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? |
|---|---|---|---|
parseInt | Truncates | Ignores | Yes |
parseFloat | Preserves | Ignores | No |
Number() | Preserves | NaN | No |
+str | Preserves | NaN | No |
Question 1 of 5
What does typeof NaN return in JavaScript?
typeof NaN returns "number" because NaN is part of the IEEE 754 numeric type in JavaScript.Question 2 of 5
What does parseInt("3.9px", 10) return?
parseInt reads left-to-right, collects the integer 3, and stops at "." because a decimal point is not a valid integer digit. It does not round: it truncates.Question 3 of 5
Which expression correctly checks if a variable x is NaN?
NaN !== NaN, comparing with === or == always returns false. Number.isNaN(x) is the reliable, strict check. There is no .isNaN() method on values.Question 4 of 5
What is the difference between isNaN("hello") and Number.isNaN("hello")?
isNaN coerces "hello" to a number first (getting NaN), so it returns true. Number.isNaN does not coerce: "hello" is a string, not NaN, so it returns false.Question 5 of 5
What does Number("42px") return?
Number() requires the entire string to be a valid number. Since "42px" contains non-numeric characters, it returns NaN. Use parseInt("42px", 10) if you want 42.