As far as I know there are three ways of finding out if an object is an Array
by isArray
function if implemented
Array.isArray()
by toString
Object.prototype.toString.apply( obj ) === "[object Array]"
and by instanceof
obj instanceof Array
Is there any reason to choose one over the other?
As far as I know there are three ways of finding out if an object is an Array
by isArray
function if implemented
Array.isArray()
by toString
Object.prototype.toString.apply( obj ) === "[object Array]"
and by instanceof
obj instanceof Array
Is there any reason to choose one over the other?
Share Improve this question edited Sep 16, 2011 at 16:30 Arnaud Le Blanc 100k24 gold badges211 silver badges196 bronze badges asked Sep 16, 2011 at 15:58 NebulaFoxNebulaFox 8,3239 gold badges51 silver badges67 bronze badges 3- The second one is easier to read, all else equal, that's enough for me to say it is better. – Andrew Commented Sep 16, 2011 at 16:04
-
I forgot about
Array.isObject
, thanks @arnaud – NebulaFox Commented Sep 16, 2011 at 16:11 - Object.prototype.toString.apply( obj ) === "[object Array]" is the one that will return true if the array was created in another window. – kennebec Commented Sep 16, 2011 at 16:50
4 Answers
Reset to default 6The best way is probably to use the standard Array.isArray()
, if it's implemented by the engine:
isArray = Array.isArray(myObject)
MDN remends to use the toString()
method when Array.isArray
isn't implemented:
Compatibility
Running the following code before any other code will create Array.isArray if it's not natively available. This relies on Object.prototype.toString being unchanged and call resolving to the native Function.prototype.call method.
if(!Array.isArray) { Array.isArray = function (arg) { return Object.prototype.toString.call(arg) == '[object Array]'; }; }
Both jQuery and underscore.js[source] take the toString() === "[object Array]"
way.
Unless it was proven that the former has significant performance benefits and my app required every last ounce of speed I would go for the latter.
The reason is readability, pure and simple.
instanceof
tests whether the given constructor (Array) is in the object's prototype chain, while your second approach only checks the actual type of the object. In other words, if your object inherits from Array, the second test will be true, but the first will be false. Now, it's not typically done to inherit from Array (it doesn't work right in IE), but walking the prototype chain presumably adds some overhead (especially if the object isn't an array).
If what you're trying to do is to decide whether a parameter passed to you is an array that you should iterate over, there's a fair amount of code out there that just looks for a .length
attribute and treats as an array or a pseudo-array if that attribute is present.
This is because there are lots of things that aren't actually arrays (but are pseudo arrays with array like capabilities) that you may want your code to treat like an array. Examples of some of these kinds of things are a jQuery object or a nodeList returned from many DOM calls. Here's a code example:
// accepts:
// single DOM element
// array of DOM elements
// nodeList as returned from various DOM functions like getElementsByClassName
// any array like object with a .length attribute and items in numeric indexes from 0 to .length-1 like a jQuery object
function hideElements(input) {
if (input.length !== undefined) {
for (var i = 0, len = input.length; i < len; i++) {
input[i].style.display = "none";
}
} else {
input.style.display = "none";
}
return(input);
}
The jQuery .each()
function also just tests the parameter passed to it for .length (and verifying that it's not a function) before deciding it's something it should iterate like an array.
If that isn't the problem you're trying to solve, I can find two references to using the first technique:
- jQuery's implementation of isArray uses the first technique.
- MDN (Mozilla Developer Network) remends the first one here.