<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Guilherme Scotti</title>
    <description>The latest articles on Forem by Guilherme Scotti (@scottibr).</description>
    <link>https://forem.com/scottibr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F211277%2F5b0d40aa-3cc8-44d2-b871-6ac2cea810aa.png</url>
      <title>Forem: Guilherme Scotti</title>
      <link>https://forem.com/scottibr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/scottibr"/>
    <language>en</language>
    <item>
      <title>JavaScript Demystified: Unveiling the Code Wizardry - Hoisting</title>
      <dc:creator>Guilherme Scotti</dc:creator>
      <pubDate>Sat, 17 Feb 2024 20:52:44 +0000</pubDate>
      <link>https://forem.com/scottibr/javascript-demystified-unveiling-the-code-wizardry-hoisting-3p5a</link>
      <guid>https://forem.com/scottibr/javascript-demystified-unveiling-the-code-wizardry-hoisting-3p5a</guid>
      <description>&lt;p&gt;People commonly explain hoisting as &lt;strong&gt;declarations of variables and functions being moved to the top of your code&lt;/strong&gt;. At the same time, this appears to be happening; it’s essential to understand precisely what is happening. Because that definition &lt;strong&gt;is a Myth&lt;/strong&gt;, a convention created to discuss the idea of lexical environment without so much overhead.&lt;/p&gt;

&lt;p&gt;Before the ECMAScript® 2015 Language Specification, "hoisting" was not used in any normative specification.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the function was called before its definition. People often claim that the function is "lifted" to the beginning of the code, but it works due to function prototypes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But that's incorrect, there's no such thing as lifting functions and variables, you see, the code isn’t moving anywhere. It isn’t magically being moved to the top of the file. At the compiler phase, all functions and variable declarations are &lt;strong&gt;Hoisted&lt;/strong&gt; at the Memory Heap, called the Lexical Environment. Then, the Engine will execute the code from top to bottom, adding every scope at the Execution Call Stack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Hoisting&lt;/em&gt; refers to the default behaviour of Javascript to process and put all variables and function declarations into memory first during the compile phase of its execution context, regardless of where they are written in the code. &lt;em&gt;Maya Shavin&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Lexical Environment&lt;/em&gt; is a data structure with identifier-variable mapping on the memory heap.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frlfc71uocpudolg0ny8l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frlfc71uocpudolg0ny8l.png" alt="Code Compile and Executon"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Difficult? Too many concepts? let's make it more simple. A few seconds before your code is executed, the compiler will go through every line, collecting variables and function declarations and storing those in the memory to optimise and utilise even before his declaration in the source code. In other words, the lexical environment is a fancy way to say "local memory scope".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fyhpzy8uvjp5ls966n181.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fyhpzy8uvjp5ls966n181.gif" alt="Execution Context animated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's see if those concepts can be put into practice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;say&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hello&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// world is undefined&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;say&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, in this example, the compiler will run through the code and find the function &lt;code&gt;say&lt;/code&gt; and save his reference into the memory, making it available to be called in the execution phase, returning &lt;code&gt;'Hello'&lt;/code&gt;, but why the &lt;code&gt;world&lt;/code&gt; variable is undefined?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JavaScript only hoists variable declarations. &lt;br&gt;
Initializations are not hoisted.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the variable declaration &lt;code&gt;world&lt;/code&gt; will be hoisted at the memory. In the execution phase, the variable is called, but since it isn't initialized yet, the returned value is &lt;code&gt;undefined&lt;/code&gt;; here's another example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;x is not defined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Defined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//result "x is not defined"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the &lt;code&gt;x&lt;/code&gt; variable is stored in memory during the compile phase but with the value of &lt;code&gt;undefined&lt;/code&gt;, because the &lt;strong&gt;assignment of value to a variable happens only in the execution phase&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;So what happens is something like this before the &lt;strong&gt;execution phase&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;x is not defined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Defined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//result "x is not defined"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see what happens if we swap the declarations of the variable 'x'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Defined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;x is not defined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//result "Defined"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time, the variable &lt;code&gt;x&lt;/code&gt; will be hoisted/lifted by the compiler with the value of &lt;code&gt;undefined&lt;/code&gt;, but when the execution phase starts, the &lt;code&gt;x&lt;/code&gt; variable will be evaluated at the first line with the value of &lt;code&gt;Defined&lt;/code&gt; making possible to run the else condition.&lt;/p&gt;

&lt;p&gt;There's also a lot of confusion about undefined and undeclared variables (Reference Error). When a variable is undefined, it means that the variable has been declared at the compiler phase but not initialized, and undeclared are variables that weren't declared in the lexical environment or the JS Engine doesn't find an available reference for them.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens with ES6 syntax?
&lt;/h2&gt;

&lt;p&gt;So, what happens with all the features of ES6, like &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;? Are they hoisted? let's see&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//ReferenceError: x is not defined&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;All declarations in JavaScript function, var, let, const, even classes, are hoisted at the compiler phase&lt;/strong&gt;, but while the var declarations are initialized with undefined, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; declarations remain uninitialized, so where it's at the first line of the example? he was in a place that we call "&lt;strong&gt;Temporal Dead Zone&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2FvwT1bQ8zojmWQFx297%2Fgiphy.gif%3Fcid%3D790b76116e2deb8ea24a2671b46cc66742667737a619f163%26rid%3Dgiphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2FvwT1bQ8zojmWQFx297%2Fgiphy.gif%3Fcid%3D790b76116e2deb8ea24a2671b46cc66742667737a619f163%26rid%3Dgiphy.gif" alt="TDZ"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;No, nothing like that, let's see what ECMAScript 2015 spec tells us about &lt;em&gt;TDZ&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE 13.2.1&lt;/strong&gt; Let and const declarations define variables scoped to the running execution context’s Lexical Environment. The variables are created when their containing Lexical Environment is instantiated &lt;strong&gt;but may not be accessed in any way until the variable’s LexicalBinding is evaluated&lt;/strong&gt;. A variable defined by a LexicalBinding with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the LexicalBinding is evaluated, not when the variable is created. If a LexicalBinding in a let declaration does not have an Initializer, the variable is assigned the value undefined when the LexicalBinding is evaluated.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are only initialized when their assignment of value is evaluated, and that happens during the execution phase, so in the example, the &lt;code&gt;x&lt;/code&gt; variable will be evaluated only after called by &lt;code&gt;console.log&lt;/code&gt;, that's why throws a ReferenceError.&lt;/p&gt;

&lt;p&gt;Now, let's see what happens when we introduce the block scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Guilherme&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// ReferenceError meaning TDZ error&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Scotti&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the global scope has a &lt;code&gt;name&lt;/code&gt; variable with the value of 'Guilherme', but when the Engine enters the block scope, it creates another execution context and hoists the variable &lt;code&gt;name&lt;/code&gt; into the TDZ of this block context. So, when the &lt;code&gt;console.log&lt;/code&gt; runs, the variable &lt;code&gt;name&lt;/code&gt; is unreachable.&lt;/p&gt;

&lt;p&gt;To summarise, TDZ means don't touch that until it is initialized.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2FCvsQzv9hZe2Ry%2Fgiphy.gif%3Fcid%3D790b7611cd36279a241394a3cc080910cf7b857e101a6a6a%26rid%3Dgiphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2FCvsQzv9hZe2Ry%2Fgiphy.gif%3Fcid%3D790b7611cd36279a241394a3cc080910cf7b857e101a6a6a%26rid%3Dgiphy.gif" alt="don't touch that"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The whole point of the TDZ is to make it easier to catch errors where accessing a variable before it’s declared in user code leads to unexpected behaviour. This happened a lot with ES5 due to hoisting and poor coding conventions. In ES6 it’s easier to avoid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Expressions or Declarations
&lt;/h2&gt;

&lt;p&gt;There are three ways to define a function in JavaScript: a function declaration (assigning the function value into a variable), function expression, and ES6 Arrow Functions. So, are they all Hosted equally?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// "Hello"&lt;/span&gt;
&lt;span class="nf"&gt;world&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// TypeError: world is not a function&lt;/span&gt;
&lt;span class="nf"&gt;exclamation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// ReferenceError: exclamation is not defined&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;exclamation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We already know that Function Declarations are Hoisted during the compile phase, so the &lt;code&gt;hello()&lt;/code&gt; function is ready to be called at line 1 because it's a function declaration.&lt;/p&gt;

&lt;p&gt;At line 2, we call the function &lt;code&gt;world()&lt;/code&gt;, but actually, we are calling the variable &lt;code&gt;world&lt;/code&gt; that was hoisted with the same rule as any other variable with the default value of &lt;code&gt;undefined&lt;/code&gt;. Therefore, when the execution reached line 2 and tried to call &lt;code&gt;world()&lt;/code&gt;, it failed returning a &lt;code&gt;TypeError&lt;/code&gt;, because undefined is not a function!&lt;/p&gt;

&lt;p&gt;That also happens with the arrow function, but with a little difference: ES6 Syntax is initialized only at value evaluation on the execution phase, so there's the Reference error because of the &lt;strong&gt;const&lt;/strong&gt; declaration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is an important foundation in the understanding of how JavaScript works under the hood, and the main conclusion that can be drawn is that &lt;strong&gt;all declarations in JavaScript function, var, let, const, even classes, are hoisted at the compiler phase, but with different assignments of value&lt;/strong&gt;. Variables are hoisted with the value of &lt;code&gt;undefined&lt;/code&gt;, except &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;, which are only initialized when their assignment of value is evaluated, and that happens during the execution phase; before that, they are in &lt;strong&gt;TDZ&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We also saw that function declarations are hoisted and evaluated normally, but function expressions are treated precisely like variables. I hope you guys can glimpse some functionalities that run on our daily code in JS.&lt;/p&gt;

&lt;p&gt;Now that you know more about Hoisting, it’s time to move to dive Deeper with &lt;a href="https://dev.to/scottibr/javascript-demystified-unveiling-the-code-wizardry-from-compilation-to-execution-dcf"&gt;Javascript Demystified Series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to dive deeper into some concepts, visit the &lt;a href="https://github.com/ScottiBR/my-articles/blob/master/JavaScript/Hoisting/References.md" rel="noopener noreferrer"&gt;References&lt;/a&gt;. Please post any feedback, questions, or requests for topics. I would also appreciate 👏 if you like the post so that others can find this too.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript Demystified:  From Compilation to Execution</title>
      <dc:creator>Guilherme Scotti</dc:creator>
      <pubDate>Sat, 17 Feb 2024 20:03:57 +0000</pubDate>
      <link>https://forem.com/scottibr/javascript-demystified-unveiling-the-code-wizardry-from-compilation-to-execution-dcf</link>
      <guid>https://forem.com/scottibr/javascript-demystified-unveiling-the-code-wizardry-from-compilation-to-execution-dcf</guid>
      <description>&lt;p&gt;We must examine how our code runs when executed to understand better how JavaScript works under the hood. By the end of this post, you should understand what execution context is and why it is essential to understand some core principles of JavaScript, like Hoisting and Closure.&lt;/p&gt;

&lt;p&gt;[TL;DR] This explores JavaScript execution and the debate of whether it is compiled or interpreted. JavaScript engines manage code execution through Execution Context and define environments. The compilation phase creates Scope Chain and hoisting while the execution phase brings the code to life.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does JavaScript work under the hood?
&lt;/h2&gt;

&lt;p&gt;How is JavaScript executed in browsers? Is it compiled or interpreted? There is a lot of debate about whether JavaScript is a compiled or hybrid language. However, one reliable specification is available.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JavaScript is a lightweight, &lt;strong&gt;interpreted&lt;/strong&gt; language &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'm unfamiliar with compilers so that I won't delve into details. However, this knowledge is essential to better understand some of the language's logic.&lt;/p&gt;

&lt;p&gt;There is a temptation to think that JavaScript code is interpreted line-by-line, top-down in order, as the program executes and therefore not compiled. However, while that is substantially true, one part of that assumption can lead to incorrect thinking about the language, and that's what we are going to see.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JavaScript is an interpreter-agnostic language&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Therefore, the language is interpreted, but there isn't an official JS Engine designed for the language. Web Browser vendors created a lot of Engines, which essentially make all the magic happen.&lt;br&gt;
But that concept diverges a lot; some academics suggest that it is a &lt;strong&gt;compiled&lt;/strong&gt; language, and many blogs and posts say that it is a &lt;strong&gt;hybrid&lt;/strong&gt; language. The official standard specification says:&lt;/p&gt;

