JavaScript array usage Summary

Source: Internet
Author: User
Tags javascript array
The javascript array is an associated array (the associated array is actually an object ). Normal Arrays can access array elements by subscript, while associated Arrays can access corresponding array elements by keyword. I. Three ways to create an array 1: Create an array vararr [] using the following standard; create an empty array vararr [a, B, c]; create an array containing three elements

The javascript array is an associated array (the associated array is actually an object ). Normal Arrays can access array elements by subscript, while associated Arrays can access corresponding array elements by keyword.

I. Three methods for creating Arrays

1: Create an array using the lower mark

Var arr = [];

Create an empty array

Var arr = ["a", "B", "c"];

Create an array containing three elements with subscripts ranging from 0 to 2

2: Create an Array Using keywords

Var arr = {"a": 1, 2: 2, 1.5: 3 };

Create an array containing three elements. The subscript is "a", 2, 1.5, and the elements are 1, 2, and 3, respectively.

3: Create an array using the lower mark using the constructor

Var arr = new Array (3 );

Create an array with a length of 3

Var arr = new Array ("a", "B", "c ");

Create an array containing three elements, subscript 0, 1, 2

The following is an incorrect method to create an array:

Var arr = {1, 2, 3 };

Var arr = [,];

2. Add/set array elements

Arr [3] = 10; // Add or set an array element. The subscript is 3 and the value is 10.

Arr. push ("a"); // Add an element "a" at the end of the array. The subscript is the maximum subscript + 1 of the original array.

3. Delete array elements

Arr. pop (); // return the last element of the array and delete it.

Arr. splice (0th); // delete one element after the first element.

4. Obtain the array Length

The array length can be obtained through the arr. length attribute, but this attribute is not always available.

The javascript array is an associated array (the associated array is actually an object ). The storage of array elements is divided into two methods: ordinary Array Storage and attribute storage.

1. Use the following method to create an array. The initial elements are stored as normal arrays and the array length is 3.

Var arr = ["a", "B", "c"];

Var arr = new Array ("a", "B", "c ");

Var arr = new Array (3 );

2. the array created using the following method will not change arr. length, and the elements are stored as attributes.

Var arr = {"a": 1, 2: 2, 1.5: 3 };

We can use the following method to obtain the element value, and the result is the same

Var x = arr ["a"]; // obtain the element of the arr Array

Var x = arr. a; // obtain the attribute of the arr array.

3. Use the following method to add an array element, which will change the array length (arr. length ).

Arr [3] = 0;

Arr. push (0 );

4. Use the following method to add an array element without changing the length of the array.

Arr ["a"] = 3;

5. var arr = [,]; // defines an array containing 10 numbers

Alert (arr. length); // display 10

Arr [15] = 34;

Alert (arr. length); // display 16

The Code also defines an array containing 10 numbers. The alert statement shows that the length is 10. Then, an element with an index of 15 is used and assigned a value of 34, that is, arr [15] = 34. Then, the array length is output using the alert statement, and the result is 16. In any case, this is a surprising feature for developers who are used to strong-type programming. In fact, the initial length of an array created in the form of newArray () is 0, and the length of the array changes only when no element is defined.

Analyze the following code

Script

Var arr = new Array (3 );

Arr [0] = 1;

Arr [1] = 2;

Arr [200] = 3;

Arr ["a"] = "";

Alert (arr. pop ());

Script

What is the length of the array after the code is executed? What value will the alert pop-up prompt be?

Answer: 3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.