Which fix removes the bug?
const n = Number(input);
if (n === NaN) {
showError(); // never runs
}
- Test with
Number.isNaN(n) - Use
n == NaNinstead - Use
typeof n === 'NaN' - Compare
n === undefined
NaN is not equal to anything, including itself, so both == and === comparisons are always false. Number.isNaN checks for it directly.
Play Problem Solving ▸