{"componentChunkName":"component---src-templates-blog-post-js","path":"/what-is-closures-in-javascript","result":{"data":{"markdownRemark":{"id":"cca980b8-bdd7-5605-9e65-5ae67841853e","html":"<!--StartFragment-->\n<p>Closures is an important topic when it comes to JS and it has become so popular that even other big programming languages are adopting the same</p>\n<p>To understand closure we first need to have a good knowledge of functions and lexical scoping because closure is dependent on that</p>\n<p>You might be using closures without even knowing about it so let's see what closures are why it is such a cool and amazing concept in JS</p>\n<p>To start I will explain about functions and higher-order functions so if you already know about it you can directly go to the closure part</p>\n<p>Let's start by revising what we know about functions</p>\n<p>We know how to call functions</p>\n<ol>\n<li>\n<p>Normally calling a function</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">hello</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\tconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>\"hi<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token function\">hello</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n</li>\n<li>\n<p>Calling with an object</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> obj <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n\tname<span class=\"token operator\">:</span><span class=\"token string\">\"Jett\"</span><span class=\"token punctuation\">,</span>\n\t<span class=\"token function\">speed</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\tconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Woos\"</span><span class=\"token punctuation\">)</span>\n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\nobj<span class=\"token punctuation\">.</span><span class=\"token function\">speed</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n</li>\n<li>\n<p>Using the call method</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">hello</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\tconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>\"hi<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token function\">hello</span><span class=\"token punctuation\">.</span><span class=\"token function\">call</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n</li>\n<li>\n<p>We can also use \"new\" operator</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> four <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Function</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"num\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"return num\"</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token function\">four</span><span class=\"token punctuation\">(</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span></code></pre></div>\n</li>\n</ol>\n<p>We know that whenever a function is called or invoked it creates an execution context and that execution context consists of the \"this\" keyword, the \"arguments\" keyword, and finally the variable environment</p>\n<blockquote>\n<p><strong><em>Functions are a special type of object which are called callable objects in JS</em></strong></p>\n</blockquote>\n<p>Why you ask, well we can back that claim up because if you imagine a box with the name of the function and some code and three properties - call, apply and bind</p>\n<p><figure class=\"gatsby-resp-image-figure\" style=\"\">\n    <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1024px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 68.359375%; position: relative; bottom: 0; left: 0; background-image: url('data:image/svg+xml,%3csvg%20xmlns=\\'http://www.w3.org/2000/svg\\'%20width=\\'400\\'%20height=\\'273\\'%20viewBox=\\'0%200%20400%20273\\'%20preserveAspectRatio=\\'none\\'%3e%3cpath%20d=\\'M0%20137v136h401V0H0v137M163%2015c-2%201-4%204-4%206l-3%205c-1%200-1%202%201%202l1%209-1%209-2%201%207%201c6%200%208-1%204-2-2-1-2-2-2-9v-9h5l6%201v8c1%2012%203%2014%2011%2010h3l5%201%205-1-1-1c-2%200-2-2-2-10%200-12%200-13-6-11-4%201-5%203-2%203l1%208c0%208-1%2010-6%208-2-1-2-1-2-11v-9h-3l-10%201h-4v-5c1-6%201-6%205-3%204%202%206%200%204-3-1-3-6-2-10%201m103-1c-14%207-11%2041%203%2041%203%200%202-2-1-3-4-3-5-8-5-18s2-16%206-17l2-3h-5m10%200v2c10%200%2011%2032%201%2036-3%202-3%203%201%203%2012-3%2016-29%206-38-3-2-7-4-8-3M40%2026c-4%203-4%208%201%2011%208%205%208%205%207%207-2%203-7%203-8-1s-3-3-4%201c0%203%200%203%203%204%206%203%2015-1%2015-6%200-3-1-5-6-8-6-3-7-6-1-7%202%200%203%200%203%203%201%203%203%202%204-1%201-5-9-7-14-3m24-1c-7%203-8%2014-2%2020%206%207%2019%203%2021-6%201-11-8-18-19-14m25%200c-3%201-4%203-1%203l1%209-1%209-2%201%206%201%206-1-1-1c-3%200-3-17%200-17%203-1%206%200%206%202%202%205%201%2014%200%2015-4%201-2%202%204%202s6%200%205-1c-2-1-2-3-2-10%200-8%200-8%203-9%204-1%205%201%205%2010%200%206%200%208-2%209-1%201-1%201%206%201%206%200%206%200%204-2-2-1-2-2-2-10%200-11-3-14-12-10h-5c-2-2-5-2-9%200h-3c0-2-1-2-6-1m47%201c-9%204-8%2019%201%2022%206%202%2015%200%2015-4l-2-1c-7%203-12%201-14-4v-3h16v-4c-1-7-9-10-16-6m68-1c-3%201-4%203-1%203l1%209-1%209-2%201%206%201c6%200%208-1%205-2-2-1-2-3-2-9v-8l3-1c5-1%206%201%206%2010l-1%208-1%201%206%201c6%200%206%200%204-2-2-1-2-2-2-10%200-12-2-14-11-11h-10m35%200c-10%204-10%2020%200%2023%205%202%2012%200%2013-3%200-2%200-2-2-2-7%204-12%201-12-7l2-8c4-3%205-3%205%201s5%205%206%201c2-4-7-7-12-5M67%2029c-4%206-1%2017%204%2017%203%200%205-4%205-9%200-6-2-10-5-10l-4%202m-47%2071c-10%201-10%201-11%2011-2%2018-1%2026%202%2029l2%203h126l2-2c3-4%203-35%200-39l-2-3H84l-64%201m0%2057c-10%201-10%201-11%2011-2%2018-1%2026%202%2029l2%203h126l2-2c3-4%203-35%200-39l-2-3H84l-64%201m237%200c-11%201-12%201-12%2024%200%2014%200%2013%202%2016l2%203h126c4-1%205-5%205-23%200-15-1-17-2-19-2-2-3-2-58-2l-63%201m-21%2063c-19%201-18%200-18%2022%200%2023-1%2022%2022%2022%2026%200%2026%200%2025-24-1-20-2-21-11-16l-7%203-1%201c-3%200-2-3%200-3%203%200%2011-5%208-5h-18m72%200l-14%201c-16%200-15%200-15%2022s-1%2021%2022%2021c25%200%2025%200%2024-24%200-18-1-20-9-20-3%200-3%200-3%204l-1%206v-5c0-5%200-6-4-5m56%200l-6%201c2%200%203%203%201%203l-3-1c-2-3-11-3-14%200-2%202-2%203-2%2019v18l3%202c2%202%203%202%2021%202%2023-1%2022%201%2022-22%200-17%200-17-2-20l-7-2h-13\\'%20fill=\\'%23d3d3d3\\'%20fill-rule=\\'evenodd\\'/%3e%3c/svg%3e'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Function\"\n        title=\"Inside of the function\"\n        src=\"/static/2eb46b2f5201643269a2f7048147ac2d/2bef9/functions-are-objects.png\"\n        srcset=\"/static/2eb46b2f5201643269a2f7048147ac2d/6f3f2/functions-are-objects.png 256w,\n/static/2eb46b2f5201643269a2f7048147ac2d/01e7c/functions-are-objects.png 512w,\n/static/2eb46b2f5201643269a2f7048147ac2d/2bef9/functions-are-objects.png 1024w,\n/static/2eb46b2f5201643269a2f7048147ac2d/f53a0/functions-are-objects.png 1062w\"\n        sizes=\"(max-width: 1024px) 100vw, 1024px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span>\n    <figcaption class=\"gatsby-resp-image-figcaption\">Inside of the function</figcaption>\n  </figure></p>\n<p>If you now go to your text editor and type a function and then see by the intelligence you will see something like this</p>\n<p><figure class=\"gatsby-resp-image-figure\" style=\"\">\n    <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 681px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 66.40625%; position: relative; bottom: 0; left: 0; background-image: url('data:image/svg+xml,%3csvg%20xmlns=\\'http://www.w3.org/2000/svg\\'%20width=\\'400\\'%20height=\\'265\\'%20viewBox=\\'0%200%20400%20265\\'%20preserveAspectRatio=\\'none\\'%3e%3cpath%20d=\\'M0%20133v132h401V0H0v133m0%200\\'%20fill=\\'%23d3d3d3\\'%20fill-rule=\\'evenodd\\'/%3e%3c/svg%3e'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Functions are objects\"\n        title=\"Functions are objects\"\n        src=\"/static/4524922295a329a4f03fac8757ee96aa/8ce52/function.png\"\n        srcset=\"/static/4524922295a329a4f03fac8757ee96aa/6f3f2/function.png 256w,\n/static/4524922295a329a4f03fac8757ee96aa/01e7c/function.png 512w,\n/static/4524922295a329a4f03fac8757ee96aa/8ce52/function.png 681w\"\n        sizes=\"(max-width: 681px) 100vw, 681px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span>\n    <figcaption class=\"gatsby-resp-image-figcaption\">Functions are objects</figcaption>\n  </figure></p>\n<p>It will give you the options or the methods that you can use with the function itself so that means it behaves like an object</p>\n<p>Now how does this concern us, well if functions are objects then we can store them around like objects, move them and pass them to another function</p>\n<blockquote>\n<p><strong><em>Functions are first-class citizens in JS</em></strong></p>\n</blockquote>\n<p>What does that mean well three things back that claim</p>\n<p>1.We can store a function into a variable</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> <span class=\"token function-variable function\">greet</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><span class=\"token punctuation\">}</span></code></pre></div>\n<p>2.We can pass a function as an argument to another function</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">greet</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">fn</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token function\">fn</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token function\">greet</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"hello\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<ol start=\"3\">\n<li>We can also return a function</li>\n</ol>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">greet</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span> console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Hi there\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">greet</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>So basically what I am trying to say is function can do pretty much everything other data types can do in JS</p>\n<h3>Higher-Order Functions (HOF)</h3>\n<p>A HOF is a function that can return a function or a function that can take function for an arguments as you have seen from the above examples</p>\n<p>And we also know about the lexical scoping in JS and if you don't then you can refer to <a href=\"https://developersdomain.netlify.app/execution-context-lexical-environmenet-scope-chain\"></a><a href=\"https://www.aviatecoders.com/execution-context-lexical-environmenet-scope-chain\">this</a> article but in short, let me tell you</p>\n<p>Lexical scoping or static scoping means the available variable data and functions are dependent on where the function is defined</p>\n<h2>Closures</h2>\n<p>Now finally to explain Closures let's start with an example</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">hello</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"greetings\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">var</span> a <span class=\"token operator\">=</span> <span class=\"token string\">\"hi\"</span>\n  <span class=\"token keyword\">return</span> <span class=\"token keyword\">function</span> <span class=\"token function\">b</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">var</span> b <span class=\"token operator\">=</span> <span class=\"token string\">\"hi there\"</span>\n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">function</span> <span class=\"token function\">c</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">var</span> c <span class=\"token operator\">=</span> <span class=\"token string\">\"hello there\"</span>\n      <span class=\"token keyword\">return</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>a<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>b<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>c<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">hello</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// hi hi there hello there</span></code></pre></div>\n<p>Now what has happened here, well I wrote the function hello and then declared a variable and returned another function which indeed returns another function, and finally, I accessed all the variables at the end and returned it</p>\n<p>Well as you guys know that when function hello is called it is pushed onto the stack and after it returns another function it gets popped off the stack (and if a function gets popped off the stack then their variable environment gets destroyed) then function b comes onto the stack return a function and again pops off the stack</p>\n<p>Finally, function c comes and return all the variables that were defined while in function hello and b</p>\n<p>But didn't I just said that those functions were popped off the stack and but then too we somehow have access to their variable</p>\n<p>And this concept of accessing the variables even if the function is popped off the stack which returning a function is called closure</p>\n<p>What happens is the first time when the JS engine parses the code it sees that the variables are mentioned even inside the function which is being returned and so because of that it just stores the variable into a contained called closure and when the function is being called then when the JS engine gets its value from the closure box and puts it in and runs it</p>\n<p>This is one of the coolest parts of JS and it can have many uses such they can be memory efficient and use encapsulation</p>\n<h3>Memory efficient</h3>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">work</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">index</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">const</span> arr <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Array</span><span class=\"token punctuation\">(</span><span class=\"token number\">7000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">fill</span><span class=\"token punctuation\">(</span><span class=\"token string\">\":)\"</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// create new array with 7000 elements of :)</span>\n\tconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"created\"</span><span class=\"token punctuation\">)</span>\n\t<span class=\"token keyword\">return</span> arr<span class=\"token punctuation\">[</span>index<span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token function\">work</span><span class=\"token punctuation\">(</span><span class=\"token number\">55</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// created</span>\n<span class=\"token function\">work</span><span class=\"token punctuation\">(</span><span class=\"token number\">99</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">//created</span>\n<span class=\"token function\">work</span><span class=\"token punctuation\">(</span><span class=\"token number\">800</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// created :)</span></code></pre></div>\n<p>Here we have created a function that creates a new Array of 7000 elements with \":)\" and finally return it will give index well this process takes time if we call it again and again and is not very memory efficient</p>\n<p>What we can do instead is</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">work</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">index</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">const</span> arr <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Array</span><span class=\"token punctuation\">(</span><span class=\"token number\">7000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">fill</span><span class=\"token punctuation\">(</span><span class=\"token string\">\":)\"</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// create new array with 7000 elements of :)</span>\n\tconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"created\"</span><span class=\"token punctuation\">)</span>\n\t<span class=\"token keyword\">return</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t\t<span class=\"token keyword\">return</span> arr<span class=\"token punctuation\">[</span>index<span class=\"token punctuation\">]</span>\n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">const</span> workHard <span class=\"token operator\">=</span> <span class=\"token function\">work</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token function\">workHard</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token function\">workHard</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token function\">workHard</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> \n<span class=\"token comment\">// created :)</span></code></pre></div>\n<p>Here even if we call the workHard function many times it will give only one output and that saves a lot of time and memory instead of creating an array every single time on invocation</p>\n<h3>Encapsulation</h3>\n<p>Also with the help of closure, we can use encapsulation which means is a process of bundling the data (i.e. variables) with the functions acting on that data as you have seen in the previous examples</p>\n<p>Was the article helpful? Do you have any doubts? Any topic you would like us to cover?</p>\n<p>Reach out to us on <a href=\"https://twitter.com/kartikey_yadav7\"></a><a href=\"https://twitter.com/aviatecoders\">Twitter</a> and <a href=\"https://instagram.com/aviatecoders\">Instagram</a> where we try to provide more value in threads and carousal formats</p>\n<p>Thank You for your time</p>\n<p>Keep learning, Keep coding :)</p>\n<!--EndFragment-->","excerpt":"Closures is an important topic when it comes to JS and it has become so popular that even other big programming languages are adopting the same To…","frontmatter":{"date":"April 10, 2021","slug":"/what-is-closures-in-javascript","title":"Closures | Javascript","description":"Learn about the famous closures and understand Javascript on an even deeper level here with examples and advantages of closures","featuredImage":{"childImageSharp":{"fluid":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAACw0lEQVQ4y12TS08TYRiFZ2NiSBDojXamc/3m3mmnV2ixgNiURAIYROsFiUJMiDFha9S9C3+De5esXbt2Z+IPcGli/AfH882UjYuTvp32e75z3vcdxaklkLKrCXy9Dd/sIKDC+WcmowNXa2X/c7UmLNZbVhup3UXg9NH01pGGG2hHt6HY1QakHMpajeHX8tqef88hLURWD149hVCbSN01bIsun3XRYN30h0iDEdqEKlYl5MFcOjW2UsTShQRnzucXso7tHgShiZ7SYYo6n/kSSmBMlw1vCMWsBLAJEtUQzmpEJYQnMFfzVvyvRAzgsjXboocRY4f1FtpODx13gLboQ9ErPgEBgRF/jPgwQWTKfuauzEqURZexZdzAaGfxu/EYO9EQE7+PSbSOaTzClJ+KQaCjBlheCPHutYvfPwz8+Wnh25WTOY+dQTYgdcWDRtWLQXZJveCjxtrgMF0OzeJlltqSQA9CC3HrZoSPbwXwV8fXL2Vcfa5id3MXxwenOHl4junmAY73T3Bv5wh7kweY3T/F7PAUG527sHiBS6BgIkUrClhVFws3fHx6L/Drew1vzhbx4bKA6XgHu9tH2eEnRy9xcXaJ57NXeHZ8jkeEvXh6genWIV37WTvkdihq0YZaclBeEhh3PTzedzDbEziYMF5JwNYaHFAIsxzyoMd+RtnUZezrdbt2J3uuFJYNFJdNQm2UFh0IfQDfHqK46KJe9qGXPfaJQA4tE7dCrprDtfIJklMOuWayzoBr9RAJIxdXTFQJddhPWwuglh1oqz5MOozNJhKjiabJSWtJBpXOpCt37kxcAwOrhZTQbk1gZUmnDJQKbEPVR6RFGOoRRtSwHmNDj3FHtBmxMY+eL36i5ZFlrQRuHzpvbshDRoCeHmLNiLCu+RioHtyyy3ZQjF4peRhzRzt8S3T5QlTzV3NktOCpOfAfxcKHnEPrRVAAAAAASUVORK5CYII=","aspectRatio":1.502145922746781,"src":"/static/ada7c0c108b988d6b962e9f5f539426b/03979/closures-cover.png","srcSet":"/static/ada7c0c108b988d6b962e9f5f539426b/6cdeb/closures-cover.png 350w,\n/static/ada7c0c108b988d6b962e9f5f539426b/b1dd8/closures-cover.png 700w,\n/static/ada7c0c108b988d6b962e9f5f539426b/03979/closures-cover.png 800w,\n/static/ada7c0c108b988d6b962e9f5f539426b/1783d/closures-cover.png 1050w,\n/static/ada7c0c108b988d6b962e9f5f539426b/942ca/closures-cover.png 1400w,\n/static/ada7c0c108b988d6b962e9f5f539426b/b5274/closures-cover.png 3000w","sizes":"(max-width: 800px) 100vw, 800px","maxHeight":533,"maxWidth":800},"sizes":{"src":"/static/ada7c0c108b988d6b962e9f5f539426b/ee604/closures-cover.png"}}}}}},"pageContext":{"id":"cca980b8-bdd7-5605-9e65-5ae67841853e","previous":{"id":"d9d55ecf-df73-5be1-af1f-ddcb87a5cfe4","frontmatter":{"slug":"/what-is-call-bind-apply-in-javascript","template":"blog-post","title":"Call, Bind and Apply In Javascript"}},"next":{"id":"909fe9fc-9e82-5ca0-bf0a-ad4a82feda65","frontmatter":{"slug":"/creating-custom-buttons-in-react-native","template":"blog-post","title":"Creating Custom Buttons in React Native"}}}},"staticQueryHashes":["228695001","3868140423"]}