Detect if parameter passed is an array? Javascript [duplicate]
if (param instanceof Array) ...
Edit. As of 2016, there is a ready-built method that catches more corner cases, Array.isArray
, used as follows:
if (Array.isArray(param)) ...
This is the approach jQuery 1.4.2 uses:
var toString = param.prototype.toString;var isArray = function(obj) { return toString.call(obj) === "[object Array]"; }
I found this here:
function isArray(obj) { return obj.constructor == Array; }
also this one
function isArray(obj) { return (obj.constructor.toString().indexOf(”Array”) != -1);}