Modern browsers have Array.isArray
built in.
For older browsers, you can test for that function and add it when necessary.
if( typeof Array.isArray !== 'function' ) {
Array.isArray = function( arr ) {
return Object.prototype.toString.call( arr ) === '[object Array]';
};
}
alert( Array.isArray( [] ) );
EDIT:
Array.isArray is part of ECMAScript 5:
15.4.3.2 Array.isArray ( arg )
The isArray function takes one argument arg, and returns the Boolean value true if the argument is an object whose class internal property is "Array"; otherwise it returns false. The following steps are taken:
- If Type(arg) is not Object, return false.
- If the value of the [[Class]] internal property of arg is "Array", then return true.
- Return false.