Introduction to using JavaScript array objects

Source: Internet
Author: User
1. Define an array object to store a series of values in a separate variable name. We use the keyword new to create an array object. The following code defines an array object named myArray: varmyArraynewArray () or: varshowText []; // creates an array showText. push (helloworld); // Number of push directions

1. Define an array

An array object is used to store a series of values in a separate variable name.

We use the keyword new to create an array object. The following code defines an array object named myArray:

Var myArray = new Array ()

Or:

Var showText = []; // create an array
ShowText. push ('Hello World'); // push adds one or more elements to the end of the array and returns a new length.

There are two ways to assign values to an array (you can add any number of values, just as you can define any number of variables you need ).

1.
Var mycars = new Array ()
Mycars [0] = "Saab"
Mycars [1] = "Volvo"
Mycars [2] = "BMW"

You can also use an integer independent variable to control the array capacity:

Var mycars = new Array (3)
Mycars [0] = "Saab"
Mycars [1] = "Volvo"
Mycars [2] = "BMW"

2. var mycars = new Array ("Saab", "Volvo", "BMW ")

 

2. Access the Array

By specifying the array name and index number, you can access a specific element.

The following is the code line:

Document. write (mycars [0]) is output below: Saab

To modify the values in an existing array, you only need to add a new value to the specified label:

Mycars [0] = "Opel"; now, the above Code: document. write (mycars [0]); will output: Opel

 

Iii. Common Methods:

1. For... In statement
Use the for... in Declaration to cyclically output the elements in the array.

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.