Why the Array.isArray algorithm is ES5 performs a type check?

Viewed 717

Every question found in SO and google about checking if an object is an Array most likely end up with this solution

function isArray(obj) {
    return Object.prototype.toString.call(obj) === '[object Array]'
}

All the other alternatives have false positives or are not completely supported.

Sources:

http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/

How to detect if a variable is an array

When I read the ES5 spec in section 15.4.3.2 found the described algorithm for the function Array.isArray that performs the same check in IE9+, Chrome 5+, Firefox 4+, Opera 10.5+ and Safari 5+ but this algorithm have two extra steps.

function isArray(obj) {
    if (typeof obj !== 'object') {
        return false;
    }
    // Here they check only against the [[Class]] part 
    // and of course they don't have to use the ugly Object.prototype.toString.call
    // but this is pretty much the same comparison
    if (Object.prototype.toString.call(obj) === '[object Array]') {
        return true;
    }
    return false;
}

Now my question is why do they check for type first? Is there an special case where this will return false for an object that still has the [[Array]] internal class?

1 Answers
Related