{"id":796,"date":"2020-09-03T20:00:32","date_gmt":"2020-09-03T20:00:32","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=796"},"modified":"2020-09-03T20:07:20","modified_gmt":"2020-09-03T20:07:20","slug":"what-is-closure-in-javascript","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/what-is-closure-in-javascript\/","title":{"rendered":"What is Closure in JavaScript"},"content":{"rendered":"\n<p>In this article I will explain closure &#8211; one of the important concepts in JavaScript. I will also cover the benefits of using closures.<\/p>\n\n\n\n<p><strong>What is a closure?<\/strong><\/p>\n\n\n\n<p>JavaScript is a function oriented programming language. Because of this we have lots of freedom in using functions. We can easily create a function,  pass it as an argument to another function (known as <a href=\"https:\/\/datatype.co.in\/blog\/what-is-callback-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"callbacks (opens in a new tab)\">callbacks<\/a>), define a function inside another function and so on. In JavaScript, the functions which are defined inside another function (i.e. the inner functions) are termed as <strong>closures<\/strong>. One important point to note here is <strong>closures have access to it&#8217;s parent functions scope even after the outer function has returned<\/strong>. <\/p>\n\n\n\n<p>Let&#8217;s understand this with an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function init() { \n  var name = 'Lokesh';\n  \/\/ this is a closure  \n  function displayName() {  \n    alert(name);  \n    \/\/ using variable declared in the parent function \n  } \n  displayName(); \n} \ninit(); <\/code><\/pre>\n\n\n\n<p>In the example above, <code><strong>displayName()<\/strong><\/code> function is a <strong>closure<\/strong>. As you see, the function has access to it&#8217;s parent scope variable <strong><code>name<\/code><\/strong>. So, when the parent function <strong><code>init()<\/code><\/strong> is called, it will display an alert containing the name Lokesh. Although I have shown just one closure in the example, it is absolutely valid to have multiple and nested closures in JavaScript. For example, we can define more functions inside <strong><code>init()<\/code><\/strong> or inside  <strong><code>displayName()<\/code><\/strong> too, which is already a closure. You can define as many closures you want.<\/p>\n\n\n\n<p>The main objective behind using closures is <strong>data hiding<\/strong> or<strong> data privacy<\/strong>. The inner functions (closures) are not exposed to outer world and are not directly accessible from outside scope. The only way to use a closure is by exposing it to it&#8217;s parent function. Only parent function can call a closure.<\/p>\n\n\n\n<p>Thank you for reading.<\/p>\n\n\n\n<p>You may also like: <a href=\"https:\/\/datatype.co.in\/blog\/what-is-callback-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"What is Callback in JavaScript (opens in a new tab)\">What is Callback in JavaScript<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I will explain closure &#8211; one of the important concepts in JavaScript. I will also cover the benefits of using closures. What is a closure? JavaScript is&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[241,242,236,238,100,237,239],"class_list":["post-796","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-benefits-of-using-closure","tag-closure-scope-in-javascript","tag-closures-in-javascript","tag-inner-function","tag-js-interview-question","tag-what-is-a-closure","tag-why-use-closure-in-javascript","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=796"}],"version-history":[{"count":25,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":821,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/796\/revisions\/821"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}