最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Number.isNaN('asd') === false in Node.js! What gives? - Stack Overflow

programmeradmin2浏览0评论

Try it: In Node.js, or Firefox, open a REPL, and type:

Number.isNaN('asdf');

How can this not be NaN? What is NaN if not this?

Try it: In Node.js, or Firefox, open a REPL, and type:

Number.isNaN('asdf');

How can this not be NaN? What is NaN if not this?

Share Improve this question edited Nov 24, 2013 at 10:54 rhughes 9,61313 gold badges64 silver badges91 bronze badges asked Nov 24, 2013 at 10:45 user3025492user3025492 3,0534 gold badges20 silver badges19 bronze badges 2
  • As an aside, I highly remend this great JSConf talk - " Everything you never wanted to know about JavaScript numbers -- JSConf EU 2013" youtube./watch?v=MqHDDtVYJRI It explains the low-level (Bit-level) details of how "Number" is implemented in JS but in a way that anyone can understand it. – Mörre Commented Nov 24, 2013 at 10:57
  • Number.isNaN(Number('asdf')) – zamnuts Commented Nov 24, 2013 at 11:21
Add a ment  | 

5 Answers 5

Reset to default 3

You have a misconception here. The fact that NaN means Not a Number does not mean that anything that is not a number is a NaN.

NaN is a special value on floating point arithmethic that represents an undefined result of an operation. For example, 0 / 0 generally yields NaN as a result.

More information here and here.

There is no notion of Number.isNaN() in the language specification. The only specified function is the global isNaN (15.1.2.4), which will correctly return true on anything that isn't a number or cannot get correctly transformed into a number.

Number.isNaN (which returns true on NaN) is probably an implementation detail part of the next standard draft (see remyabel' answer) which could be used internally by isNaN.

Just a note, it seems that Number.IsNaN has limited support.

This output:

console.log(Number.isNaN("blabla"));

Gives false, and false !== NaN:

console.log(false !== NaN)
console.log(false !== Number.NaN);

Here's a reference:

When the Number.isNaN is called with one argument number, the following steps are taken:

If Type(number) is not Number, return false.
If number is NaN, return true.
Otherwise, return false.

This function differs from the global isNaN function (18.2.3) is that it does not convert its argument to a Number before determining whether it is NaN.

I am l33t for I have 1337 reputation.

Try this:

function checkNaN(value)
{
    return value !== value;
}

It works because (I believe) NaN is the only thing in JS that does not strictly equal itself.

This is part of the EcmaScript 6 Standard.

Number.isNaN returns whether a value is NaN without coercing the parameter.

Number.isNaN("xmfds"); // false
Number.isNaN("ose"); // false

isNaN returns whether the value is NaN after coercing the parameter to Number type.

isNaN("ose") -> isNaN( Number("ose") ) -> isNaN(NaN); // true
发布评论

评论列表(0)

  1. 暂无评论