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

How to define an object array in JavaScript

醉折花枝作酒筹
Release: 2023-01-07 11:44:26
Original
4006 people have browsed it

In JavaScript, you can use the keyword new to create an array object, and the syntax format is "var array name=new Array()". Array objects are used to store a series of values ​​in separate variable names.

How to define an object array in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Array objects are used to store a series of values ​​in separate variable names.

We use the keyword new to create an array object. The following code defines an array object named myArray:

var myArray=new Array()
Copy after login

There are two ways to assign values ​​to an array (you can add as many values ​​as you want, just like you can define as many variables as you need ).

1,

var mycars=new Array()mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"
Copy after login

You can also use an integer independent variable to control the capacity of the array:

var mycars=new Array(3)mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"
Copy after login

2,

var mycars=new Array("Saab","Volvo","BMW")
Copy after login

Note: If you need to If a numerical or logical value is specified in the array, the variable type should be a numerical variable or a Boolean variable, not a character variable.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to define an object 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