JavaScript array and loop detail_javascript skills

Source: Internet
Author: User
This article mainly introduces the JavaScript array and loop details, this article explains how to traverse arrays in a loop, store and access values in order, store and access values in the opposite order, and search in an array, A friend may refer to the following array as an ordered combination of elements. In JavaScript, Arrays can be created using formal Object Notation or initialized using direct quantitative notation.

The Code is as follows:


Var arrObject = new Array ("val1", "val2"); // acts as an Array of Objects
Var arrLiteral = ["val1", "val2"]; // number of direct Arrays

For developers, there is no difference: An Array method can be called on both the direct volume and the object. For the JavaScript engine, it must be re-interpreted every time the array is accessed directly, especially when it is used in a function.

Use the new operator to create a new Array object:

The Code is as follows:


Var arrObject = new Array ();


You can also create a new array with some values:

The Code is as follows:


Var arrObject = new Array ("val1", "val2 ");

Arrays in JavaScript are indexed from 0, which means that the index of the first element is 0, and the last element is the length of the array minus 1.

1. Traverse arrays cyclically

Problem: it is easy to access all elements of the array.

Solution:

To access an array, the most common method is to use the for Loop:

The Code is as follows:


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.