{"id":104,"date":"2026-05-02T07:21:37","date_gmt":"2026-05-02T07:21:37","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=104"},"modified":"2026-05-02T07:21:40","modified_gmt":"2026-05-02T07:21:40","slug":"scope-and-closures","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=scope-and-closures","title":{"rendered":"Scope and Closures"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Scope and closures are fundamental concepts in JavaScript that control how variables are accessed and how functions interact with their surrounding environment. Understanding these concepts is essential for writing clean, efficient, and bug free code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is Scope<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Scope determines where variables can be accessed in a program. It defines the visibility and lifetime of variables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Types of Scope<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Global Scope<br>Variables declared outside any function or block are in the global scope and can be accessed anywhere in the code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function Scope<br>Variables declared inside a function are only accessible within that function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Block Scope<br>Variables declared using let and const inside a block such as loops or conditionals are limited to that block.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Scope<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>let globalVar = \"I am global\";<br><br>function testScope() {<br>  let functionVar = \"I am inside function\";<br>  <br>  if (true) {<br>    let blockVar = \"I am inside block\";<br>    console.log(globalVar);<br>    console.log(functionVar);<br>    console.log(blockVar);<br>  }<br>  <br>  \/\/ console.log(blockVar); This will cause an error<br>}<br><br>testScope();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">What is Closure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A closure is created when a function remembers variables from its outer scope even after the outer function has finished executing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Closures allow functions to access and preserve data in a controlled way.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Closure<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function outerFunction() {<br>  let count = 0;<br>  <br>  return function innerFunction() {<br>    count++;<br>    console.log(count);<br>  };<br>}<br><br>const counter = outerFunction();<br><br>counter();<br>counter();<br>counter();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How Closures Work<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When the outer function runs, it creates a local variable. The inner function keeps a reference to that variable even after the outer function has completed. This allows the inner function to use and update the variable later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits of Closures<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Data privacy and encapsulation<br>Helps create reusable functions<br>Useful in event handlers and callbacks<br>Maintains state in asynchronous programming<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Real World Use Case<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Closures are commonly used in counters, timers, and maintaining state in applications without exposing variables globally.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Scope and closures are powerful features of JavaScript that help manage variable access and maintain data. Mastering them improves code structure, readability, and performance.<\/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\">Intermediate JavaScript > Functions > Scope and Closures<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777706450078\"><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-1777706449847\"><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-104","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>Scope and Closures - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript scope and closures with examples to control variables, manage data, and build efficient interactive applications\" \/>\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=scope-and-closures\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scope and Closures - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript scope and closures with examples to control variables, manage data, and build efficient interactive applications\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=scope-and-closures\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-02T07:21:40+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=scope-and-closures\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=scope-and-closures\",\"name\":\"Scope and Closures - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-02T07:21:37+00:00\",\"dateModified\":\"2026-05-02T07:21:40+00:00\",\"description\":\"Learn JavaScript scope and closures with examples to control variables, manage data, and build efficient interactive applications\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=scope-and-closures#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=scope-and-closures\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=scope-and-closures#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate JavaScript > Functions > Scope and 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":"Scope and Closures - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript scope and closures with examples to control variables, manage data, and build efficient interactive applications","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=scope-and-closures","og_locale":"en_US","og_type":"article","og_title":"Scope and Closures - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript scope and closures with examples to control variables, manage data, and build efficient interactive applications","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=scope-and-closures","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-02T07:21:40+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=scope-and-closures","url":"https:\/\/gigz.pk\/javascript\/?lesson=scope-and-closures","name":"Scope and Closures - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-02T07:21:37+00:00","dateModified":"2026-05-02T07:21:40+00:00","description":"Learn JavaScript scope and closures with examples to control variables, manage data, and build efficient interactive applications","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=scope-and-closures#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=scope-and-closures"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=scope-and-closures#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Intermediate JavaScript > Functions > Scope and 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\/104","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=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}