简体   繁体   中英

Can someone explain me this javascript behaviour? isNaN([])

I just don't get it:

isNaN([]) === false -> true

In the browser or in node.js

I'd like to understand why.

this has to do with type coercion. when isNaN receives anything but a number, it coerces the value to a number... so it basically does +value . since +[] is 0 and 0 is a number, isNaN returns false .

If you want to check whether a value is not a number, you need to convert it with parseInt (or parseFloat ). both those functions will return NaN for an empty array.

The difference is with conversion you're explicitly telling it the input is number-like and should therefore be able to be converted into a value of type Number without issues. Since [] is not number-like, trying to parse (convert) it will result in NaN.

This is an extremely simplified explanation of how parsing and conversion work. For one, they are two separate things. if you want to learn more about how these two work, start here

With coercion, however (which is what you're doing), you essentially passed it a value and "assumed" (for lack of a better word) the values type is compatible with the parameters' requirements. So isNaN receives an array and guesses you meant to pass it a number, and coerces it to its number equivalent (in the case of an empty array, 0) before performing the actual operation. so isNaN is performed on 0, not an empty array.

Again, this explanation is oversimplified. for more information about type coercion in JS, go here

NAN is what is returned when a math function fails. NaN is present in almost all languages that do floating point math. However some Javascript functions (and the internal logic that coerces values) also return NaN when values are coerced to being a number that do not have a numeric representation. It's a lot easier to understand why isNaN is defined the way it is if you think about NaN as being intended for the case where your math gave a nonsense answer and ignore its role in representing numeric coersion failures. Obviously when writing Javascript, you'll need to consider (and use) the coersion NaN, but this particular language design choice makes more sense if you ignore that for the moment.

So, for example math.sqrt(-1) is NAN. the isNAN function returns true if and only if its input argument is NAN. Since [] is not NAN, I'd expect isNAN([]) to return false. isNAN is the wrong function for determining if something is a number. It does have strange coersion rules, buteven without them, the answer would be the same in the case of isNAN([] . [] is not the NAN object, so isNAN([]) is false. As @Nicolás Straub points out, the implementation happens to coerce [] to a number first, but we'd expect the same result given the interface contract of isNAN even if it did not.

ReferenceError: ISNAN is not defined
at repl:1:1
at REPLServer.defaultEval (repl.js:272:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:441:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:203:10)
at REPLServer.Interface._line (readline.js:542:8)
at REPLServer.Interface._ttyWrite (readline.js:819:14)

isNaN [Function: isNaN]

isNaN("bar") true

However, "bar" is not actually the IEEE not a number floating point construct, so we'd expect that a function would tell us "bar" is not NaN . Because isNaN coerces its argument to a number, it gives us the wrong answer. The es6 Number.isNaN function would give us the correct answer "bar" is not NaN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM