DEV Community

Discussion on: Some Strange Concept of JavaScript

Collapse
 
peerreynders profile image
peerreynders • Edited

In other words, “the type of not-a-number is number!” Hooray for confusing names and semantics.

The thing is:

  1. that's not even a "JavaScript thing"
  2. it applies to any language that supports the IEEE 754 floating-point storage format — and many languages (including C and C++) do.

What Every Programmer Should Know About Floating-Point Arithmetic or Why don’t my numbers add up?

Any value where all 11 bits of the exponent are set to "1" are considered NaN in IEEE 754-1985.

From that perspective the heading should have been

NaN (not a number)

JavaScript's problem was that it only supports the floating point format (now there's BigInt though that's not as performant as sticking to integers that can be represented accurately within the 53 bit signifcand).

Furthermore isNaN() exists for backward compatibility. New code should be using Number.isNaN() instead:

show('NaN');     // "old: true new: false"
show(undefined); // "old: true new: false"
show({});        // "old: true new: false"
show('blabla');  // "old: true new: false"

function show(value) {
  console.log(`old: ${isNaN(value)} new: ${Number.isNaN(value)}`);
}
Enter fullscreen mode Exit fullscreen mode