Home  >  Article  >  Web Front-end  >  How to determine whether an object is an array in javascript

How to determine whether an object is an array in javascript

coldplay.xixi
coldplay.xixiOriginal
2021-04-02 13:56:012659browse

Javascript method to determine whether an object is an array: 1. Use the isArray method, the code is [if (Array.isArray(cars))]; 2. Use the instanceof operator, the code is [if (cars instanceof Array) 】.

How to determine whether an object is an array in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Javascript method to determine whether an object is an array:

1. Use the isArray method

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

2. Use instanceof operator

var cars=new Array();
cars[0]="Saab";
cars[1]="Volvo";
cars[2]="BMW";
if (cars instanceof Array) {
    document.write("该对象是一个数组。") ;
}

Related free learning recommendations: javascript video tutorial

The above is the detailed content of How to determine whether an object 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