<?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>DEV Community: Gemahar Rafi</title>
    <description>The latest articles on DEV Community by Gemahar Rafi (@gemhar).</description>
    <link>https://dev.to/gemhar</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%2F177748%2F54f55fd1-fd88-4a17-8c3a-1761e7d1e970.jpg</url>
      <title>DEV Community: Gemahar Rafi</title>
      <link>https://dev.to/gemhar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gemhar"/>
    <language>en</language>
    <item>
      <title>Execution Context &amp; the Secret Life of Functions</title>
      <dc:creator>Gemahar Rafi</dc:creator>
      <pubDate>Sun, 30 Jun 2019 11:33:52 +0000</pubDate>
      <link>https://dev.to/gemhar/execution-context-the-secret-life-of-functions-1bl1</link>
      <guid>https://dev.to/gemhar/execution-context-the-secret-life-of-functions-1bl1</guid>
      <description>&lt;p&gt;Understanding the Execution Context in one of the most vitally fundamental parts of learning Javascript. This is because understanding the Execution Context paves the way to comprehend more advanced topics like hoisting, scope chains, and closures with clarity. So in this piece, I will be covering the complete life cycle of the Execution Context.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Execution Context?
&lt;/h2&gt;

&lt;p&gt;Execution Context is the way in which the javascript engine modularizes the process of interpreting and running our code; thereby reducing the complexity of the process. There are two types of Execution Context :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global Execution Context&lt;/li&gt;
&lt;li&gt;Function Execution Context&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Global Execution Context
&lt;/h2&gt;

&lt;p&gt;Whenever we run a javascript file, even in the case of an empty javascript file by default the engine creates the Global Execution Context for us. Initially, this Execution Context will consist of two things - a global object and a variable called '&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" rel="noopener noreferrer"&gt;this&lt;/a&gt;', and it will reference the global object which will be 'window' if you're running JavaScript in the browser or 'global' if you're running it in a Node environment.&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%2Fn0nf2uoyblcjq8vr49if.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%2Fn0nf2uoyblcjq8vr49if.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we have variables and functions in our Execution Context, the code undergoes a two-stage process by default and the two phases are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The global creation phase.&lt;/li&gt;
&lt;li&gt;The global executing phase.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Creation Phase will do the following:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a global object for us.&lt;/li&gt;
&lt;li&gt;Create an object called "&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" rel="noopener noreferrer"&gt;this&lt;/a&gt;" and initializes to window/global base on our, environment.&lt;/li&gt;
&lt;li&gt;Creates the Variable environment that is the memory space for variables and functions.&lt;/li&gt;
&lt;li&gt;Initializes all 'var' identifier declarations to "undefined" and places all function declarations in memory.&lt;/li&gt;
&lt;/ol&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%2Fu459a4xifwb20impw7ep.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%2Fu459a4xifwb20impw7ep.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the Execution phase, the JavaScript engine starts running our code line by line and executing it.&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%2Fc7ybal1b8wp82siagsd1.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%2Fc7ybal1b8wp82siagsd1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Note: 
&lt;/h3&gt;

&lt;p&gt;This process of assigning variable declarations a default value of 'undefined' during the creation phase is called &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="noopener noreferrer"&gt;Hoisting&lt;/a&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="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;firstName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//logs --&amp;gt;undefined &lt;/span&gt;

&lt;span class="c1"&gt;//it does not throw an error but logs --&amp;gt;undefined;&lt;/span&gt;
&lt;span class="c1"&gt;//this happens because of hoisting happening during the creation phase&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rick&lt;/span&gt;&lt;span class="dl"&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;firstName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//logs --&amp;gt;Rick&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function Execution Context
&lt;/h2&gt;

&lt;p&gt;Function Execution Context is almost exactly identical to the Global Execution Context with a little difference. Only whenever we invoke any function a function Execution Context is created.&lt;/p&gt;

&lt;p&gt;Even the Function Execution Context undergoes a two-stage process by default and the two phases are identical to the Global Execution Context they are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The creation phase.&lt;/li&gt;
&lt;li&gt;The executing phase.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creation Phase will do the following:
&lt;/h3&gt;

&lt;p&gt;Even in Function Execution Context's creation phase is similar to the Global Creation phase but it changes the first step so instead of creating a global object it creates the arguments object with the received arguments. So the steps happening in the creation phase are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an argument object with the received arguments.&lt;/li&gt;
&lt;li&gt;Create an object called '&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" rel="noopener noreferrer"&gt;this&lt;/a&gt;' and point to the callee or in the absence of callee the to the window/global object.&lt;/li&gt;
&lt;li&gt;Creates the Variable environment that is the memory space for local variables and functions.&lt;/li&gt;
&lt;li&gt;Initializes all 'var' identifier declarations to "undefined" and places all function declarations in memory.&lt;/li&gt;
&lt;/ol&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%2Fatxcrx19cx4c2ndj8zzc.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%2Fatxcrx19cx4c2ndj8zzc.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the Execution phase again, the JavaScript engine starts running our code line by line and executing it.&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%2Fbp93v422pw35e5dfntc6.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%2Fbp93v422pw35e5dfntc6.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After when a function execution is over, that is by implicit/explicit return statement the function Execution Context gets deleted with all its variable environment(not always there is a special case called closures that we will see later) and becomes garbage for the garbage collector.&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%2F5nvu7655etonexht22gk.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%2F5nvu7655etonexht22gk.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Call Stack
&lt;/h2&gt;

