Home >Web Front-end >JS Tutorial >JavaScript minimalist introductory tutorial (3): Array_javascript skills

JavaScript minimalist introductory tutorial (3): Array_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:32:541178browse

Reading this article requires programming experience in other languages.

In JavaScript arrays are objects (not linearly allocated memory).

Create an array via array literal:

Copy code The code is as follows:

var empty = [];
var numbers = [
'zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine'
];
empty[1] // undefined
numbers[1] // 'one'
empty.length // 0
numbers.length // 10

Arrays have an attribute length (but objects do not) that represents the length of the array. The value of length is the largest integer attribute name of the array plus 1:

Copy code The code is as follows:

var myArray = [];
myArray.length; // 0
myArray[1000000] = true;
myArray.length; // 1000001

We can modify the length directly:

Changing the length will not cause more space to be allocated
length is changed to smaller, and all attributes with subscripts greater than or equal to length are deleted
Since arrays are also objects, you can use delete to delete elements in the array:

Copy code The code is as follows:

delete number[2];
number[2] === undefined;

Deleting an element in the array will leave a hole.

JavaScript provides a set of array methods, which are placed in Array.prototype (not detailed here).

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