An array of javascript

Source: Internet
Author: User
Tags arrays definition numeric numeric value

One, the definition of the array

An array is an ordered set of values, a single value called an element, where they are numbered, starting with 0, and the entire array is represented by square brackets.

var arr = [12, 34, 56];

The code above indicates that an array is created, 12 is position No. 0, 34 is 11th, 56 is the 2nd position, and the array subscript in Java is the same, starting at 0.

The array can be defined first, and then assigned, in addition to the definition of a value.

var arr;

Arr[0] = 12;

Arr[1] = 34;

ARR[2] = 56;

Any type of data can be placed in an array,

var arr = [12,3.14, ' a ', ' 123 ', null,undefined, function () {return true;}];

The above are put into plastic, floating-point, character, string, null,undefined, function.

Arrays can also be put in, and if they are placed in an array, they form a multidimensional array.

Second, the relationship between the array and the object

Arrays are also objects in nature and are a variant of the dictionary structure.

Console.log (typeof [1,2,3])//object

The code above shows that the array is a special object, all typeof operators, and the type of the returned array is object.

The special body of the array now, its keys are by default an ordered integer (1,2,3,4 ...)., all arrays do not specify a key name for each element, and each member of the object must specify a key name, in addition, the array identifies the key by a string, and the key name of the string is converted to a string. So the value or string as the key name can read the members of the array.

var arr = [12, 34, 56];

arr[' 0 ']//12

ARR[0]//12

The above code can read the array by using either a numeric value or a string as the key name.

Note that the above rule is also true when assigning values, and if a value can be converted to an integer, the value is the key, equal to the corresponding integer as the key name,

var a = [];

a[' 1000 '] = ' abc ';

A[1000]//' ABC '

a[1.00] = 6;

A[1]//1

As you can see from the code above, ' 1000 ' and ' 1.00 ' can all be converted to integers.

An object has two methods for reading members: Point structure (OBJECT.KEY) and bracket structure (Object[key]), but arr.0 is illegal for the key names of arrays, because individual numbers cannot be identifiers, so array members can only read members through the square bracket structure. Arr[0] (square brackets are operators that can accept numeric values).

Three, Length property

The length property of the array that returns the number of members of the array.

var arr = [12,34,56];

Console.log (arr.length); 3

JavaScript uses a 32-bit integer to hold the number of elements in an array, which means that the group has a maximum of 2 32-1 (4294967295), which means the maximum value of the length property is 4294967295.

The length property of an array differs from the length property of an object, primarily an array, and there must be a length property, and the object does not necessarily have one. Also, the length property of an array is a dynamic value equal to the largest integer plus 1 in the key name.

var arr = [' A ', ' B '];

Arr.length//2

ARR[2] = ' C ';

Arr.length//3

ARR[9] = ' d ';

Arr.length//10

ARR[1000] = ' e ';

Arr.length//1001

The code above also indicates that the key value of an array does not need to be contiguous, the length property is always the same as the largest integer plus 1, in addition, this indicates that the array is a dynamic data structure that can be added or subtracted from the member at any time.

The length property is writable, and if you artificially set a value that is less than the number of the current member, the members of the array are automatically reduced to the value of the length setting.

var arr = [' A ', ' B ', ' C '];

Arr.length//3

Arr.length = 2;

Alert (arr); ["A", "B"]

The above code, the length property is set to 2, the maximum value of the array subscript can only be 1, so arr[2] This value is deleted.

One of the easiest ways we can find a way to empty an array element is to set the length property to 0.

var arr = [' A ', ' B ', ' C '];

arr.length = 0;

Alert (arr); // []

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.