Why does isNaN(true) return 'false'?

Asked

Viewed 45 times

0

Guys, I was studying but I didn’t understand, because isNaN(true) is 'false', if true is not a number? (I’m Beginner!)

  • Does the question refer to any specific language or general?

  • general, but may be better in js (if there are differences depending on the language)

  • Implementations are language-specific or frameworks. JS, by the way, is well known for its inconsistency. As for the question, it is best to edit it to make clear what you want to know and edit the tags accordingly.

  • there are numerous presentations about JS, such as this: https://www.youtube.com/watch?v=x8tL8NdHaY

  • Like isNaN does not exist in Python, I think better to limit the question to JS - in Python there is something similar in the module math, but mixing languages in the same question would leave it wide. If you want to know about Python, I suggest asking another specific question.

  • I understand, Thanks for the tips on the questions, I had this account here but had not used before, really I’m here to learn.

  • 1

    Incoherent actions? Javascript incoherent. https://answall.com/a/30884/69296

  • 1

    And speaking of incoherent Javascript: https://www.destroyallsoftware.com/talks/wat

  • More here: https://www.youtube.com/watch?v=et8xNAc2ic8 lecture repository: https://github.com/denysdovhan/wtfjs

Show 4 more comments

2 answers

1

See the documentation:

true if the Given value is Nan; otherwise, false.

That is, it returns true if the value is global ownership NaN, otherwise (for anything other than NaN) returns false.

Of course, this in general lines. Because the behavior of isNaN for non-numerical arguments is more confusing than that.

If you pass something that is not a number, first a coercion is made to Number, and then checked whether the result is NaN.

This is confusing because of the name of the property NaN: "not a number" may imply that anything other than a number (such as the value true, strings, other objects) be one NaN, but in fact only when coercion to Number result in NaN is that the function returns true.

An example is the empty string, which when converted to Number, results in zero (and therefore, isNaN returns false):

console.log(Number('')); // 0
console.log(isNaN('')); // false

Something similar occurs with true, because by converting it to Number, results in 1:

console.log(Number(true)); // 1
console.log(isNaN(true)); // false

But if I pass a string like 'abc'', isNaN returns true, for the conversion of this string to Number returns NaN:

console.log(Number('abc')); // NaN
console.log(isNaN('abc')); // true

And to make it even more confusing:

let d = new Date();
console.log(Number(d)); // 1612194374804 (o valor varia pois corresponde à data atual)
console.log(isNaN(d)); // false

console.log(Number(d.toString())); // NaN
console.log(isNaN(d.toString())); // true

For one Date can be directly converted to Number, but the string returned toString() nay.


The complete algorithm is in language specification, and that is what has already been said:

  1. Let num be ToNumber(number).
  2. If num is NaN, Return true.
  3. Otherwise, Return false.

And the algorithm of the operation ToNumber is explained here.

-3

isNaN = Isn’t that a number? By stating with true, it means that isNaN ie it is not a number.

isNaN(1) = false
isNaN(true) = false

Browser other questions tagged

You are not signed in. Login or sign up in order to post.