Arrays in JavaScript

Source: Internet
Author: User
Tags array length

Array

(1), the definition of an array

An array is an ordered collection of values

JavaScript arrays are untyped, array elements can be of any type, and different elements of the same array may have different types.

Each value is called an element, and each element has a position in the array

(2), creating an array

1. Using the array direct amount is the simplest way to create an array, separating the array elements with commas in square brackets.

Eg:var empty = []; An array with no elements

Eg:var primes = [2,3,4,5,7]; Array with 5 values

Eg:var misc = [1.1,true, "a",]; There are 3 different types of elements and the end of the comma array in the direct amount of the value is not necessarily a constant, can be any expression.

Eg:var base = 1602;

var table = [Base+1,base+2,base+3];

If you omit a value from the array's direct amount, the omitted element is given the undefined value.

var count = [1,,3]; The array has 3 elements, and the middle element has a value of undefined

var undefs = [,,]; The array has two elements, all of which are the syntax of the direct amount of the undefined array allowing for an optional trailing comma, so [,] there are only two elements and not three.

2. Calling the constructor array () is another way to create an array. Constructors can be called in three ways.

① called without arguments: var a = new Array (); The method creates an empty array without any elements, equivalent to the array direct amount [].

② is called with a numeric parameter that specifies the length: var a = new Array (10) This form of the array () constructor can be used to pre-allocate an array space when the desired number of elements is known beforehand.

③ displays a non-numeric element that specifies two or more array elements or arrays: var a = new Array (5,4,3,2,1, "test"); In this form, the parameters of the constructor will be the elements of the new array. Using array literals is much simpler than using the array () constructor.

(3), reading and writing of array elements

Use the [] operator to access an element in an array element. The reference to the array is on the left side of the square brackets. In square brackets is an arbitrary expression that returns a non-negative integer value. You can use this syntax to read or write an element of an array.

1. By specifying the array name and the index number, you can access a particular element. Eg:mycars[0]

2. If you want to modify the values in an existing array, simply add a new value to the specified label: eg:mycars[0]= "Opel";

(4), array length

Definition: The Length property can set or return the number of elements in the array. (starting from 1)

The length property of an array is always larger than the subscript of the last element defined in the array

1. For regular arrays that have continuous elements and begin with element 0, the attribute length declares the number of elements in the array.

The length property of an array is initialized when an array is created with the constructor array (). When you add a new element to an array, the value of length is updated if necessary.

Set the Length property to change the size of the array. If you set a value that is smaller than its current value, the array is truncated and the elements at its tail are lost. If the value is set larger than its current value, the array will grow, and the new elements are added to the end of the array, their values are undefined.

Eg:[].length ==0

[1,2,3].length ==3

(5), detection is not an array

1. Use the instanceof operator to determine if an object is not an array

Instanceof is a two-dollar operator, the left operand is an object, not a return false, the right operand is a function object or a function constructor, and not the words return false. The principle is the prototype property of a constructor that has a right operand on the prototype chain of the object that determines the left operand.

Eg:arr instanceof Array

2. You can use Array.isarray (arr)

This ES5 adds an array method, which is a static function of the array object, which is used to determine whether an object is an array.

Arrays in JavaScript

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.