-2

Just a simple question...

Why this: isNaN(true) or this: isNaN(false) return false ?

I can understand that false is evaluate to 0 and true to 1 or something...

But is it not the purpose of isNaN(...) to return false only if a variable or a value is "exactly" a number ?

MathKimRobin
  • 1,268
  • 3
  • 21
  • 52
  • isNaN return true only if the value **Is Not a Number** – Luis felipe De jesus Munoz Feb 06 '20 at 14:53
  • 3
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN – Uzair Feb 06 '20 at 14:54
  • 2
    "The function should be interpreted as answering the question, "is this value, when coerced to a numeric value, an IEEE-754 'Not A Number' value?"" Meaning, the underlying value is still a number, 0 or 1. – Adrian Feb 06 '20 at 14:54
  • While the questions are subtly different, the answers of [Why is IsNaNnull == false in JS?](https://stackoverflow.com/questions/115548/why-is-isnannull-false-in-js) go over why thinking `isNaN` is a suitable replacement for a `isNumeric` function is not correct. – Heretic Monkey Feb 06 '20 at 15:02

1 Answers1

1

As you think, they return false, because isNaN first coerces its argument into a Number, and then it returns whether the argument is NaN.

Number(true) is 1 and Number(false) is 0, so neither of them is NaN.

To perform a stricter check, and avoid implicit coercion, call Number.isNaN instead:

Both Number.isNaN(true) and Number.isNaN(false) returns true.

FZs
  • 16,581
  • 13
  • 41
  • 50