Home >Web Front-end >JS Tutorial >How to determine whether it is an array in javascript

How to determine whether it is an array in javascript

青灯夜游
青灯夜游Original
2021-04-12 18:12:135686browse

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.

How to determine whether it is an array in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn