Created
April 25, 2019 14:37
-
-
Save shchegol/b2c24661d5abb12b6fe088fd01f60104 to your computer and use it in GitHub Desktop.
Checking for arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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