&lt;p&gt;The JavaScript engine creates an 'Execution Stack' (also known as the "Call Stack") with Global Execution Context as its first or bottom-most item. Anytime a function is invoked, a new Execution Context is created and added to the Execution Stack. Whenever a function is finished running through both the Creation and Execution phase, it gets popped off the Execution Stack.&lt;br&gt;
So a nested Execution Context will look like this:&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%2Fmtsdy5lyka61ksrrzeww.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%2Fmtsdy5lyka61ksrrzeww.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Secret Life of functions does not end here there are more interesting things like &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Scope" rel="noopener noreferrer"&gt;scopes&lt;/a&gt; and magical-like &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures" rel="noopener noreferrer"&gt;closures&lt;/a&gt;. Do check them out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/javascript" rel="noopener noreferrer"&gt;JavaScript | MDN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://frontendmasters.com/courses/javascript-hard-parts/" rel="noopener noreferrer"&gt;Javascript The Hard-parts by William Sentance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Images were taken using &lt;a href="https://tylermcginnis.com/javascript-visualizer/" rel="noopener noreferrer"&gt;JavaScript Visualizer&lt;/a&gt; by Tyler Mcginnis&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>executioncontext</category>
      <category>es6</category>
      <category>javascript</category>
    </item>
    <item>
      <title>var, let and const in JavaScript - Decoded...</title>
      <dc:creator>Gemahar Rafi</dc:creator>
      <pubDate>Sun, 23 Jun 2019 08:25:49 +0000</pubDate>
      <link>https://dev.to/gemhar/var-let-and-const-decoded-30k2</link>
      <guid>https://dev.to/gemhar/var-let-and-const-decoded-30k2</guid>
      <description>&lt;p&gt;While I was getting the hang of the fundamentals of JavaScript, I came across three ways of declaring a variable, that is by &lt;em&gt;var&lt;/em&gt;, &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; statements. So in this piece, I have tried to summarize all my findings to differentiate each of the declaration statements.&lt;/p&gt;

&lt;p&gt;To really get a grip on the differences between &lt;em&gt;var&lt;/em&gt;, &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; we must apprehend the following four concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variable declaration&lt;/li&gt;
&lt;li&gt;Variable initialization&lt;/li&gt;
&lt;li&gt;Scope&lt;/li&gt;
&lt;li&gt;Hoisting&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Variable declaration
&lt;/h2&gt;

&lt;p&gt;Variable declaration is the process of introducing a new identifier into our program; to be specific to our scope(I will talk about scopes later). In JavaScript, identifiers are by default given a value of undefined when declared with &lt;em&gt;var&lt;/em&gt; keyword(this is automatically done by the interpreter).&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;foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declaration&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs--&amp;gt;undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variable initialization
&lt;/h2&gt;

&lt;p&gt;Variable initialization is the process of assigning values to the identifier initially, so when we declare a binding with &lt;em&gt;var&lt;/em&gt; keyword the interpreter automatically initializes it to undefined.&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;foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//declaration&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs --&amp;gt;undefined&lt;/span&gt;

&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;something&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Initialization&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs --&amp;gt;something&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Scope
&lt;/h2&gt;

&lt;p&gt;The scope of a variable actually defines the context in which the variables and functions are accessible and can be referenced in a program. The scope defines the visibility and lifetime of variables and parameters. If a variable is not "in the current scope," then it is unavailable for use. Scopes can also be layered in a hierarchy, so that child scopes have access to parent scopes, but not vice versa.&lt;/p&gt;

&lt;p&gt;Basically, there are two types of scoping&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;function scope&lt;/li&gt;
&lt;li&gt;block scope&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  function scope:
&lt;/h3&gt;

&lt;p&gt;Variables declared inside a function are scoped to the function and all the subsequent nested function; irrespective of blocks;&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="nx"&gt;foo&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="kc"&gt;true&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;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;var inside the if block&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//logs --&amp;gt;var inside the if block&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; 
  &lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;var outside the if block&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//logs --&amp;gt;var outside the if block&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  block scope
&lt;/h3&gt;

&lt;p&gt;Variables declared inside a block are scoped only to its block and all the subsequent nested blocks but not outside of the block not even in the same function; blocks here include if...else blocks or looping blocks.&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="nx"&gt;bar&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="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;let inside the if block&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//logs --&amp;gt;let inside the if block&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Uncaught Reference Error: l is not defined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Hoisting:
&lt;/h2&gt;

&lt;p&gt;MDN defines Hoisting as :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Hoisting is a term you will not find used in any normative specification prose prior to ES2015 Language Specification. Hoisting was thought up as a general way of thinking about how execution contexts (specifically the creation and execution phases) work in JavaScript. However, the concept can be a little confusing at first.&lt;/p&gt;

