把生命浪费在美好的代码上

How to Check If it is Array?

Got some instruction about how to check if a variable is Array in JavaScript from this book.

Here I just copy and record sth to make me more sensitive for how to check Array in JavaScript.

In the environment which support ES5, just use Array.isArrary(). For example:

COPY
Array.isArray([]); // true

// trying to fool the check
// with an array-like object
Array.isArray({
length: 1,
0: 1,
slice: function () {}
}); // false

In somewhere that no ES5 support, just do this:

COPY
if (typeof Array.isArray === undefined) {
Array.isArray = function (arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
Authorship: Zhao Biao
Article Link: https://buildall.github.io/2015/11/21/checkarray/
Copyright: All posts on this blog are licensed under the CC BY-NC-SA 4.0 license unless otherwise stated. Please cite 把生命浪费在美好的代码上 !