Javascript for vs forEach vs for…in loop

Photo by Tine Ivanič on Unsplash

Javascript has many features that make it nice to work with. Iterating over objects is one of those features. Classically in programming we use a for loop to iterate over a list of items or objects.

The traditional for loops looks as follows:

This is nice but now you have to worry about indices. This can get a bit hairy and if your like me and hate looking at tons of variables then id use this as a last resort.

Javascript has created two alternatives forEach and for…in


If you have an array. Such as the following:

In javascript all arrays have the functional property forEach. You use it by specifying a function that takes a parameter. Everytime your function is called its being called with each item in the array.

If you don’t have a need to break early while iterating over an array this is a great function to use.


A for in loop can be very useful. Use a for…in loop to iterate over any object thats enumerable. Meaning an array or object or strings. If you are using a for…in loop over an object it will give you the value to each key in the object.

For…in loops do not have a specific order for execution. Therefore you shouldn’t use it if you are expecting a specific order.

If you need to guarantee order on an object. I recommend specifying either an index in your objects or put your keys in an array.


Javascript has made functions that are easier to iterate over enumerable objects on. While we can use these there may be a few cases when we will still want to rely on the plain old for loop.

Happy coding!

follow me on twitter: @DuffinAvery

Im a software engineer, react developer, inventor, salesman, family man, religious, and outdoor connoisseur

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store