Internally, there is no type called “array” in JavaScript. When used on an array, typeof returns "object" instead. To check if something is an array, use Array.isArray() instead. // `typeof` an array returns “object” because JS has no type called “ar...
Markdown supported · ⌘↵ to post
In modern browsers you can do:
Array.isArray(obj)
For backward compatibility you can add the following:
// only implement if no native implementation is available
if (typeof Array.isArray === 'undefined') {
Array.isArray = function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
};
Purvi Barot
Software Engineer
In modern browsers you can do:
Array.isArray(obj)For backward compatibility you can add the following:
// only implement if no native implementation is available if (typeof Array.isArray === 'undefined') { Array.isArray = function(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; } };