How does javascript identify whether it is an array

青灯夜游
Release: 2022-01-18 16:02:51
Original
7962 people have browsed it

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

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

[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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!