&lt;p&gt;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 compile phase, but stay exactly where you typed them in your code."&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//logs --&amp;gt;undefined &lt;/span&gt;

&lt;span class="c1"&gt;//it does not throw an error but logs --&amp;gt;undefined;&lt;/span&gt;
&lt;span class="c1"&gt;//this happens because of hoisting&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;something&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//Initialization&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//logs --&amp;gt;something&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the above code, how JS interpreter evaluation can be simplified as :&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;foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Hoisted declaration of 'foo'&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="nx"&gt;logs&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;something&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//logs --&amp;gt;something&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;var&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;&lt;em&gt;var&lt;/em&gt;&lt;/em&gt; statement declares a variable, optionally initializing it to a value. Any variable declared with &lt;em&gt;var&lt;/em&gt; statement is function scoped and also identifies declared with &lt;em&gt;var&lt;/em&gt; keywords are hoisted and initialized with undefined&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//logs --&amp;gt;undefined&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//the above code does not throw an error because of hoisting;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;let&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;&lt;em&gt;let&lt;/em&gt;&lt;/em&gt; statement declares a local variable. Any variable declared with &lt;em&gt;let&lt;/em&gt; statement is block scoped. The identifies declared with &lt;em&gt;let&lt;/em&gt; keywords are hoisted and are not initialized&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;let&lt;/span&gt; &lt;span class="nx"&gt;foo&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Uncaught Reference Error: l is not defined&lt;/span&gt;

&lt;span class="c1"&gt;//the above code throws an error because identifiers declared with let keywords are not initialized;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;let&lt;/em&gt; bindings are created at the top of the (block) scope containing the declaration, commonly referred to as "hoisting". Unlike variables declared with var, which will start with the value undefined, let variables are not initialized until their definition is evaluated. Accessing the variable before the initialization results in a Reference Error.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;const&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;const&lt;/em&gt; statement declares a local variable very much like the &lt;em&gt;let&lt;/em&gt; statement but it has one added property and that is; they cannot be reassigned; meaning once the &lt;em&gt;const&lt;/em&gt; binding is initialized it cannot be reassigned with any other value.&lt;br&gt;&lt;br&gt;
Due to the above reason, a &lt;em&gt;const&lt;/em&gt; binding will always have to be initialized when declared otherwise it throws an error.&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;const&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;something&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;other thing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Uncaught TypeError: Assignment to constant variable.    &lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//Uncaught SyntaxError: Missing initializer in const declaration&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;br&gt;
One thing to observe here is that when we use &lt;em&gt;const&lt;/em&gt; binding to an object, the object itself cannot be changed and will continue to point at the same object, the contents of that object might change.&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;const&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;visitors&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;span class="na"&gt;home&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;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;visitors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This is okay  &lt;/span&gt;
&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;visitors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;home&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// Uncaught TypeError: Assignment to constant variable.&lt;/span&gt;
&lt;span class="c1"&gt;// This isn't allowed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  One last fun fact:
&lt;/h2&gt;

&lt;p&gt;Bindings that are declared in a function without a declaring keyword will become a global variable. Let me explain this 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funFact&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;isGloballyAvailable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;funFact&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isGloballyAvailable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To understand this we must go back to our hoisting concept, usually what happens is that whenever we initialize a variable in our code the interpreter goes and searches the hoisted variables and then assigns or reassigns the value of the variable, but when the interpreter cannot find the variable in the function it goes and searches in its parent function's hoisted variables and this process repeats until the global scope; &lt;/p&gt;

&lt;p&gt;In our case the interpreter will not find our 'isGloballyAvailable' binding even in the global scope so, the interpreter automatically adds the variable to the global scope.&lt;/p&gt;

&lt;p&gt;This is an extremely dangerous process and must be avoided at all cost; so keep in mind that we must not declare a binding without the: &lt;em&gt;var&lt;/em&gt;, &lt;em&gt;let&lt;/em&gt; or &lt;em&gt;const&lt;/em&gt; keyword anywhere in our code.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;So when should we use &lt;em&gt;var&lt;/em&gt;, &lt;em&gt;let&lt;/em&gt; or &lt;em&gt;const&lt;/em&gt; ?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;ES2015 (ES6) introduced &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt;, why would the JavaScript designers introduce them? maybe to fix some issue with &lt;em&gt;var&lt;/em&gt; or maybe for better readability... right? &lt;/p&gt;

&lt;p&gt;One major issue with &lt;em&gt;var&lt;/em&gt; is that it allows for re-declarations in code, which does not throw errors, which can create unintended side-effects in your code. &lt;/p&gt;

&lt;h3&gt;
  
  
  The popular and as well as my opinion is that :
&lt;/h3&gt;

&lt;p&gt;We should always prefer &lt;em&gt;const&lt;/em&gt; if the value assigned to our variable is not going to change, this tells the future developers that the identifier has a constant value.&lt;br&gt;
On the other hand use &lt;em&gt;let&lt;/em&gt; if the identifier needs to change its value later on, but I do not see a use case in which we should use &lt;em&gt;var&lt;/em&gt;. &lt;/p&gt;

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