{"id":631,"date":"2022-08-30T15:04:34","date_gmt":"2022-08-30T15:04:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/what-are-advantages-of-predefining-variable-in-global-scope-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:34","modified_gmt":"2022-08-30T15:04:34","slug":"what-are-advantages-of-predefining-variable-in-global-scope-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/what-are-advantages-of-predefining-variable-in-global-scope-collection-of-common-programming-errors\/","title":{"rendered":"What are advantages of predefining variable in global scope?-Collection of common programming errors"},"content":{"rendered":"<p>As others have suggested you must read more about variable hoisting in Javascript. I will try to demonstrate it.<\/p>\n<pre><code>function test() {\n\n    var t1 = msg + \" hello\";  \n    console.log(t1); \/\/ prints \"undefined hello\" because msg is undefined\n    var t2 = msgx + \" hello\"; \/\/ msgx ReferenceError\n\n    var a = 1;\n    var b = 2;\n    if (a &gt; b) {\n        var msg = 'sample';\n    }\n\n}\n\ntest();\n<\/code><\/pre>\n<p>Here in the example you can see that <code>msg<\/code> is declared. but it is undefined. On the other hand <code>msgx<\/code> causes reference error. It is not declared anywhere. So the point is the statement <code>var msg = 'sample';<\/code> which comes later in the function inside <code>if<\/code> braces makes <code>msg<\/code> a valid variable everywhere in the function. So again declaring <code>msg<\/code> will give you predefined variable warning.<\/p>\n<p>In javascript only a function can create a scope. Everything declared in a function will be available in the whole function scope even though there are inner braces of other control statements. All the variables declared in different lines inside a function will be hoisted and treated as if declared in the starting line of the function.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:09:08. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>As others have suggested you must read more about variable hoisting in Javascript. I will try to demonstrate it. function test() { var t1 = msg + &#8221; hello&#8221;; console.log(t1); \/\/ prints &#8220;undefined hello&#8221; because msg is undefined var t2 = msgx + &#8221; hello&#8221;; \/\/ msgx ReferenceError var a = 1; var b = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-631","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=631"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/631\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}