search
HomeDaily ProgrammingHTML KnowledgeHow to determine whether an object is an array in JS


Use JS to determine whether the object is an array. You can use the JavaScript Array.isArray() method to check whether the object (or variable) is an array. If the return value is true, it is represented as an array; otherwise, false is returned.

How to determine whether an object is an array in JS

Below we will combine specific code examples to introduce to you how to use js to determine whether an object is an array.

First we create some variables to check whether these variables are objects.

The code example is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
</body>
<script>
    // 创建一些变量
    var v1 = {name: "John", age: 18};
    var v2 = ["red", "green", "blue", "yellow"];
    var v3 = [1, 2, 3, 4, 5];
    var v4 = null;

    // 测试变量数据类型
    document.write(typeof(v1) + "<br>"); 
    document.write(typeof(v2) + "<br>"); 
    document.write(typeof(v3) + "<br>"); 
    document.write(typeof(v3) + "<br>"); 
</html>

Here we use typeof() to detect the data types of the above four variables.

The result is as follows:

How to determine whether an object is an array in JS

As shown in the figure, v1, v2, v3, and v4 are all object types.

So how do we determine whether these objects are arrays?

The code example is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
</body>
<script>
    // 创建一些变量
    var v1 = {name: "John", age: 18};
    var v2 = ["red", "green", "blue", "yellow"];
    var v3 = [1, 2, 3, 4, 5];
    var v4 = null;
    // 测试变量是否为数组
    document.write(Array.isArray(v1) + "<br>");  
    document.write(Array.isArray(v2) + "<br>");  
    document.write(Array.isArray(v3) + "<br>");  
    document.write(Array.isArray(v4) + "<br>");  
</script>
</html>

JavaScript isArray() method. The isArray() method is used to determine whether an object is an array. Returns true if the object is an array, false otherwise.

The judgment results are as follows:

How to determine whether an object is an array in JS

As shown in the figure, objects v2 and v3 are arrays.

Note: All mainstream browsers support the Array.isArray() method, such as Chrome, Firefox, IE (version 9 and above), etc.

This article is a detailed introduction to JS to determine whether an object is an array. It is also very simple and easy to understand. I hope it will be helpful to friends in need!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft