Home >Web Front-end >JS Tutorial >What does array mean in javascript?

What does array mean in javascript?

藏色散人
藏色散人Original
2021-03-31 16:42:066212browse

array in JavaScript refers to an array, and an array object uses a separate variable name to store a series of values; an array can store all values ​​with one variable name, and can access any value with a variable name; Each element in the array has its own ID so that it can be easily accessed.

What does array mean in javascript?

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

JavaScript Array (array) object

The role of an array object is to store a series of values ​​using separate variable names.

What is an array?

An array object uses separate variable names to store a series of values.

If you have a set of data (for example: car name), there are separate variables as follows:

var car1="Saab";
var car2="Volvo";
var car3="BMW";

However, what if you want to find a certain car from it? And not 3 cars, but 300 cars? This will not be an easy task!

The best way is to use an array.

Arrays can store all values ​​with one variable name, and any value can be accessed with the variable name.

Each element in the array has its own ID so that it can be easily accessed.

Create an array

There are three ways to create an array.

The following code defines an array object named myCars:

1: Regular way:

var myCars=new Array();
myCars[0]="Saab";      
myCars[1]="Volvo";
myCars[2]="BMW";

2: Concise way:

var myCars=new Array("Saab","Volvo","BMW");

3 : Literal:

var myCars=["Saab","Volvo","BMW"];

[Recommended learning: js basic tutorial]

Access the array

By specifying the array name and index Number you can access a specific element.

The following example can access the first value of the myCars array:

var name=myCars[0];

The following example modifies the first element of the array myCars:

myCars[0]="Opel";

lamp [0] is an array the first element of . [1] is the second element of the array.

You can have different objects in an array

All JavaScript variables are objects. Array elements are objects. Functions are objects.

So, you can have different variable types in the array.

You can include object elements, functions, arrays in an array:

myArray[0]=Date.now;
myArray[1]=myFunction;
myArray[2]=myCars;

Array methods and properties

Use the array object predefined properties and Method:

var x=myCars.length             // myCars 中元素的数量
var y=myCars.indexOf("Volvo")   // "Volvo" 值的索引值

Instance

Create an array and assign a value to it:

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

The above is the detailed content of What does array mean 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