Back to Blog

ES6 — For In / For of

March 14, 2024
ES6 — For In / For of

This article was created to share some knowledge on ES6 features, alongside some usage samples.

Here I will create a small sample of an array to be accessed by For in and For of loops.

I will receive this result if I go through the array using For In loop.

We can observe that, operating for in loop we can access the index of each position of the array.

Now let’s try with the For Of.

Now we got access to the actual value of each position.

In conclusion, the For In can be used to access the index of each array element, and the For Of is used to access the values of each position.

Now, let's complicate it a little bit.

We changed our array into a object, in order to see what kind of effects and things we can do using these loops.

On the For in perspective, we still can access the index of each element.

Now let's see what we can do for For of.

We will use the Object.entries to convert the object into an array.

So now we can access both index and value.

Thanks for the reading and keep tuned for the next article.

Gustavo Kleist

Software Engineer

February 20, 2024

We can help!

What you get is faster time to market, improved security, unlimited scalability and better customer experience. We can help kickstart and support your cloud native adoption. Contact us through the options below: