2

Why does Number.isNaN("he") give false instead of true seeing that "he" is a string and not a number; but then Number.isNaN(NaN), only, gives true and nothing else give true? What is then the essence of this function?

Reading through mdn, I see that The Number.isNaN() method determines whether the passed value is NaN and its type is Number. Now I'm starting to think of a value whose value is NaN but has a type Number. I understand the usecase of isNaN(). I also want to know in what scenario one is to use Number.isNaN().

// this works well
isNaN("7") //false works as expected
isNaN("Hello") // true works as expected
Number.isNaN(NaN) // true works as expected
Number.isNaN("hello") // false wasn't expecting this
Mazimia
  • 192
  • 2
  • 11
  • Well, `NaN` ? ... – Jonas Wilms Nov 07 '19 at 11:21
  • `Number.isNaN` did get introduced additionally to the global `isNaN` exactly because it returns true iff the parameter **is** NaN. No coercion, no shenanigans. As you explain: Not of type number? return false! – ASDFGerte Nov 07 '19 at 11:21
  • Have a look here: https://stackoverflow.com/questions/33164725/confusion-between-isnan-and-number-isnan-in-javascript. Duplicate – Adrian Roworth Nov 07 '19 at 11:23
  • 1
    See the linked question's answers for details, but basically: [`Number.isNaN`](https://tc39.es/ecma262/#sec-number.isnan) is behaving exactly as it's supposed to. Unlike `isNaN`, `Number.isNaN` **doesn't coerce/convert** the value you give it. That's what it's for, it's not just another name for the global `isNaN`. `"hello"` isn't `NaN` because it's not a `number` value. The other `Number` static functions are similar (`isFinite("10")` is `true`, but `Number.isFinite("10")` is `false` because `"10"` is a string, not a number). – T.J. Crowder Nov 07 '19 at 11:23
  • From the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#Description): *"only values of the type number, that are also NaN, return true"* – adiga Nov 07 '19 at 11:24

0 Answers0