<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Meet Patel on Medium]]></title>
        <description><![CDATA[Stories by Meet Patel on Medium]]></description>
        <link>https://medium.com/@patelmeet8233?source=rss-68314d49964e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1B1gSfHLZ8p1-ZeD0ETTEg.jpeg</url>
            <title>Stories by Meet Patel on Medium</title>
            <link>https://medium.com/@patelmeet8233?source=rss-68314d49964e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:59:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@patelmeet8233/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Iterators and Generators in Javascript]]></title>
            <link>https://medium.com/simform-engineering/iterators-and-generators-in-javascript-22da731b7fe3?source=rss-68314d49964e------2</link>
            <guid isPermaLink="false">https://medium.com/p/22da731b7fe3</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[generator]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[iterators]]></category>
            <dc:creator><![CDATA[Meet Patel]]></dc:creator>
            <pubDate>Mon, 17 Jul 2023 05:08:31 GMT</pubDate>
            <atom:updated>2023-07-17T10:27:26.591Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A Comprehensive Guide to JavaScript’s Iteration Mechanisms.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*bBOlwYw863-BK_2q3ElsXw.png" /></figure><p>In the world of JavaScript, <strong>Iterators and Generator Functions</strong> are powerful tools that empower developers to have control over data iteration and flow. Together, these functions form an impressive duo that enables us to write concise, efficient, and flexible JavaScript code.</p><blockquote><strong>Iterator </strong>and <strong>generator functions</strong> are advanced concepts in JavaScript that allow for efficient and customizable looping through data structures. They also provide a mechanism for customizing the behavior of <strong>for…of</strong> loops.</blockquote><p>Let’s understand what exactly iterator and generator functions entail.</p><h3><strong>Iterators</strong></h3><p>In JavaScript, an iterator function is a unique function that returns an iterator object. An iterator object is an object that has a<strong> </strong><strong>next()</strong> method, which returns an object with two properties: <strong>value</strong> and <strong>done</strong>. The value property represents the next value in the sequence, and the done property indicates whether the iterator has reached the end of the sequence.</p><p>Iterator functions can be used to iterate over collections of data, such as arrays or objects.</p><p>Here is an example of an iterator function that iterates over an array:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/565275b806efdab16fb1e8d994b49b3c/href">https://medium.com/media/565275b806efdab16fb1e8d994b49b3c/href</a></iframe><p>The above code defines a function called <strong><em>Iterator</em></strong> that takes an array as an argument and returns an iterator object. The iterator object has a method called <strong>next,</strong> which returns the next item in the array and updates an internal <strong>nextIndex</strong> variable to keep track of the array index.</p><p>The <strong>next</strong> method checks whether <strong>nextIndex</strong> is less than the length of the array. If it is, the method returns an object with the value of the array at the <strong>nextIndex</strong> position and sets the <strong>done</strong> property to <strong>false</strong>. The <strong>nextIndex</strong> variable is then incremented by one. If <strong>nextIndex</strong> is greater than or equal to the array length, <strong>next</strong> sets the <strong>done</strong> property to <strong>true</strong>.</p><p>The code then defines an array of numbers [1, 2, 3, 4, 5] and creates an iterator object from it using the <strong><em>iterator </em></strong>function. The <strong>arrayValue</strong> variable is assigned to the iterator object.</p><p>The code then calls the next method on the <strong>arrayValue</strong> iterator object multiple times, logging the returned objects to the console. Each time the <strong>next</strong> method is called, it returns an object with either the value of the next item in the array or a <strong>done</strong> property of true, indicating that there are no more items in the array.</p><p>The first few calls to <strong>next</strong> will log the values [1, 2, 3, 4, 5], and the subsequent calls will log objects with a <strong>done</strong> property of true and a value of undefined.</p><p>But hold on… we can directly use the <strong><em>Symbol.iterator </em></strong>function to iterate over an array.</p><p>Let’s see an example of it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/298eafd9eef3ff4c4c00777ef337803c/href">https://medium.com/media/298eafd9eef3ff4c4c00777ef337803c/href</a></iframe><p>Here, we can see that the <strong><em>Symbol.iterator</em></strong> is a function that returns the next function, which is the same as the function that we had made earlier🤩.</p><h3><strong>Generators</strong></h3><p>A generator function is a special type of function that can be used to control the flow of execution by <strong>yielding</strong> values one at a time instead of returning them all at once. When a generator function is called, it does not execute immediately but instead returns a generator object that can be used to control the execution of the function.</p><p>The generator object has a <strong>next()</strong> method that can be used to resume execution of the function, and each time the function encounters a <strong>yield</strong> statement, it returns the yielded value and suspends execution until the <strong>next()</strong> method is called.</p><p>Generator functions are useful for creating iterators and writing asynchronous code using <strong>async/await</strong> syntax. They allow you to write code that looks like synchronous code but executes asynchronously in the background.</p><p>Generator functions are declared using the <strong>function*</strong> syntax, which is similar to regular function syntax but with an <strong>asterisk (*) </strong>after the function keyword.</p><p>Here&#39;s an example of a simple generator function that yields the numbers 1, 2, and 3:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ed1a4c1e6e8a6e4c2182b550e7ef64f/href">https://medium.com/media/4ed1a4c1e6e8a6e4c2182b550e7ef64f/href</a></iframe><p>Here, when calling the <strong>myGenerator()</strong> function, which returns a generator object, the <strong>next()</strong> method can be called on the generator object to resume execution of the generator function and return the next yielded value.</p><p>Each call to <strong>next()</strong> resumes execution of the generator function from the point at which it was paused by the last yield statement. When the generator function has no more values to yield, it returns <strong>{ value: undefined, done: true }.</strong></p><p>Let’s understand it with another example of the <strong>Fibonacci series</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96076a34e234b7eaf4f4dea043e9de39/href">https://medium.com/media/96076a34e234b7eaf4f4dea043e9de39/href</a></iframe><p>In this example, we define a generator function called <strong>fibonacciGenerator()</strong>. It uses the <strong>yield</strong> keyword to return the current Fibonacci number and then continues execution from where it left off.</p><p>Inside the generator function, we maintain two variables: <strong>current</strong> and <strong>next</strong>. We start with the <strong>current</strong> set to 0 and <strong>next</strong> set to 1, representing the first two Fibonacci numbers.</p><p>The generator enters an infinite loop using <strong>while(true)</strong>. In each iteration, it yields the current Fibonacci number using <strong>yield current</strong> and then calculates the next Fibonacci number by adding <strong>current</strong> and <strong>next</strong>. Finally, it updates the values of <strong>current</strong> and <strong>next</strong> using destructuring assignment: <strong>[current, next] = [next, current + next]</strong>.</p><p>To use the generator, we create an instance of it using <strong>const fibonacci =</strong> <strong>fibonacciGenerator()</strong>. Then we can call <strong>fibonacci.next().value</strong> to get the next Fibonacci number in the sequence.</p><p>Here, we generate the first 10 Fibonacci numbers by calling <strong>fibonacci.next().value</strong> in a loop and printing the result to the console.</p><h3><strong>Advantages of Iterator and Generator Functions</strong></h3><p>Iterators and generator functions offer several advantages. Here are some key benefits:</p><p><strong>1. Iterating over collections</strong></p><ul><li>Iterators and generator functions provide a standardized way to iterate over collections of data, such as arrays, sets, maps, or custom data structures.</li><li>They allow you to access each element in a sequence one at a time, simplifying the process of working with collections.</li></ul><p><strong>2. Lazy Evaluation</strong></p><ul><li>Both iterators and generator functions enable lazy evaluation, which means that the data is generated or fetched on demand as it is iterated over.</li><li>This can greatly reduce memory usage and processing overhead, especially when working with large datasets, as it allows you to process data one piece at a time instead of loading the entire dataset into memory.</li></ul><p><strong>3. Code Reusability</strong></p><ul><li>Both iterators and generator functions promote code reusability as they provide a consistent way to iterate over different types of data collections.</li><li>Once you’ve implemented an iterator or generator function for a specific type of data, you can reuse it in multiple parts of your codebase without having to duplicate the iteration logic.</li></ul><p><strong>4. Asynchronous Iteration</strong></p><ul><li>Generator functions can be used to implement asynchronous iteration, allowing you to work with asynchronous data sources, such as APIs or streams, in a more streamlined way.</li><li>This can simplify your asynchronous code and make it easier to handle errors and manage resources.</li></ul><p><strong>5. Improves Performance</strong></p><ul><li>Using iterator and generator functions often leads to improved performance compared to traditional approaches, especially when working with large datasets.</li><li>Lazy evaluation and customizable iteration logic can help optimize memory usage and processing time, resulting in faster and more efficient code.</li></ul><h3><strong>Conclusion</strong></h3><p><strong>Iterator</strong> and <strong>Generator functions</strong> help you fetch and process data efficiently, with <strong><em>flexibility</em></strong>, <strong><em>reusability</em></strong>, and <strong><em>improved performance,</em></strong> making your codebase more manageable and scalable. Just like how a well-organized kitchen helps a chef create delicious meals, <strong>Iterator</strong> and <strong>Generator functions </strong>are essential tools for modern JavaScript developers to handle data processing requirements effectively.</p><blockquote><strong>For more updates on the latest tools and technologies, follow the </strong><a href="https://medium.com/simform-engineering"><strong>Simform Engineering</strong></a><strong> blog.</strong></blockquote><blockquote><strong>Follow Us: </strong><a href="https://twitter.com/simform"><strong>Twitter</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/simform/"><strong>LinkedIn</strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22da731b7fe3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/simform-engineering/iterators-and-generators-in-javascript-22da731b7fe3">Iterators and Generators in Javascript</a> was originally published in <a href="https://medium.com/simform-engineering">Simform Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>