-2
console.log(isNaN('hello'))

Output = true

console.log(Number.isNaN('hello'))

Output = false

I don't understand why I am getting [false] here. Can anyone please help me understand the concept between these two?

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140

2 Answers2

1

The Number.isNaN() method determines whether the passed value is NaN and its type is Number. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN

The value "hello" does not have a type of Number, so Number.isNaN will return false.

if the value hello does not have a type number, what happens if I pass a number instead of Hello ... console.log(Number.isNaN(1234)) because the type of 1234 is number

If you pass in 1234, it will have the type Number, but will not be NaN, so again Number.isNaN will return false.

The only cases where it will return true are if you literally pass it NaN or something that would lead to it:

Number.isNaN(NaN) // true
Number.isNaN(0/0) // true
Number.isNaN(1/0) // false (javascript defines 1/0 === Infinity, which is a Number)
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
0

this functions has different implementations. Number.isNaN is more strict and you will only get true for NaN values

Number.isNaN(NaN); // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0) // true
h37l3x
  • 84
  • 2