{"id":86631,"date":"2017-06-17T11:38:01","date_gmt":"2017-06-17T11:38:01","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=86631"},"modified":"2017-06-17T11:38:01","modified_gmt":"2017-06-17T11:38:01","slug":"javascript-scope","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/javascript-scope\/","title":{"rendered":"JavaScript Scope"},"content":{"rendered":"<p>JavaScript has a feature called scope. The concept of the scope in not that easy to understand for many new developers but I will try my best to explain them in simple way. Understanding scope will make your code better and key to writing bulletproof code and being a better developer.<\/p>\n<p><strong>What Is Scope?<\/strong><\/p>\n<p>In JavaScript scope is the set of variables, functions and objects you have accessed to. Scope refers to the current context of your code. scope can be defined globally and locally.<\/p>\n<h1 style=\"text-align: center\"><strong>Global Scope<\/strong><\/h1>\n<p>Before writing code in JavaScript, you are in what we call <strong>Global Scope<\/strong>. If we declare a variable, It&#8217;s considered as <strong>Global Variable<\/strong>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-86634\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_1-3.png\" alt=\"\" width=\"1160\" height=\"67\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_1-3.png 1160w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_1-3-250x14.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_1-3-300x17.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_1-3-768x44.png 768w\" sizes=\"(max-width: 1160px) 100vw, 1160px\" loading=\"lazy\" \/><\/p>\n<p>Variables inside the global scope can be accessed and altered in any other scope.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-86635\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_2-4.png\" alt=\"\" width=\"1165\" height=\"237\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_2-4.png 1165w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_2-4-250x51.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_2-4-300x61.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_2-4-768x156.png 768w\" sizes=\"(max-width: 1165px) 100vw, 1165px\" loading=\"lazy\" \/><\/p>\n<h1 style=\"text-align: center\"><strong>Local Scope<\/strong><\/h1>\n<p>There is typically one global scope and each function defined has its own local scope. Any function defined with in another function has a local scope of the parent function.<\/p>\n<p>If I create function and defined some variables, those variable will be the local variables of that function. Take an example &#8211;<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-86636\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_3-3.png\" alt=\"\" width=\"1161\" height=\"244\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_3-3.png 1161w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_3-3-250x53.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_3-3-300x63.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_3-3-768x161.png 768w\" sizes=\"(max-width: 1161px) 100vw, 1161px\" loading=\"lazy\" \/><\/p>\n<p><strong>Automatically Global<\/strong><\/p>\n<p>If we assign a value to a variable that has not been declared then that variable will be considered as <strong>Global Variable<\/strong>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-86639\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_5-2.png\" alt=\"\" width=\"1165\" height=\"145\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_5-2.png 1165w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_5-2-250x31.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_5-2-300x37.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot_5-2-768x96.png 768w\" sizes=\"(max-width: 1165px) 100vw, 1165px\" loading=\"lazy\" \/><\/p>\n<p>I hope you got a better understanding of JavaScript Scope and things around it. Thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript has a feature called scope. The concept of the scope in not that easy to understand for many new developers but I will try my best to explain them in simple way. Understanding scope will make your code better and key to writing bulletproof code and being a better developer. What Is Scope? In <a href=\"https:\/\/webkul.com\/blog\/javascript-scope\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":71,"featured_media":84482,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198,1],"tags":[],"class_list":["post-86631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Scope - Webkul Blog<\/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:\/\/webkul.com\/blog\/javascript-scope\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Scope - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"JavaScript has a feature called scope. The concept of the scope in not that easy to understand for many new developers but I will try my best to explain them in simple way. Understanding scope will make your code better and key to writing bulletproof code and being a better developer. What Is Scope? In [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/javascript-scope\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-17T11:38:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Sambit Pattanayak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sambit Pattanayak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/\"},\"author\":{\"name\":\"Sambit Pattanayak\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/23309d7bfba5b06d58b50ea5e3191f01\"},\"headline\":\"JavaScript Scope\",\"datePublished\":\"2017-06-17T11:38:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/\"},\"wordCount\":219,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/javascript-scope\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/\",\"url\":\"https:\/\/webkul.com\/blog\/javascript-scope\/\",\"name\":\"JavaScript Scope - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"datePublished\":\"2017-06-17T11:38:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/javascript-scope\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"width\":\"825\",\"height\":\"260\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/javascript-scope\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Scope\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/23309d7bfba5b06d58b50ea5e3191f01\",\"name\":\"Sambit Pattanayak\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b470681418ca47d8211cf413884189c631a12188462ba25500fa51c897318975?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b470681418ca47d8211cf413884189c631a12188462ba25500fa51c897318975?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Sambit Pattanayak\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/sambit-p819\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Scope - Webkul Blog","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:\/\/webkul.com\/blog\/javascript-scope\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Scope - Webkul Blog","og_description":"JavaScript has a feature called scope. The concept of the scope in not that easy to understand for many new developers but I will try my best to explain them in simple way. Understanding scope will make your code better and key to writing bulletproof code and being a better developer. What Is Scope? In [...]","og_url":"https:\/\/webkul.com\/blog\/javascript-scope\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2017-06-17T11:38:01+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","type":"image\/png"}],"author":"Sambit Pattanayak","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Sambit Pattanayak","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/javascript-scope\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/javascript-scope\/"},"author":{"name":"Sambit Pattanayak","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/23309d7bfba5b06d58b50ea5e3191f01"},"headline":"JavaScript Scope","datePublished":"2017-06-17T11:38:01+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/javascript-scope\/"},"wordCount":219,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/javascript-scope\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/javascript-scope\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/javascript-scope\/","url":"https:\/\/webkul.com\/blog\/javascript-scope\/","name":"JavaScript Scope - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/javascript-scope\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/javascript-scope\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","datePublished":"2017-06-17T11:38:01+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/javascript-scope\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/javascript-scope\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/javascript-scope\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","width":"825","height":"260"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/javascript-scope\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript Scope"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/23309d7bfba5b06d58b50ea5e3191f01","name":"Sambit Pattanayak","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b470681418ca47d8211cf413884189c631a12188462ba25500fa51c897318975?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b470681418ca47d8211cf413884189c631a12188462ba25500fa51c897318975?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Sambit Pattanayak"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/sambit-p819\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/86631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/71"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=86631"}],"version-history":[{"count":1,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/86631\/revisions"}],"predecessor-version":[{"id":86640,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/86631\/revisions\/86640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/84482"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=86631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=86631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=86631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}