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.