An array of javascript

Source: Internet
Author: User

In JavaScript, arrays are also classes, so you can instantiate an array object with the following code

var Myarr = new Array ();

After instantiating, add an array element, such as the following code:

myarr[0]=1;myarr[1]= ' test '; myarr[' both ']=2;myarr[' three ']= "3333";

However, you can also create arrays and add array elements directly using the following code

var myarr=[1, ' Test ', 2, ' 3333 ']

It should be noted that the keys and values of the arrays in JavaScript can be strings, which is modeled by the object, which has already been said, the array is also the class (the class in JavaScript is very special), because Object.property is equivalent to the object[' property I've learned that PHP is used to this way, but when iterating through arrays, it's important to note that array subscripts are not numbers, especially for loops and while loops, while JavaScript has a foreach () {} loop similar to PHP. But JavaScript is for. In Loops, such as the following code

var myarr=new Array (); myarr[0]=1;myarr[1]= ' test '; myarr[']=2;myarr[' three ']= ' 3333 '; for (iter in Myarr) { document.write (iter+ ' ==> ' +myarr[iter]);}

In front of ITER is an iterator that automatically points to the next element each time.

The way in which traversal is traversed depends on the contents of the array, on the one hand, and on the individual preferences.

An array of javascript

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.