{"id":4297,"date":"2022-03-03T08:38:43","date_gmt":"2022-03-03T08:38:43","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4297"},"modified":"2022-03-03T08:38:45","modified_gmt":"2022-03-03T08:38:45","slug":"javascript-let-var-and-const","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/03\/03\/javascript-let-var-and-const\/","title":{"rendered":"Javascript &#8211; let, var and const"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>There are some small but important differences between using <em>var<\/em>, <em>let<\/em> and <em>const<\/em>:<\/p>\n\n\n\n<p>A <em>var<\/em> can be re-declared and reassigned different values. <em>Let<\/em> variables can be reassigned different values but cannot be redeclared. A <em>const<\/em> can only be declared and assigned once. <\/p>\n\n\n\n<p>In terms of scope a <em>var<\/em> variable is either function scoped or global scoped. That means if a <em>var<\/em> is declared within a function, then it is visible throughout the function and if a <em>var<\/em> is declared at the global level then it is visible everywhere. If a <em>var<\/em> is declared at the global and also at the function level then the function level declaration overrides the global declaration.<\/p>\n\n\n\n<p><em>Let<\/em> variables and <em>const<\/em> variable have block scope. A block scope is any block of code within opening and closing curly braces &#8211; {}. So they have much more fine scope control.<\/p>\n\n\n\n<p>The sample code below shows the various characteristics describe above<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n&lt;html&gt;\n\t&lt;head&gt;\n\t&lt;script&gt;\n\n\t\tvar varItem = &quot;one&quot;;\n\t\tdocument.write(varItem + &quot;&lt;br&gt;&quot;);\n\t\tvar varItem = 24.55;\n\t\tdocument.write(varItem + &quot;&lt;br&gt;&quot;);\n\t\tdocument.write(&quot;&lt;hr&gt;&quot;);\n\t\tlet letItem = &quot;two&quot;;\n\t\tdocument.write(letItem + &quot;&lt;br&gt;&quot;);\n\t\t\/\/let letItem = 189; \/\/ this line will give an error\n\t\tdocument.write(letItem + &quot;&lt;br&gt;&quot;);\n\t\tdocument.write(&quot;&lt;hr&gt;&quot;);\n\t\tconst constItem =449;\n\t\tdocument.write(constItem + &quot;&lt;br&gt;&quot;);\n\t\t\/\/constItem = 444; \/\/ this line will give an error\n\t\tdocument.write(constItem + &quot;&lt;br&gt;&quot;);\n\n\t\tlet globalLet = &quot;global&quot;;\n\t\tdocument.write(&quot;globalLet in global scope=&quot; + globalLet + &quot;&lt;br&gt;&quot;);\n\n\t\tfunc();\n\n\t\tvar testVar = &quot;test1&quot;;\n\t\tfunction func() {\n\t\t\tvar testVar = &quot;test2&quot;;\n\t\t\tif (1 &lt; 2) {\n\t\t\t  var funcVar = &quot;functionVar&quot;;\n\t\t\t}\n\t\t\tlet globalLet = &quot;function&quot;;\n\t\t\tdocument.write(&quot;globalLet in function scope=&quot; + globalLet + &quot;&lt;br&gt;&quot;);\n\t\t\tdocument.write(&quot;testar=&quot; + testVar + &quot;&lt;br&gt;&quot;);\n\t\t\tdocument.write(&quot;funcVar=&quot; + funcVar);\n\t\t}\n\t&lt;\/script&gt;\n\t&lt;\/head&gt;\n\t&lt;body&gt;\n\t&lt;\/body&gt;\n&lt;\/html&gt;\n\n<\/pre><\/div>\n\n\n<p>The output is shown below<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">one\n24.55\n\ntwo\ntwo\n\n449\n449\nglobalLet in global scope=global\nglobalLet in function scope=function\ntestar=test2\nfuncVar=functionVar <\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW There are some small but important differences between using var, let and const: A var can be re-declared and reassigned different values. Let variables <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/03\/03\/javascript-let-var-and-const\/\" title=\"Javascript &#8211; let, var and const\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-4297","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4297"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4297\/revisions"}],"predecessor-version":[{"id":4300,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4297\/revisions\/4300"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2463"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}