Skip to content

Instantly share code, notes, and snippets.

@shamasis
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamasis/11c3da8eff4a2d7a6349 to your computer and use it in GitHub Desktop.
Save shamasis/11c3da8eff4a2d7a6349 to your computer and use it in GitHub Desktop.
Best JavaScript Array Detection Technique
/**
* Check whether an object is Array or not
* @type Boolean
* @param {object} subject is the variable that is
* tested for Array identity check
*/
var isArray = (function () {
// Use browser's own `isArray` when available
if (Array.isArray) {
return Array.isArray;
}
// Retain references to variables for performance optimization
var objToStringFn = Object.prototype.toString,
arrToStringResult = objToStringFn.call([]);
// Use object prototype's toString method as comparison
return function (subject) {
return objToStringFn.call(subject) === arrToStringResult;
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment