3 ms·
JavaScript, how do I check if this variable is an array
Sometimes you are running a function the return a result in JavaScript. It can return multiple data type. It is hard to detect if the object is an array, because an array is an object.
http://idiallo.com/blog/2013/04/javascript-how-do-i-check-if-this-variable-is-an-array
- bdfh42 14y agoFirst point - this is not a QA site - so Stackexchange is the goto place. You could experiment with the typeof operator - which will return "object" for an array. Then if there was still doubt you could play with the instanceof operator.
- jameswyse 14y agoin ES5: Array.isArray(['one', 'two']) === true Until then, you can use a polyfill such as ES5-Shim[1], another library such as Underscore.js[2] or even something simple like this: if(!Array.isArray) { Array.isArray = function (vArg) { return Object.prototype.toString.call(vArg) === "[object Array]"; }; } [1] https://github.com/kriskowal/es5-shim https://github.com/kriskowal/es5-shim [2] http://underscorejs.org/#isArray http://underscorejs.org/#isArray