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?
Best Answer
The best way is probably to use the standard
Array.isArray()
, if it's implemented by the engine:MDN recommends to use the
toString()
method whenArray.isArray
isn't implemented:Both jQuery and underscore.js[source] take the
toString() === "[object Array]"
way.