Easy Tutorial
❮ Bash Shortcut Summary Of Network ❯

JavaScript Determine if the Object is an Array

Category Programming Techniques

1. typeof

Firstly, we might think of using typeof to detect the data type, but for basic types such as Function, String, Number, Undefined, etc., using typeof can detect them, as shown in the following code:

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

However, when it comes to arrays or regular expressions, using typeof is not sufficient, because when we check for arrays or regular expressions, 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, it's easy to think of using instanceof to check if an object is an instance of an array. This check will return a boolean value, true if it is an array, otherwise false; let's look at the code for checking if it is an array as follows:

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 example;

3. constructor Property

In JavaScript, each 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, with the code shown below:

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 example.

Now we can see that for the second and third points, using the instanceof method and the constructor property respectively, it seems that we can determine whether it is an array, but there are exceptions, such as when using arrays in a cross-frame iframe, it will fail, because arrays created in different iframe frameworks do not share their prototype properties; the following code can be tested to 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 method is not supported in IE, but is available in standard browsers like 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

The above methods cannot determine whether an object is an array; however, we can see in ECMA262 that we can use the Object.prototype.toString.call() method to determine whether an object is an array; the following code:

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

** Click to Share Notes

Cancel

-

-

-

❮ Bash Shortcut Summary Of Network ❯