{"id":386,"date":"2018-04-09T17:55:02","date_gmt":"2018-04-09T17:55:02","guid":{"rendered":"https:\/\/tinjurewp.com\/jsblog\/?p=386"},"modified":"2018-04-09T17:55:02","modified_gmt":"2018-04-09T17:55:02","slug":"thoughts-1-javascript-scope-hoisting","status":"publish","type":"post","link":"https:\/\/learncode.tinjurewp.com\/thoughts-1-javascript-scope-hoisting\/","title":{"rendered":"Thoughts #1: JavaScript Scope &#038; Hoisting"},"content":{"rendered":"<p>I am in my very early stage of JS learning and just beginning to get used to and familiarize myself with the fundamentals of the the JS language. Last week, I spend lot of time understanding the basic JS function (declaration, expression, arrow functions) and write my <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-javascript-functions-the-basics\/\" target=\"_blank\" rel=\"noopener\">learning-note about it<\/a>. I have had already my share of many many frustrating moments to understand other JS topics clearly. After spending almost a week, I am still confused. The topic is not straight forward but <a href=\"https:\/\/medium.com\/bluekiri\/the-confusion-of-javascript-scope-and-hoisting-3e9c759eb419\" target=\"_blank\" rel=\"noopener\">confusing<\/a>.<\/p>\n<blockquote><p>And don&#8217;t think everything will become totally logical: JavaScript is confusing.<\/p><\/blockquote>\n<h5>Callback Function<\/h5>\n<p>While learning functions, I came across yet another function \u2013 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Callback_function\" target=\"_blank\" rel=\"noopener\">callback function<\/a>. I spend some time learning about it and realized that it could be confusing to newbie like me. Nevertheless, I wrote a <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-js-callback-functions\/\" target=\"_blank\" rel=\"noopener\">learning-note about callback function<\/a> too.<\/p>\n<h5>Function Declaration vs Function Expression<\/h5>\n<p>Before I move on to other topics (eg. Objects), I opted to devote some time to understand the difference between <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/function\" target=\"_blank\" rel=\"noopener\">function declaration<\/a> &amp; <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/function\" target=\"_blank\" rel=\"noopener\">function expression<\/a>. After learning basic difference in syntax, I started getting confused with <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/function\" target=\"_blank\" rel=\"noopener\">conditionally created functions<\/a>. To better understand this concept, basic knowledge of variable hoisting with <code>var<\/code>, <code>let<\/code> and <code>const<\/code> and var\/function scope is essential.<\/p>\n<h5>Scope &amp; Hoisting<\/h5>\n<p>While learning functions (declaration &amp; expression) and variables but I get confused when I start trying to untangle <strong>scope<\/strong> (variable &amp; function), <strong>hoisting<\/strong>. My plan for next week or so is to\u00a0understand <strong>variables<\/strong> ( <code>var<\/code>, <code>let<\/code> &amp; <code>const<\/code>) and Function Scopes &amp; <code>closures<\/code>.<\/p>\n<h5>Waiting for My &#8220;Ah ha&#8221; Moment in JS Functions<\/h5>\n<p>Finally, I did some goggling to learn how other JS developers with similar to mine non-programmer background overcome their most frustrating learning experience. In a 2012 <a href=\"https:\/\/css-tricks.com\/\" target=\"_blank\" rel=\"noopener\">CSS-Tricks<\/a> forum post Chris\u00a0 Coyier \u00a0sought inputs from his site\u2019s participant to share their <a href=\"https:\/\/css-tricks.com\/the-javascript-ah-ha-moment\/\" target=\"_blank\" rel=\"noopener\">The \u00a0JavaScipt \u201cAh ha!\u201d Moment<\/a>. \u00a0Most forum participant commented that understanding of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Closures\" target=\"_blank\" rel=\"noopener\">closure<\/a>, <strong>scope<\/strong>, <strong>hoisting<\/strong>,<strong> objects<\/strong> made them feel their \u201cAh ha\u201d moment. From their comments, I recognized that JS Closure &amp; Scope were the most frequently mentioned confusing topics to understand.<\/p>\n<blockquote><p>JavaScript is a confusing language to learn. I am Still Waiting for My &#8216;Ah ha&#8217; moments in JS <strong>Function<\/strong> (<code>scope<\/code>, <code>hoisting<\/code>)<\/p><\/blockquote>\n<p>In a more recent post in <a href=\"https:\/\/medium.com\/\" target=\"_blank\" rel=\"noopener\">Medium<\/a> <em>Tan Moy<\/em> (Web Developer \/ Data Scientist) wrote that his <a href=\"https:\/\/medium.com\/@tanmoy\/aha-moments-javascript-callbacks-bce593d4c086\" target=\"_blank\" rel=\"noopener\">Aha! Moments in JavaScript was JS Callbacks<\/a>.<\/p>\n<h6>Book Recommendation<\/h6>\n<p><em>Tom McFarlin<\/em> (WordPress developer) <a href=\"https:\/\/tommcfarlin.com\/recommended-javascript-books\/\" target=\"_blank\" rel=\"noopener\">writes<\/a> that <a href=\"https:\/\/www.amazon.com\/JavaScript-Good-Parts-ebook\/dp\/B0026OR2ZY\" target=\"_blank\" rel=\"noopener\">JavaScript: The Good Parts<\/a> (O\u2019Reilly Publication) solidified his understanding of the JavaScript language.<\/p>\n<h6>Useful Resources<\/h6>\n<p>The following resources appear to be relevant to further review these topics:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/understanding-variables-scope-hoisting-in-javascript\" target=\"_blank\" rel=\"noopener\">Understanding Variables, Scope, and Hoisting in JavaScript<\/a> &#8211; <em>Tania Rascia<\/em><\/li>\n<li><a href=\"https:\/\/dmitripavlutin.com\/javascript-hoisting-in-details\/\" target=\"_blank\" rel=\"noopener\">JavaScript variables hoisting in details<\/a> &#8211; <em>Dmitri Pavlutin<\/em><\/li>\n<li><a href=\"https:\/\/dmitripavlutin.com\/6-ways-to-declare-javascript-functions\/\" target=\"_blank\" rel=\"noopener\">6 ways to declare JavaScript functions<\/a> &#8211; <em>Dmitri Pavlutin<\/em><\/li>\n<li><a href=\"https:\/\/css-tricks.com\/javascript-scope-closures\/\" target=\"_blank\" rel=\"noopener\">JavaScript Scope and\u00a0Closures<\/a> &#8211; <em>Zell Liew<\/em> (CSS-Tricks)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I am in my very early stage of JS learning and just beginning to get used to and familiarize myself with the fundamentals of the the JS language. Last week, I spend lot of time understanding the basic JS function (declaration, expression, arrow functions) and write my learning-note about it. I have had already my [&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":[4,37],"tags":[],"class_list":["post-386","post","type-post","status-publish","format-standard","hentry","category-javascript","category-thoughts"],"_links":{"self":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/386","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=386"}],"version-history":[{"count":0,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"wp:attachment":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}