1. typeof

First, we would think of using typeof to detect the data type, but for basic types such as Function, String, Number, Undefined, etc., typeof can detect them all. For example, the code is as follows:

function test(){} console.log(typeof 1); // number console.log(typeof test); // function console.log(typeof "yunxi"); // string console.log(typeof undefined); // undefined

But for arrays or regular expressions, using typeof to detect them will not suffice, because when we detect an array or a regular expression, the returned type will be an object. As shown in the following code:

console.log(typeof []); // object console.log(typeof /\d+/g); // object

2. Instanceof

From this, we can easily think of using instanceof to detect whether an object is an instance of an array. This detection returns a boolean. If it is an array, it returns true; otherwise, it returns false. Let's look at the code above to detect whether it is an array:

console.log([] instanceof Array); // true console.log(/\d+/g instanceof Array); // false

As can be seen above, using instanceof can indeed determine whether it is an array.

3. constructor property

In JavaScript, every object has a constructor property, which references the constructor function that initialized the object. For example, to determine the type of an unknown object, we can write a method as follows:

function isArray(obj) { return typeof obj == 'object' && obj.constructor == Array } //Test demo console.log(isArray([])); // true var a = {"a":1}; console.log(isArray(a)); // false var b = [1,2,3]; console.log(isArray(b)); // true console.log(isArray(/\d+/g));// false

As can be seen above, by calling the isArray method, we can also determine whether it is an array.

We can now see that for points 2 and 3, using the instanceof method and the constructor property respectively seem to be able to determine whether it is an array. However, there are exceptions. For example, when using an array from a page across frames (iframe), it will fail, because in different frames (iframes), arrays created do not share their prototype property with each other. The following code test can verify this~

var iframe = document.createElement('iframe'); document.body.appendChild(iframe); xArray = window.frames[window.frames.length-1].Array; var arr = new xArray("1","2","3","4","5"); //This syntax is not supported in IE; it is available in standard browsers such as Firefox and Chrome. console.log(arr); //Prints out ["1", "2", "3", "4", "5"] console.log(arr instanceof Array); // false console.log(arr.constructor === Array); // false

None of the above methods can determine whether an object is an array; however, when we look at ECMA262, we can see that the Object.prototype.toString.call() method can be used to determine whether an object is an array. The code is as follows:

function isArray(obj) { return Object.prototype.toString.call(obj) == '[object Array]'; } //Code call console.log(isArray([])); // true console.log(isArray([1,2,3])); // true var iframe = document.createElement('iframe'); document.body.appendChild(iframe); xArray = window.frames[window.frames.length-1].Array; var arr = new xArray("1","2","3","4","5"); console.log(arr); // ["1","2","3","4","5"] console.log(isArray(arr)); // true