GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — JavaScript Array Loop - Traversing Arrays JavaScript array looplast modified last modified October 18, 2023 In this article we show how to loop over arrays in JavaScript. We can loop over elements with forEach method and for and while statements. An array is a collection of a number of values. The array items are called elements of the array. The following ways can be used to iterate over elements of an array in JavaScript:
JavaScript array loop with forEach
The foreach.js
const words = ['pen', 'pencil', 'falcon', 'rock', 'sky', 'earth'];
words.forEach(e => console.log(e));
console.log('-------------------------');
words.forEach((e, idx) => console.log(`${e} has index ${idx}`));
We have an array of words. With the words.forEach(e => console.log(e)); We loop over elements of the array. words.forEach((e, idx) => console.log(`${e} has index ${idx}`));
In this form, we have an element and its index at disposal. $ node foreach.js pen pencil falcon rock sky earth ------------------------- pen has index 0 pencil has index 1 falcon has index 2 rock has index 3 sky has index 4 earth has index 5 In the following example, we work with numbers. foreach2.js
let vals = [1, 2, 3, 4, 5];
vals.forEach(e => console.log(e * e))
console.dir(vals);
console.log('----------------');
let vals2 = vals.map(e => e * e);
vals2.forEach(e => console.log(e));
console.dir(vals2);
We apply a numeric operation on the array values. let vals = [1, 2, 3, 4, 5]; We have an array of values. vals.forEach(e => console.log(e * e)) We traverse the array and power all its elements. console.dir(vals);
The contents of the array are shown with let vals2 = vals.map(e => e * e);
With the vals2.forEach(e => console.log(e)); We go through the array of the newly created array. $ node foreach2.js 1 4 9 16 25 [ 1, 2, 3, 4, 5 ] ---------------- 1 4 9 16 25 [ 1, 4, 9, 16, 25 ] JavaScript array loop with for in
The for_in.js
let words = ['pen', 'pencil', 'falcon', 'rock', 'sky', 'earth'];
for (let idx in words) {
console.log(`${words[idx]} has index ${idx}`);
}
The example iterates over indexes of the array of words. It prints words along with their indexes. $ node for_in.js pen has index 0 pencil has index 1 falcon has index 2 rock has index 3 sky has index 4 earth has index 5 JavaScript array loop with for of
With the for_of.js
let words = ['pen', 'pencil', 'falcon', 'rock', 'sky', 'earth'];
for (let word of words) {
console.log(word);
}
The example prints all the words of the JavaScript array loop with classic for statementJavaScript supports the classic C-style for statement. It uses an auxiliary counter variable to traverse the array. A for loop has three phases: initialization, condition and code block execution, and incrementation. classic_for.js
let words = ['pen', 'pencil', 'falcon', 'rock', 'sky', 'earth'];
for (let i=0; i<words.length; i++) {
console.log(words[i]);
}
The example loops over the array of words with the classic for loop. for (let i=0; i<words.length; i++) {
The
In the first phase, we initiate the counter JavaScript array loop with while statementThe while statement is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while keyword executes the statements inside the block enclosed by the curly brackets. The statements are executed each time the expression is evaluated to true. while_loop.js
let words = ['pen', 'pencil', 'falcon', 'rock', 'sky', 'earth'];
let i = 0;
while (i < words.length) {
console.log(words[i]);
i++;
}
The while loop is similar to the for loop; We have the counter variable and there are three phases of the while loop. SourceIn the article we have covered several ways of looping arrays in JavaScript. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |