Home > Article > Web Front-end > How does javascript identify whether it is an array
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)".
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!