isNaN versus Number.isNaN

To me, a method called isNaN should intuitively return false only on numbers and true on everything else. That is exactly what the isNaN() global method does. However, if you're looking to compare a value to NaN (which you cannot do with === or ==), then Number.isNaN is your choice.

For example:

isNaN(' '); // false => because Number(' ') is equal to 0 (a number)isNaN(true); // false => because Number(true) is equal to 1 (a number)

In short, isNaN also tries to perform type conversion. That is why some developers consider it broken.

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.