{"id":170,"date":"2026-05-06T16:26:05","date_gmt":"2026-05-06T16:26:05","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=170"},"modified":"2026-05-06T16:26:06","modified_gmt":"2026-05-06T16:26:06","slug":"closures","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=closures","title":{"rendered":"Closures"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<br>Closures are one of the most important and powerful concepts in JavaScript. They allow a function to access variables from another function even after that outer function has finished executing. This helps in managing data, creating private variables, and writing more efficient code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is a Closure<br>A closure is created when a function remembers and can access its lexical scope even when it is executed outside that scope. In simple terms, a closure gives you access to an outer function\u2019s variables from an inner function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How Closures Work<br>When a function is defined inside another function, the inner function has access to the outer function\u2019s variables. Even if the outer function finishes execution, the inner function keeps a reference to those variables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function outerFunction() {<br>  let count = 0;<br><br>  function innerFunction() {<br>    count++;<br>    console.log(count);<br>  }<br><br>  return innerFunction;<br>}<br><br>const counter = outerFunction();<br><br>counter();<br>counter();<br>counter();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the innerFunction remembers the variable count even after outerFunction has finished executing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Key Concepts of Closures<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lexical Scope<br>Closures are based on lexical scope, meaning a function can access variables defined in its outer scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Data Persistence<br>Variables inside a closure are preserved in memory and can be reused.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Encapsulation<br>Closures allow you to create private variables that cannot be accessed directly from outside.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Practical Uses of Closures<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Data Privacy<br>Closures help protect data by restricting direct access to variables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function Factories<br>You can create multiple functions with customized behavior.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Event Handlers<br>Closures are often used in callbacks and event listeners.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Memoization<br>Closures help store results of expensive function calls to improve performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example of Data Privacy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function createBankAccount() {<br>  let balance = 0;<br><br>  return {<br>    deposit: function(amount) {<br>      balance += amount;<br>      console.log(\"Balance:\", balance);<br>    },<br>    withdraw: function(amount) {<br>      balance -= amount;<br>      console.log(\"Balance:\", balance);<br>    }<br>  };<br>}<br><br>const account = createBankAccount();<br>account.deposit(100);<br>account.withdraw(50);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the balance variable is private and can only be accessed through the returned functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Advantages of Closures<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Helps create private variables<br>Improves code organization<br>Supports functional programming<br>Reduces global variable usage<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common Mistakes<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using closures inside loops without proper scope handling<br>Holding unnecessary data in memory which may cause memory leaks<br>Overusing closures can make code harder to understand<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<br>Closures are a core concept in JavaScript that enable powerful programming techniques like data encapsulation and function reuse. Understanding closures will help you write cleaner, more efficient, and professional code.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Professional JavaScript > Advanced Concepts > Closures<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778084723477\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778084723213\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-170","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Closures - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript closures with simple examples Understand scope data privacy and function behavior to write better code\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=closures\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Closures - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript closures with simple examples Understand scope data privacy and function behavior to write better code\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=closures\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-06T16:26:06+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=closures\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=closures\",\"name\":\"Closures - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-06T16:26:05+00:00\",\"dateModified\":\"2026-05-06T16:26:06+00:00\",\"description\":\"Learn JavaScript closures with simple examples Understand scope data privacy and function behavior to write better code\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=closures#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=closures\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=closures#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Professional JavaScript > Advanced Concepts > Closures\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Closures - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript closures with simple examples Understand scope data privacy and function behavior to write better code","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:\/\/gigz.pk\/javascript\/?lesson=closures","og_locale":"en_US","og_type":"article","og_title":"Closures - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript closures with simple examples Understand scope data privacy and function behavior to write better code","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=closures","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-06T16:26:06+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=closures","url":"https:\/\/gigz.pk\/javascript\/?lesson=closures","name":"Closures - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-06T16:26:05+00:00","dateModified":"2026-05-06T16:26:06+00:00","description":"Learn JavaScript closures with simple examples Understand scope data privacy and function behavior to write better code","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=closures#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=closures"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=closures#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Professional JavaScript > Advanced Concepts > Closures"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}