EasyType Coercion🐛 Debug Challenge

NaN comparison is always false

Buggy Code — Can you spot the issue?

function validate(n) {
  if (n === NaN) {
    console.log('invalid');
  } else {
    console.log('valid: ' + n);
  }
}

validate(NaN);
validate(5);

Fixed Code

function validate(n) {
  if (Number.isNaN(n)) {
    console.log('invalid');
  } else {
    console.log('valid: ' + n);
  }
}

validate(NaN);
validate(5);

Bug Explained

Bug: NaN is the only value not equal to itself. n === NaN is always false for any value, including NaN.

Explanation: Number.isNaN() is the only reliable way to detect NaN. The global isNaN() coerces its argument first — Number.isNaN() does not.

Key Insight: Never use === NaN. NaN !== NaN is always true. Use Number.isNaN().

More Type Coercion Debug Challenges

EasyLoose equality treats 0 and "" as falseMediumPlus operator concatenates strings instead of adding

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab