{"componentChunkName":"component---src-templates-blog-post-js","path":"/JavaScript/javascript-concept-4---scope-and-closure/","webpackCompilationHash":"f92dbf25810602c1889e","result":{"data":{"site":{"siteMetadata":{"title":"Web Devlog","author":"RossenaHuh","siteUrl":"https://github.com/rossenahuh","comment":{"disqusShortName":"RossenaHuh","utterances":"rossenahuh/my-dev-blog.git"},"sponsor":{"buyMeACoffeeId":"rossena"}}},"markdownRemark":{"id":"938a03b2-3dbd-5137-b3a2-abe3643d4a28","excerpt":"1. What is Scope?A scope in JavaScript defines what variables are accessible. There are two kinds of scope - global scope and local scopeGlobal scope (Not advised)When a varible is declared outside all functions or curly brackets.\nThere’s a chance of naming collisions if two or…","html":"<h2 id=\"1-what-is-scope\"><a href=\"#1-what-is-scope\" aria-label=\"1 what is scope permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. What is Scope?</h2>\n<p>A scope in JavaScript defines what variables are accessible. There are two kinds of scope - <strong>global scope</strong> and <strong>local scope</strong></p>\n<br>\n<p><strong>Global scope (Not advised)</strong></p>\n<p>When a varible is declared outside all functions or curly brackets.\nThere’s a chance of naming collisions if two or more variables are declared with the same name using let or const.</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Declaring variables with the same name using let(const) in global scope</span>\n\n<span class=\"token keyword\">let</span> greeting <span class=\"token operator\">=</span> <span class=\"token string\">'hi'</span>\n<span class=\"token keyword\">let</span> greeting <span class=\"token operator\">=</span> <span class=\"token string\">'hello'</span> <span class=\"token comment\">// Error : Already been declared</span></code></pre></div>\n<p>Though it’s possible to do so when you declare variables with var, it is still not advised because this will make the code hard to debug.</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Don't do this</span>\n<span class=\"token comment\">// It will mess up the code, hard to debug</span>\n\n<span class=\"token keyword\">var</span> greeting <span class=\"token operator\">=</span> <span class=\"token string\">'hi'</span>\n<span class=\"token keyword\">var</span> greeting <span class=\"token operator\">=</span> <span class=\"token string\">'hello'</span> <span class=\"token comment\">// doesn't throw error</span>\n\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>greeting<span class=\"token punctuation\">)</span> <span class=\"token comment\">// 'hello'</span></code></pre></div>\n<br>\n<p><strong>Local Scope</strong></p>\n<p>When variables are accessible only in a specific part of the code\nThere are two kinds of local scope: <strong>function scope</strong> and <strong>block scope</strong>.</p>\n<h2 id=\"2-what-does-it-mean-that-javascript-uses-lexical-scoping\"><a href=\"#2-what-does-it-mean-that-javascript-uses-lexical-scoping\" aria-label=\"2 what does it mean that javascript uses lexical scoping permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. What does it mean that JavaScript uses lexical scoping?</h2>\n<p>Inner variable is not accessible from outside but anything that is defined outside is automatically available inside the function. This behavior is called <strong>lexical scoping</strong>.</p>\n<h2 id=\"3-explain-some-examples-of-closure\"><a href=\"#3-explain-some-examples-of-closure\" aria-label=\"3 explain some examples of closure permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Explain some examples of closure</h2>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// The simplest example</span>\n\n<span class=\"token keyword\">let</span> passed <span class=\"token operator\">=</span> <span class=\"token number\">3</span>\n\n<span class=\"token keyword\">let</span> <span class=\"token function-variable function\">addTo</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">let</span> inner <span class=\"token operator\">=</span> <span class=\"token number\">2</span>\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>inner<span class=\"token punctuation\">)</span>\n<span class=\"gatsby-highlight-code-line\">  <span class=\"token keyword\">return</span> passed <span class=\"token operator\">+</span> inner <span class=\"token comment\">// using a variable outside function</span></span><span class=\"token punctuation\">}</span>\n\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">addTo</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 5</span></code></pre></div>\n<p>Any functions where you are using variables outside function are <strong>closures</strong>.</p>\n<p>In order to execute addTo function it will look through the varibles it needs. Since <em>passed</em> is not defined inside the function, it will look at the scope outside function. It would keep going up until it founds the variable.</p>\n<p>Let’s look at the another example.</p>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">let</span> <span class=\"token function-variable function\">addTo</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">passed</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">let</span> <span class=\"token function-variable function\">add</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">inner</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> passed <span class=\"token operator\">+</span> inner\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">return</span> add\n<span class=\"token punctuation\">}</span>\n\n<span class=\"gatsby-highlight-code-line\"><span class=\"token keyword\">let</span> addToThree <span class=\"token operator\">=</span> <span class=\"token function\">addTo</span><span class=\"token punctuation\">(</span><span class=\"token number\">3</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// preserves value inside</span></span>console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">addToThree</span><span class=\"token punctuation\">(</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 7</span></code></pre></div>\n<p>addTo(3) function preserves 3 value inside the function as closure. This means I can create unlimited number of functions using closure. It keeps the varible it needs to execute.</p>\n<br>\n<blockquote>\n<p>Conclusion : Closure is a <strong>function with preserved data</strong></p>\n</blockquote>\n<br>\n<br>\n<p><em>References</em></p>\n<p><a href=\"https://www.youtube.com/watch?v=71AtaJpJHw0\">https://www.youtube.com/watch?v=71AtaJpJHw0</a>\n<a href=\"https://css-tricks.com/javascript-scope-closures/\">https://css-tricks.com/javascript-scope-closures/</a>\n<a href=\"https://2ality.com/2015/02/es6-scoping.html\">https://2ality.com/2015/02/es6-scoping.html</a></p>\n<br>\n<br>","frontmatter":{"title":"JavaScript Concept 4 - Scope and Closure","date":"July 04, 2019"}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"/JavaScript/javascript-concept-4---scope-and-closure/","previous":{"fields":{"slug":"/JavaScript/javascript-concept-3---var-let-const/"},"frontmatter":{"title":"JavaScript Concept 3 - var let const","category":"JavaScript"}},"next":{"fields":{"slug":"/JavaScript/javascript-concept-5---statement(definition)-and-expression/"},"frontmatter":{"title":"JavaScript Concept 5 - Statement(Definition) and Expression","category":"JavaScript"}}}}}