Uncomplicated - (for ... in) VS (for ... of) loop
Tags: javascript, webdev, typescript, react
Published: 2021-06-06 18:54:53

A 12 year old <b>Nirof</b> 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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyvrro4bjrqy4obutkyo.jpg)<center><h6>Blind Man's Buff.</h6></center><br>

<h1>Enumerables VS Iterables</h1>

<u><i>For simplicity just remember the following two points before I elaborate on them:</i></u>

<b>1 - All objects in JavaScript support enumeration.</b> 
<b>2 - Not all objects in JavaScript are iterable.‍</b>

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 '<b>@@iterator</b>'. 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 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol">here</a>.

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. 
<hr>
This will get clearer with an example. <b>Array, String, Map etc. have internally implemented the '@@iterator'</b> 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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzqagbn5hl9tfp945n2l.png)<center><h6>Iteration Example. <a href="https://codepen.io/mayankav/pen/rNypWPq">Try on codepen</a>.</h6></center>

![Enumeration Example.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w29cnax2muucamy6vw32.png)<center><h6>Enumeration Example. <a href="https://codepen.io/mayankav/pen/rNypWPq">Try on codepen</a>.</h6></center><br>

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 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">property descriptor</a>. 

Do you still remember <b>Nirof</b>, 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. 

<h1>Conclusion</h1>

<b>1 -</b> <u>All objects in JavaScript support enumeration.</u> 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).
<b>2 -</b> <u>Not all objects in JavaScript are iterable.</u> Only the objects which implement the  '@@iterator' function can be called iterable and will work with the "for...of" loop.
<b>3 -</b> <u>Simple Objects</u> - Non-iterable & Support Enumeration 
<b>4 -</b> <u>Arrays, Strings etc..</u> - Iterable & Support Enumeration 

<h2> Originally posted here -  </h2> https://mayankav.webflow.io/blog/for-in-vs-for-of-loop