Multiple definitions of arrays in JavaScript and introduction to common functions _ basics

Source: Internet
Author: User


The definition of an array:
Method 1.

Copy Code code as follows:

var mycars=new Array ()
mycars[0]= "Sharejs.com"
mycars[1]= "Volvo"
mycars[2]= "BMW"

Method 2.
Define and initialize together:

Copy Code code as follows:
var mycars=new Array ("Saab", "Volvo", "BMW");

Or:

Copy Code code as follows:
var mycars=["Saab", "Volvo", "BMW"];

JavaScript two-dimensional array, with one-dimensional arrays to simulate:
Method 1.

Copy Code code as follows:
var arr = new Array ([' A ', ' B ', ' C '],[' d ', ' e ', ' f ']);

ARR[0] Returns the first one-dimensional array, arr[0][0] returns the first element ' a ' of the first one-dimensional array, the same below.
Method 2.
Copy Code code as follows:

Arr=new Array ();
for (i=0;i<100;i++) {
Arr[i]=new Array (...);
}

Method 3.
Copy Code code as follows:

var arr=new Array (
New Array (),
New Array (),
New Array ()
);

The JavaScript array does not need to be set in length, it expands itself, and the array name. Length returns the number of elements

JavaScript arrays commonly used functions:
ToString (): Converts an array to a string
toLocaleString (): Converts an array to a string
Join (): Converts an array into a symbolic concatenated string
Shift (): Moves an element of an array's head out
Unshift (): Inserts an element in the header of an array
Pop (): Deletes an element from the tail of an array
Push (): Adds an element to the tail of the array
Concat (): Adding elements to an array
Slice (): Returns the part of an array
Reverse (): Sort the array in reverse order
Sort (): Sorting operations on array
Splice (): Inserts, deletes, or replaces an array element

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.