Why does typeof NaN return 'number'?

前端 未结 21 1231
误落风尘
误落风尘 2020-11-22 05:38

Just out of curiosity.

It doesn\'t seem very logical that typeof NaN is number. Just like NaN === NaN or NaN == NaN returning

相关标签:
21条回答
  • 2020-11-22 06:40

    Javascript has only one numeric data type, which is the standard 64-bit double-precision float. Everything is a double. NaN is a special value of double, but it's a double nonetheless.

    All that parseInt does is to "cast" your string into a numeric data type, so the result is always "number"; only if the original string wasn't parseable, its value will be NaN.

    0 讨论(0)
  • 2020-11-22 06:40

    We could argue that NaN is a special case object. In this case, NaN's object represents a number that makes no mathematical sense. There are some other special case objects in math like INFINITE and so on.

    You can still do some calculations with it, but that will yield strange behaviours.

    More info here: http://www.concentric.net/~ttwang/tech/javafloat.htm (java based, not javascript)

    0 讨论(0)
  • 2020-11-22 06:41

    You've got to love Javascript. It has some interesting little quirks.

    http://wtfjs.com/page/13

    Most of those quirks can be explained if you stop to work them out logically, or if you know a bit about number theory, but nevertheless they can still catch you out if you don't know about them.

    By the way, I recommend reading the rest of http://wtfjs.com/ -- there's a lot more interesting quirks than this one to be found!

    0 讨论(0)
提交回复
热议问题