{"id":11651,"date":"2014-02-04T17:49:30","date_gmt":"2014-02-04T12:19:30","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11651"},"modified":"2016-12-19T15:31:04","modified_gmt":"2016-12-19T10:01:04","slug":"scope-in-javascript","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/scope-in-javascript\/","title":{"rendered":"Scope In JavaScript"},"content":{"rendered":"<p>Many a times we get confused with <strong>scope<\/strong> in <strong>JavaScript<\/strong>, So here I am going to put some light on <strong>scope<\/strong>. There are two types of <strong>scope<\/strong> in <strong>JavaScript<\/strong>. First one is, <strong>function<\/strong> <strong>scope<\/strong> which is also called as <strong>local<\/strong> <strong>scope<\/strong>, and second one is <strong>global<\/strong> <strong>scope<\/strong>.<\/p>\n<p><strong>Function<\/strong> <strong>Scope<\/strong>: In <strong>JavaScript<\/strong>, variables are defined with <strong>function<\/strong> <strong>scope<\/strong> instead of <strong>block<\/strong> <strong>scope<\/strong>. If any variable is defined in <strong>&#8220;if\/else block&#8221;<\/strong> or <strong>&#8220;for block&#8221;<\/strong> then that variable will be defined with <strong>function<\/strong> <strong>scope<\/strong> instead of <strong>block<\/strong> <strong>scope<\/strong>, i.e. it will be visible to whole <strong>function<\/strong>, unlike other programming languages where variable is defined with <strong>block<\/strong> <strong>scope<\/strong> and is visible to that <strong>block<\/strong> only.<\/p>\n<p><strong>Global<\/strong> <strong>Scope<\/strong>: Variables which are defined outside the <strong>function<\/strong>, are defined with <strong>global<\/strong> <strong>scope<\/strong>. We should minimize the use of <strong>global<\/strong> <strong>scope<\/strong> variables, because if two or more than two functions\/<a title=\"Nodejs Developers Team\" href=\"http:\/\/www.tothenew.com\/mean-node-js-development-consulting\">developers are using the global variable<\/a> of same name then result may be unexpected and can lead to hard-to-find bugs. So we always declare variables with <strong>var<\/strong> keyword.<\/p>\n<p>Lets understand <strong>scope<\/strong> more with examples:<\/p>\n<p>[js]<br \/>\nvar global1 = 10; \/\/ global scope variable.<br \/>\nfunction scopeTest() {<br \/>\n    var local1 = 20; \/\/ function scope variable.<br \/>\n    global2 = 30; \/\/ global scope variable.<br \/>\n    if(true) {<br \/>\n        var local2 = 40; \/\/ function scope variable.<br \/>\n    }<br \/>\n    console.log(local2); \/\/ 40 (local2 variable is visible to whole function.)<br \/>\n}<br \/>\nconsole.log(global1); \/\/ 10<br \/>\nconsole.log(local1); \/\/ local1 is not defined<br \/>\nconsole.log(local2); \/\/ local2 is not defined<br \/>\n[\/js]<\/p>\n<p>Here local2 variable is declared in &#8220;if block&#8221;, so unlike other languages, local2 variable will be defined with <strong>function<\/strong> <strong>scope<\/strong> instead of <strong>block<\/strong> <strong>scope<\/strong>, and will be accessible in whole <strong>function<\/strong>.<\/p>\n<p><strong>Note<\/strong>: if we define a variable without using the &#8216;var&#8217; keyword in <strong>function<\/strong> then that variable will be not local variable i.e. it will be defined with <strong>global<\/strong> <strong>scope<\/strong> instead of <strong>function<\/strong> <strong>scope<\/strong>.This variable will be visible outside the <strong>function<\/strong> too. But global2 variable will be defined with <strong>global<\/strong> <strong>scope<\/strong> when for the first time scopeTest <strong>function<\/strong> will be invoked. Before that global2 will be not defined.<\/p>\n<p>[js]<br \/>\nconsole.log(global2); \/\/ global2 is not defined<br \/>\nscopeTest();<br \/>\nconsole.log(global2); \/\/ 30<br \/>\n[\/js]<\/p>\n<p>Lets see some twisty code.<\/p>\n<p>[js]<br \/>\n1. var testValue = 10;<br \/>\n2. function localScopeTest() {<br \/>\n3.     console.log(testValue); \/\/ undefined<br \/>\n4.     var testValue = 20;<br \/>\n5.     console.log(testValue); \/\/ 20<br \/>\n6. }<br \/>\n7. localScopeTest();<br \/>\n[\/js]<\/p>\n<p>You might be expecting output &#8220;10 and 20&#8221;. But correct output will be &#8220;undefined and 20&#8221;, because inside the <strong>function<\/strong> &#8220;<strong>function<\/strong> <strong>scope<\/strong> variable&#8221; gets preference. You might be thinking that testValue is defined at line number 4 and we are logging its value on line number 3, so it should log <strong>global<\/strong> <strong>scope<\/strong> testValue, which is 10. But NO, <strong>JavaScript<\/strong> engine executes <strong>JavaScript<\/strong> in two steps, first it compiles the <strong>JavaScript<\/strong> then executes. At the time of compilation, it gets to know that testValue is declared in <strong>function<\/strong> <strong>scope<\/strong>, but till line number 3, its was not assigned any value so its logging undefined.<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many a times we get confused with scope in JavaScript, So here I am going to put some light on scope. There are two types of scope in JavaScript. First one is, function scope which is also called as local scope, and second one is global scope. Function Scope: In JavaScript, variables are defined with [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1185],"tags":[1121,1301,55,1300],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11651"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11651"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11651\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}