last modified June 16, 2022 Show
JavaScript array loop tutorial shows 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 throug 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. In the tutorial, we have covered several ways of looping arrays in JavaScript. List all JavaScript tutorials. How do you make an array loop?If we want to loop through an array, we can use the length property to specify that the loop should continue until we reach the last element of our array. In the loop above, we first initialized the index number so that it begins with 0 .
Can we use for in loop for array in JavaScript?Therefore, the for ... in syntax should not be used for looping through Arrays.
How do you add an array to a for loop?“how to add elements in array in java using for loop” Code Answer's. int[] nums = new int[5];. for(int i = 0; i < nums. length; i++){. nums[i] = i + 2;. System. out. println(nums[i]);. OUTPUT:. 2 3 4 5 6.. How do you create an array for loops in Java?for loop Vs for-each loop. Using for loop. class Main { public static void main(String[] args) { char[] vowels = {'a', 'e', 'i', 'o', 'u'}; // iterating through an array using a for loop for (int i = 0; i < vowels.length; ++ i) { System.out.println(vowels[i]); } } } ... . Using for-each Loop.. |