<?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 Meera Sundar on Medium]]></title>
        <description><![CDATA[Stories by Meera Sundar on Medium]]></description>
        <link>https://medium.com/@msundr?source=rss-4455a6051d4e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*jt-0JxBMpwAhUfIXo8wzgQ.png</url>
            <title>Stories by Meera Sundar on Medium</title>
            <link>https://medium.com/@msundr?source=rss-4455a6051d4e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 12:41:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@msundr/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[Reachability & Scope in Javascript]]></title>
            <link>https://medium.com/@msundr/reachability-scope-in-javascript-f7712aed14f4?source=rss-4455a6051d4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7712aed14f4</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[memory-management]]></category>
            <dc:creator><![CDATA[Meera Sundar]]></dc:creator>
            <pubDate>Fri, 16 Aug 2019 04:09:36 GMT</pubDate>
            <atom:updated>2019-08-16T04:09:36.897Z</atom:updated>
            <content:encoded><![CDATA[<p>Javascript is a multi-paradigm language, which was created with the best ideas borrowed from older programming languages. When it comes to memory management, JS uses automatic <a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)">Garbage Collection</a>.</p><blockquote><strong>..garbage collection</strong> (<strong>GC</strong>) is a form of automatic memory management. The <em>garbage collector</em>, or just <em>collector</em>, attempts to reclaim <em>garbage</em>, or memory occupied by <a href="https://en.wikipedia.org/wiki/Object_(computer_science)">objects</a> that are no longer in use by the program. — Wikipedia</blockquote><p>The JS Garbage Collection works on the concept of ‘<strong>Reachability</strong>’. Here is a simple code snippet to illustrate this idea-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*7JIH_wWO1cFKoTSzwgqemg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*6ff4UJlcb5337hXQ1YHgOQ.png" /><figcaption><strong>Function y does not know variable a. It reaches out to its parent function x to see if it knows ‘a’. No! Parent x reaches out to its parent, the global function, and yes it knows a as 5.</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*NdQ1hHm8V2AkWkftdNSXhA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*z0FG--7nFeiB8Mi7k3-6OA.png" /><figcaption><strong>Function y does not know variable a. It reaches out to its parent function x, and x reaches out to the global a, and re-assigns the value of 50 to it.</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*XYrwYurXl9wA8rAs-HYRiw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*qRaD9WrWsFyVWX_XRkrlpA.png" /><figcaption><strong>Function y does not know variable a. It reaches out to x and x reaches out to the global context. Variable is reassigned the value 500 now.</strong></figcaption></figure><h4>The above code snippets also use the concept of <strong>Variable hoisting. </strong>Function y is called even before declaring/defining it (line #5 above)</h4><p>Few things to note here:</p><blockquote>JS has <strong>first-class functions</strong>. Functions are treated like any other variable</blockquote><blockquote>Even after the function return statement, JS makes an exception for a variable/function declaration and executes it</blockquote><blockquote>The function y declaration is ‘<strong>hoisted</strong>’ above, and executed even before we see the declaration down below</blockquote><h3>The ‘var’ keyword &amp; Scope</h3><p>Modifying the last code a bit-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/1*lF9a1_VtLouLyecrEnNSOg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/308/1*88gkSa_PjuodUQM6tOTFUg.png" /><figcaption><strong>Using the ‘Var’ keyword in each scope, creates a new variable ‘a’ within each function’s scope. Function y does not know or care about ‘a’ in Function x. Function x does not know or care about ‘a’ in the Global scope.</strong></figcaption></figure><p>The variable myX hold on to function y even after the execution of y() is over. a = 500 is still reachable via myX and function y. Hence, the value of a = 500 is not destroyed once y() finishes executing.</p><h3>Summary:</h3><ol><li>JS uses the concept of Reachability for Memory management.</li><li>Variable hoisting is a way of lifting variable/function declarations to the top of their scope.</li><li>‘Var’ is an optional keyword, but makes a big difference in function scope.</li></ol><h4>Next Part: Closures in JS</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7712aed14f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>