JavaScript: What do you mean by the "Scope"?
Tags: javascript, es6, beginners
Published: 2019-09-05 08:08:12

<!-- wp:paragraph -->
<p>Like many others programmers when start with javascript in a self-taught way we find a very recurrent word around us <strong>"scope"</strong>, and as expected the first thing we do is go to the source of all the answers <span style="text-decoration:underline;">The great Google</span> of course, we search what is <strong>Scope</strong> or <strong>variable scope</strong>?.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But this is kind of confusing and not very clear; so, let me explain what is the <strong>"Scope"</strong>  and how this works inside JavaScript.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>What is the scope of a variable?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Scoping is determining where variables, functions, and objects are accessible in your code during runtime. This means the scope of a variable(where it can be accessed) is controlled by the location of the variable declaration.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are two scopes in JavaScript, the <strong>Global scope </strong>and <strong>Local scope</strong>, when we start programing in JavaScript there is a moment when <em>"referenceError:" </em>starts to pop up and the main reason of this is because of the scope, when you are debugging, if you can understand how the global and local scope works you will be more efficient when debugging and you'll be able to handle most of the <span style="text-decoration:underline;">reference and declaration errors.</span></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4><em><strong>   The global scope</strong>:</em></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>There is only one Global scope throughout a JavaScript document. A variable is in the Global scope if it’s defined outside of a function. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":80,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://pokymovieshome.files.wordpress.com/2019/09/screenshot_20190904_094028.jpg?w=1024" alt="" class="wp-image-80"/><figcaption>The variables in global scope are always outside functions declarations.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>As you see in the above code, the variable "glovalV" is declared in the global scope because is not inside a function, you can go and test for yourself declaring and logging variables inside and outside a function and check what errors you find.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>"<em>You can also access and alter any variable declared in a global scope from any other scope."</em></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>   <strong><em>The local scope:</em></strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Variables declared within a function are in the local scope. Local scope is also called function scope because local scope is created by functions in Javascript. Variables in the local scope are only accessible within the function in which they are defined, i.e they are bound to their respective functions each having different scopes. This allows us to create variables that have the same name and can be used in different functions.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":91,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://pokymovieshome.files.wordpress.com/2019/09/screenshot_20190904_101421.jpg?w=1024" alt="" class="wp-image-91"/><figcaption>local scope within functions </figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>In JavaScript, variables with the same name can be specified at multiple layers of nested scope. In such case local variables gain priority over global variables. If you declare a local variable and a global variable with the same name, the local variable will take precedence when you use it inside a function. This type of behavior is called <strong><span style="text-decoration:underline;">shadowing</span></strong>. Simply put, the inner variable shadows the outer. This is how the Javascript interpreter finds a particular variable; it searches for the variable at the innermost scope being executed at the time, and continues until the first match is found, even if there are other variables with the same name in the outer scope.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":94,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://pokymovieshome.files.wordpress.com/2019/09/screenshot_20190904_103258.jpg?w=1024" alt="" class="wp-image-94"/><figcaption>Variables of the same name inside functions.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2><strong>In conclusion</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Understanding the scope can be a little confuse at the beginning but what I recommend is playing with the code and console.log the content of variables and function calls, I hope this was a little help in the understanding of JavaScript, in my next post I'll talk about the block scope related with the reserved word "<strong>let</strong>" in the ES6 or ECMAScript2015 JavaScript specifications.</p>
<!-- /wp:paragraph -->