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: