JavaScript - Iterate Over an Array Last Updated : 10 Jan, 2025 Comments Improve Suggest changes Like Article Like Report JavaScript for Loop can be used to iterate over an array. The for loop runs for the length of the array and in each iteration it executes the code defined inside. We can access the array elements using the index number.1. Using for...of LoopThe for…of loop iterates over the values of an iterable object such as an array. It is a better choice for traversing items of iterables compared to traditional for and for in loops, especially when we have a break or continue statements. JavaScript let a = [10, 20, 30, 40, 50]; for (let item of a) { console.log(item); } Output10 20 30 40 50 2. Using forEach() MethodThe forEach() Method calls the provided function once for every array element in the order. It does not return a new array and does not modify the original array. It’s commonly used for iteration and performing actions on each array element. JavaScript let a = [10, 20, 30, 40, 50]; a.forEach((item) => { console.log(item); }); Output10 20 30 40 50 3. Using for...in LoopA for...in Loop iterates over the enumerable properties of an object, allowing you to access each key or property name in turn. The for…in loop can also works to iterate over the properties of an array if maintaining index order is important. JavaScript let a = [10, 20, 30, 40, 50]; for (let i in a) { console.log(a[i]); } Output10 20 30 40 50 4. Using for loopThe for Loop executes a set of instructions repeatedly until the given condition becomes false. It is similar to loops in other languages like C/C++, Java, etc. JavaScript let a = [10, 20, 30, 40, 50]; for (let i = 0; i < a.length; i++) { console.log(a[i]); } Output10 20 30 40 50 5. Using while LoopA while loop in JavaScript is a control flow statement that allows the code to be executed repeatedly based on the given boolean condition. JavaScript let a = [10, 20, 30, 40, 50]; let i = 0; while (i < a.length) { console.log(a[i]); i++; } Output10 20 30 40 50 6. Using do...while LoopThe do...while Loop in JavaScript is a control structure where the code executes repeatedly based on a given boolean condition. The do...while loop executes the code at least once. JavaScript let a = [10, 20, 30, 40, 50]; let i = 0; do { console.log(a[i]); i++; } while (i < a.length); Output10 20 30 40 50 Comment More infoAdvertise with us Next Article JavaScript - Iterate Over an Array V Vineet Joshi Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads Java Program to Iterate Over Arrays Using for and for-each Loop In Java, arrays are a collection of elements of the same type. To access and manipulate elements, we can use iteration techniques such as the for loop and for-each loop (also known as the enhanced for loop).Program to Iterate Over Arrays Using for and for-each Loop in JavaIn the below example, we wi 1 min read Java - Loop Through an Array In Java, looping through an array or Iterating over arrays means accessing the elements of the array one by one. We have multiple ways to loop through an array in Java. Example 1: Here, we are using the most simple method i.e. using for loop to loop through an array.Java// Java program to loop throu 3 min read Java for loop vs Enhanced for loop In Java, loops are fundamental constructs for iterating over data structures or repeating blocks of code. Two commonly used loops are the for loop and the enhanced for loop. While they serve similar purposes, their applications and usage vary based on the scenario.for loop vs Enhanced for loopBelow 4 min read Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem 6 min read ArrayDeque iterator() Method in Java The Java.util.ArrayDeque.iterator() method is used to return an iterator of the elements of the ArrayDeque. Syntax: Iterator iterate_value = Array_Deque.iterator(); Parameters: The method does not take any parameter. Return Value: The method iterates over the elements of the deque and returns the va 2 min read String Array with Enhanced For Loop in Java Enhanced for loop(for-each loop) was introduced in java version 1.5 and it is also a control flow statement that iterates a part of the program multiple times. This for-loop provides another way for traversing the array or collections and hence it is mainly used for traversing arrays or collections. 1 min read How to Print an Array in Java Without using Loop? Given an array arr in Java, the task is to print the contents of this array without using any loop. First let's see the loop method. Loop method: The first thing that comes to mind is to write a for loop from i = 0 to n, and print each element by arr[i].Pseudo Code: for(int i = 0; i < Array.lengt 2 min read Traverse Through ArrayList in Forward Direction in Java ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java. The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in a proper organized sequence). ArrayList can be 3 min read ArrayList iterator() method in Java with Examples The iterator() method of ArrayList class in Java Collection Framework is used to get an iterator over the elements in this list in proper sequence. The returned iterator is fail-fast. Syntax: Iterator iterator() Parameter: This method do not accept any parameter. Return Value: This method returns an 2 min read Traverse through a HashSet in Java As we all know HashSet elements are unordered so the traversed elements can be printed in any order. In order to perform operations over our HashSet such as insertion, deletion, updating elements than first we need to reach out in order to access the HashSet. below are few ways with which we can ite 3 min read Like