{"componentChunkName":"component---src-templates-post-js","path":"/blog/2020/04/24/iterators/","result":{"data":{"post":{"html":"<p>Using a loop is almost a must in our day to day life. But have you ever thought what kind of loop should you use? Do you know the difference between enumerables and iterables? This article sheds some light in this space, so read on if you’re interested.</p>\n<!--more-->\n<h2 id=\"background\" style=\"position:relative;\"><a href=\"#background\" aria-label=\"background permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Background</h2>\n<p>Looping has seen quite a few changes from when I started programming. I remember using while loops and thinking wow, this is cool (I was printing starts on console using MS-DOS).</p>\n<p>For loop is another way to iterate through a data structure which contains many items. But non of these methods iterate over the actual structure. They use a sequence, usually named <code class=\"language-text\">i</code> which mirrors the identifier for you.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"17264531853744970000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const fruits = ['🍉', '🍎', '🍌'];\r\n\r\nfor (let i = 0; i < fruits.length; i++) {\r\n  console.log(fruits[i]);\r\n}\r\n\r\n// 🍉, 🍎, 🍌`, `17264531853744970000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> fruits <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'🍉'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'🍎'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'🍌'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> fruits<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>fruits<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// 🍉, 🍎, 🍌</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Here <code class=\"language-text\">i</code> is an index which allows you to access the elements of fruits array and is not part of the array itself. But with progress on <strong>JavaScript</strong>  towards more modern ways of programming, things have changes now. We have <code class=\"language-text\">for...in</code>, and <code class=\"language-text\">for...of</code> loops which use a different mechanism for going through items in a collection.</p>\n<h2 id=\"concepts\" style=\"position:relative;\"><a href=\"#concepts\" aria-label=\"concepts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Concepts</h2>\n<p>Before we delve into these newer ways of iteration, we need to be on the same page around some concepts. So let’s go through them now:</p>\n<ul>\n<li><strong>Iterables</strong>: This is a kind of loop where we are performing a repetition. Meaning the same set of actions are performed on each item.</li>\n<li><strong>Enumerables</strong>: This is a kind of loop where we making mention of, or counting items one at a time. This means the collection’s elements can be distinctly identified and referenced.</li>\n</ul>\n<p>That didn’t click for me at first, but after going through some examples, it finally made sense. If you consider a <em>full pencil case</em>, that’s an enumerable. When you <em>line up at Aldi</em> to pay, that line is an iterable. A <em>wad of cash</em> is an enumerable, whereas a <em>row of keys on your keyboard</em> is an iterable.</p>\n<blockquote>\n<p>💡 In order for an object to be <code class=\"language-text\">iterable</code>, it MUST implement an <code class=\"language-text\">@@iterator</code> property which returns an iterator. An iterator object is one which has a <code class=\"language-text\">next</code> method which returns the next item in the collection. That’s why an object is not an iterable.</p>\n</blockquote>\n<p>By now you should have started to see the pattern here. The best analogy I came across is:</p>\n<blockquote>\n<p>😍 <strong>Enumerables</strong> are like rectangles whereas <strong>iterables</strong> are squares. This means all <code class=\"language-text\">iterables</code> are <code class=\"language-text\">enumerables</code>, however, not all <code class=\"language-text\">enumerables</code> are <code class=\"language-text\">iterables</code>.</p>\n</blockquote>\n<h2 id=\"code-classlanguage-textforincode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textforincode\" aria-label=\"code classlanguage textforincode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">for...in</code></h2>\n<p>So let’s start from <code class=\"language-text\">enumerables</code>. You can go through enumerables using <code class=\"language-text\">for...in</code>. The use case is mainly to go through key-value pairs in an object:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"91993184911439900000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const superCar = {\r\n  make: 'Lamborghini',\r\n  model: 'Veneno',\r\n  year: '2020'\r\n};\r\n\r\nfor (let key in superCar) {\r\n  console.log(\\`\\${key} --> \\${superCar[key]}\\`);\r\n}\r\n\r\n// make --> Lamborghini\r\n// model --> Veneno\r\n// year --> 2020`, `91993184911439900000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> superCar <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token literal-property property\">make</span><span class=\"token operator\">:</span> <span class=\"token string\">'Lamborghini'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token literal-property property\">model</span><span class=\"token operator\">:</span> <span class=\"token string\">'Veneno'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token literal-property property\">year</span><span class=\"token operator\">:</span> <span class=\"token string\">'2020'</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> key <span class=\"token keyword\">in</span> superCar<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>key<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> --> </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>superCar<span class=\"token punctuation\">[</span>key<span class=\"token punctuation\">]</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// make --> Lamborghini</span>\r\n<span class=\"token comment\">// model --> Veneno</span>\r\n<span class=\"token comment\">// year --> 2020</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>You can also use <code class=\"language-text\">for...in</code> to go through index values of an iterable like an array or a string:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"72419494336356745000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let fact = 'Lamborghini is the best!';\r\n\r\nfor (let index in fact) {\r\n  console.log(\\`Index of \\${fact[index]}: \\${index}\\`);\r\n}\r\n\r\n// Index of L: 0\r\n// Index of a: 1\r\n// Index of m: 2\r\n// Index of b: 3\r\n// ...`, `72419494336356745000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> fact <span class=\"token operator\">=</span> <span class=\"token string\">'Lamborghini is the best!'</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> index <span class=\"token keyword\">in</span> fact<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Index of </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>fact<span class=\"token punctuation\">[</span>index<span class=\"token punctuation\">]</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>index<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// Index of L: 0</span>\r\n<span class=\"token comment\">// Index of a: 1</span>\r\n<span class=\"token comment\">// Index of m: 2</span>\r\n<span class=\"token comment\">// Index of b: 3</span>\r\n<span class=\"token comment\">// ...</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<blockquote>\n<p>💡 Beware that you can’t iterate on <code class=\"language-text\">Symbol</code> properties with <code class=\"language-text\">for...in</code> and that’s because <code class=\"language-text\">Symbols</code> are primitive data types that are <em>always</em> unique. They are generally used as private properties to avoid name clashes when inheritance is used.</p>\n</blockquote>\n<h2 id=\"using-code-classlanguage-textforofcode\" style=\"position:relative;\"><a href=\"#using-code-classlanguage-textforofcode\" aria-label=\"using code classlanguage textforofcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using <code class=\"language-text\">for...of</code></h2>\n<p>This kind of loop is applicable to “iterable objects” meaning the item after <code class=\"language-text\">of</code> MUST be an <code class=\"language-text\">iterable</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"98770471580507750000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const fruits = ['🍉', '🍎', '🍌'];\r\n\r\nfor (let fruit of fruits) {\r\n  console.log(\\`\\${fruit} is delicious.\\`);\r\n}\r\n\r\n// 🍉 is delicious.\r\n// 🍎 is delicious.\r\n// 🍌 is delicious.`, `98770471580507750000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> fruits <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'🍉'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'🍎'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'🍌'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> fruit <span class=\"token keyword\">of</span> fruits<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>fruit<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> is delicious.</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// 🍉 is delicious.</span>\r\n<span class=\"token comment\">// 🍎 is delicious.</span>\r\n<span class=\"token comment\">// 🍌 is delicious.</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>And again we can use it on strings, but with a slight difference:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"71855282145736960000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let fact = 'Yas';\r\n\r\nfor (let char of fact) {\r\n  console.log(char);\r\n}\r\n\r\n// Y\r\n// a\r\n// s`, `71855282145736960000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> fact <span class=\"token operator\">=</span> <span class=\"token string\">'Yas'</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> char <span class=\"token keyword\">of</span> fact<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>char<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// Y</span>\r\n<span class=\"token comment\">// a</span>\r\n<span class=\"token comment\">// s</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>We saw the difference between these two modern ways of looping through collections, let’s make more informed decisions as to use what where and write cleaner, more readable code 👊🏽.</p>","timeToRead":3,"frontmatter":{"unformattedDate":"2020-04-24T00:00:00.000Z","date":"April 24, 2020","title":"Have you ever thought about for loops? ➰","subtitle":null,"id":null,"author":"Yaser Adel Mehraban","gravatar":null,"canonical_url":null,"tags":["showdev","webdev","javascript","forloops"],"thumbnail":null,"cover":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB8UlEQVR42h3O2Y6bMBQGYB66fYjeVxqpL1G1akaaGc20M1lMAmRvYiAsToCw2caYsIaoTqVPv/4Ln+MjrRGB4cWISyO+OLg8keqOVkdaIVIhXPusOed1wJqA1SFr/KxGpPRp5cRMmgc5jMsFSjcB2/jMCHMrys2QmWEGAwoDBv3Mirgb5yjhTsSsmDtJlVxuHqkkEOQywr8my99L82m6/bMwhks4XBqv0/Xju/Ixh2+zzcdCH6/N0dp8Hs/lrQW9PMivCJcS8DItrVekg7izaYdYf+K9X4i8efzqFTdPfFL0x6J3eX/kvegO6wzS7cOLBPxMi0rFzRSbLo7533NhJpVDWpR1fn712NUm7R1tLNIccGPT1iQNxO32zCVgJZqdKSZRXaYhLsxRvvG4mdQWbgUzbXXc6rSF5O5ecKOTdnMuJACjhUOXNlbtbKLHAjASxeUK4updoR4L1b+oca3hVg3b0aHRwnqHxTCXZIvOxmD5/EMx4sfJbgB2T9pBE6+PXD0Vqlfc81SIXStSfn/pPn+6/tTqFWtmXi6Jawev4OHrw9RKZlY6dbDsEjGsnQrN+08UscsvV/FlINdfvnVvRr3CzcSm0mjrTfTkfR/LhxSYCbBS2aWyEcnwLOuhvBcZARsDhwhTJ1FP6RQRYJHhPvwH64oPKxnnzDwAAAAASUVORK5CYII="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/3c8f6aeab4c36ac4fab0c123e77d3f29/a2195/socialpreview.png","srcSet":"/static/3c8f6aeab4c36ac4fab0c123e77d3f29/cadb4/socialpreview.png 750w,\n/static/3c8f6aeab4c36ac4fab0c123e77d3f29/a2195/socialpreview.png 800w","sizes":"(min-width: 800px) 800px, 100vw"},"sources":[{"srcSet":"/static/3c8f6aeab4c36ac4fab0c123e77d3f29/11ba1/socialpreview.webp 750w,\n/static/3c8f6aeab4c36ac4fab0c123e77d3f29/5f042/socialpreview.webp 800w","type":"image/webp","sizes":"(min-width: 800px) 800px, 100vw"}]},"width":800,"height":400}}},"img":null,"socialPreview":{"publicURL":"/static/3c8f6aeab4c36ac4fab0c123e77d3f29/socialpreview.png"}}}},"pageContext":{"title":"Have you ever thought about for loops? ➰","postPath":"/blog/2020/04/24/iterators","nextPost":"/blog/2020/05/03/sorting","previousPost":"/blog/2020/04/13/lighthouse-ci"}},"staticQueryHashes":[],"slicesMap":{}}