Basic knowledge of Array Structure in JavaScript programming

Source: Internet
Author: User
This article mainly introduces the Array Structure in JavaScript programming, which is the basic knowledge in JS getting started learning. If you need it, you can refer to the following array objects: use a separate variable name to store a series of values.
Create an array and assign values to it:
Instance

var mycars = new Array();mycars[0] = "Saab";mycars[1] = "Volvo";mycars[2] = "BMW";

What is an array?
An array object uses a separate variable name to store a series of values.
If you have a group of data (for example, car name), there are independent variables as follows:

var car1="Saab";var car2="Volvo";var car3="BMW";

However, if you want to find a car? What about 300 instead of 3 vehicles? This is not an easy task!
The best way is to use arrays.
An array can store all values with a variable name, and can access any value with the variable name.
Each element in the array has its own ID so that it can be easily accessed.
Create an array
There are three methods to create an array.
The following code defines an array object named myCars:
1: conventional method:

var myCars=new Array(); myCars[0]="Saab";    myCars[1]="Volvo";myCars[2]="BMW";

2: simple mode:

var myCars=new Array("Saab","Volvo","BMW");

3: literal:

var myCars=["Saab","Volvo","BMW"];

Access Array
By specifying the array name and index number, you can access a specific element.
The following instance can access the first value of the myCars array:

var name=myCars[0];

The following example modifies the first element of the array myCars:

myCars[0]="Opel";

Lamp [0] is the first element of the array. [1] is the second element of the array.

You can have different objects in an array.
All JavaScript variables are objects. An array element is an object. A function is an object.
Therefore, you can have different variable types in the array.
You can include object elements, functions, and arrays in an array:

myArray[0]=Date.now;myArray[1]=myFunction;myArray[2]=myCars;

Array methods and attributes
Use the predefined attributes and methods of the array object:

var x=myCars.length       // the number of elements in myCarsvar y=myCars.indexOf("Volvo")  // the index position of "Volvo"


Create a New Method
The prototype is a JavaScript global constructor. It can build the attributes and methods of New Javascript objects.
Instance: Create a new method.

Array. prototype. ucase = function () {for (I = 0; I
 
  

In the preceding example, a new array method is created to convert lowercase characters of an array into uppercase characters.

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.