&lt;p&gt;Therefore the language is interpreted, but there isn't an official JS Engine for the language, and the community created a lot of Engines to suppress that need.&lt;/p&gt;
&lt;h2&gt;
  
  
  JS Engines
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff245q94qb8yblvdjmcf7.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff245q94qb8yblvdjmcf7.PNG" alt="JS Engines"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While each JavaScript engine varies in its implementation details, the intention here is not to delve too deeply into those specifics; instead, we will focus on the broader aspects of code execution. These engines play a pivotal role in interpreting JavaScript code, yet they operate under a distinct constraint compared to languages like Java or C#. Unlike languages that undergo extensive compilation before execution, JavaScript engines, whether in a browser or Node.js environment, do not have the luxury of ample compilation time.&lt;/p&gt;

&lt;p&gt;As JavaScript code runs, the engine initiates the processing sequence, navigating through a series of steps, with one critical phase being the Execution Context. This context encompasses the environment in which the code is executed, including variables, functions, and the scope chain. Understanding the intricacies of the Execution Context is vital for developers seeking to optimize their code for performance.&lt;/p&gt;
&lt;h2&gt;
  
  
  Execution Context
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Execution context is defined as the environment in which the JavaScript code is executed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When code is run in JavaScript, the environment in which it is executed is very important. By the environment, I mean variables, objects, this, functions, and every Execution Context in JavaScript must be one of these three types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Global Context&lt;/strong&gt;: The main execution context in which JS code starts its execution when the bundle first loads at the browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function Context&lt;/strong&gt;: This is the context created by executing the code inside a function, and that environment has access to the global Environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eval&lt;/strong&gt;: An execution context confined within the eval function adds complexity to the script. While subtle, Eval introduces a unique dynamic, influencing the course of execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, to create these kinds of contexts, the Engine must go through a series of stages, and we are going to focus on the two most important stages&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Compilation / Creation Phase&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution Phase&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the compilation phase, the Engine scans the code line-by-line, top-down, to create the Scope Chain, Hoist variables and functions and determine the value of "this". Essentially, all that happens a few microseconds before the execution phase, thus leading to the misunderstanding between interpreted or compiled language, knowing that it's essential to understand some key concepts of how the language works.&lt;/p&gt;

&lt;p&gt;After the compilation phase is complete, the execution of the code starts. Let's visualize it with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//The code simply calls itself 3 times, incrementing the value of ´i´ by 1.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;recursive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;recursive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;recursive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdavidshariff.com%2Fblog%2Fwp-content%2Fuploads%2F2012%2F06%2Fes1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdavidshariff.com%2Fblog%2Fwp-content%2Fuploads%2F2012%2F06%2Fes1.gif" alt="Execution Context"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every time a function is about to be executed, a context is created for it and added to the Stack. Once all the function's code is executed, JS engines pop out that function. Each function call creates a new context, creating a Lexical Scope where anything declared inside the function can not be directly accessed from outside the current function scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding the inner workings of JavaScript execution is crucial for developers. The debate over whether JavaScript is compiled or interpreted has nuances, and it's essential to grasp the role of JavaScript engines in executing code. JavaScript is an interpreter-agnostic language. Therefore, the language is interpreted, but there isn't an official JS Engine designed for the language; due to that, Web Browser vendors created many Engines responsible for interpreting the code and creating the Execution context, which is the environment where the code is executed.&lt;/p&gt;

&lt;p&gt;Those environments are created in two stages, the Compilation and the Execution stage, and they are responsible for creating the Scope Chain, hoisting variables and functions, and executing the code.&lt;/p&gt;

&lt;p&gt;The heart of this process lies in the Execution Context, where the environment for code execution is defined, comprising Global, Function, and Eval contexts. Delving into the compilation and execution phases unveils the intricacies of creating the Scope Chain, hoisting variables and functions, and determining the value of "this."&lt;/p&gt;

&lt;p&gt;Now that you know more about Execution Context, it's time to move to the next chapter of this series of articles and learn more about &lt;a href="https://dev.to/scottibr/javascript-demystified-unveiling-the-code-wizardry-hoisting-3p5a"&gt;Hoisting&lt;/a&gt;. did you know the term "hoisting" didn't exist at EcmaScript Specification until the ES6 release?&lt;/p&gt;

&lt;p&gt;If you want to dive deeper into some concepts, visit the &lt;a href="https://github.com/ScottiBR/my-articles/blob/master/JavaScript/Hoisting/References.md" rel="noopener noreferrer"&gt;References&lt;/a&gt;. Please post any feedback, questions, or requests for topics. I would also appreciate 👏 if you like the post so that others can find this too.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>showdev</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
