Uncomplicated - (for ... in) VS (for ... of) loop

mayankav

mayankav

Posted on June 6, 2021

Uncomplicated - (for ... in) VS (for ... of) loop

A 12 year old Nirof along with some of her friends is playing the blind man's buff in the community park. Nirof is blindfolded so she'd just run into anyone (friends and strangers). Since she can not free her eyes, there is simply no way she can tell who she catches hold of. Her friends while trying to save themselves would also run into people, they could however identify the people they do not know. What am I even saying? I will finish the story (not really a story) towards the end of the post. Before we delve into the real deal, let us try to understand something much important and way much easier to grasp. Let us talk about Enumerables and Iterables.

Blind Man's Buff.

Blind Man's Buff.

Enumerables VS Iterables

For simplicity just remember the following two points before I elaborate on them:

1 - All objects in JavaScript support enumeration.
2 - Not all objects in JavaScript are iterable.‍

If not entirely the same, when you google the terms enumeration and iteration you find quite a bit similarity in their meaning. How do we differentiate between them in JavaScript? To answer that let me tell you that to fall under the category of "Iterables", a JavaScript object must have an implementation of a special method known as '@@iterator'. To be honest the name of the function goes something like this [Symbol.iterator]. If you can then please ignore the silly name and let's call it the '@@iterator' method for simplicity. If you want to delve deep, read more here.

Continuing to answer our question, not every object has this function implemented, so not every object is an iterable. Every object still supports enumeration. Basically, when we say that a JavaScript object supports enumeration, it simply means that we can use a "for...in" loop to read keys of its properties ({key : value}) one by one. On the other hand when we call an object iterable, we indirectly say that the particular object has implemented the '@@iterator' function and we can use a "for..of" loop on the object.



This will get clearer with an example. Array, String, Map etc. have internally implemented the '@@iterator' function all in their own way. A "for...of" loop on an Array object returns elements of the array one by one because the '@@iterator' function it implements is designed to do so. For no other reason, a "for...of" loop on a String object, gives the characters that make up the string. ‍

Iteration Example.

Iteration Example. Try on codepen.

Enumeration Example.

Enumeration Example. Try on codepen.

From the example on iteration, you can easily understand that the iterator on an array simply returns its content. It doesn't have to do anything with the fact that arrays in JavaScript are also objects. It successfully ignores any additional properties added to the JavaScript object but considers all the elements added to the array. On the other hand, when you study the enumeration example you will see that the "for..in" loop is concerned about arrays being objects. It treats the array as an object and gives you the keys to the properties of the object. In the case of arrays, the keys are nothing but indices of the elements in the array. To prevent a property from being considered for enumeration you can simply set "enumerable" option to "false" in the property descriptor.

Do you still remember Nirof, the 12 year old girl. "ni...rof" is the "for..in" loop, she can catch hold of anyone without discrimination. The "for..in" loop works on any kind of object in JavaScript. Nirof's friends are the "for...of" loop (not blind-folded) who can identify the people they know by their faces, the face being the '@@iterator' implementation. "for..of" loops won't play with any object that does not have this face.

Conclusion

1 - All objects in JavaScript support enumeration. You can use the "for..in" loop to read their properties ( keys ) one by one. Basically its the properties of an object which are enumerable or non enumerable (property descriptor).
2 - Not all objects in JavaScript are iterable. Only the objects which implement the  '@@iterator' function can be called iterable and will work with the "for...of" loop.
3 - Simple Objects - Non-iterable & Support Enumeration
4 - Arrays, Strings etc.. - Iterable & Support Enumeration

Originally posted here -

https://mayankav.webflow.io/blog/for-in-vs-for-of-loop
💖 💪 🙅 🚩
mayankav
mayankav

Posted on June 6, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Micro Frontends with Vite and Bit
javascript Micro Frontends with Vite and Bit

March 13, 2024

#2 React Query: Infinite Scroll
javascript #2 React Query: Infinite Scroll

January 28, 2024

Power of Javascript Array Methods 🔍🛠️
javascript Power of Javascript Array Methods 🔍🛠️

September 29, 2023

Better API Calls 🎣
javascript Better API Calls 🎣

September 24, 2023

Mastering JavaScript Date and Time 📆
javascript Mastering JavaScript Date and Time 📆

September 22, 2023