{"id":5417,"date":"2024-11-19T04:07:46","date_gmt":"2024-11-19T04:07:46","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=5417"},"modified":"2024-11-19T04:12:48","modified_gmt":"2024-11-19T04:12:48","slug":"how-javascript-closures-work","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/","title":{"rendered":"How JavaScript Closures Work"},"content":{"rendered":"\n<p>JavaScript closures are powerful feature rarely used by web developers. In fact, many of them often wonder what are JavaScript closures and how to use them. JavaScript closures help developers use variable scope in a powerful new way. However, it can be tricky to get past the initial learning curve. In this article, we will learn how JavaScript closures work.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#What_Are_JavaScript_Closures\" >What Are JavaScript Closures<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#How_JavaScript_Closures_Work\" >How JavaScript Closures Work<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#Scope\" >Scope<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#Global_Variables\" >Global Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#Nested_Functions\" >Nested Functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#Closures\" >Closures<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#When_to_Use_JavaScript_Closures\" >When to Use JavaScript Closures<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Are_JavaScript_Closures\"><\/span>What Are JavaScript Closures<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>First of all, let us understand the definition of JavaScript closures. A closure is a combination of a function along with its surrounding state information. In other words, it is a function that can access the scope of its parent function, even after the parent function has stopped execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_JavaScript_Closures_Work\"><\/span>How JavaScript Closures Work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before we learn more about closures, it is essential to understand a little bit about scope, global variables and nested functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Scope\"><\/span>Scope<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Scope is the boundary within which a variable is accessible. Generally, JavaScript variables have global or local scope. Global scope variables are accessible from any part of the JavaScript code. Local variables are accessible only within the function where they are defined.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a = 1;<br>function b(){<br> console.log(a*2); \/\/output is 2<br>}<br><br>b();<\/pre>\n\n\n\n<p>In the above example, the function b() is able to access the value of global variable.<\/p>\n\n\n\n<p>A function can access all variables that are defined in it as well as all global variables defined outside the function. If a function is defined inside another function, then such a function is called nested function, and the outer function is called parent function. A nested function can also access variables defined in its parent function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function out(){<br>  var a=1;<br>  function in(){<br>     console.log(a*2); \/\/ output is 2<br>   }<br>  in();<br>}<br><br>out();<\/pre>\n\n\n\n<p>In the above code, nested function in() is able to access variable a defined in parent function out().<\/p>\n\n\n\n<p>But a function cannot access variables that are defined in another function that is not a parent function. Similarly, a parent function cannot access variables defined in its nested function. The following code will not work properly.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function a(){<br>   var c=1;<br> }<br><br>function b(){<br>  console.log(c*2); \/\/output is undefined<br>}<br><br>b();<\/pre>\n\n\n\n<p>Sounds confusing? Sadly, it is so. If we are not careful about variable scope, your variables may end up getting modified without your knowledge and you may find it difficult to debug this problem.<\/p>\n\n\n\n<p>Suppose you want to create a simple counter function that keeps a count. And you want to make this accessible to all parts of your program. One of the simple ways is to use a global variable as shown below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Global_Variables\"><\/span>Global Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here is the code to implement a counter() function using global variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var count=1<br><br>function counter(){<br>  count+=1;<br>  console.log(count);<br>}<\/pre>\n\n\n\n<p>Let us call the above function twice hoping that the counter variable will be incremented in each call.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">counter(); \/\/ output is 2<br>counter(); \/\/ output is 3<\/pre>\n\n\n\n<p>The above code works fine but the problem is that any code on your page can modify the value of count variable. So it is not safe.<\/p>\n\n\n\n<p>Instead, if you define it as local variable, then the count variable will be initialized in every call. This defeats our purpose.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function counter(){<br>  var count=1;<br>  count+=1;<br>  console.log(count);<br>}<br><br>counter(); \/\/output is 2<br>counter(); \/\/output is 2<br>counter(); \/\/output is 2<\/pre>\n\n\n\n<p>To protect your variables from being modified unnecessarily, you can try using nested functions as shown below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Nested_Functions\"><\/span>Nested Functions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here is an example of in() function nested inside out() function. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function out(){<br>  var count=1;<br>  function in(){<br>     count+=1;<br>     console.log(count);<br>  }<br>  in();<br>}<br><br>out();<\/pre>\n\n\n\n<p>In the above code, in() function is able to access and modify count variable defined in its parent function out(). But every time, we can run in() function only by calling out() function. Every time we call out() function, count variable is reset to 1. It would have been nice if we could call in() from outside out() function. This is where closures come in.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Closures\"><\/span>Closures<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here is a simple example of using closures with self-invoking function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var add = (function () {<br>  var count = 1;<br>  return function () {count += 1; return count;}<br>})();<br><\/pre>\n\n\n\n<p>In the above code, the self-invoking function is run only once. It sets count to 1. Every time we call add() function, it will increment count variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add(); \/\/ count is 2<br>add(); \/\/ count is 3<br>add(); \/\/ count is 4<\/pre>\n\n\n\n<p>This provides two significant advantages. First of all, it protects count variable from external manipulation. Secondly, it preserves the value of count variable across multiple function calls.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"When_to_Use_JavaScript_Closures\"><\/span>When to Use JavaScript Closures<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are some interesting use cases for JavaScript closures<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Private variables<\/strong> &#8211; Closures allow you to protect your variables from being accessed or modified by another parts of your code. They allow you to correctly process the variable and only return the final value. <\/li>\n\n\n\n<li><strong>State Persistence<\/strong> &#8211; In our example of counters, we have seen how the count() function retains the value of count variable, across different function calls. So they are useful for event driven programming which require state persistence.<\/li>\n\n\n\n<li><strong>Modular code<\/strong> &#8211; Since closures define function scope, they allow web developers to build modular code blocks.<\/li>\n\n\n\n<li><strong>Callbacks<\/strong> &#8211; Closures are also useful for callbacks and asynchronous programming.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt what are JavaScript Closures and how they work. You can use them as per your requirement. They are unique and serve their purpose very well. They might seem intimidating at first but once you get past the initial learning curve, you&#8217;ll see that they are very handy.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-whether-string-contains-substring-in-javascript\/\">How to Check Whether String Contains Substring in JavaScript<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-remove-property-from-javascript-object\/\">How to Remove Property from JavaScript Object<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-see-foreign-keys-related-to-table-or-column\/\">How to See Foreign Keys Related to Table in MySQL<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often web developers want to learn how closures work in JavaScript. Here is how JavaScript closures work.<\/p>\n","protected":false},"author":1,"featured_media":5437,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[295],"tags":[332],"class_list":["post-5417","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-closures"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How JavaScript Closures Work - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often web developers want to learn how closures work in JavaScript. Here is how JavaScript closures work.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How JavaScript Closures Work - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often web developers want to learn how closures work in JavaScript. Here is how JavaScript closures work.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-19T04:07:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-19T04:12:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"301\" \/>\n\t<meta property=\"og:image:height\" content=\"201\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How JavaScript Closures Work\",\"datePublished\":\"2024-11-19T04:07:46+00:00\",\"dateModified\":\"2024-11-19T04:12:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/\"},\"wordCount\":823,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg?fit=301%2C201&ssl=1\",\"keywords\":[\"closures\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/\",\"name\":\"How JavaScript Closures Work - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg?fit=301%2C201&ssl=1\",\"datePublished\":\"2024-11-19T04:07:46+00:00\",\"dateModified\":\"2024-11-19T04:12:48+00:00\",\"author\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often web developers want to learn how closures work in JavaScript. Here is how JavaScript closures work.\",\"breadcrumb\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg?fit=301%2C201&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg?fit=301%2C201&ssl=1\",\"width\":301,\"height\":201,\"caption\":\"how javascript closures work\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ubiq.co\/tech-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How JavaScript Closures Work\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/\"],\"url\":\"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How JavaScript Closures Work - Ubiq BI","description":"Often web developers want to learn how closures work in JavaScript. Here is how JavaScript closures work.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/","og_locale":"en_US","og_type":"article","og_title":"How JavaScript Closures Work - Ubiq BI","og_description":"Often web developers want to learn how closures work in JavaScript. Here is how JavaScript closures work.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-11-19T04:07:46+00:00","article_modified_time":"2024-11-19T04:12:48+00:00","og_image":[{"width":301,"height":201,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How JavaScript Closures Work","datePublished":"2024-11-19T04:07:46+00:00","dateModified":"2024-11-19T04:12:48+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/"},"wordCount":823,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg?fit=301%2C201&ssl=1","keywords":["closures"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/","url":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/","name":"How JavaScript Closures Work - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg?fit=301%2C201&ssl=1","datePublished":"2024-11-19T04:07:46+00:00","dateModified":"2024-11-19T04:12:48+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often web developers want to learn how closures work in JavaScript. Here is how JavaScript closures work.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg?fit=301%2C201&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg?fit=301%2C201&ssl=1","width":301,"height":201,"caption":"how javascript closures work"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-javascript-closures-work\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How JavaScript Closures Work"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/11\/javascript-closures.jpg?fit=301%2C201&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1pn","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=5417"}],"version-history":[{"count":29,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5417\/revisions"}],"predecessor-version":[{"id":5448,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5417\/revisions\/5448"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/5437"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=5417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=5417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=5417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}