Skip navigation

Monthly Archives: May 2015


typeof method in JavaScript returns wrong value if you pass an array as parameter. To fix this, we can add isArray method to JavaScript’s native Array object. In future, if browser includes one such method, the below code will not break as it will use browser’s native code in that case.

This will be handy fix for typeof known bug for Arrays.

/**
 * @author rajakvk <rajakvk(at)gmail.com>
 */
if (!Array.isArray) {
    
    /**
     * Returns true if an object is an array, false if it is not.
     * @name isArray
     * @methodOf Array
     * @param {*} value 
     * @returns {Boolean}
     */
    Array.isArray  = function (value) {
                
        return Object.prototype.toString.call(value) === '[object Array]';
        
    };

}

Advertisements