In JavaScript, you can use the isArray() method to determine whether it is an array. The syntax format is "Array.isArray(obj)"; isArray() is a static method of the Array type. You can use it to determine a value. Whether it is an array.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JS determines whether it is an array: use the isArray() method
JavaScript isArray() is a static method of the Array type. You can use it to determine whether a value is an array. .
var a = [1,2,3]; console.log(typeof a); //返回“object” console.log(Array.isArray(a)); //true
In the above code, the typeof operator can only show that the type of the array is Object, and the Array.isArray() method can directly return a Boolean value. This method is very useful in conditional expressions.
Example
In the following code, the array has a key named 2. Since the key names are all strings, the value 2 will be automatically converted into a string.
var a = [1,2,3]; console.log(2 in a); //true console.log('2' in a); //true console.log(4 in a); //false
If a position in the array is empty, the in operator will return false.
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to determine whether it is an array in javascript. For more information, please follow other related articles on the PHP Chinese website!