Could someone explain how the array at index 1 in the array below is somehow acting like a number and yet described as a object when I use typeof? I don’t understand why [2] is making it past the filters. See the console.log comments below to see what I’m talking about.
typeof will always return ‘object’ for an array, so don’t let that throw you. If you really need to know whether it is a regular object or an array then use Array.isArray().
An array with a single element will be coerced to its single value, so passing [2] into isNan() will be the same as isNaN(2) which will return false. But if the array you pass in has more than one value then you will get true for isNaN().
Same thing for the >= comparison, the array with a single element will be coerced to that element and thus [2] >= 0 is true.
As another gotcha, remember that isNaN does not test is something is a number or not, it checks to see if it is NaN. When it tries to coerce “[3, [[4]]]” it can’t do it an it evaluates to NaN.