Home  >  Article  >  Web Front-end  >  How does javascript identify whether it is an array

How does javascript identify whether it is an array

青灯夜游
青灯夜游Original
2022-01-18 16:02:518113browse

How to identify an array in JavaScript: 1. Use the instanceof operator, with the syntax "variable name instanceof Array"; 2. Use the isArray() method, with the syntax "Array.isArray (variable name)".

How does javascript identify whether it is an array

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript identifies whether it is an array

Method 1: Use instanceof operator

var cars=new Array();
cars[0]="Saab";
cars[1]="Volvo";
cars[2]="BMW";
// 判断是否支持该方法
if (cars instanceof Array) {
    document.write("是一个数组。") ;
}

Method 2: Use isArray method

var cars=new Array();
cars[0]="Saab";
cars[1]="Volvo";
cars[2]="BMW";
// 判断是否支持该方法
if (Array.isArray) {
    if(Array.isArray(cars)) {
        document.write("该对象是一个数组。") ;
    }}

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How does javascript identify whether it is an array. 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