Home >Web Front-end >JS Tutorial >How to define an object array in JavaScript

How to define an object array in JavaScript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-20 15:04:034046browse

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()

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"

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"

2,

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

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!

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