About Javascript Array (recommended) and javascript

Source: Internet
Author: User

About Javascript Array (recommended) and javascript

In programming languages, the importance of arrays is self-evident. arrays in JavaScript are also one of the most commonly used objects. arrays are ordered sets of values. Due to the weak type, arrays in JavaScript are flexible and powerful. arrays of high-level languages such as Java can only store the same type or its sub-type elements. JavaScript can store multiple types of elements in the same array, the length can also be dynamically adjusted. The array length can be automatically changed as the data increases or decreases.

First, let's talk about the basic usage of arrays.

Array, or Array type, is one of the most common types in development. The biggest difference between arrays in javascript and other languages isEach item can save any type of data, and the size of the array can be dynamically adjusted.? Check the code.

  1. Create an array:

Var arr = new Array (20); var arr1 = ["umbrella", 1, true, undefined, [2, "King"], ""]

The first line uses the Array Construction Method to create an array with a length value of 20, and the second line uses the array literal representation to create an array with multiple data types.

The method of the first line is a bit pitfall. The brackets specify the length of the array, instead of 20 for the first element of the array. To create an array with an element of 20, you can use the literal method to create a file.

There is also a small pitfall, look at the code

var arr=[1,2,];var arr1=[,,,,,]

The first line will create an array with values of 1, 2, and undefined in IE8 and earlier versions. other browsers will generate an array with two values: 1 and 2.

The second line may contain 5 or 6 items in different browsers. (This problem has been fixed in IE9 and later versions, but this method is not recommended)

  2. Basic operations on array elements

Js arrays are very flexible and have many array element operation methods, but there are also some small pitfalls, such:

var arr=[];arr[0]=1;arr[1]=2;arr.push(3); //arr=[1,2,3]arr[arr.length]=4; //arr=[1,2,3,4]arr.unshift(0); //arr=[0,1,2,3,4];delect arr[2]; //arr=[0,1,undefined,3,4]arr[0]=undefined //arr=[undefined,1,undefined,3,4]

The push () method adds an element to the last side of the array. The obtained value can be annotated, ^_^

You can also manually expand the array subscript to think that, for example, the third row is actually equivalent to push (4 );
If you want to add the elements in the front of the array, don't worry, there is an unshift () method.

The corresponding pop () and shift () methods for deleting elements correspond to push () and unshift () respectively, so we will not repeat them here.

The delect operator does not delete the elements of an array. It can be said that undefined is used to take a seat, which is the same as direct assignment.

  3. sparse array

The sparse array is the subscript of the index group and does not start from 0. Generally, the length of the array is more than the number of elements:

Var arr1 = [undefined]; var arr2 = new Array (1); 0 in arr1; // true0 in arr2; // falsevar arr3 = new Array (100 ); arr3 [99] = "assign value"; 99 in arr3; // true98 in arr3; // false

The reason in arr1 is that the array 1st column has a value. Although it is undefined, arr2 only specifies the length of the array, and the 1st column does not have a value. Therefore, the in statement returns false. In arr3, if a value is assigned to element 100th, a value is detected, which does not affect the key of other columns. Therefore, false is returned.

The above is a small Editor to introduce you to the relevant knowledge of the Javascript array, I hope to help you, if you have any questions, please leave a message, the small editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.