Unexpected NaN in a JavaScript calculation
Unexpected NaN in a JavaScript calculation in JavaScript means arithmetic on undefined, or on a string that is not a clean number, produces NaN — and every calculation that touches NaN is NaN too.
How do I fix Unexpected NaN in a JavaScript calculation?
NaN in a JavaScript calculation means one value in the maths was undefined or a string that is not a clean number. Common causes are a missing object property, a missing function argument, or Number("1,000"). Log each operand to find the first NaN, use item.price ?? 0 for missing values, and check results with Number.isNaN(value), not === NaN.
- 1. Log each operandLog every value in the calculation. The first undefined or odd string is the source of the NaN.
- 2. Check missing propertiesOne item without a field adds undefined to a sum. Use item.price ?? 0 inside reduce.
- 3. Use Number.isNaNvalue === NaN is always false. Use Number.isNaN(value) or Number.isFinite(value) to check.
What does Unexpected NaN in a JavaScript calculation mean?
NaN ("not a number") is what JavaScript returns instead of throwing when arithmetic makes no sense. It spreads: NaN + 1 is NaN, so one bad value poisons a whole sum. It is also the only value not equal to itself — NaN === NaN is false — so checks with === never catch it.
Why does Unexpected NaN in a JavaScript calculation happen?
1. A missing property in a sum
One item without the field contributes undefined, and undefined + number is NaN.
Breaks
const total = cart.reduce((sum, item) => sum + item.price, 0);Works
const total = cart.reduce((sum, item) => sum + (item.price ?? 0), 0);2. Converting a string that is not a clean number
Number("1,000") and Number("12px") are NaN. parseInt("") is NaN too.
Breaks
const n = Number("1,000");Works
const n = Number("1,000".replaceAll(",", ""));3. A function called with a missing argument
A missing argument is undefined, and undefined * number is NaN.
Breaks
function area(w, h) {
return w * h;
}
area(5); // NaNWorks
function area(w, h) {
return w * h;
}
area(5, 3); // 154. Checking for NaN with ===
NaN is never equal to anything, including NaN.
Breaks
if (value === NaN) { /* never runs */ }Works
if (Number.isNaN(value)) { /* ... */ }How to find it in your own code
Find the first NaN, not the last: log each operand of the calculation and look for undefined or a non-numeric string. Validate numbers where they enter the program (form fields, JSON, query strings) with Number.isFinite, so a bad value fails loudly at the boundary instead of quietly becoming NaN later.
Try it: fix this bug
easysumAll([1, 2, 3]) returns NaN.
function sumAll(nums) {
let total = 0;
for (let i = 0; i <= nums.length; i++) {
total = total + nums[i];
}
return total;
}More challenges with this error
Each of these challenges runs into this error. Find the bug and fix it, no install and no account needed.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.