javascript--Array (i)

Source: Internet
Author: User
Tags array length

I. Basic concepts of arrays

An array is an ordered set of values, each of which is called an element, and each element has its own position in the array, represented by a number, called an index (identified from 0).

In JS, arrays have no specific type, and array elements can be of any type and different elements in the same array may have different types.

  

Two. Creating an array

1. There are two ways to create an array, but using an array of direct quantities (or array literals) is the simplest way to create an array;

    1.1 Array Direct amount (array literal): separates array elements with commas in square brackets

Syntax: var array name = [element 1, Element 2, Element 3,......]

If you omit a value from the array's direct amount, the omitted value is given the undefined value, for example:

var arr1 = [1,,3]; The array has 3 elements and the middle element value is undefined

var arr2 = [,,]; The array has two elements, all of which are undefined

Note: There are two commas in this arr2, why not three elements but two elements?

Because the syntax of the array's direct volume allows for an optional trailing comma, there are only two elements in the ARR2 and not three.

    1.2 Call constructor Array ()

? No parameters:var arr1 = new Array ();

This creates an empty array without any elements (equivalent to the Var arr1 of the array's direct representation = [])

?   One parameter:var arr2 = new Array (10);

This creates an array of length 10.

If there is only one numeric parameter that is an integer , then this parameter specifies the length

      ? Two or more parameters:var arr3 = new Array (All-in-one, "test", "test");

If there are two or more parameters, the passed in parameter is the element of this array

Three. Sparse arrays

   A sparse array is an array that contains a discontinuous index starting at 0. Typically, the length property value of an array represents the number of elements in the array, and if the array is sparse, the length property value is greater than the number of array elements.

You can create a sparse array by using the array () constructor or simply specifying that the index value of the array is greater than the current array length.

A = new Array (5); The array has no elements, but A.length is 5

a = []; Create an empty array with length of 0

A = [100]; An assignment adds an element, but the length is 101

You can also use the delete operator to produce sparse arrays, which are not explained in detail here.

Sparse arrays are typically slower to implement than dense arrays, with higher memory utilization, and the time to find elements in such arrays is as long as regular object properties.

Note: A sparse array is not created when the value is omitted from the array's direct quantity. The omitted element is present in the array, and the value is undefined, which is different from the existence of the group element at all.

var arr = [,,,]; The array is [undefined,undefined,undefined];

But when the array uses a contiguous comma, such as [1, 3], the resulting array is also a sparse array, and the omitted value does not exist.

Four. Array length

  Each array has a length property, for dense arrays (non-sparse arrays), the Length property value represents the number of elements in the array, and its value is 1 larger than the largest index value in the array.

[ ]. Length//0 array with no elements

[' A ', ' B ', ' C '].length//3 The maximum index value is 2,length 3

    

 

  

javascript--Array (i)

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.