{"id":7234,"date":"2021-07-31T12:13:29","date_gmt":"2021-07-31T12:13:29","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=7234"},"modified":"2022-04-13T16:30:47","modified_gmt":"2022-04-13T16:30:47","slug":"how-does-javascript-scope-work","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/","title":{"rendered":"How does JavaScript scope work?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Variable Scope<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">JavaScript has two scopes: &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Global<\/strong><\/li><li><strong>Local<\/strong><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Global Scope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A variable that is declared outside a function definition is a global variable, and its value is accessible and modifiable throughout your program. In a web browser, global variables are deleted when you close the browser window (or tab) but remain available to new pages loaded into the same window.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/c68b12b5f3a0b23aa8e7eca7249fae9b.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Local Scope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A variable that is declared inside a function definition is local. It is created and destroyed every time the function is executed, and it cannot be accessed by any code outside the function. Inside a function, if a variable has not been declared with var it is created as a global variable.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/b5fc9aafd382023f3f8d73566a8cc803.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Local Scope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If there is a function inside a function the inner function can access outer function\u201fs variables but the outer function can not access inner function\u201fs variables. Function arguments (parameters) work as local variables inside functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Block Scope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Variables declared with var do not have block scope.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/bc60fd19190634293c62fa5b80a548f5.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Block Scope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Identifiers declared with let and const do have block scope.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/dfd4e1170e7409581f15e693eb660e10.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Variable Hoisting<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hoisting is JavaScript&#8217;s default behavior of moving declaration to the top of the function, if defined in a function, or the top of the global context, if outside a function.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/9ef97bc4c430b9dad808185ee64df7c8.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/16f6be6d8143fdaec7ff87839003fe21.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Variable Hoisting<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A variable can be used before it has been declared. Only variable declarations are hoisted to the top, not variable initialization.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/949b08c361c97ee4912fccdfe40ee3ff.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/d93670b96f5e555e58ddf9b75b3eda78.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Closure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A closure is a function having access to the parent scope. It preserves the data from the outside.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A closure is an inner function that has access to the outer (enclosing) function\u201fs variables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For every closure we have three scopes:-<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Local Scope ( Own scope)<\/li><li>Outer Functions Scope<\/li><li>Global Scope<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Variable Scope JavaScript has two scopes: &#8211; Global Local Global Scope A variable that is declared outside a function definition [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[16],"tags":[4826,692,5095,317],"class_list":["post-7234","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-global","tag-hosting","tag-javascript","tag-variables"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How does JavaScript scope work? - scmGalaxy<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How does JavaScript scope work? - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"Variable Scope JavaScript has two scopes: &#8211; Global Local Global Scope A variable that is declared outside a function definition [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-31T12:13:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-13T16:30:47+00:00\" \/>\n<meta name=\"author\" content=\"rajeshkumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rajeshkumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-does-javascript-scope-work\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-does-javascript-scope-work\\\/\"},\"author\":{\"name\":\"rajeshkumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"headline\":\"How does JavaScript scope work?\",\"datePublished\":\"2021-07-31T12:13:29+00:00\",\"dateModified\":\"2022-04-13T16:30:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-does-javascript-scope-work\\\/\"},\"wordCount\":290,\"commentCount\":0,\"keywords\":[\"Global\",\"hosting\",\"Javascript\",\"Variables\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-does-javascript-scope-work\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-does-javascript-scope-work\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-does-javascript-scope-work\\\/\",\"name\":\"How does JavaScript scope work? - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"datePublished\":\"2021-07-31T12:13:29+00:00\",\"dateModified\":\"2022-04-13T16:30:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-does-javascript-scope-work\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-does-javascript-scope-work\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-does-javascript-scope-work\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How does JavaScript scope work?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\",\"name\":\"rajeshkumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"caption\":\"rajeshkumar\"},\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/rajeshkumar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How does JavaScript scope work? - scmGalaxy","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:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/","og_locale":"en_US","og_type":"article","og_title":"How does JavaScript scope work? - scmGalaxy","og_description":"Variable Scope JavaScript has two scopes: &#8211; Global Local Global Scope A variable that is declared outside a function definition [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/","og_site_name":"scmGalaxy","article_published_time":"2021-07-31T12:13:29+00:00","article_modified_time":"2022-04-13T16:30:47+00:00","author":"rajeshkumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rajeshkumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/"},"author":{"name":"rajeshkumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"headline":"How does JavaScript scope work?","datePublished":"2021-07-31T12:13:29+00:00","dateModified":"2022-04-13T16:30:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/"},"wordCount":290,"commentCount":0,"keywords":["Global","hosting","Javascript","Variables"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/","name":"How does JavaScript scope work? - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"datePublished":"2021-07-31T12:13:29+00:00","dateModified":"2022-04-13T16:30:47+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-does-javascript-scope-work\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"How does JavaScript scope work?"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361","name":"rajeshkumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","caption":"rajeshkumar"},"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/rajeshkumar\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=7234"}],"version-history":[{"count":1,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7234\/revisions"}],"predecessor-version":[{"id":7236,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7234\/revisions\/7236"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=7234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=7234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=7234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}