JS-035

Beginner4 min~4 min read#nan#number#type-conversion
NaN
Not a Number
Goal
Understand what NaN means and when it appears.
Explanation
- NaN means Not a Number.
- It appears when JavaScript cannot produce a valid number.
- For example, Number("Pizza") returns NaN.
- NaN has the Number type, even though it actually represents an invalid numeric result.
- Use Number.isNaN() to check for NaN.
Analogy
Imagine giving a calculator the task of calculating the word “Pizza.” It does not know what to do, so it returns the special value NaN.
Code
console.log(Number("Pizza"));
console.log(Number("123"));Result: NaN 123
Common mistake
Beginners may think NaN is a string or an error. It is actually a special numeric value.
Remember
- ✦ NaN means Not a Number.
- ✦ NaN appears after a failed numeric conversion.
- ✦ Number.isNaN() checks for NaN.
- ✦ NaN has the Number type.
Quick Quiz
What does Number("Pizza") return?