{"id":4714,"date":"2018-04-24T10:33:36","date_gmt":"2018-04-24T10:33:36","guid":{"rendered":"https:\/\/tinjurewp.com\/jsblog\/?p=692"},"modified":"2018-04-24T10:33:36","modified_gmt":"2018-04-24T10:33:36","slug":"thoughts-2-understanding-js-variables","status":"publish","type":"post","link":"https:\/\/learncode.tinjurewp.com\/thoughts-2-understanding-js-variables\/","title":{"rendered":"Thoughts #2 &#8211; Understanding JS Variables"},"content":{"rendered":"<p>I have been revisiting <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Grammar_and_types#Declarations\" target=\"_blank\" rel=\"noopener\">JS variables<\/a> (or declaration) for about two-weeks with an aim to understand deeply. I had only a general overview of <a href=\"https:\/\/www.w3schools.com\/js\/js_variables.asp\" target=\"_blank\" rel=\"noopener\">JS variables<\/a> previously but had avoided learning associated topics like <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-js-variables-scopes-hoisting\/\">scope &amp; hoisting<\/a>. Nevertheless, I had felt that I understood those topics but clearly I was wrong.<\/p>\n<p>This time around I spend couple of weeks (late March to mid-April) learning <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-javascript-functions-the-basics\/\">JS functions<\/a>, <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-js-variables-scopes-hoisting\/\">scope &amp; hoisting<\/a> more deeply but realized that JS variable is too confusing because its context base process. Value of JS variable depends upon context it is being used and untangling all the use context is very confusing. The &#8216;<code>old var<\/code>&#8216; makes everything muddy and making variable declaration more confusing. <em>Vojtech Ruzicka<\/em> <a href=\"https:\/\/www.vojtechruzicka.com\/javascript-hoisting-var-let-const-variables\/\" target=\"_blank\" rel=\"noopener\">post<\/a> greatly helped me to untangle some of the mysteries of variable declaration.<\/p>\n<h5>Variable Declaration<\/h5>\n<p>Looking at the tutorials, blog posts and most JS resources, it becomes clear that variables are declared using <code>var<\/code> keyword. Confusion related with <code>var<\/code> declaration due to <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/scope\" target=\"_blank\" rel=\"noopener\">scope<\/a> &amp; <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Hoisting\" target=\"_blank\" rel=\"noopener\">hoisting<\/a> process are described ubiquitously. Recent documents &amp; resources suggest avoiding the use of <code>var<\/code> and instead declare variable using <code>let<\/code> &amp; <code>const<\/code> keywords.<\/p>\n<h5>A Good Understanding of Scope &amp; Hoisting<\/h5>\n<p>I encountered many blog posts and tutorials where even experienced developers expressed scope &amp; hoisting as confusing. This probably justifies a newbie like me, struggling to get a clear understanding of variable declaration, scope, hoisting &amp; function. This time around, I first learned <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-javascript-functions-the-basics\/\" target=\"_blank\" rel=\"noopener\">functions<\/a>, <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-js-variables-scopes-hoisting\/\" target=\"_blank\" rel=\"noopener\">scope &amp; hoisting<\/a> and <a href=\"https:\/\/tinjurewp.com\/jsblog\/javascript-functions-declaration-and-expression\/\" target=\"_blank\" rel=\"noopener\">function declaration vs function expression<\/a> and only then returned to re-learn <a href=\"https:\/\/tinjurewp.com\/jsblog\/declaring-js-variables-var-let-const\/\" target=\"_blank\" rel=\"noopener\">variable declaration<\/a> I had a much better understanding variable declaration.<\/p>\n<h5>Difference Between Declared vs Undeclared<\/h5>\n<p>In <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/var#Description\" target=\"_blank\" rel=\"noopener\">MDN <code>var<\/code> Description<\/a>, difference between <em>declared<\/em> vs <em>undeclared<\/em> has been described with examples. Understanding these differences with examples helped me to better understand variable declaration with <code>var<\/code>, <code>let<\/code> and <code>const<\/code> keywords. The MDN recommends <em>to always declare variables, regardless of whether they are in a function or global scope<\/em>. It looks like a a good practice that I must get use to it.<\/p>\n<h5>Hoisting Rules with\u00a0 <code>var<\/code> &amp; <code>let<\/code><\/h5>\n<p>This time around I used MDN documentation on <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/var&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;var&lt;\/a&gt;<\/code>, <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/let&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;let&lt;\/a&gt;<\/code> and <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/const&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;const&lt;\/a&gt;<\/code> as my primary learning resource. Because these documents don&#8217;t always explain for beginners, I had to resort to developers blog posts for additional explanation. After learning hoisting rules for <code>var<\/code> &amp; <code>let<\/code> I got much clearer picture of variable declaration.<\/p>\n<h6><code>var<\/code> vs <code>let<\/code> vs <code>const<\/code><\/h6>\n<p>It is recommended to use <code>let<\/code> and <code>const<\/code> to declare variables. Most experience developers suggest using <code>const<\/code> than <code>let<\/code> except when required (eg. conditional statements). This is a topic that I plan to return after some code writing experience only.<\/p>\n<h5>Temporal Dead Zone<\/h5>\n<p>While I was learning variable declaration, a new terminology \u2018<em>Temporal Dead zone<\/em>\u2019 pops up here and there. <a href=\"https:\/\/dmitripavlutin.com\/j\" target=\"_blank\" rel=\"noopener\">Dmitri Pavlutin<\/a> has explained <a href=\"https:\/\/dmitripavlutin.com\/javascript-hoisting-in-details\/#hoistingandlet\" target=\"_blank\" rel=\"noopener\"><em>temporal dead zone<\/em><\/a> process in his <a href=\"https:\/\/dmitripavlutin.com\/javascript-hoisting-in-details\/\" target=\"_blank\" rel=\"noopener\">variable hoisting<\/a> &amp; <a href=\"https:\/\/dmitripavlutin.com\/variables-lifecycle-and-why-let-is-not-hoisted\/\" target=\"_blank\" rel=\"noopener\">variable life cycles<\/a> posts in very detail. It is essential to understand the process in relation to variable declaration with <code>let<\/code> keyword. I prepared <a href=\"https:\/\/tinjurewp.com\/jsblog\/variable-declaration-with-let-temporal-dead-zone\/\" target=\"_blank\" rel=\"noopener\">a brief learning-note of this process<\/a> is posted separately.<\/p>\n<h5>A Note on Googling<\/h5>\n<p>Now-a-days, it is almost ubiquitous that whenever we need any information on any topic we just google it. This is true for either looking for a grocery or electronic items in the Amazon or looking for technical topics like JS functions, variable hoisting etc. I do spend significant time googling terms like JS variables, CSS grid or many other similar topics.<\/p>\n<p>My recent experience in googling JS variables, scope, hoisting etc is that (a) most <a href=\"https:\/\/medium.com\/\" target=\"_blank\" rel=\"noopener\">Medium<\/a> articles\/posts are brief, done either as advertisement or anchorage for authors&#8217; website link, (b) I benefited most from blog posts of recognized developers (eg. <a href=\"https:\/\/dmitripavlutin.com\/\" target=\"_blank\" rel=\"noopener\">Dimitri Pavlutin<\/a>, <a href=\"https:\/\/wesbos.com\/blog\/\" target=\"_blank\" rel=\"noopener\">Wes Bos<\/a>,\u00a0 <a href=\"https:\/\/zellwk.com\/blog\/\" target=\"_blank\" rel=\"noopener\">Zell Liew<\/a>, <a href=\"http:\/\/lenafaure.com\/\" target=\"_blank\" rel=\"noopener\">Lena Fure<\/a> and others). Such developers blog posts are very detailed, suitable even for beginners.<\/p>\n<h5>\u201cAh ha\u201d Moment in JS Declarations<\/h5>\n<p>Like many others, I was having problems understanding <em>scope<\/em>, <em>hoisting<\/em>, <em>temporal dead zone<\/em> for some time. My &#8216;Ah ha&#8217; moments in understanding these concepts came from the blog posts of <a href=\"https:\/\/dmitripavlutin.com\/\" target=\"_blank\" rel=\"noopener\">Dmitri Pavlutin<\/a>, <a href=\"https:\/\/www.vojtechruzicka.com\/\" target=\"_blank\" rel=\"noopener\">Vojtech Ruzicka<\/a>, <a href=\"https:\/\/zellwk.com\/blog\/\" target=\"_blank\" rel=\"noopener\">Zell Liew<\/a>, <a href=\"http:\/\/lenafaure.com\/\" target=\"_blank\" rel=\"noopener\">Lena Faure<\/a> and others.<\/p>\n<h6>Inspiring Resources<\/h6>\n<ul>\n<li><a href=\"https:\/\/www.vojtechruzicka.com\/javascript-hoisting-var-let-const-variables\/\" target=\"_blank\" rel=\"noopener\">Javascript hoisting, var, let and const variables<\/a> &#8211; by <em>Vojtech Ruzicka<\/em> (<a href=\"https:\/\/www.vojtechruzicka.com\/\" target=\"_blank\" rel=\"noopener\">vojtechruzicka.com<\/a>)<\/li>\n<li><a href=\"https:\/\/dmitripavlutin.com\/javascript-hoisting-in-details\/\" target=\"_blank\" rel=\"noopener\">JavaScript variables hoisting in details<\/a> &#8211; by <em>Dmitri Pavlutin<\/em> (<a href=\"https:\/\/dmitripavlutin.com\/\" target=\"_blank\" rel=\"noopener\">dmitripavlution.com<\/a>)<\/li>\n<li><a href=\"https:\/\/dmitripavlutin.com\/variables-lifecycle-and-why-let-is-not-hoisted\/#5letvariableslifecycle\" target=\"_blank\" rel=\"noopener\">les lifecycle: why let is not hoisted<\/a> &#8211; by <em>Dmitri Pavlutin<\/em> (<a href=\"https:\/\/dmitripavlutin.com\/\" target=\"_blank\" rel=\"noopener\">dmitripavlution.com<\/a>)<\/li>\n<li><a href=\"https:\/\/hackernoon.com\/hoisting-in-javascript-a-quick-guide-cc4d9597bbd7\" target=\"_blank\" rel=\"noopener\">Hoisting in JavaScript: a Quick\u00a0Guide<\/a> &#8211; by <em>Lena Faure<\/em> (<strong>medium.com<\/strong>)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I have been revisiting JS variables (or declaration) for about two-weeks with an aim to understand deeply. I had only a general overview of JS variables previously but had avoided learning associated topics like scope &amp; hoisting. Nevertheless, I had felt that I understood those topics but clearly I was wrong. This time around I [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[40],"tags":[],"class_list":["post-4714","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/4714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/comments?post=4714"}],"version-history":[{"count":0,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/4714\/revisions"}],"wp:attachment":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/media?parent=4714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/categories?post=4714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/tags?post=4714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}