Home > Web Front-end > JS Tutorial > body text

How to determine whether an object is an array in javascript

coldplay.xixi
Release: 2021-04-02 13:56:01
Original
2804 people have browsed it

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("该对象是一个数组。") ;
    }
}
Copy after login

2. Use instanceof operator

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template