Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
for-of

What is a for-of loop in JavaScript?

Educative Answers Team

A for-of loop is like a for loop that specializes in iterating through iterable bodies. Iterating through iterable bodies is a very common programming practice, so, having a dedicated loop for it can be very useful.


Example

// Using the traditional for-loop

iterable = [11,22,33,44,55]
 
 for(let i =0; i<iterable.length; i+=1)
 {
     console.log(iterable[i])
 }

For-of vs. for-in

Another loop that does a very similar job is the for-in loop. However, it is very important that you do not confuse them.

// Using the for-in loop

iterable = [11,22,33,44,55]
 
 for(let index in iterable) // The for-in loop
 {
     console.log(index) // prints the indices rather than elements
 }
svg viewer
for-of loop
svg viewer
for-of loop

RELATED TAGS

javascript
for-of
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring