Let’s be honest, the more we go on with the years the more we realize how old-style traditional for loops are becoming old school. A lot of new constructs have been created that let us write more concise and less error-prone code. The loop for of in Javascript is one of those, let’s try to analyze it in detail.

How to use it

Use for of in Javascript when you need to iterate over the values of an iterable (Arrays, Strings, Set, Map) like: 

For of method basic usage
For of method basic usage

See? This solution doesn’t involve using any index, incrementing statement, making it a lot easier to read and understand while accomplishing the same result. In case you want to check another example of how I used the for of in Javascript to solve a problem in a more clear way checkout this article about reversing strings

Using ‘const’ instead of ‘let’

In the example above, if we are sure that recipe won’t change, we can also use the ‘const’keyword instead of ‘let’:

Iterating through a String

Since a String is considered to be an iterable in Javascript, we can use the for of loop also in this case like: 

Iterating through a string
Iterating through a string


As you have noticed, Javascript is moving forward providing to its users a lot of new interesting constructs to write better code daily, and the for of loop is one of these constructs. Hope it will help you write better loops!

For more information on how to use for of in Javascript, visit the official documentation.


Please enter your comment!
Please enter your name here