All VideosJavaScript Tutorials

What is For Of Loop in JavaScript | JavaScript Tutorials in Hindi | Interview Question #47

The for...of loop in JavaScript is used to iterate over iterable objects like arrays, strings, maps, sets, etc. It provides a simpler syntax compared to traditional for loops or forEach methods.

Here’s the basic syntax:

for (variable of iterable) {
  // code block to be executed
}

In each iteration, variable represents the current element of the iterable being iterated over.

For example, iterating over an array:

const array = [1, 2, 3, 4, 5];

for (const element of array) {
  console.log(element);
}

This loop will output:

1
2
3
4
5

One key advantage of for...of loops over traditional for loops is that it automatically handles retrieving the values from the iterable, making the code cleaner and more readable. Additionally, it can be used with any iterable, not just arrays.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!