Skip to content

Instantly share code, notes, and snippets.

@shchegol
Created April 25, 2019 14:37
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 shchegol/b2c24661d5abb12b6fe088fd01f60104 to your computer and use it in GitHub Desktop.
Save shchegol/b2c24661d5abb12b6fe088fd01f60104 to your computer and use it in GitHub Desktop.
Checking for arrays
// from https://medium.com/devschacht/javascripts-new-private-class-fields-c60daffe361b
// METHOD 1: constructor property
// Not reliable
function isArray(value) {
return typeof value == 'object' && value.constructor === Array;
}
// METHOD 2: instanceof
// Not reliable since an object's prototype can be changed
// Unexpected results within frames
function isArray(value) {
return value instanceof Array;
}
// METHOD 3: Object.prototype.toString()
// Better option and very similar to ES6 Array.isArray()
function isArray(value) {
return Object.prototype.toString.call(value) === '[object Array]';
}
// METHOD 4: ES6 Array.isArray()
function isArray(value) {
return Array.isArray(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment