Talking about JavaScript array (recommended) _javascript techniques

Source: Internet
Author: User

The importance of arrays in programming languages is self-evident, and in JavaScript, arrays are one of the most commonly used objects, arrays are ordered collections of values, and because of the weak type, the arrays in JavaScript are flexible and powerful, It's not like a Java. Strong-type advanced language arrays can hold only the same type or its subtype elements, JavaScript may hold multiple types of elements in the same array, and can be dynamically adjusted in length, which can be changed as the data increases or decreases the automatic array length.

First, the basic usage of the array is probably said.

arrays, or array types, are one of the most common types in development, and the biggest difference between arrays in JavaScript and other languages is that each item can hold any type of data, and the size of the array can be dynamically adjusted , a bit around? Look at the code.

  1. The creation of the array:

var arr=new Array (20);

The first line creates an array with a length value of 20 using the array constructor, and the second row creates an array of multiple data types in the array literal notation.

The first line is a bit of a pit, with the length of the array specified in parentheses, not the array. The first element is 20, to create an array with an element of 20, or to create it by literal method.

There's a little pit, look at the code.

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

The first line will create an array with a value of 1,2,undefined in IE8 and previous versions, and the other browsers will produce an array of two 1,2, respectively.

The second line may contain 5 or 6 items in different browsers. (IE9 above fixes this problem, but still does not recommend this kind of wording)

  2. Basic operation of array elements

JS arrays are very flexible, with a lot of array element operation methods, but there are some small pits, such as:

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];d elect 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 edge of the array, and the resulting value can be viewed as a comment, ^_^

can also be directly manually in the array subscript expansion thought, such as the third line, in fact, is equivalent to push (4);
So if you want to add in the front of the array element, don't worry, there's a Unshift () method.

The corresponding pop () method and the Shift () method, which corresponds to the push () and unshift (), are no longer redundant.

The operation of an array of delect operators does not delete the elements of the arrays, which can be said to occupy a seat with undefined, and directly assign the same value.

  3. Sparse arrays

A sparse array is the subscript of an exponential group and does not start at 0, and the general array has more lengths than the elements:

var arr1=[undefined];
var arr2=new Array (1);
0 in arr1; True
0 in arr2//false
var arr3=new Array (MB);
Arr3[99]= "Assigned value";
ARR3 in; True in
Arr3;//false

ARR1 is because the 1th column of the array is a value, and although the UNDEFINED,ARR2 only specifies the length of the array and the 1th column has no value, the in statement returns false. After assigning a value to the 100th element in the ARR3, the detection is a value and has no effect on the other column's key, so it returns false.

The above is a small set of JavaScript to introduce the relevant knowledge of the array, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.