Arrays under JavaScript and their operations

Source: Internet
Author: User
Tags array array length arrays tostring

This article describes the following aspects:

1. How to create an array

2. How to operate the array (add, delete, read)

3. Array common methods and properties

How to create an array, generally based on the initialization of simple to divide into 3 kinds of:

1. Simply create an array:

var arr=new Array ();

Important: Create an Array object array () with the new keyword, and the array () object is a local class that you can use to create an object with new

2. Create an array and specify the size of the array:

var arr=new array (10);//This creates an array with an initialization size of 10.

Note: When you initialize the size by using an array size operation, the array is automatically propped up and does not make errors like the C language. Dynamic growth is a property of the JS array. In addition, JS supports the maximum array length of 4294967295

3. Direct initialization:

var arr=new array ("Paramecium", "Love", "fluffy");//This initializes the array directly.

or var arr=["Paramecium", "Love", "fluffy"];//parentheses can also declare an array object

Of course, like the C language, you can also define 2-D 3-dimensional arrays, which are not discussed here.

Properties of array: length

Arr.length returns the length of the array arr, which is common to the traversal of the arrays in the loop, such as:

for(var i=0;i<arr.length;i++){
执行部分
}

Array element access: Arr[index], in which index represents the array cardinality, from 0, a total of arr.length elements. For example: arr[0] access to the first array element, arr[1] access to the second array element .... How to do arrays by analogy: first overview of the following operations array of the common methods (13)

toString(),valueOf(),toLocalString(),join(),split(),slice(),concat(),
pop(),push(),shift(),unshift(),sort(),splice()

The features and usage of these methods are analyzed below.

ToString (), valueof (), tolocalstring ():

Function: Returns all elements of an array

Note: Array names can also return the entire array

Code:

var m=["AM", "bm", "CM"];//declare an array object with parentheses

Alert (m.tostring ());//tostring () returns all the contents of the array object, separated by commas, that is am,bm,cm

Alert (m.valueof ());//valueof () also returns all contents of an array object

Alert (m.tolocalestring ());//tolocalestring () also returns all the contents of the array object, but there are regional language differences, no research

Alert (m);//array name also returns all contents of an array object

var m=["am","bm","cm"];//用括号声明一个数组对象 alert(m.toString());//toString()返回数组对象的所有内容,用逗号分割,即am,bm,cm alert(m.valueOf());//valueOf()也返回数组对象的所有内容 alert(m.toLocaleString());//toLocaleString()也返回数组对象的所有内容,但有地区语言区别,暂不研究 alert(m);//数组名也返回数组对象的所有内容Run: <script>var m=["AM", "bm", "cm"];alert (m.tostring ()), Alert (m.valueof ()), Alert (m.tolocalestring ()); </ Script>

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.