NaN Result
Quick summary
Your calculation resulted in `NaN` — usually because you mixed numbers with `undefined`, non-numeric strings, or other invalid values.
Why this happens
Parsed a non-numeric string with
parseInt()orNumber()Performed arithmetic with
undefinedor a non-numeric valueDivided zero by zero (
0 / 0) or took the square root of a negative number
Minimal example
const result = 10 + parseInt("abc");
console.log(result); // NaNconst n = parseInt("abc") || 0;
const result = 10 + n;
console.log(result); // 10parseInt("abc") returns NaN, and 10 + NaN is NaN — use || 0 to fall back to 0 when parsing fails.
How to diagnose
Log each operand to find which one is not a number
Use
Number.isNaN()to check if a value is NaN (not the globalisNaN)Trace where the non-numeric value entered the calculation
How to fix
Validate inputs are numbers before performing arithmetic
Use
Number(value)orparseFloat()explicitly and check the resultProvide a default value (e.g.,
const n = parseInt(str) || 0)
How to prevent
Validate numeric inputs at the boundary (API, user input)
Use
Number.isNaN()instead of the globalisNaN()to avoid type coercion
Related Resources
Related Lessons
- Data Types
Learn about JavaScript data types
Related Practice
- JavaScript Primitive Types
Understand primitive types and how NaN behaves.