{"id":766,"date":"2020-09-01T19:05:04","date_gmt":"2020-09-01T19:05:04","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=766"},"modified":"2020-09-19T18:04:03","modified_gmt":"2020-09-19T18:04:03","slug":"what-is-hoisting-and-temporal-dead-zone-in-es6","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/what-is-hoisting-and-temporal-dead-zone-in-es6\/","title":{"rendered":"What is Hoisting and Temporal Dead Zone in ES6"},"content":{"rendered":"\n<p><strong>Hoisting<\/strong> is important JavaScript concept introduced in ECMAScript 6 (ES6) where variables and function declarations (declared using <strong><code>var<\/code><\/strong>) are moved to the top of their scope before code execution. Inevitably, this means that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.<\/p>\n\n\n\n<p><strong>Variable Hoisting:<\/strong><\/p>\n\n\n\n<p>\n\nIt is important to note that, JavaScript only hoists declarations, not initializations. If a variable is declared and initialized after using it, the value will be undefined. \n\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Hoisted\nconsole.log(num); \/\/ undefined\nvar num; \/\/ Declaration\n\n\/\/ Not hoisted\nconsole.log(num); \/\/ ReferenceError: num is not defined\nvar num = 10; \/\/ Initialization<\/code><\/pre>\n\n\n\n<p><strong>Function Hoisting:<\/strong><\/p>\n\n\n\n<p>let&#8217;s consider an example to understand function hoisting. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Calling function before declaration\nadd(5, 10); Output: 15\n\nfunction add(x, y){\n   console.log(x + y);\n}<\/code><\/pre>\n\n\n\n<p>As you see, the function <code><strong>add()<\/strong><\/code> is called before it was declared, but the code will work fine without any error because of hoisting mechanism. JavaScript will consider the above code as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function add(x, y){\n   console.log(x + y);\n}\nadd(5, 10); Output: 15<\/code><\/pre>\n\n\n\n<p>The <code><strong>add()<\/strong><\/code> function declaration is moved to the top before executing it.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> &#8220;Conceptually, for example, a strict definition of hoisting suggests that variable and function declarations are physically moved to the top of your code, but this is not in fact what happens. Instead, the variable and function declarations are put into memory during the&nbsp;<em>compile<\/em>&nbsp;phase, but stay exactly where you typed them in your code. &#8220;<\/p><\/blockquote>\n\n\n\n<p><strong>Temporal Dead Zone:<\/strong><\/p>\n\n\n\n<p>In ECMAScript 6 (ES6), accessing a <strong><code>let<\/code><\/strong> or <strong><code>const<\/code><\/strong> variable before its declaration within its scope causes a ReferenceError (i.e. does not support hoisting). The time span when that happens, between the creation of a variable&#8217;s binding and its declaration, is known as the Temporal Dead Zone.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(num); \/\/ ReferenceError: num is not defined\nlet num;<\/code><\/pre>\n\n\n\n<p>To learn more about <strong>let<\/strong>, <strong>const<\/strong> and other new features introduced in ES6 you may read <a rel=\"noreferrer noopener\" aria-label=\"this article (opens in a new tab)\" href=\"https:\/\/datatype.co.in\/blog\/what-is-new-in-es6\/\" target=\"_blank\">this article<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hoisting is important JavaScript concept introduced in ECMAScript 6 (ES6) where variables and function declarations (declared using var) are moved to the top of their scope before code execution. Inevitably,&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[235,233,234],"class_list":["post-766","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-hoisting-es6","tag-hoisting-in-javascript","tag-temporal-dead-zone-es6","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=766"}],"version-history":[{"count":16,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/766\/revisions"}],"predecessor-version":[{"id":783,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/766\/revisions\/783"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}