{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/iterators-and-generators.html","result":{"data":{"markdownRemark":{"id":"7c11a779-bcbb-573a-bba2-ab476696459d","excerpt":"Iterables An object is deemed iterable if it has an implementation for the Symbol.iterator property.\nSome built-in types like Array, Map, Set, String, Int…","html":"<h1 id=\"iterables\"><a href=\"#iterables\" aria-label=\"iterables permalink\" class=\"anchor\"><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>Iterables</h1>\n<p>An object is deemed iterable if it has an implementation for the <a href=\"Symbols.md#symboliterator\"><code>Symbol.iterator</code></a> property.\nSome built-in types like <code>Array</code>, <code>Map</code>, <code>Set</code>, <code>String</code>, <code>Int32Array</code>, <code>Uint32Array</code>, etc. have their <code>Symbol.iterator</code> property already implemented.\n<code>Symbol.iterator</code> function on an object is responsible for returning the list of values to iterate on.</p>\n<h2 id=\"forof-statements\"><a href=\"#forof-statements\" aria-label=\"forof statements permalink\" class=\"anchor\"><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>for..of</code> statements</h2>\n<p><code>for..of</code> loops over an iterable object, invoking the <code>Symbol.iterator</code> property on the object.\nHere is a simple <code>for..of</code> loop on an array:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> someArray = [</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"string\"</span><span style=\"color: #000000\">, </span><span style=\"color: #0000FF\">false</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> entry </span><span style=\"color: #0000FF\">of</span><span style=\"color: #000000\"> someArray) {</span>\n<span style=\"color: #000000\">    console.log(entry); </span><span style=\"color: #008000\">// 1, \"string\", false</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h3 id=\"forof-vs-forin-statements\"><a href=\"#forof-vs-forin-statements\" aria-label=\"forof vs forin statements permalink\" class=\"anchor\"><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>for..of</code> vs. <code>for..in</code> statements</h3>\n<p>Both <code>for..of</code> and <code>for..in</code> statements iterate over lists; the values iterated on are different though, <code>for..in</code> returns a list of <em>keys</em> on the object being iterated, whereas <code>for..of</code> returns a list of <em>values</em> of the numeric properties of the object being iterated.</p>\n<p>Here is an example that demonstrates this distinction:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> list = [</span><span style=\"color: #09835A\">4</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">6</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> i </span><span style=\"color: #0000FF\">in</span><span style=\"color: #000000\"> list) {</span>\n<span style=\"color: #000000\">    console.log(i); </span><span style=\"color: #008000\">// \"0\", \"1\", \"2\",</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> i </span><span style=\"color: #0000FF\">of</span><span style=\"color: #000000\"> list) {</span>\n<span style=\"color: #000000\">    console.log(i); </span><span style=\"color: #008000\">// \"4\", \"5\", \"6\"</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Another distinction is that <code>for..in</code> operates on any object; it serves as a way to inspect properties on this object.\n<code>for..of</code> on the other hand, is mainly interested in values of iterable objects. Built-in objects like <code>Map</code> and <code>Set</code> implement <code>Symbol.iterator</code> property allowing access to stored values.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pets = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Set([</span><span style=\"color: #A31515\">\"Cat\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Dog\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Hamster\"</span><span style=\"color: #000000\">]);</span>\n<span style=\"color: #000000\">pets[</span><span style=\"color: #A31515\">\"species\"</span><span style=\"color: #000000\">] = </span><span style=\"color: #A31515\">\"mammals\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pet </span><span style=\"color: #0000FF\">in</span><span style=\"color: #000000\"> pets) {</span>\n<span style=\"color: #000000\">    console.log(pet); </span><span style=\"color: #008000\">// \"species\"</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pet </span><span style=\"color: #0000FF\">of</span><span style=\"color: #000000\"> pets) {</span>\n<span style=\"color: #000000\">    console.log(pet); </span><span style=\"color: #008000\">// \"Cat\", \"Dog\", \"Hamster\"</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h3 id=\"code-generation\"><a href=\"#code-generation\" aria-label=\"code generation permalink\" class=\"anchor\"><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 generation</h3>\n<h4 id=\"targeting-es5-and-es3\"><a href=\"#targeting-es5-and-es3\" aria-label=\"targeting es5 and es3 permalink\" class=\"anchor\"><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>Targeting ES5 and ES3</h4>\n<p>When targeting an ES5 or ES3-compliant engine, iterators are only allowed on values of <code>Array</code> type.\nIt is an error to use <code>for..of</code> loops on non-Array values, even if these non-Array values implement the <code>Symbol.iterator</code> property.</p>\n<p>The compiler will generate a simple <code>for</code> loop for a <code>for..of</code> loop, for instance:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> numbers = [</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">3</span><span style=\"color: #000000\">];</span>\n<span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> num </span><span style=\"color: #0000FF\">of</span><span style=\"color: #000000\"> numbers) {</span>\n<span style=\"color: #000000\">    console.log(num);</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>will be generated as:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> numbers = [</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">3</span><span style=\"color: #000000\">];</span>\n<span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> _i = </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">; _i &lt; numbers.length; _i++) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> num = numbers[_i];</span>\n<span style=\"color: #000000\">    console.log(num);</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h4 id=\"targeting-ecmascript-2015-and-higher\"><a href=\"#targeting-ecmascript-2015-and-higher\" aria-label=\"targeting ecmascript 2015 and higher permalink\" class=\"anchor\"><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>Targeting ECMAScript 2015 and higher</h4>\n<p>When targeting an ECMAScipt 2015-compliant engine, the compiler will generate <code>for..of</code> loops to target the built-in iterator implementation in the engine.</p>","headings":[{"value":"Iterables","depth":1},{"value":"for..of statements","depth":2},{"value":"for..of vs. for..in statements","depth":3},{"value":"Code generation","depth":3},{"value":"Targeting ES5 and ES3","depth":4},{"value":"Targeting ECMAScript 2015 and higher","depth":4}],"frontmatter":{"permalink":"/docs/handbook/iterators-and-generators.html","title":"Iterators and Generators"}}},"pageContext":{"slug":"/docs/handbook/iterators-and-generators.html","isOldHandbook":true}}}