{"id":19583,"date":"2020-07-16T06:29:20","date_gmt":"2020-07-16T13:29:20","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19583"},"modified":"2020-12-29T13:48:15","modified_gmt":"2020-12-29T21:48:15","slug":"javascript-closure","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/javascript-closure\/","title":{"rendered":"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer"},"content":{"rendered":"\n<p><em>One of the biggest buzzwords in the JavaScript language is closure. It\u2019s the subject of many job interview questions at FAANG companies. In this article, we\u2019ll talk about closure and scope, illustrate its concepts with simple examples, and then finish out with a sample question from an interview with one of the bigger tech giants.<\/em><br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Scope<\/h2>\n\n\n\n<p><em>When someone tells you something is or isn\u2019t in scope of a project, what does that mean?&nbsp;<\/em><br><\/p>\n\n\n\n<p>I\u2019d like to think of a periscope or a telescope when I think of the answer to this. These instruments show us all sorts of things within the confines of the lens it has:&nbsp; it\u2019s in <em>scope<\/em>. If it\u2019s outside the <em>scope<\/em>, you can\u2019t see past the diameter of the lens. And shining a light on something outside the diameter is not possible. You should be thinking about this as we talk about three very important and distinct types of scope in JavaScript: local, global and lexical.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Local Scope<\/h3>\n\n\n\n<p>Local Scope is the smallest of the three scopes we will be talking about today. When we declare a function, anything inside the brackets ({}) is considered to be local to the function. When the JavaScript engine reads the function it will declare the variables; when it ends it will destroy the variables.&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function greeting() {\n var websiteName = 'Career Karma';\n return `Hello ${websiteName}`;\n}\n \nconsole.log(greeting()); \/\/ Hello Career Karma\nconsole.log(websiteName); \/\/ ReferenceError: websiteName is not defined<\/pre><\/div>\n\n\n\n<p>As you can see, when we \u201cconsole.log()\u201d the outcome of the invoked greeting function, we are able to access the websiteName after the function was executed. This gives us the \u2018Hello Career Karma\u2019 string that we were looking for. The <code>console.log()<\/code> of the variable that was declared inside the function throws an error because it\u2019s not defined.<br><\/p>\n\n\n\n<p>As mentioned already, the reason why websiteName is undefined is because variables are created inside functions when they are invoked and then destroyed when the terminal statement runs. Anything outside of the function does not have access to stuff inside the function unless it has a special setup.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Global Scope<\/h3>\n\n\n\n<p>This next scope is pretty much a literal translation of the phrase. A global scope takes the items declared outside of a function and reserves them in a space where all the scripts and methods and functions can access and use them for their logic.&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre> \nlet counter = 0; \/\/ global -- declared outside function\n \nconst add = () =&gt; { \/\/ function declaration\n   let counter = 0; \/\/ local -- declared inside function\n   counter += 1; \n   \/\/ counter increased by 1 -- which counter variable increased?\n   return counter;\n}\n \nadd(); \/\/ invoke\nadd(); \/\/  three\nadd(); \/\/  times\nconsole.log(counter) \/\/ is this 3 or 0? Why? \n<\/pre><\/div>\n\n\n\n<p>What does the code above do if we <code>console.log()<\/code> the counter at the end of the code? What do you expect to happen?&nbsp;<br><\/p>\n\n\n\n<p>Let\u2019s walk through the code:&nbsp;<br><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Counter variable declared and initiated in the global environment.&nbsp;<\/li><li>Add function declared in the global environment.&nbsp;<\/li><li>Add is invoked.<\/li><li>Counter variable declared and initiated in the local environment.&nbsp;<\/li><li>The local counter increases by 1 \u21d0 why local and not global?&nbsp;<\/li><li>Counter is returned. Function terminates.<\/li><li>Add is invoked again<\/li><li>Walk through steps 4 to 6 again.&nbsp;<\/li><li>Repeat steps 3 to 6 again.<\/li><li><code>console.log(counter)<\/code>; \u21d0 What is returned?&nbsp;<\/li><\/ol>\n\n\n\n<p>Because the function terminates when the counter is at 1 every time, our local counter variable is redeclared and re-initiated at 0 every time the function runs. No matter what happens, the counter will always stop at 1 on the local level.&nbsp;<br><\/p>\n\n\n\n<p>If a function finds a variable within its scope, it doesn\u2019t look to the global scope for the variable \u2013 so the global variable never changes. So, our <code>console.log() <\/code>will output 0 as our closest defined variable within that statement\u2019s environment is in the global environment.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Lexical Scope<\/h3>\n\n\n\n<p>The lexical scope is one of the most fundamental concepts in JavaScript. It\u2019s the idea that the creation of a function or of a variable will be accessible to certain parts of the code and then inaccessible to other parts of the code. It all depends on where the declaration of each variable and function is.<br><\/p>\n\n\n\n<p>Let\u2019s take a look at this block of code:&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>const init = () =&gt; { \/\/ &lt;== This is our outer function\n const var1 = 'Career'; \/\/ outer scope\n const second = () =&gt; { \/\/ &lt;== This is our inner function\n   const var2 = 'Karma'; \/\/ inner scope\n   console.log(var1); \/\/ Career\n   console.log(var2); \/\/ Karma\n   return var1 + &quot; &quot; + var2;\n };\n \n \/\/ console.log(var2); \/\/ undefined\n \n \n return second();\n};\ninit();\n \n<\/pre><\/div>\n\n\n\n<p>Here we have a set of nested functions. The <code>init() <\/code>function declares a variable called var1, declares a function called second and invokes <code>second()<\/code>.&nbsp;<br><\/p>\n\n\n\n<p>When the compiler passes through this code the first time, it takes a high level look at what we have:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>init() <\/code>function<\/li><li>invoke<code> init()<\/code><\/li><\/ol>\n\n\n\n<p>At this point, we can\u2019t see anything else inside the init() function \u2013 we just know the function exists. When our init() func is invoked, the compiler takes another high level look at what\u2019s inside the function:<br><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>var1<\/code><\/li><li><code>second() <\/code>function<\/li><li>invoke <code>second()<\/code><\/li><\/ol>\n\n\n\n<p>The <code>init() <\/code>function knows nothing about what is going on inside the <code>second() <\/code>block. It can only see what is in its <em>lexical environment<\/em> \u2013 its surrounding state.<br><\/p>\n\n\n\n<p>Each nested function is in a smaller container, like a set of those Russian matryoshka nesting dolls (see top of page for example if you\u2019re unsure of what they are). The dolls only know about what\u2019s going on inside their container and what\u2019s already occurred or declared\/read in the parent. The largest doll for example only knows that the next doll in its container exists. It doesn\u2019t know about any of the other dolls in the set, just what\u2019s in its lexical environment (its state) and what\u2019s already happened (the outer scope).&nbsp;<br><\/p>\n\n\n\n<p>In essence, we know two things:<br><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The outer scope can\u2019t see the inner scope.<\/li><li>The inner scope has access to the outer scope.&nbsp;<\/li><\/ol>\n\n\n\n<p>Because the outer cope can\u2019t see what\u2019s going on in the inner scope, we can safely say that this is a one-way relationship. Inner can see and use variables from the outer scope, but outer can\u2019t see inner. This is called <strong><em>lexical scope<\/em><\/strong>.&nbsp;<br><\/p>\n\n\n\n<p>The beauty of lexical scoping is that the value of a variable is determined by its placement in the code. The functions look for the meaning of a variable inside it\u2019s local environment first \u2013 if it can\u2019t find it, it moves to the function that defined that function. If it can\u2019t find it there, it moves up the chain to the next defined function.<br><\/p>\n\n\n\n<p>This becomes a very important concept in JavaScript that will come up time and again as you learn more about JavaScript frameworks and how they work. You can pass down from the outer, but you can never pass \u201cup\u201d in the other direction. This is very important as we get to the major topic at hand: <strong>closure<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Closure<\/h2>\n\n\n\n<p>Closure\u2019s definition is very similar to that of lexical scope. The main difference between the two is that closure is a higher order function and lexical scoping is not. A higher order function has one basic characteristic: it either returns a function or uses a function as a parameter.&nbsp;<br><\/p>\n\n\n\n<p>Closure is a function that can access its lexical scope, even when that function is being invoked later.&nbsp;<br><\/p>\n\n\n\n<p>Both closure and lexical scope have their own variables, can access a parent function\u2019s variables and parameters, and can use global variables. Let\u2019s walk through the following code:&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function greeting() { \/\/outer scope (parent function)\n const userName = &quot;CrrKrma1952&quot;; \/\/ parent variable\n function welcomeGreeting() { \/\/ inner function\n   console.log(&quot;Hello, &quot; + userName); \/\/ accesses parent var\n   return &quot;Hello, &quot; + userName; \/\/ terminal statement\n }\n return welcomeGreeting; \/\/ returns a function (which makes it HOF)\n} \/\/ end of greeting()\n \nconst greetUser = greeting(); \/\/\ngreetUser(); \/\/  Hello, CrrKrma1952<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\"><li><code>greeting() <\/code>function exists, but we don\u2019t know the contents yet.&nbsp;<\/li><li><code>greetUser<\/code> exists, but don\u2019t know contents yet<\/li><li><code>greetUser() <\/code>&#8211; this invokes the previous line, which, in turn, invokes the greeting() function.&nbsp;<\/li><li>userName declared<\/li><li><code>welcomeGreeting() <\/code>exists, but don\u2019t know contents yet<\/li><li>Return statement below the <code>welcomeGreeting()<\/code> block returns that very same function<\/li><li><code>console.log(\u2018Hello, \u2018 + userName)<\/code>; Our console.log here can access the parent scope to get the value of userName<\/li><li>Terminal statement that ends the function and destroys the meaning of the variables inside the code block.<\/li><\/ol>\n\n\n\n<p>In this code, we are passing around information by nesting functions together so that the parent scope can be accessed later.<\/p>\n\n\n\n<iframe loading=\"lazy\" frameborder=\"0\" width=\"100%\" height=\"400px\" src=\"https:\/\/repl.it\/@careerkarma\/Web-Fundamentals-Scope-and-JavaScript-Closure-a-Primer?lite=true\"><\/iframe>\n<br>\n<br>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we talked about a pretty hefty JavaScript subject: Scope and Closures. I would recommend branching out and reading several different articles on the subject. The way this is taught can come from a variety of points-of-view \u2013 which means, of course, that there\u2019s lots of ways to learn it. I hope this primer was helpful to you! Good luck in continuing your study on closures!<br><\/p>\n","protected":false},"excerpt":{"rendered":"One of the biggest buzzwords in the JavaScript language is closure. It\u2019s the subject of many job interview questions at FAANG companies. In this article, we\u2019ll talk about closure and scope, illustrate its concepts with simple examples, and then finish out with a sample question from an interview with one of the bigger tech giants.&hellip;","protected":false},"author":77,"featured_media":19584,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11933],"tags":[],"class_list":{"0":"post-19583","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"JavaScript","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer | Career Karma<\/title>\n<meta name=\"description\" content=\"Closure is a very important concept to know about in JavaScript. This tutorial covers what scope is and how scope is used in closure.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/javascript-closure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer\" \/>\n<meta property=\"og:description\" content=\"Closure is a very important concept to know about in JavaScript. This tutorial covers what scope is and how scope is used in closure.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/javascript-closure\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-16T13:29:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-29T21:48:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/doll-314345_1280.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"794\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Christina Kopecky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Christina Kopecky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/\"},\"author\":{\"name\":\"Christina Kopecky\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/ae0cdc4a5d198690d78482646894074e\"},\"headline\":\"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer\",\"datePublished\":\"2020-07-16T13:29:20+00:00\",\"dateModified\":\"2020-12-29T21:48:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/\"},\"wordCount\":1319,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/doll-314345_1280.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/\",\"name\":\"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/doll-314345_1280.jpg\",\"datePublished\":\"2020-07-16T13:29:20+00:00\",\"dateModified\":\"2020-12-29T21:48:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/ae0cdc4a5d198690d78482646894074e\"},\"description\":\"Closure is a very important concept to know about in JavaScript. This tutorial covers what scope is and how scope is used in closure.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/doll-314345_1280.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/doll-314345_1280.jpg\",\"width\":1200,\"height\":794},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-closure\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/ae0cdc4a5d198690d78482646894074e\",\"name\":\"Christina Kopecky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/image-3-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/image-3-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/image-3-150x150.jpg\",\"caption\":\"Christina Kopecky\"},\"description\":\"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.\",\"sameAs\":[\"http:\\\/\\\/www.linkedin.com\\\/in\\\/cmvnk\"],\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/christina-kopecky\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer | Career Karma","description":"Closure is a very important concept to know about in JavaScript. This tutorial covers what scope is and how scope is used in closure.","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:\/\/careerkarma.com\/blog\/javascript-closure\/","og_locale":"en_US","og_type":"article","og_title":"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer","og_description":"Closure is a very important concept to know about in JavaScript. This tutorial covers what scope is and how scope is used in closure.","og_url":"https:\/\/careerkarma.com\/blog\/javascript-closure\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-07-16T13:29:20+00:00","article_modified_time":"2020-12-29T21:48:15+00:00","og_image":[{"width":1200,"height":794,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/doll-314345_1280.jpg","type":"image\/jpeg"}],"author":"Christina Kopecky","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Christina Kopecky","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/"},"author":{"name":"Christina Kopecky","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"headline":"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer","datePublished":"2020-07-16T13:29:20+00:00","dateModified":"2020-12-29T21:48:15+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/"},"wordCount":1319,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/doll-314345_1280.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/javascript-closure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/","url":"https:\/\/careerkarma.com\/blog\/javascript-closure\/","name":"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/doll-314345_1280.jpg","datePublished":"2020-07-16T13:29:20+00:00","dateModified":"2020-12-29T21:48:15+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"description":"Closure is a very important concept to know about in JavaScript. This tutorial covers what scope is and how scope is used in closure.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/javascript-closure\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/doll-314345_1280.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/doll-314345_1280.jpg","width":1200,"height":794},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/javascript-closure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/careerkarma.com\/blog\/javascript\/"},{"@type":"ListItem","position":3,"name":"Web Fundamentals: Scope and JavaScript Closure \u2013 a Primer"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e","name":"Christina Kopecky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","caption":"Christina Kopecky"},"description":"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.","sameAs":["http:\/\/www.linkedin.com\/in\/cmvnk"],"url":"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19583"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19583\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/19584"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}