Home  >  Article  >  Web Front-end  >  Are there arrays in javascript?

Are there arrays in javascript?

青灯夜游
青灯夜游Original
2022-02-18 17:00:482427browse

There are arrays in javascript. In JavaScript, an array is a collection of data arranged in order. Each member of the array is called an element, and the name (key) of each element is called the array subscript (Index); the length of the array is elastic. , readable and writable.

Are there arrays in javascript?

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

There are arrays in javascript.

Array in javascript

In javascript, an array (Array) is a collection of ordered data. Each element in the array Each member is called an element, and the name (key) of each element is called an array index.

The length of the array is flexible and readable and writable (use the length attribute to read and write).

How to define an array

Method 1: Use square brackets [ ], and each element in the array is separated by commas

var a = [];  //空数组
var a = [1, true, "0", [1,0], {x:1,y:0}];  //包含具体元素的数组

Method 2: Call the Array() function

var a = new Array();  //空数组
var a = new Array(1, true, "string", [1,2], {x:1,y:2});  //实数组

Note: If the Array() function passes a numeric parameter, you can define the length of the array, that is, the number of elements it contains.

var a = new Array(5);  //指定长度的数组

Elements in the array can be accessed by index. Indexes in an array start at 0 and increase sequentially, that is, the first element of the array has index 0, the second element has index 1, the third element has index 2, and so on. As shown in the following example:

var arr = [1, 2, 3.14, 'Hello', null, true];
console.log(arr[0]);  // 输出索引为 0 的元素,即 1
console.log(arr[5]);  // 输出索引为 5 的元素,即 true
console.log(arr[6]);  // 索引超出了范围,返回 undefined

[Related recommendations: javascript learning tutorial]

The above is the detailed content of Are there arrays 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