algorhythm
algorhythm

Reputation: 8728

Why is an Array with one Number a Number in Javascript?

I've found that an empty array or an arwith exactly one Number is a Number.

This topic is not really an explanation for this special case I think: Why does isNaN(" ") equal false

document.write( isNaN([1,2,3]) ); // true
document.write( isNaN([1,2,'abc']) ); // true
document.write( isNaN(['abc']) ); // true

// maybe explained through the above link
document.write( isNaN([]) ); // false

// but...
document.write( isNaN([1]) ); // false
document.write( isNaN([-3]) ); // false
document.write( isNaN([1234567]) ); // false
document.write( isNaN([-1.234]) ); // false
document.write( isNaN([[123]]) ); // false
document.write( isNaN(['1']) ); // false

Who can tell me why it makes sense?

Upvotes: 1

Views: 292

Answers (2)

lucono
lucono

Reputation: 21

You're using the isNaN global function which has some confusing behavior due to its coercion of non-numbers to a numeric type which can then result in the NaN value. This is what is happening in your case as the string representation of an array containing a single number element will successfully parse to the number value of the single number element.

From MDN:

Since the very earliest versions of the isNaN function specification, its behavior for non-numeric arguments has been confusing. When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the "false" returned value may be unexpected; the empty string, for example, is surely "not a number."

Note also that with ECMAScript 6, there is also now the Number.isNaN method, which according to MDN:

In comparison to the global isNaN() function, Number.isNaN() doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to NaN, but aren't actually the same value as NaN. This also means that only values of the type number, that are also NaN, return true.

Unfortunately:

Even the ECMAScript 6 Number.isNaN method has its own issues, as outlined in the blog post - Fixing the ugly JavaScript and ES6 NaN problem.

Upvotes: 0

Scimonster
Scimonster

Reputation: 33409

isNaN coerces its value to a number. (See MDN)

Because the string representation of an array is all of its items concatenated with a comma. And the numerical representation of that is NaN because of the comma.

But if there's only one item, hence no comma, it's able to be converted to a number.

Upvotes: 3

Related Questions