-3

Why

isNaN(undefined) !== Number.isNaN(undefined) 

is true.

Why true

console.log(isNaN(undefined)) //is true

But if I add Number.

console.log(Number.isNaN(undefined)) // is false
TSR
  • 17,242
  • 27
  • 93
  • 197

2 Answers2

2

The Number.isNaN function "is a more robust version" of the global isNaN function. Specifically, the rules for the global isNaN function are odd and that's why Number.isNaN is typically preferred. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#Description.

According to that, isNaN essentially checks whether your value is not a number, whereas Number.isNaN checks whether your value is NaN. Of course, not all nonnumeric values are NaN - "test" for example, is not NaN but is also not a number.

Ian
  • 5,704
  • 6
  • 40
  • 72
  • 2
    "*isNaN essentially checks whether your value is not a number*" more specifically it checks if the value you supply *when converted to a number* produces `NaN`. So, `isNaN("123")` will report `false` because `Number("123")` does not result in `NaN`, although the value itself is definitely *not* a number but a string. However, `isNaN("hello")` returns `true` because `Number("hello")` does produce `NaN`. In short, yes - confusing. – VLAZ Jul 31 '19 at 13:54
0

isNaN and !anything will always give you Booleans.

parseFloat('27') gives you the Number 27.

27 is not false, even with type conversion.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335