<?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 Abhishek Khatri on Medium]]></title>
        <description><![CDATA[Stories by Abhishek Khatri on Medium]]></description>
        <link>https://medium.com/@abhikhatri67?source=rss-de026aa7c77a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RlKqbEc_pbAbI8kvDHnd8w.jpeg</url>
            <title>Stories by Abhishek Khatri on Medium</title>
            <link>https://medium.com/@abhikhatri67?source=rss-de026aa7c77a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:12:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abhikhatri67/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[Simplifying Closure in JavaScript.]]></title>
            <link>https://blog.goteams.de/simplifying-closure-in-javascript-7fb825c781a6?source=rss-de026aa7c77a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7fb825c781a6</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[closure]]></category>
            <dc:creator><![CDATA[Abhishek Khatri]]></dc:creator>
            <pubDate>Sun, 13 Dec 2020 12:32:27 GMT</pubDate>
            <atom:updated>2025-10-08T07:52:55.780Z</atom:updated>
            <content:encoded><![CDATA[<h3>Simplifying Closures in JavaScript (Beginner-Friendly Guide)</h3><p>Many developers face difficulty understanding the concept of closure in JavaScript. I had these difficulties too when I started as a JavaScript developer, so I thought maybe I should give it a try to explain it to all my fellow developers who are learning JavaScript.</p><blockquote><strong>According to MDN:</strong> A <strong>closure</strong> is the combination of a function bundled together (enclosed) with references to its surrounding state (the <strong>lexical environment</strong>).</blockquote><blockquote>In other words, a closure gives you access to an outer function’s scope from an inner function.</blockquote><p>Let’s understand this in terms of code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63a24fba3eb34dd90b13504d639f579e/href">https://medium.com/media/63a24fba3eb34dd90b13504d639f579e/href</a></iframe><p>That is the closure?</p><p>Yes, it is.</p><figure><img alt="Paused in Debugger." src="https://cdn-images-1.medium.com/max/1024/1*TXr9TXJjlfttd2am4jbDFg.png" /><figcaption>Paused in the debugger.</figcaption></figure><p>When it paused in the debugger and under the scope section, you can see local, <strong>Closure (foo)</strong>, and Global. It created a Closure over foo, which contains <em>(text)</em> its surrounding state (the lexical environment).</p><p>Let’s see another example: we return a function instead of calling it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57e0aef17aef10f15e3a92fa523d9b18/href">https://medium.com/media/57e0aef17aef10f15e3a92fa523d9b18/href</a></iframe><blockquote>OR</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d9f1488e16338357969dbbf709043de/href">https://medium.com/media/6d9f1488e16338357969dbbf709043de/href</a></iframe><p>In this example, the foo function is executed and removed from the call stack, but as we returned a bar function, now it no longer resides under foo and it can be accessed anywhere in the code.</p><blockquote>But what happen when you invoke baz.</blockquote><pre>baz(); // Hello, Closure.</pre><p>So here comes the concept of closure in the picture.</p><blockquote><strong>When functions are returned from another function they still remember their lexical scope along with their function definition.</strong></blockquote><p>Here’s a slightly more interesting example — a counter function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fe9d18d1a1c25b755d8f36662f31b00/href">https://medium.com/media/8fe9d18d1a1c25b755d8f36662f31b00/href</a></iframe><p>In this example, you can see the state of the counter maintained for both <em>count1</em> &amp; <em>count2.</em> Line 10 &amp; line 14 execute the counter function once, then it is removed from the call stack, but <em>count1 &amp;</em> <em>count2</em> remember the returned function and its surrounding state.</p><h4>Conclusion</h4><p>Hopefully, this article gave you a good introduction to closure and simplified it for you. Follow the GoGroup Tech Blog for more tech advice articles, and <a href="https://www.gogroup.tech/careers">check out our job openings here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D2tzZdi5C-ClWg_W.png" /><figcaption><a href="https://www.gogroup.co/">Click here for more information on GoGroup!</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7fb825c781a6" width="1" height="1" alt=""><hr><p><a href="https://blog.goteams.de/simplifying-closure-in-javascript-7fb825c781a6">Simplifying Closure in JavaScript.</a> was originally published in <a href="https://blog.goteams.de/">The GoTeams Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>