JavaScript for…of Loop
The JavaScript for…of loop is a modern, iteration statement introduced in ECMAScript 2015 (ES6). It allows developers to iterate over iterable objects such as arrays, strings, maps, sets, and more. It provides a simpler syntax compared to traditional for loops.
What is the for…of Loop?
The for…of loop iterates over the values of an iterable object, which is different from the for…in loop that iterates over the enumerable properties of an object.
Syntax:
for ( variable of Iterable ) { // Code block to be executed }
Parameters:
- variable: Represents the current value of each iteration from the iterable.
- iterable: Any object that can be iterated over (e.g., arrays, strings, maps).
Examples of Using for…of Loop
1. Iterate Over an Array using for…of Loop
Arrays are one of the most common data structures in JavaScript. The for…of loop provides a concise way to iterate over array elements:
Example: The code initializes an array with values 1 through 5. It then iterates over each element of the array using a for…of loop, logging each element to the console.
const array = [ 1, 2, 3, 4, 5 ];
for (const item of array) {
console.log(item);
}
Output
1 2 3 4 5
2. Iterating Over a String using for…of Loop
Strings are also iterable objects in JavaScript. You can use the for…of loop to iterate over each character in a string.
const str = "Hello";
for (const char of str) {
console.log(char);
}
Output
H e l l o
Code Explanation
stris the string you want to loop over.Initiates thefor...ofloop, wherecharrepresents each character in the string during each iteration.console.log(char)prints each character to the console during each iteration of the loop.
3. Iterating Over a Map using for…of Loop
Maps are a new data structure in ES6 that store key-value pairs. The for…of loop can be used to iterate over the entries of a map.
const map = new Map([
["name", "Akash"],
["age", 25],
["city", "Noida"]
]);
for (let [key, value] of map) {
console.log(`${key}: ${value}`);
}
Output
name: Akash age: 25 city: Noida
Code Explanation
- First, we create
a Map object where we want to iterate over. - Initiates the
for...ofloop, where[key, value]represents each key-value pair in the Map during each iteration. - Inside the loop,
console.log(`${key}: ${value}`);prints each key-value pair to the console during each iteration of the loop.
4. Iterating Over a Set using for…of Loop
Sets are another new data structure in ES6 that store unique values. You can use the for…of loop to iterate over the elements of a set.
let set = new Set([1, 2, 3, 4, 5]);
for (let value of set) {
console.log(value);
}
Output
1 2 3 4 5
5. Iterating Over an Object’s Properties using for…of Loop
While the for…of loop is not directly used to iterate over object properties, you can use it in combination with Object.keys(), Object.values(), or Object.entries() to achieve this.
let person = {
name: "Akash",
age: 25,
city: "Noida"
};
for (let key of Object.keys(person)) {
console.log(`${key}: ${person[key]}`);
}
Output
name: Akash age: 25 city: Noida
The for…of loop in JavaScript provides a powerful and efficient way to iterate over iterable objects. Its syntax is simpler and more intuitive compared to traditional loops, making code easier to read and maintain. For more advanced JavaScript tutorials and examples, explore our JavaScript Tutorial and JavaScript Examples.
JavaScript for…of Loop – FAQs
What is a for-of loop in JavaScript?
A for-of loop is a control structure that allows you to iterate over the values of an iterable object, such as arrays, strings, maps, sets, and more. This loop is designed to traverse the values directly rather than the keys or indices.
What is the basic syntax of a for-of loop?
The basic syntax of a for-of loop includes the for keyword, followed by a variable to hold each value, the of keyword, and the iterable object to iterate over. The code block to be executed for each value is placed inside curly braces.
How does the for-of loop work?
The for-of loop iterates over the values of an iterable object. For each value, the loop assigns the value to the specified variable and executes the code block.
What is the difference between for-of and for-in loops?
for-of loop: Iterates over the values of an iterable object (such as arrays, strings, maps, etc.).
for-in loop: Iterates over the enumerable properties (keys) of an object.
Can a for-of loop be used with objects?
A for-of loop cannot be used directly with objects because objects are not inherently iterable. However, you can use a for-of loop in conjunction with Object.keys(), Object.values(), or Object.entries() to iterate over an object’s keys, values, or key-value pairs.

