Home>Article>Web Front-end> How to determine whether an object is an array in javascript
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) 】.
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!