<?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 Catalina Hernandez on Medium]]></title>
        <description><![CDATA[Stories by Catalina Hernandez on Medium]]></description>
        <link>https://medium.com/@hernandezcat7?source=rss-5ef70bd3a446------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yGwgOAClkH3_SzPZz2t4lA.jpeg</url>
            <title>Stories by Catalina Hernandez on Medium</title>
            <link>https://medium.com/@hernandezcat7?source=rss-5ef70bd3a446------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 11:31:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hernandezcat7/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[So, you’re learning about scope…]]></title>
            <link>https://hernandezcat7.medium.com/so-youre-learning-about-scope-6eb27db2037a?source=rss-5ef70bd3a446------2</link>
            <guid isPermaLink="false">https://medium.com/p/6eb27db2037a</guid>
            <category><![CDATA[scopes]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[function]]></category>
            <dc:creator><![CDATA[Catalina Hernandez]]></dc:creator>
            <pubDate>Fri, 20 Nov 2020 22:40:59 GMT</pubDate>
            <atom:updated>2020-11-30T17:16:57.942Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Scope?</h3><p>According to <a href="https://www.w3schools.com/js/js_scope.asp">W3 schools</a>, “Scope determines the accessibility (visibility) of variables”. In words I can understand, scope determines where a variable can be used. I like to think of scope as a microscope; the more zoomed in you are the more specific you get. In javascript, there is a global scope, function scope, and block scope.</p><h3>Global Scope</h3><p>Global scope is the broadest scope there is. Variables that are declared in the global scope can be accessed from anywhere in the code. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oKxA4MaiMU_OZkN2Lmf9eQ.png" /></figure><p>When I invoked the “youCanCallMe” function, it was able to use the “myFirstName” variable because it was in the global scope. The “myFirstName” variable was declared in the global scope meaning that it can be used anywhere in the code. If I had done:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q-eJaLMhti0YLoSYmY5dRg.png" /></figure><p>I would get a reference error saying “myNickname” is not defined. We are trying to invoke the function with a variable we can’t use. The return statement in the first example was able to use the “myFirstName” variable because a block of code can look outside of its scope to find a variable. The code block would start with block scope then function scope and then check global scope. The inverse is not true. We cannot go from global scope to block scope.</p><p>If we return to our microscope then it’s basically like the detail is too fine to see because we aren’t zoomed in. It’s also a messed up microscope because we aren’t allowed to zoom in if we start at the lowest lens.</p><h3>Function and Block Scope</h3><p>Variables that are declared inside of a function have function scope. If a variable is declared inside of a function and within the curly brackets then it has block scope.</p><blockquote><em>Want to read this story later? </em><a href="http://usejournal.com/app/signup?save_link=https://blog.usejournal.com/so-youre-learning-about-scope-6eb27db2037a"><em>Save it in Journal.</em></a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pmpGP3sQAKUdQuPTY-0fjg.png" /></figure><p>In the example above the “myFullName” variable has function scope. It was declared within our function and so it has function scope. When we invoke this we will see, “My name is Catalina but you can call me Cat”.</p><p>A better example of block and function scope is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_FKOSaJq-8HPXZGb3fqNdA.png" /></figure><p>The function above will find how many Friday the thirteenths there are in a given year. What’s really important is which variables have function scope and which have block scope. In this function, the “spookyDayCounter” variable has function scope but not block scope. It was declared in the function and is accessible throughout the entire function. The “day” variable has block scope because it was declared within the for loop’s code block and so it is only accessible within the for loop’s code block.</p><h3>How do variable declarations affect scope?</h3><p>Unfortunately, javascript is really anal so the variable keyword you use is also important. Of course, any variable declared outside of a function has global scope.</p><h3>let and const</h3><p>When we use let or const to declare variables, the variable can have either block scope or function scope depending on where the variable was declared. If the variable was declared in the global scope using let or const it will have global scope. As seen in our “fridayThe13ths” and “youCanCallMe” functions.</p><h3>var</h3><p>The var keyword isn’t really used anymore in the lords year (whenever you read this) but it’s still important to know for legacy code.</p><p>The var keyword can have function scope or global scope. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-eHtUiGBji9FtwHIBR60tA.png" /></figure><h3>What if we don’t use a variable declaration?</h3><p>According to <a href="https://www.w3schools.com/js/js_scope.asp">W3 Schools</a>, a variable declared without a keyword will automatically have global scope. However, I get reference errors when trying to declare a variable without a keyword. Either way, I wouldn’t recommend declaring a variable without a keyword.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O-o_lXWHpbkGdWzW45JIQw.png" /></figure><h3>TL;DR</h3><ul><li>Variables in the global scope can be accessed from anywhere in the code</li><li>Variables in the function can be accessed from anywhere within the function</li><li>Variables within the block scope can only be accessed in a code block (curly brackets {})</li><li>Any variable that isn’t declared in a function has global scope</li><li>The let and const declarations can have function or block scope</li><li>The var declaration has function scope.</li><li>If you don’t use a variable declaration keyword it will automatically have global scope or throw an error</li></ul><p>I hope this was helpful! Thank you for reading and have a good day/evening/night!</p><h4>More from <a href="http://usejournal.com/">Journal</a></h4><p>There are many Black creators doing incredible work in Tech. This collection of resources shines a light on some of us:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fusejournal.com%2Fapp%2Fembed%2Fjournal%3Aspace%3Aproject%2F5c84f6f0-715c-48b8-9387-9e74f33b7a7b&amp;display_name=Journal&amp;url=https%3A%2F%2Fusejournal.com%2Fapp%2Fspace%2Fjournal%3Aspace%3Aproject%2F5c84f6f0-715c-48b8-9387-9e74f33b7a7b&amp;image=https%3A%2F%2Fimperio.usejournal.com%2Fimage%2Fjournal%3Aspace%3Aproject%2F5c84f6f0-715c-48b8-9387-9e74f33b7a7b%2F1602668529%3Fclient%3Dfacebook&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=usejournal" width="900" height="710" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca84c63a615be91123438987244ec06c/href">https://medium.com/media/ca84c63a615be91123438987244ec06c/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6eb27db2037a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>