<?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: Sobit Prasad</title>
    <description>The latest articles on DEV Community by Sobit Prasad (@sobitp59).</description>
    <link>https://dev.to/sobitp59</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%2F741185%2F24fdc6ee-50fc-45b6-9da3-36394bae4db2.jpg</url>
      <title>DEV Community: Sobit Prasad</title>
      <link>https://dev.to/sobitp59</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sobitp59"/>
    <language>en</language>
    <item>
      <title>How Asynchronous JavaScript Works: A Deep Dive into Its Execution Process</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Fri, 24 Mar 2023 08:23:31 +0000</pubDate>
      <link>https://dev.to/sobitp59/how-asynchronous-javascript-works-a-deep-dive-into-its-execution-process-3o9g</link>
      <guid>https://dev.to/sobitp59/how-asynchronous-javascript-works-a-deep-dive-into-its-execution-process-3o9g</guid>
      <description>&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;If you are an aspiring JavaScript developer, you will likely be asked whether JavaScript is synchronous or asynchronous.&lt;/p&gt;

&lt;p&gt;And when you look around for the answers, you might get mixed answers for it. Some considered that JavaScript is synchronous while others say that it is asynchronous.&lt;/p&gt;

&lt;p&gt;Then, what is the correct answer? Is it synchronous or asynchronous? Well, at its base JavaScript is synchronous but somehow it can be manipulated to work asynchronously.&lt;/p&gt;

&lt;p&gt;But, how does it all happens? So, In this blog, we will dive deep and will try to understand how JavaScript is executed under the hood asynchronously.&lt;/p&gt;

&lt;h1&gt;
  
  
  Understanding Synchronous and Asynchronous
&lt;/h1&gt;

&lt;p&gt;In JavaScript, both synchronous and asynchronous are different ways of executing the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Synchronous
&lt;/h2&gt;

&lt;p&gt;Synchronous codes are executed in order, one statement at a time. Each statement must finish its execution before the next one is executed.&lt;/p&gt;

&lt;p&gt;Think of it as a running race track, but only with one single track for all the athletes. Once the athlete at the very first reaches the finish line, then only the next athlete can start the race and so on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zz4lamBg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n19fhzt0wrxw7w1pbgro.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zz4lamBg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n19fhzt0wrxw7w1pbgro.png" alt="Image description" width="880" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous
&lt;/h2&gt;

&lt;p&gt;Asynchronous codes are executed out of order, multiple statements at a time. All the statements can be executed parallelly.&lt;/p&gt;

&lt;p&gt;Think of it as a regular running race track, each track for each athlete. Here, the athletes don't have to wait for the other athletes to start the race.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R4Mxf-Qk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3udhdqucjh6b59czgiy6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R4Mxf-Qk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3udhdqucjh6b59czgiy6.png" alt="Image description" width="880" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Understanding JavaScript Engine
&lt;/h1&gt;

&lt;p&gt;JavaScript engines are programs that help in executing the JavaScript code. They are typically responsible for compiling the JavaScript code from human-readable to machine-readable instructions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1OaN2yPt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5s3ev60hc9ivuqd26rm5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1OaN2yPt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5s3ev60hc9ivuqd26rm5.png" alt="Image description" width="880" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are several JavaScript Engines. Some of the most popular ones are :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;V8&lt;/strong&gt;: It was developed by Google for use in the Chrome browser and is also used in Node.js.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SpiderMonkey&lt;/strong&gt;: It was developed by Mozilla for use in Firefox.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chakra&lt;/strong&gt;: It was developed by Microsoft for use in the Edge browser.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How JavaScript Engine Compiles the Code?
&lt;/h2&gt;

&lt;p&gt;JavaScript Engines have to go through multiple phases to compile the code from human-readable format to machine-readable format. So that, the computer can understand and executes the code.&lt;/p&gt;

&lt;p&gt;When we give our code to JavaScript Engines, it has to go through - &lt;em&gt;parsing&lt;/em&gt;, &lt;em&gt;compilation&lt;/em&gt; and &lt;em&gt;execution&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y2acw32L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lboyyeb2qtdt0b8dgfg3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y2acw32L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lboyyeb2qtdt0b8dgfg3.png" alt="Image description" width="880" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every JavaScript Engine might have different algorithms to execute the code. However, the fundamentals of how JavaScript code is parsed, compiled, and executed are generally the same across different JavaScript engines.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Parsing Phase&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;During parsing, JavaScript Engines break down the code into its tokens such as keywords, identifiers, or operators. These tokens are used to form the Abstract Syntax Tree(AST) which is the high-level representation of the source code.&lt;/p&gt;

&lt;p&gt;Let's take an example and see how it looks when it gets converted to Abstract Syntax Tree.&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;hw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello world&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;hw&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will be broken into individual tokens during a process called tokenization.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9-WOCfie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84cdd805xjts62jcux50.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9-WOCfie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84cdd805xjts62jcux50.png" alt="Image description" width="880" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These tokens and the source code are then used to generate the Abstract Syntax Tree(AST).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mMGfeK1M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nscogb1zib40s06w9zsq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mMGfeK1M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nscogb1zib40s06w9zsq.png" alt="Image description" width="880" height="617"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See Abstract Syntax Tree in JSON format below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/sobitp59/2edf4ade0a749dd5be9c2a5e4c118e16"&gt;open GitHub Gist&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Compilation Phase&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;During compilation, JavaScript Engine takes the Abstract Syntax Tree(AST) and turns it into low-level bytecode that the computer understands. Let's see how it does.&lt;/p&gt;

&lt;p&gt;In the beginning, JavaScript Engines uses interpreters to compile the code. The interpreter makes debugging easier because it stops and updates whenever it finds an error. But the interpreter works slower as it has to translate the code line-by-line.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An interpreter reads and executes the code line by line. As each line gets interpreted, it is executed by the computer, and the interpreter proceeds to the next line and so on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To overcome the interpreter's inefficiency, JavaScript Engines started using compilers along with interpreters. Compilers are fast but debugging the code was slower.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A compiler translates the entire source code into machine language before it is executed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this is what we called a Just-in-Time(JIT) compilation. JIT uses the best of both interpreter and compiler. JIT compilers make debugging easier because of the interpreter and faster because of compilers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S4YDhtBW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3s61ik8lcvi585z37xl7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S4YDhtBW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3s61ik8lcvi585z37xl7.png" alt="Image description" width="880" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Execution Phase&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In most JavaScript engines, the compilation and execution of code happen concurrently and hand-in-hand, rather than in a strict sequential order.&lt;/p&gt;

&lt;p&gt;As the code gets compiled, the JavaScript engine &lt;strong&gt;starts the execution&lt;/strong&gt; before it has finished compiling the entire program and this is possible because of the JIT compiler.&lt;/p&gt;

&lt;p&gt;In the execution phase, we have three important components - the &lt;strong&gt;call stack&lt;/strong&gt;, the &lt;strong&gt;memory heap&lt;/strong&gt; and the &lt;strong&gt;garbage collector&lt;/strong&gt;. Let's understand each of them in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding CallStack and Memory Management
&lt;/h2&gt;

&lt;p&gt;When the code reaches the execution phase, it is the job of the call stack, memory heap and garbage collector to ensure that the programs run smoothly and efficiently.&lt;/p&gt;

&lt;p&gt;So, what are their jobs, let's understand each of them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mp-3DrLF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65hbgyuzv4mecyomjcdo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mp-3DrLF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65hbgyuzv4mecyomjcdo.png" alt="Image description" width="880" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Understanding Call Stack&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When we say JavaScript is a &lt;strong&gt;single-threaded language&lt;/strong&gt;, it means that it has only &lt;strong&gt;one call stack&lt;/strong&gt;. The call stack can do only one thing at a time.&lt;/p&gt;

&lt;p&gt;The job of the call stack is to track the execution context of a running program and it does by keeping track of all the functions that currently running.&lt;/p&gt;

&lt;p&gt;It is based on Last-In-First-Out(LIFO) data structure, which means that the most recently called function is the first to be executed and the last to be completed.&lt;/p&gt;

&lt;p&gt;So, whenever there’s a function call it gets pushed to the top of the stack. After it finishes executing it is removed from the top of the stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Understanding Memory Management&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;JavaScript Engines manage the memory for us, removing the burden of explicitly allocating and deallocating memory unlike we do in other programming languages like C or C++. Let's understand in brief how JavaScript Engines manage memory.&lt;/p&gt;

&lt;p&gt;The three phases of the memory life cycle are allocation, utilization, and deallocation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BKNkIaTw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgreb1sotph1a7n41j3e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BKNkIaTw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgreb1sotph1a7n41j3e.png" alt="Image description" width="880" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;allocation&lt;/strong&gt;: reserving memory for a particular object or data structure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;utilization&lt;/strong&gt;: use of that memory by the program, which may involve reading from or writing to the memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;deallocation&lt;/strong&gt;: releasing the memory when it is no longer needed, to make it available for other uses.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript Engines can allocate memory in two places, either on the stack or a memory heap.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stack
&lt;/h3&gt;

&lt;p&gt;All the primitive values like &lt;em&gt;strings&lt;/em&gt;, &lt;em&gt;numbers&lt;/em&gt;, &lt;em&gt;booleans&lt;/em&gt;, &lt;em&gt;undefined&lt;/em&gt;, and &lt;em&gt;null&lt;/em&gt; are stored on the stack.&lt;/p&gt;

&lt;p&gt;When we pass a variable having primitive values as a reference to a new variable, a copy of the value is passed to the new variable. So, changing the value of the new variable will not affect the one that is passed as a reference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qWt-6Hf2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2res9yun5xazphe8h5r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qWt-6Hf2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2res9yun5xazphe8h5r.png" alt="Image description" width="880" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We have assigned &lt;code&gt;buy vegetables&lt;/code&gt; (a primitive value) to variable &lt;code&gt;todoOne&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we created another variable &lt;code&gt;todoTwo&lt;/code&gt; and assigned it with the variable &lt;code&gt;todoOne&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here, the copy of the value of the variable &lt;code&gt;todoOne&lt;/code&gt; i.e. &lt;code&gt;buy vegetables&lt;/code&gt; is assigned to the variable &lt;code&gt;todoTwo&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, we redeclared the variable &lt;code&gt;todoTwo&lt;/code&gt; with &lt;code&gt;exercise : 500 skipping&lt;/code&gt;. Here, re-declaring the variable &lt;code&gt;todoTwo&lt;/code&gt; will not affect the variable &lt;code&gt;todoOne&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So, when we consoled the variable &lt;code&gt;todoOne&lt;/code&gt;, its value remained the same.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Memory Heap
&lt;/h3&gt;

&lt;p&gt;All non-primitive values like arrays, objects or functions are first stored on the heap and then passed as a reference to the stack.&lt;/p&gt;

&lt;p&gt;When we pass a variable having non-primitive values as a reference to a new variable, the reference of the memory location is passed to the new variable. So, changing the value of the new variable will also affect the one that is passed as a reference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c78SVBx9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9znm7v4z8bw4n5r3g4r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c78SVBx9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9znm7v4z8bw4n5r3g4r.png" alt="Image description" width="880" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We have assigned an object (a non-primitive value) to variable &lt;code&gt;userOne&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, we created another variable &lt;code&gt;userTwo&lt;/code&gt; and assigned it to variable &lt;code&gt;userOne&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here, the reference of the variable &lt;code&gt;userOne&lt;/code&gt; will be assigned and not a copy of it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So, when we changed the property &lt;code&gt;name&lt;/code&gt; of the variable &lt;code&gt;userTwo&lt;/code&gt; , the property(&lt;code&gt;name&lt;/code&gt;) of variable &lt;code&gt;userOne&lt;/code&gt; also got changed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we also need to de-allocate the memory that is not in use, and this is done by &lt;strong&gt;Garbage Collector&lt;/strong&gt;. It works by periodically finding and removing the objects that are no longer referenced by the program.&lt;/p&gt;

&lt;p&gt;Till now, we have only understood how JavaScript Engine works under the hood from parsing, compiling, and executing to memory management. But where does it all happen? And that's what we will be learning in the next section.&lt;/p&gt;

&lt;h1&gt;
  
  
  Understanding JavaScript Runtime Environment
&lt;/h1&gt;

&lt;p&gt;As human beings, we need some environment to work effectively. For example, to study, we need a quiet environment, books, etc. To cook food we need a kitchen provided it has all the necessary items to cook food like gas stoves, groceries, water, etc.&lt;/p&gt;

&lt;p&gt;It also applies to JavaScript Engine as well. JavaScript Engine cannot work independently, it needs some environment. And that environment is called &lt;strong&gt;JavaScript Runtime Environment(JRE)&lt;/strong&gt; for JavaScript Engines.&lt;/p&gt;

&lt;p&gt;JavaScript Runtime Environment(JRE) provides all the necessary components to JavaScript Engine so that it can work effectively. &lt;strong&gt;And you know what, it's the&lt;/strong&gt; &lt;strong&gt;JRE only that manipulates JavaScript to work asynchronously&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;First, let's discuss the important components of the JavaScript Runtime Environment and then we will discuss how these components work together with JavaScript Engine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Components of JavaScript Runtime Environment
&lt;/h2&gt;

&lt;p&gt;The core components of the JavaScript Runtime Environment(JRE) are JavaScript Engine itself, which we have discussed earlier and the &lt;strong&gt;Web APIs&lt;/strong&gt;, &lt;strong&gt;microtask queue&lt;/strong&gt;, &lt;strong&gt;callback queue&lt;/strong&gt;, and the &lt;strong&gt;event loop&lt;/strong&gt;. Let's discuss each of them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NPMkfDhX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdn1qc36g3fkqo13sw96.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NPMkfDhX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdn1qc36g3fkqo13sw96.png" alt="Image description" width="880" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Web APIs
&lt;/h3&gt;

&lt;p&gt;Web APIs are a set of built-in interfaces and functionalities provided by the Javascript Runtime Environment(JRE) to interact with external systems like servers, databases and other web services.&lt;/p&gt;

&lt;p&gt;These Web APIs help in &lt;strong&gt;handling asynchronous operations&lt;/strong&gt; like making a network request, processing large amounts of data, animating elements on a web page, etc.&lt;/p&gt;

&lt;p&gt;There are several built-in interfaces and functionalities, let's discuss some of them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model"&gt;DOM API&lt;/a&gt;: Document Object Model(DOM) API helps in accessing and manipulating the elements and attributes of an HTML or XML document in real-time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Console_API"&gt;Console API&lt;/a&gt;: Console API helps in performing debugging tasks, such as logging messages or the values of variables at set points in your code, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API"&gt;Fetch API&lt;/a&gt;: Fetch API helps in making HTTP requests to retrieve resources from servers without blocking the main thread.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/setTimeout"&gt;setTimeOut&lt;/a&gt;: This method allows us to schedule the execution of a function after a certain amount of time has passed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Callback Queue and Microtask Queue
&lt;/h3&gt;

&lt;p&gt;A callback queue is a data structure that stores a queue of callback functions waiting to be executed. It is based on First-In-First-Out(FIFO) data structure which means the first in the queue will be the first to execute.&lt;/p&gt;

&lt;p&gt;A microtask queue also stores a queue of callback functions waiting to be executed but the one with the &lt;strong&gt;higher priority&lt;/strong&gt;. The callback functions with higher priority are the ones that came through &lt;strong&gt;Promises&lt;/strong&gt;, &lt;strong&gt;mutation observer&lt;/strong&gt;, &lt;strong&gt;queueMicrotask&lt;/strong&gt;, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Event Loop
&lt;/h3&gt;

&lt;p&gt;Event Loop helps in executing the callback functions that are waiting on the callback queue. It does this by constantly checking the call stack and the event queues(callback queue and microtask queue).&lt;/p&gt;

&lt;p&gt;The event loop checks if the call stack is empty or not and if it is then it checks if any callback functions are waiting to be executed in the callback queue or microtask queue.&lt;/p&gt;

&lt;p&gt;And if there are any callback functions to be executed, it takes the first callback function from the queue and pushes it to the call stack.&lt;/p&gt;

&lt;p&gt;The event loop then waits for the call stack to be emptied again so that it can push the next callback function in the queue. It does until all the callback functions are executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  How JavaScript Runtime Environment Works?
&lt;/h2&gt;

&lt;p&gt;So far, we have discussed and understood the JavaScript Engine and the core components of the JavaScript Runtime Environment(JRE). But how do all these work together? Let's find out with the help of an example.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;When the JavaScript Engine goes through the code, it first creates a &lt;strong&gt;Global Execution Context(GEC)&lt;/strong&gt; and put it on the Call Stack. The global execution context includes information about the global scope, such as global variables and functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And whenever a function is invoked it creates a new &lt;strong&gt;Execution Context(FEC)&lt;/strong&gt; for that function and put it on the Call Stack. The function execution context includes information about the local scope of the function, such as its arguments and local variables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All synchronous operations must complete their execution before the asynchronous operation gets executed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the example below, we have defined four functions &lt;code&gt;greetUser()&lt;/code&gt;, &lt;code&gt;delayedBy20ms()&lt;/code&gt;, &lt;code&gt;getMovie()&lt;/code&gt; and &lt;code&gt;letMeRun()&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;greetUser()&lt;/code&gt; function has a variable &lt;code&gt;message&lt;/code&gt; assigned with &lt;code&gt;hello user! welcome to moviehub&lt;/code&gt; and simply consoling the value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;delayedBy20ms()&lt;/code&gt; function returning &lt;code&gt;setTimeOut&lt;/code&gt; method to delay the execution by 20 milliseconds.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Syntax of setTimeout:&lt;/p&gt;

&lt;p&gt;setTimeout(callbackFunction, delayInMilliseconds);&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;callbackFunction&lt;/strong&gt;- a function containing a block of code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;delayInMilliseconds&lt;/strong&gt;- the time after which the function is executed&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;getMovie()&lt;/code&gt; function is returning a promise that fetches some movies from the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;greetUser()&lt;/code&gt; function has a variable &lt;code&gt;run&lt;/code&gt;assigned with value &lt;code&gt;let me run please!!&lt;/code&gt; and it also simply consoling the value.&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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;greetUser&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello user! welcome to moviehub&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;delayedBy20ms&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;    
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I will be print at least after 20 milliseconds!!&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="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getMovie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;movieName&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;movieName&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inception&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;here is your movie from the server!&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;reject&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;message&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;movie not found!!&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="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;letMeRun&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;run&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;let me run please!!&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;run&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;span class="nx"&gt;greetUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;delayedBy20ms&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;getMovie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inception&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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Phase 1: When programs execute for the first time&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--heJahH7U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrzca42654fhea6c1gjs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--heJahH7U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrzca42654fhea6c1gjs.png" alt="Image description" width="880" height="1056"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do you remember what will happen when JavaScript Engine goes through the code? Let's find out.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;So first, a Global Execution Context(GCE) is created and pushed in the Call Stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Global Execution Context will hold information about all the functions' definitions and variables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also for each function, a function object is created and stored in the memory heap. This function object will contain the function's code and any variables declared within that function.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Phase 2 : When greetUser() function is invoked&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1P6tpLAi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urlqhg4h2v147oscbhrf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1P6tpLAi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urlqhg4h2v147oscbhrf.png" alt="Image description" width="880" height="1066"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After defining all the functions in the global execution context, now all these functions will execute when they are invoked. So, first, we are invoking &lt;code&gt;greetUser.&lt;/code&gt; Let's understand what will happen when it gets invoked.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, an execution context of &lt;code&gt;greetUser&lt;/code&gt; function will be created and pushed to the call stack. The execution context includes the variable &lt;code&gt;message&lt;/code&gt; and value &lt;code&gt;hello user! welcome to moviehub&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;greetUser&lt;/code&gt; function is consoling the variable &lt;code&gt;message&lt;/code&gt;. It will be pushed into the call stack and tries to find the value of the variable in the execution context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When it gets the value of &lt;code&gt;message&lt;/code&gt;, it simply prints in the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since &lt;code&gt;greetUser&lt;/code&gt; function finishes its execution, its execution context will be destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, &lt;code&gt;greetUser&lt;/code&gt; function is popped off from the call stack.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now the program moves to the next line of code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Phase 3: When delayedBy20ms() is invoked
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X4OofvdJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqdipjkth72upxhdeax0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X4OofvdJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqdipjkth72upxhdeax0.png" alt="Image description" width="880" height="1065"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;When &lt;code&gt;delayedBy20ms&lt;/code&gt; function is invoked, a new execution context is created and it will be pushed to the call stack. It is returning a &lt;code&gt;setTimeout&lt;/code&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When &lt;code&gt;setTimeout&lt;/code&gt;method is invoked a new execution context for &lt;code&gt;setTimeout&lt;/code&gt; is created and will also be pushed in the call stack. Inside the &lt;code&gt;setTimeout&lt;/code&gt; execution context, we have a &lt;strong&gt;callback function&lt;/strong&gt; and a &lt;strong&gt;timer&lt;/strong&gt; of &lt;strong&gt;20 milliseconds&lt;/strong&gt;, which says that the &lt;code&gt;setTimeout&lt;/code&gt; method will be executed at least after 20 milliseconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since &lt;code&gt;setTimeout&lt;/code&gt; is an asynchronous operation provided by Web APIs, it will not execute immediately. So, the callback function and timer (20 ms) will be moved to Web APIs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Meanwhile, the execution context of &lt;code&gt;setTimeout&lt;/code&gt; will be destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then &lt;code&gt;setTimeout&lt;/code&gt; method will be popped off from the call stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then the execution context of &lt;code&gt;delayedBy20ms&lt;/code&gt; function will be destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And finally, &lt;code&gt;delayedBy20ms&lt;/code&gt; pops off from the call stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, when the timer of 20ms expires the callback function moved to the callback queue&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The event loop is continuously checking if there is any function in the callback queue ready to be executed. But the call stack is not emptied yet, so the callback function will wait in the callback queue till the call stack is empty.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Phase 4: When getMovie() function is invoked
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9ZveCwfO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1r6y86tk9kvlbalmgq7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9ZveCwfO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1r6y86tk9kvlbalmgq7.png" alt="Image description" width="880" height="932"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Now &lt;code&gt;getMovie()&lt;/code&gt; the function is invoked, a new execution context for it will be created and it will be pushed into the call stack. It holds the argument &lt;code&gt;movieName&lt;/code&gt; and value &lt;code&gt;inception&lt;/code&gt; and also it is returning a promise.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since &lt;code&gt;getMovie&lt;/code&gt; function is returning a &lt;code&gt;promise&lt;/code&gt;, so when the promise executes a new execution context will be created and it will be pushed into the call stack. The execution context includes &lt;code&gt;value&lt;/code&gt; which is initially &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;status&lt;/code&gt; which is &lt;code&gt;pending&lt;/code&gt; and function definitions when the promise is resolved and rejected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As &lt;strong&gt;Promise&lt;/strong&gt; is an asynchronous operation, it will be moved to Web API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The execution context of &lt;strong&gt;Promise&lt;/strong&gt; will be destroyed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And it will be popped off from the call stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now the execution context of &lt;code&gt;getMovie&lt;/code&gt; function will also be destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And then popped off from the call stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;Promise&lt;/strong&gt; waits in the Web API till it gets a response from the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When it gets the response from the server &lt;code&gt;value&lt;/code&gt; will be the data returned from the server, &lt;code&gt;status&lt;/code&gt; changes to &lt;code&gt;resolved&lt;/code&gt; from &lt;code&gt;pending&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Based on whether the promise is resolved or rejected, a callback function is pushed into the microtask queue. In this case, the callback function is &lt;code&gt;(response) =&amp;gt; console.log(response.data).&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q. Why Promise is pushed into the microtask queue and setTimeout into the callback queue?&lt;/p&gt;

&lt;p&gt;A. Because Promise has &lt;strong&gt;higher priority&lt;/strong&gt; than setTimeout.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The event loop is still checking if there is any task to be executed from the microtask or callback queue and also if the call stack is empty or not. Since the call stack is not emptied yet it cannot execute the callback functions that are ready to be executed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Phase 5: When letMeRun() function is invoked
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iRFASlxr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pcpzihvnqfnka9vnl76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iRFASlxr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pcpzihvnqfnka9vnl76.png" alt="Image description" width="880" height="1056"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;When &lt;code&gt;letMeRun()&lt;/code&gt; is invoked a new execution context will be created and pushed into the call stack. The execution context includes the variable &lt;code&gt;run&lt;/code&gt; and its value &lt;code&gt;let me run!!.&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;letmMeRun&lt;/code&gt; function calling the &lt;code&gt;console.log(run).&lt;/code&gt; So it will be pushed into the call stack and tries to find the value of the variable &lt;code&gt;run&lt;/code&gt; in the execution context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When it gets the value from the execution context, the value will be printed on the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, the execution context of &lt;code&gt;letMeRun&lt;/code&gt; function will be destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then &lt;code&gt;letMeRun&lt;/code&gt; function will be popped off from the call stack&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since there is nothing to execute anymore the global execution context will also be popped off from the call stack.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Phase 6: When the microtask queue executes
&lt;/h3&gt;

&lt;p&gt;Now we have one callback function(Promise) in the microtask queue and one callback function(setTimeout) in the callback queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bn77Neni--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xo8uwsrcidwcs6tkf8a2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bn77Neni--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xo8uwsrcidwcs6tkf8a2.png" alt="Image description" width="880" height="1056"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The event loop finds that the call stack is empty and also there is one callback function in the microtask queue and one callback function in the callback queue is ready to be executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since the callback function(Promise) resides in the microtask queue have higher priority, therefore it will execute first.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the callback function executes, a new execution context is created and pushed into the call stack. The execution context includes arguments &lt;code&gt;response&lt;/code&gt; and value &lt;code&gt;{data : 'here is your movie from the server!'}&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now &lt;code&gt;console.log(response.data)&lt;/code&gt; is invoked, it gets the value from the execution context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The value &lt;code&gt;here is your movie from the server!&lt;/code&gt; is printed on the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The execution context of the callback function is destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And finally popped off from the call stack.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Phase 7: When the callback queue executes
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K6fa7p5l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1v63zfbvv0petiiczc6o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K6fa7p5l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1v63zfbvv0petiiczc6o.png" alt="Image description" width="880" height="1056"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Again event loop finds that the call stack is empty and there is a task in the callback queue ready to be executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The callback function gets executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So, a new execution context is created. Then it is pushed into the call stack. The execution context includes an argument &lt;code&gt;1&lt;/code&gt; and value &lt;code&gt;i will be print at least after 20 milliseconds!!&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, console.log() is called, it gets the value from the execution context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then the is printed on the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The execution context of the callback function is destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And finally, the callback function is popped off from the call stack.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The output
&lt;/h3&gt;

&lt;p&gt;After executing all the programs we get the output as :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hello user! welcome to moviehub
let me run please!!
here is your movie from the server!
i will be print at least after 20 milliseconds!!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TWGBdui7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6k9xocwnjgo7fxudf5n4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TWGBdui7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6k9xocwnjgo7fxudf5n4.png" alt="Image description" width="880" height="1001"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though we have executed the code in the order below, we get the output in a different order, and this is how the JavaScript Runtime Environment gives an illusion of asynchronicity.&lt;/p&gt;

&lt;p&gt;I hope now it makes sense to you whenever you get the output as the above one.&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;// ORDER OF INVOCATION OF THE FUNCTIONS&lt;/span&gt;
&lt;span class="nx"&gt;greetUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//1&lt;/span&gt;

&lt;span class="nx"&gt;delayedBy20ms&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//2&lt;/span&gt;

&lt;span class="nx"&gt;getMovie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inception&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//3&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;letMeRun&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I know this was a long article but I hope you have read all the sections carefully. We learned a lot of things, aren't we? So let's quickly recap what we have learned so far.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JavaScript at its base is synchronous, but with the help of &lt;strong&gt;JavaScript Runtime Environment(JRE),&lt;/strong&gt; it can work asynchronously.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every browser has a JavaScript Engine, and the runtime environment is the browser itself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;JavaScript Engine helps in parsing, compiling and executing the code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;During parsing, code breaks down into small tokens and it gets converted to Abstract Syntax Tree(AST).&lt;/li&gt;
&lt;li&gt;During compilation, the AST is converted into the bytecode.&lt;/li&gt;
&lt;li&gt;During execution, JavaScript Engine executes the code as the code gets compiled.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;We don't need to worry about memory management in JavaScript.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript Engine takes care of all these, from allocation to utilization to de-allocation.&lt;/li&gt;
&lt;li&gt;All the primitive values like &lt;em&gt;strings&lt;/em&gt;, &lt;em&gt;numbers&lt;/em&gt;, &lt;em&gt;booleans&lt;/em&gt;, &lt;em&gt;undefined&lt;/em&gt;, and &lt;em&gt;null&lt;/em&gt; are stored on the stack.&lt;/li&gt;
&lt;li&gt;All non-primitive values like arrays, objects or functions are first stored on the heap&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;The core components of the JavaScript Runtime Environment are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Web APIs: it helps in interacting with external systems like servers, databases, etc.&lt;/li&gt;
&lt;li&gt;Microtask Queue: it stores a queue of callback functions waiting to be executed with a higher priority.&lt;/li&gt;
&lt;li&gt;Callback Queue: it stores a queue of callback functions waiting to be executed after the microtask queue becomes empty.&lt;/li&gt;
&lt;li&gt;Event Loop: it helps in executing the callback functions that are waiting on the callback queue by constantly checking if the call stack is empty or not.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While writing this article I came to know about so many amazing things and my love for JavaScript increases even more. I hope this article helped you too to understand how Asynchronous JavaScript works under the hood.&lt;/p&gt;

&lt;p&gt;Additionally, I welcome your feedback. If you come across any areas that require correction or you just want to connect, feel free to contact me via email at &lt;a href="mailto:sobitwrites@gmail.com"&gt;sobitwrites@gmail.com&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;References and Useful Resources&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=fOdcuDigxfw"&gt;JavaScript the Hard Parts by CodeSmith&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API"&gt;JavaScript Web APIs&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=svJerixawV0"&gt;JIT Compiler by Programming With Avelx&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://felixgerschau.com/javascript-memory-management/"&gt;JavaScript's Memory Management Explained by Felix Gerschau&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://astexplorer.net/"&gt;Abstract Syntax Tree(AST) viewer&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Why do browser consoles return undefined? Explained</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Sun, 19 Feb 2023 12:35:27 +0000</pubDate>
      <link>https://dev.to/sobitp59/why-do-browser-consoles-return-undefined-explained-26lm</link>
      <guid>https://dev.to/sobitp59/why-do-browser-consoles-return-undefined-explained-26lm</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;As web developers, we are blessed with consoles in the browsers to interact and debug our web pages and applications.&lt;/p&gt;

&lt;p&gt;The most common type of console is the JavaScript console, which is found in most modern web browsers.&lt;/p&gt;

&lt;p&gt;These consoles are built into the web browser that provides a command-line interface(CLI) for executing JavaScript commands and inspecting and debugging the state of our web pages.&lt;/p&gt;

&lt;p&gt;In this blog, we will try to find the answer to "Why browser console returns undefined?".&lt;/p&gt;

&lt;h2&gt;
  
  
  The Browser Console
&lt;/h2&gt;

&lt;p&gt;The first time I interacted with the console(press F12) was during following a course on JavaScript on Udemy.&lt;/p&gt;

&lt;p&gt;I performed a lot of operations and getting the results in real-time in the console felt so convenient.&lt;/p&gt;

&lt;p&gt;But, one thing that constantly bothered me was in some operations it returns the result as expected like adding two numbers &lt;code&gt;10 + 39&lt;/code&gt; gives &lt;code&gt;49&lt;/code&gt;, while in other cases like after defining a function or variable it gives &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F457rdl8g8s2ua13cqje9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F457rdl8g8s2ua13cqje9.png" alt=" " width="800" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, let's understand the reason behind getting undefined. But, before that, we have to know what &lt;code&gt;undefined&lt;/code&gt; actually is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Undefined
&lt;/h2&gt;

&lt;p&gt;In JavaScript, &lt;code&gt;undefined&lt;/code&gt;is a primitive value that is assigned to variables that have been declared but have not been initialized with a value, or to function parameters that have not been provided with an argument.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgf681ovqizww1ejmkbf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgf681ovqizww1ejmkbf.png" alt=" " width="800" height="493"&gt;&lt;/a&gt;&lt;br&gt;
We can say that &lt;code&gt;undefined&lt;/code&gt;is like a placeholder for the variables and functions' arguments in the memory space until it's not get assigned with any value or passed with any arguments in functions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding the working of REPL and Console
&lt;/h2&gt;

&lt;p&gt;The way code is executed in the console of a web browser is similar to the Read-Eval-Print Loop (REPL) methodology that is commonly used in interactive programming environments.&lt;/p&gt;

&lt;p&gt;However, the console of a web browser is not strictly a REPL environment but it does share some similarities in terms of how code is entered, executed, and displayed.&lt;/p&gt;

&lt;p&gt;This is different from how we code in a traditional programming environment, where we typically have to save our code to a file, compile it (if necessary), and then run it.&lt;/p&gt;

&lt;p&gt;Now first, let's understand what each part of the abbreviation of REPL tells us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read: Hey! I am Read and I help in reading the input(code) that you enter. If your input is not correct(syntactically), I will not be able to pass your input to Eval.&lt;/li&gt;
&lt;li&gt;Eval: Heya! I am Eval(not evil) and I help in evaluating the input that you gave me via Read so that I can determine the results for your input.&lt;/li&gt;
&lt;li&gt;Print: Helloo! I am Print and I help in displaying the result of the evaluation provided by the Eval on the console.&lt;/li&gt;
&lt;li&gt;Loop: Hello! I am Loop and I help in taking you back to the Read so that you continue your chit-chat(giving input) with Read.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9mf1hwbclixfwy4zsj29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9mf1hwbclixfwy4zsj29.png" alt=" " width="800" height="494"&gt;&lt;/a&gt;&lt;br&gt;
So basically, it is a type of interactive programming environment that reads the code, evaluates it, prints the result (if there is one), and then waits for us to enter more code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding the Reason Behind Undefined
&lt;/h2&gt;

&lt;p&gt;We know that because of the REPL methodology, the codes get executed immediately after we press ENTER.&lt;/p&gt;

&lt;p&gt;We also know there are three steps( Read, Evaluate and Print ) in between before it gets ready to read our code again (basically before Loop ).&lt;/p&gt;

&lt;p&gt;At the Print stage, it is decided what should be printed in the console by JavaScript Console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvdcliki46omoc6tfuzax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvdcliki46omoc6tfuzax.png" alt=" " width="800" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, let's understand the different scenarios that probably might help us to understand when the JavaScript console decides to print &lt;code&gt;undefined&lt;/code&gt;, and when not.&lt;/p&gt;
&lt;h3&gt;
  
  
  the console.log()
&lt;/h3&gt;

&lt;p&gt;In JavaScript, every function returns a value, even if there is no explicit specification of one using the return keyword. If no return statement is used, the function automatically returns &lt;code&gt;undefined&lt;/code&gt; by default.&lt;/p&gt;

&lt;p&gt;When we call &lt;code&gt;console.log()&lt;/code&gt; with a value as an argument, the value is printed to the console and then &lt;code&gt;undefined&lt;/code&gt; is printed because &lt;code&gt;console.log()&lt;/code&gt; itself does not have a return value.&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello world!&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 world!&lt;/span&gt;
&lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is designed to output messages to the console, but it doesn't return a value that could be used elsewhere in the code.&lt;/p&gt;

&lt;p&gt;So, after the value is printed to the console, the function returns undefined as its default return value.&lt;/p&gt;

&lt;h3&gt;
  
  
  the explicit return type
&lt;/h3&gt;

&lt;p&gt;Since we know that every function in JavaScript returns something. If there will be nothing, then it will be undefined by default.&lt;/p&gt;

&lt;p&gt;So, let's tell the browser console explicitly what we want to return and see how it responds to us.&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;animeLists&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;naruto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;attack on titans&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one piece&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="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="c1"&gt;// animeLists()&lt;/span&gt;
&lt;span class="c1"&gt;// ▸(3) ['naruto', 'attack on titans', 'one piece']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we run the above code in the console, at first we will get undefined. This is because just defining a function does not produce a return value, so the console does not have anything to display as a result of the function definition.&lt;/p&gt;

&lt;p&gt;The function definition is just stored in memory somewhere and is not executed until we call it.&lt;/p&gt;

&lt;p&gt;In the next line, when we called the function &lt;code&gt;animeLists&lt;/code&gt; and since we are explicitly telling the console to return the &lt;code&gt;['naruto', 'attack on titans', 'one piece']&lt;/code&gt; using &lt;code&gt;return&lt;/code&gt; keyword, we will only get the output and not &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  the expressions
&lt;/h3&gt;

&lt;p&gt;An expression in JavaScript is a combination of values, variables, and operators that can be evaluated to a single value.&lt;/p&gt;

&lt;p&gt;It can be as simple as a single value or it can be as complex as a combination of multiple expressions.&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;//01: Arithmetic Expression&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;// returns 14&lt;/span&gt;

&lt;span class="c1"&gt;//02: Function call Expression&lt;/span&gt;
&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns 3&lt;/span&gt;

&lt;span class="c1"&gt;//03: Ternary operator Expression&lt;/span&gt;
&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;y&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 greater&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;y is greater&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// returns the greater one&lt;/span&gt;

&lt;span class="c1"&gt;//04: Boolean Expression&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// returns false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In all the examples above, the console does not display the value &lt;code&gt;undefined&lt;/code&gt; because after reading the expression, it gets evaluated and the result is immediately printed on the console.&lt;/p&gt;

&lt;p&gt;After printing the result, it loops back to Read so that it starts taking more inputs from us.&lt;/p&gt;

&lt;h3&gt;
  
  
  the final example
&lt;/h3&gt;

&lt;p&gt;Till now if you have got a little idea about how all these work from the examples above, then let's finally conclude this, understanding the example below step by step.&lt;/p&gt;

&lt;p&gt;Before that, let's give aliases to "Read", "Eval", "Print "and "Loop" as "Mr. Read", "Mr. Eval", "Mr. Print" and "Mr. Loop" respectively.&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;// Run this code in console&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greetUser&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hey user! welcome to console&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;message&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="s1"&gt;your total cost for the stay is : &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;2100&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;100&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;ul&gt;
&lt;li&gt;Step 1: When we enter this input in the console and press ENTER, Mr. Read will look into our code and finds everything fine. So, he tells Mr. Eval to look into it.&lt;/li&gt;
&lt;li&gt;Step 2: Now, when Mr. Eval looks into the code, he finds that it is just a function definition because the function hasn't been called yet. Since there is nothing to return, Mr Eval returns undefined and sends it to Mr. Print.&lt;/li&gt;
&lt;li&gt;Step 3: The job of Mr. Print is to display whatever is received by Mr. Eval on the console. This time it was undefined, so he prints the same on the console.&lt;/li&gt;
&lt;li&gt;Step 4: Since the final result is printed for the execution of code till now, Mr. Loop will tell Mr. Read, "hey Mr. Read! you can now take another input from the user". And this process continues.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till now, we have only gotten the undefined because we haven't called the function yet, so let's finally call the function &lt;code&gt;greetUser()&lt;/code&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="nf"&gt;greetUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// calling the function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same process will be followed this time as well, let's understand this one as well.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: After we call the function greetUser, Mr. Read will look into our code and reads the various commands we have given.&lt;/li&gt;
&lt;li&gt;Step 2: Now, Mr. Loop will evaluate our code and since we are explicitly returning with the return keyword, we will not get undefined. After evaluation, Mr. Loop sends the result to Mr. Print.&lt;/li&gt;
&lt;li&gt;Step 3: Now, Mr. Print displays the final result on the console.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hey user! welcome to console
your total cost for the stay is : 
2000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Step 4: Again, Mr. Loop will tell Mr. Read, "hey Mr. Read! you can now take another input from the user". And this process continues.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A console is a built-in tool in modern web browsers that provides a command-line interface for debugging and interacting with web pages.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is similar to the Read-Eval-Print Loop (REPL) methodology used in interactive programming environments where,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Read: It reads the input we enter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Eval: It evaluates and determines the results of the input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print: It prints the result on the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Loop: It loops back to Read so that Read can take another input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In JavaScript, every function returns a value, by default it is undefined. And since console.log() does not have a return value so it prints undefined.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If we explicitly state the return value in the code then it resolves the issue of getting "undefined" as output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I hope this blog helps you to understand at least the basic concept behind returning undefined to the console. If it does, do share it and if you want to connect with me, say Hi! &lt;a href="https://twitter.com/sobit_prasad" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>discuss</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Understanding The Stealth Startup : The Pros And The Cons</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Wed, 11 Jan 2023 12:46:38 +0000</pubDate>
      <link>https://dev.to/sobitp59/understanding-the-stealth-startup-the-pros-and-the-cons-4hfh</link>
      <guid>https://dev.to/sobitp59/understanding-the-stealth-startup-the-pros-and-the-cons-4hfh</guid>
      <description>&lt;p&gt;When we hear the term "startup," we often think of a small, innovative company that is working on developing a unique product or service. We may also think of entrepreneurs and innovators who are taking risks and trying to bring their ideas to life. Overall, the term "startup" is often associated with innovation, risk-taking, and the pursuit of new and exciting opportunities.&lt;/p&gt;

&lt;p&gt;But do you know there exists another version of startups as well? Do you know what it's called? Yes, you guessed it right! It's called "Stealth Startup". So, in this article, we will explore what stealth startups are, their pros and cons and many more things.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a Stealth Startup?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://nubela.co/blog/stealth-startups-the-good-the-bad-where-to-find-them/?utm_campaign=writers%20domain&amp;amp;utm_source=social&amp;amp;utm_medium=review&amp;amp;utm_content=stealth%20startups%20good%20bad%20find" rel="noopener noreferrer"&gt;Stealth startup&lt;/a&gt; is a term used to describe a startup company that is in the development phase and does not want to draw attention to itself or reveal its existence to the public.&lt;/p&gt;

&lt;p&gt;These companies often operate in stealth mode for a variety of reasons, such as wanting to keep their ideas and plans secret from competitors or wanting to avoid the pressure and scrutiny that comes with being in the public eye.&lt;/p&gt;

&lt;p&gt;While stealth startups can offer some benefits, they also come with several drawbacks and challenges.&lt;/p&gt;

&lt;h1&gt;
  
  
  Pros of Stealth Startup
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Secrecy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;One of the main benefits of operating in stealth mode is that it allows a startup to keep its ideas and plans secret from competitors. This can be especially important in industries where innovation is key and the risk of copying is high. By operating in stealth mode, a startup can work on its product or service without having to worry about competitors finding out about it and stealing its ideas.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Focus&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Another benefit of operating in stealth mode is that it allows a startup to focus on the development of its product or service without being distracted by the media or other external pressures. This can be especially important for startups that are working on complex or cutting-edge technologies, as it can take a lot of time and effort to get things right. By operating in stealth mode, a startup can focus on its work without being pulled in different directions by external forces.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Fundraising&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Some investors are more likely to invest in a stealth startup because they see it as a lower-risk investment. This is because a stealth startup is less likely to have public attention, which means that there is less chance of bad press or negative publicity affecting the value of the company. As a result, operating in stealth mode can make it easier for a startup to raise money from investors.&lt;/p&gt;

&lt;h1&gt;
  
  
  Cons of Stealth Startup
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Lack of Visibility&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;One of the main drawbacks of operating in stealth mode is that it can be difficult for a startup to gain visibility and attract customers. This is because the company is not making any public announcements or promoting itself, which means that it is not getting the same level of exposure as other companies. This can make it harder for a startup to build a customer base and generate revenue.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Missed Opportunities&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Another drawback of operating in stealth mode is that it can be harder for a startup to take advantage of opportunities that arise. For example, if a startup is not making any public announcements, it may miss out on partnerships or other opportunities that could help it grow and succeed. This can be a significant disadvantage for a startup that is trying to build a business in a competitive market.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Attracting Talent&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;It can also be more difficult for a stealth startup to attract top talent, as potential employees may be hesitant to join a company that is not publicly visible. This can make it harder for a startup to build a strong team and achieve its goals.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Proxycurl will help a Stealth Startup?
&lt;/h1&gt;

&lt;p&gt;Stealth startups have a lot of benefits and so are the consequences. So, how &lt;a href="https://nubela.co/proxycurl-dev/" rel="noopener noreferrer"&gt;Proxycurl&lt;/a&gt; is going to help you?&lt;/p&gt;

&lt;p&gt;Proxycurl has tons of data that allows you to discover and connect with a gem of a stealth startup. With the &lt;a href="https://nubela.co/proxycurl/linkedin" rel="noopener noreferrer"&gt;Person Profile API Endpoint&lt;/a&gt;, you can track the movements of entrepreneurial talents and if they're setting up a company.&lt;/p&gt;

&lt;p&gt;Proxycurl can help track the movements of entrepreneurial talents, follow current employees and their movement, and identify &lt;a href="https://nubela.co/proxycurl/solutions/alternative-data-for-investment-firms?utm_campaign=writers%20domain&amp;amp;utm_source=website&amp;amp;utm_medium=review&amp;amp;utm_content=stealth%20startups%20good%20bad%20find" rel="noopener noreferrer"&gt;investment&lt;/a&gt; &lt;a href="https://nubela.co/proxycurl/solutions/alternative-data-for-investment-firms?utm_campaign=writers%20domain&amp;amp;utm_source=website&amp;amp;utm_medium=review&amp;amp;utm_content=stealth%20startups%20good%20bad%20find%22%E2%80%8C%22" rel="noopener noreferrer"&gt;signals&lt;/a&gt;. It does this by using data from social media and other online sources to track the activities and connections of individuals and companies.&lt;/p&gt;

&lt;p&gt;This can be especially useful for investors or recruiters who are looking for promising startups or talented individuals to invest in or hire. With the &lt;a href="https://nubela.co/proxycurl/linkedin/list-employee" rel="noopener noreferrer"&gt;Employee Listing API Endpoint&lt;/a&gt;, you can &lt;a href="https://nubela.co/proxycurl/solutions/alternative-data-for-investment-firms?utm_campaign=writers%20domain&amp;amp;utm_source=website&amp;amp;utm_medium=review&amp;amp;utm_content=stealth%20startups%20good%20bad%20find" rel="noopener noreferrer"&gt;follow current FAANG/MAMAA employees&lt;/a&gt; and their movements in the job market.&lt;/p&gt;

&lt;p&gt;By using Proxycurl, it is possible to get a better understanding of the movements and activities of stealth startups and other companies that may not be publicly visible, which can help with decision-making and strategy development.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In conclusion, stealth startup is a term used to describe a startup company that is in the development phase and does not want to draw attention to itself or reveal its existence to the public. While operating in stealth mode can offer some benefits, such as secrecy, focus, and easier fundraising, it also comes with several drawbacks and challenges, including lack of visibility, missed opportunities, and difficulty attracting top talent. &lt;a href="https://nubela.co/proxycurl-dev/" rel="noopener noreferrer"&gt;Proxycurl&lt;/a&gt; can be a useful tool for tracking the movements and activities of stealth startups and other companies that may not be publicly visible, which can help with decision making and strategy development. While stealth startups can offer some benefits, it is important for entrepreneurs and investors to carefully weigh the pros and cons of operating in stealth mode and consider whether it is the right approach for their business.&lt;/p&gt;

</description>
      <category>career</category>
      <category>java</category>
      <category>springboot</category>
      <category>cloudcomputing</category>
    </item>
    <item>
      <title>LinkDB : A Database of 117million Linkedin Profiles</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Wed, 19 Oct 2022 05:38:40 +0000</pubDate>
      <link>https://dev.to/sobitp59/linkdb-a-database-of-117million-linkedin-profiles-1ik8</link>
      <guid>https://dev.to/sobitp59/linkdb-a-database-of-117million-linkedin-profiles-1ik8</guid>
      <description>&lt;h2&gt;
  
  
  What is LinkDB?
&lt;/h2&gt;

&lt;p&gt;Hey everyone this is Sobit,  now before you read this article, I have a quick question for you below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Are you a middle-market company based in the US or Europe with at least 50 employees or a startup funded by a reputable venture capital firm of at least 1M USD?&lt;/strong&gt; If your answer is Yes, then you are going to read one of the profitable articles for your company. Don't believe me? Just bare with me.&lt;/p&gt;

&lt;p&gt;So, what is &lt;a href="https://nubela.co/blog/linkdb-an-exhaustive-dataset-of-linkedin-members-and-companies/"&gt;LinkDB&lt;/a&gt;? It is basically a Postgresql database that contains more than 117M publically accessible Linkedin members and companies worldwide. One of the advantages of &lt;a href="https://nubela.co/blog/linkdb-an-exhaustive-dataset-of-linkedin-members-and-companies/"&gt;LinkDB&lt;/a&gt; is that here datasets are categorized and separated according to different regions which include countries like the United States, Canada, the United Kingdom, Israel, Singapore, Australia, New Zealand, and Ireland which will eventually help you to reach your target members and companies.&lt;/p&gt;

&lt;h2&gt;
  
  
  How LinkDB helps you?
&lt;/h2&gt;

&lt;p&gt;Now, if you get a little familiar with &lt;a href="https://nubela.co/blog/linkdb-an-exhaustive-dataset-of-linkedin-members-and-companies/"&gt;LinkDB&lt;/a&gt;, the next question that definitely comes to your mind is - &lt;em&gt;'' Hey Sobit, What is basically the use of LinkDB? How it will benefit my company's growth? ''&lt;/em&gt;. So here are some of the ways in which you can use LinkDB for: &lt;/p&gt;

&lt;h3&gt;
  
  
  • Using LinkDB for Recruitment
&lt;/h3&gt;

&lt;p&gt;Finding the right candidate is one of the most tedious tasks for any company. Ultimately, these will be the people who will be contributing to your company's growth in the future. Now, with LinkDB you can easily perform operations according to your need to find the right candidate. For example, suppose you want to find all employees working at Apple or you want to find people who have studied Computer Science at Stanford, you can easily do this with LinkDB.&lt;/p&gt;

&lt;h3&gt;
  
  
  • Using LinkDB for Sales/Marketing
&lt;/h3&gt;

&lt;p&gt;Every company wants to be in profit and reaching out to the potential customer is one of the most frustrating and tedious work. For example, Suppose you want to promote your product to a specific group of people, what could be a better place than LinkedIn to do so, you can easily find your potential customer using LinkDB which will help you to generate more sales.&lt;/p&gt;

&lt;h3&gt;
  
  
  • Using LinkDB for Investment Analysis
&lt;/h3&gt;

&lt;p&gt;You can use various parameters to analyze and make investment decisions for the growth of your company.&lt;br&gt;
This will give you an edge against your competitors.&lt;/p&gt;

&lt;p&gt;These are only a few uses I have mentioned here, but It is not limited to this only. You can use it for various other purposes like training AI models and much more.&lt;/p&gt;

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

&lt;p&gt;As I have mentioned at the very beginning of this article, if you are a middle-market company based in the US or Europe with at least 50 employees, or to startups funded by reputable venture capital firms for at least 1M USD, then you should definitely check out &lt;a href="https://nubela.co/blog/linkdb-an-exhaustive-dataset-of-linkedin-members-and-companies/"&gt;LinkDB&lt;/a&gt;. These datasets are going to help immensely for the growth of your company.&lt;/p&gt;

&lt;p&gt;Now, if you want any further information like pricing or datasets for your region or any kind of information regarding LinkDB, you can click this &lt;a href="https://nubela.co/blog/linkdb-an-exhaustive-dataset-of-linkedin-members-and-companies/"&gt;link&lt;/a&gt; or you can send your mail to hello[@]nubela[dot]co.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>variable shadowing in javascript 👥</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Fri, 01 Apr 2022 13:18:23 +0000</pubDate>
      <link>https://dev.to/sobitp59/variable-shadowing-in-javascript-5cnl</link>
      <guid>https://dev.to/sobitp59/variable-shadowing-in-javascript-5cnl</guid>
      <description>&lt;h1&gt;
  
  
  Introduction 🐥
&lt;/h1&gt;

&lt;p&gt;Hello everyone, this article is going to be very short but also important. Before reading this blog, please read my previous blog on &lt;a href="https://sobitprasad.hashnode.dev/scope-scope-chain-and-lexical-environment-in-javascript"&gt;Scope, Scope-Chain, and Lexical Environment in JavaScript&lt;/a&gt;. So, in this blog we will talk on shadowing in javascript, what is illegal shadowing. So, please read this blog carefully, because it will help you in your web dev journey. So, let's start.&lt;/p&gt;

&lt;h1&gt;
  
  
  Variable Shadowing in JavaScript 👀
&lt;/h1&gt;

&lt;p&gt;In my previous blog, we have talked a lot about scopes in javascript like global, local, and block scope. Now, let's understand shadowing with the help of an example. We will use the &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; keywords, as &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; behave the same in variable shadowing so we will skip &lt;code&gt;const&lt;/code&gt; here.&lt;/p&gt;

&lt;h1&gt;
  
  
  variable shadowing with &lt;code&gt;var&lt;/code&gt; keyword 🦉
&lt;/h1&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared in global scope&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared inside a 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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So, what will be the output here 🤔? Before answering, let's understand the above code. We have declared two variables with the same name &lt;code&gt;a&lt;/code&gt; one in the global space and one inside the block, and we are consoling &lt;code&gt;a&lt;/code&gt; in the global scope. So, now you can tell me your answer. If your answer is &lt;code&gt;100&lt;/code&gt;, congratulations 🥳, it's the right answer. But why we are getting &lt;code&gt;100&lt;/code&gt; even though we have written &lt;code&gt;console.log(a)&lt;/code&gt; in the global scope. This is because, both the variables are pointing to the same memory location i.e. both are pointing to the Global Memory Scope.&lt;/p&gt;

&lt;p&gt;So, what is variable shadowing here? Here, the variable inside the block is shadowing the variable in the global scope. In simpler terms you can say, a variable in block scope is hiding the value of the variable in global scope with its shadow and printing its own value. But what if we write the variable declared inside the block, in a function. 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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared in global scope&lt;/span&gt;
&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared inside a function&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So, here output will be &lt;code&gt;10&lt;/code&gt;, Why? Because both the variables are stored in different memory spaces. As keyword &lt;code&gt;var&lt;/code&gt; is a function/local scoped i.e. variable declared inside the function can be accessed inside that function only, we will not be able to access it outside its boundary. And here, the variable &lt;code&gt;a&lt;/code&gt; inside function fails to shadow the variable &lt;code&gt;a&lt;/code&gt; in the global scope.&lt;/p&gt;

&lt;h1&gt;
  
  
  variable shadowing with &lt;code&gt;let&lt;/code&gt; keyword 🦚
&lt;/h1&gt;

&lt;p&gt;Let's understand this with the above example only.&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared in global scope&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared inside a 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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So, here our output will be &lt;code&gt;10&lt;/code&gt;, we should print &lt;code&gt;100&lt;/code&gt; are you saying? No, it's not like that in the case of &lt;code&gt;let&lt;/code&gt;, because &lt;code&gt;let&lt;/code&gt; is a block-scope type i.e. if we declare a variable with &lt;code&gt;let&lt;/code&gt; inside any block whether inside a function, inside an if-else, or inside a while/for loop, we will never be able to access &lt;code&gt;let&lt;/code&gt; outside that block. As &lt;code&gt;let&lt;/code&gt; is a block-scope type, in the above example both the variables have different memory spaces and the variable inside the block is not able to shadow the variable in the global space.&lt;/p&gt;

&lt;h1&gt;
  
  
  Illegal Shadowing in JavaScript 🧛‍♀️
&lt;/h1&gt;

&lt;p&gt;To understand illegal shadowing we will use &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; keywords in our example. So, let's try to understand illegal shadowing.&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared in global scope using `let` keyword&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared inside a block using `var` keyword&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now, here we will get an error &lt;code&gt;Uncaught SyntaxError: Identifier 'a' has already been declared&lt;/code&gt;. Although here both the variables are pointing to the global memory space, the variable &lt;code&gt;a&lt;/code&gt; inside the block fails to shadow the variable &lt;code&gt;a&lt;/code&gt; from the global space. This is said to be illegal shadowing.&lt;/p&gt;

&lt;p&gt;But what if we swap the keywords &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;var&lt;/code&gt; or write the var &lt;code&gt;a&lt;/code&gt; in a function instead in a block. So, let's see.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1: Swapping variables &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;var&lt;/code&gt; in the above example
&lt;/h2&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared in global scope using `var` keyword&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared inside a block using `let` keyword&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here, we will get &lt;code&gt;10&lt;/code&gt; in the console and not an error, but why? Because, both the variables are pointing to separate memory spaces. Although, the variable inside the block will not shadow the variable in the global space, because &lt;code&gt;let&lt;/code&gt; has its own separate memory space.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2: Writing variable &lt;code&gt;var&lt;/code&gt; inside a function in the above example
&lt;/h2&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared in global scope using `let` keyword&lt;/span&gt;
    &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// variable declared inside a block using `var` keyword&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here, we will also get &lt;code&gt;10&lt;/code&gt; in the console because variable &lt;code&gt;a&lt;/code&gt; inside the function and variable &lt;code&gt;a&lt;/code&gt; outside the function is pointing to the different memory locations and here also var &lt;code&gt;a&lt;/code&gt; inside the function will not shadow the variable declared with &lt;code&gt;let&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;So, that's it guys for this blog. I will be very glad if you let me know any suggestions/corrections in any of my blog articles. If you find this article helpful, say hi to me on &lt;a href="https://www.linkedin.com/in/sobit-prasad/"&gt;LinkedIn&lt;/a&gt; 🌸&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Scope, Scope-Chain, and Lexical Environment in JavaScript</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Sat, 26 Mar 2022 18:05:42 +0000</pubDate>
      <link>https://dev.to/sobitp59/scope-scope-chain-and-lexical-environment-in-javascript-37ji</link>
      <guid>https://dev.to/sobitp59/scope-scope-chain-and-lexical-environment-in-javascript-37ji</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;What comes to your mind when you heard the term "scope"? It probably might be a "binocular" or if you play any battle-royale game (like PUBG, Fortnite, etc), then it might be a 2X, 3X, 4X scopes, etc, Right? Whatever it might be, let's see scope from the perspective of a battle royale game. So, from the perspective of a battle-royale game, the scope is used to see/find enemies that fall in the range of the scope, isn't it? And in JavaScript, the scope works something like this.&lt;/p&gt;

&lt;p&gt;Namaste everyone, in this blog article we are going to explore a bunch of things like scope, scope-chain, lexical environment, etc. But, before reading this article I will recommend you to read my blog on JavaScript Behind The Scene so that you got familiar with how JavaScript works behind the scene. So, let's explore.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scope in JavaScript
&lt;/h2&gt;

&lt;p&gt;Scope in JavaScript is the range where we can access specific variables and functions or you can say, scopes tell us where we can access particular variables and functions in our code. So, there are basically three types of scopes in JavaScript :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global Scope&lt;/li&gt;
&lt;li&gt;Local/Function Scope&lt;/li&gt;
&lt;li&gt;Block Scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's explore all these scopes one by one and see how it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Global Scope in JavaScript
&lt;/h2&gt;

&lt;p&gt;When we declare variables or functions at the top of our code i.e. in the global space then those variables or functions are said to be in the Global Scope. We can access these variables or functions from anywhere inside our code. Let's understand this with the help of 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;// variables and functions declared in the global space&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;globalScopeVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`variable "globalScopeVariable" declared in Global space`&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;globalScopeVariable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;globalScopeFunction&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="s2"&gt;`function "globalScopeFunction()"" declared in Global space and accessing 
            "globalScopeVariable" below : `&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
           &lt;span class="c1"&gt;// accessing variable "globalScopeVariable" &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="s2"&gt;`Accessed "globalScopeVariable" : &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;globalScopeVariable&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nx"&gt;globalScopeFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;callingGlobalScopeFunction&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="s2"&gt;`an another function "callingGlobalScopeFunction()" declared in Global space and 
            accessing "globalScopeFunction" below : `&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="c1"&gt;// accessing function "globalScopeFunction()"&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="s2"&gt;`Accessed "globalScopeFunction()" : `&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;globalScopeFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nx"&gt;callingGlobalScopeFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The above code might overwhelm you, but don't worry we will understand each and every line of code. In the above example, we have declared a variable &lt;code&gt;globalScopeVariable&lt;/code&gt;and two functions &lt;code&gt;globalScopeFunction()&lt;/code&gt; and &lt;code&gt;callingGlobalScopeFunction()&lt;/code&gt; all in the global space. And function &lt;code&gt;globalScopeFunction()&lt;/code&gt; is accessing the variable &lt;code&gt;globalScopeVariable&lt;/code&gt;and function &lt;code&gt;callingGlobalScopeFunction()&lt;/code&gt; is accessing the function &lt;code&gt;globalScopeFunction()&lt;/code&gt;, we can also access function &lt;code&gt;callingGlobalScopeFunction()&lt;/code&gt; inside an another function.&lt;/p&gt;

&lt;p&gt;But how are we able to access the variable &lt;code&gt;globalScopeVariable&lt;/code&gt;inside function &lt;code&gt;globalScopeFunction()&lt;/code&gt; and function &lt;code&gt;globalScopeFunction()&lt;/code&gt; inside function &lt;code&gt;callingGlobalScopeFunction()&lt;/code&gt; which are not physically present inside that code. The answer is very simple, it's because we have declared all the variables and functions in the global space and thus we are able to access these variables and functions.&lt;/p&gt;

&lt;p&gt;But what if we reverse the situation, i.e. what if we declare a variable or function inside a function and try to access that, outside the code or in the global space. What do you think? And here ladies and gentlemen come the term Local/Function scope, so let's explore this one as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Local/Function Scope in JavaScript
&lt;/h2&gt;

&lt;p&gt;First, let's understand this term, and later we will understand more deeply with the help of examples.&lt;/p&gt;

&lt;p&gt;If we declare a variable or a function inside a function then, the scope of that variable and function is said to be Local/Function scope i.e., we can't access those variables and functions outside that function. Let's understand this with the help of 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="mi"&gt;1&lt;/span&gt;   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// start of scope of variable "varOne"&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;varOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`I am inside function "funcOne()"`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;4&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;varOne&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;      &lt;span class="nx"&gt;funcTwo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;      &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcTwo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt;       &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// start of scope of variable "varTwo"&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt;            &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;varTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`I am inside function "funcTwo()."`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;10&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;varOne&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;11&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;varTwo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt;           
&lt;span class="mi"&gt;13&lt;/span&gt;          &lt;span class="nx"&gt;funcThree&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="mi"&gt;14&lt;/span&gt;          &lt;span class="kd"&gt;function&lt;/span&gt;  &lt;span class="nx"&gt;funcThree&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="mi"&gt;15&lt;/span&gt;           &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// start of scope of variable "varThree"&lt;/span&gt;
&lt;span class="mi"&gt;16&lt;/span&gt;                &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;varThree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`I am inside function 
                "funcThree()".`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;17&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;varOne&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;18&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;varTwo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;19&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;varThree&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="mi"&gt;20&lt;/span&gt;           &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// end of scope of variable "varThree"&lt;/span&gt;
&lt;span class="mi"&gt;21&lt;/span&gt;
&lt;span class="mi"&gt;22&lt;/span&gt;        &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;// end of scope of variable "varTwo"&lt;/span&gt;
&lt;span class="mi"&gt;23&lt;/span&gt; 
&lt;span class="mi"&gt;24&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// end of scope of variable "varOne"&lt;/span&gt;
&lt;span class="mi"&gt;25&lt;/span&gt;        
&lt;span class="mi"&gt;26&lt;/span&gt;     &lt;span class="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above example, we have three functions &lt;code&gt;funcOne()&lt;/code&gt;, &lt;code&gt;funcTwo()&lt;/code&gt; and &lt;code&gt;funcThree()&lt;/code&gt;, and also we have three variables namely &lt;code&gt;varOne&lt;/code&gt;, &lt;code&gt;varTwo&lt;/code&gt;and &lt;code&gt;varThree&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;funcOne()&lt;/code&gt; is declared inside &lt;code&gt;funcTwo()&lt;/code&gt;, &lt;code&gt;funcTwo()&lt;/code&gt; is declared inside &lt;code&gt;funcThree&lt;/code&gt;, and &lt;code&gt;funcThree()&lt;/code&gt; is declared in the global space. And variables &lt;code&gt;varOne&lt;/code&gt;, &lt;code&gt;varTwo&lt;/code&gt;, and &lt;code&gt;varThree&lt;/code&gt;are declared inside functions &lt;code&gt;funcOne&lt;/code&gt;, &lt;code&gt;funcTwo&lt;/code&gt;, and &lt;code&gt;funcThree&lt;/code&gt;respectively.&lt;/p&gt;

&lt;p&gt;The scope of these variables starts with the opening curly brace { and ends with the closing curly brace } of their respective functions, i.e. we cannot access these variables outside that function. And same in the case of functions, in the above example, if we will try to invoke/call function &lt;code&gt;funcOne()&lt;/code&gt; outside the function &lt;code&gt;funcOne()&lt;/code&gt; we will get ReferenceError.&lt;/p&gt;

&lt;p&gt;But, have you noticed that inside function &lt;code&gt;funcTwo()&lt;/code&gt; we are trying to access variable &lt;code&gt;varOne&lt;/code&gt; and inside function &lt;code&gt;funcThree&lt;/code&gt;we are trying to access variables &lt;code&gt;varOne&lt;/code&gt;and &lt;code&gt;varTwo&lt;/code&gt;which are physically not present there. But how are we able to do it? Why we are not getting any errors? All these are possible because of the Lexical Scope/Environment.&lt;/p&gt;

&lt;p&gt;And here we will need the concept of Global Execution Context and Callstack, that's why I recommended reading that blog at the beginning of this article. So guys, let's explore this one as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical Scope/Environment in JavaScript
&lt;/h2&gt;

&lt;p&gt;We know that when we run our code, a Global Execution Context is created, and with every invocation of a function another Execution Context gets created and all these are pushed to the Callstack in order of their invocation.&lt;/p&gt;

&lt;p&gt;But what is Lexical Scope/Environment? Don't worry we will understand this more deeply. First, let's understand how lexical scope/environment is created. So, when Global Execution Context is created, a lexical scope/environment is also created i.e. with every Execution Context there is something called lexical scope/environment is present with it. Let's understand this with the help of the figure below by taking the example of the code above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PC28kr----/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnvr9uz29jl3y5eh78hv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PC28kr----/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnvr9uz29jl3y5eh78hv.png" alt="Image description" width="820" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, when we will run the above code a Global Execution Context is created, and &lt;code&gt;funcOne&lt;/code&gt;will be assigned with whole code inside the { .. } curly braces and &lt;code&gt;varOne&lt;/code&gt;will be assigned with undefined in the memory component and when the code runs in the code component &lt;code&gt;varOne&lt;/code&gt;will be assigned with its actual value i.e. &lt;code&gt;I am inside function "funcOne()"&lt;/code&gt; and another Execution Context is created for &lt;code&gt;funcTwo&lt;/code&gt;and &lt;code&gt;funcThree&lt;/code&gt; as well.&lt;/p&gt;

&lt;p&gt;Now remember this paragraph we have discussed above&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But, have you noticed that inside function &lt;code&gt;funcTwo()&lt;/code&gt; we are trying to access variable &lt;code&gt;varOne&lt;/code&gt; and inside function &lt;code&gt;funcThree&lt;/code&gt;we are trying to access variables &lt;code&gt;varOne&lt;/code&gt;and &lt;code&gt;varTwo&lt;/code&gt;which are physically not present there. But how are we able to do it? Why we are not getting any errors? All these are possible because of the Lexical Scope/Environment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, here is the answer. The lexical environment is the local memory along with the lexical environment of its parent. Didn't understand? Let's go deep and understand this with the help of the code above.&lt;/p&gt;

&lt;p&gt;In function &lt;code&gt;funcThree&lt;/code&gt;, we have only declared variable &lt;code&gt;varThree&lt;/code&gt;. So, when &lt;code&gt;funcThree&lt;/code&gt;executes, JavaScript Engine will assign undefined to all the variables i.e. to &lt;code&gt;varOne&lt;/code&gt;, &lt;code&gt;varTwo&lt;/code&gt;, and &lt;code&gt;varThree&lt;/code&gt;in the memory component. But, &lt;code&gt;varOne&lt;/code&gt;, and &lt;code&gt;varTwo&lt;/code&gt; are not initialized inside &lt;code&gt;funcThree&lt;/code&gt;. So, the lexical environment of &lt;code&gt;funcThree&lt;/code&gt;will start looking for the values of its parent i.e. inside function &lt;code&gt;funcTwo&lt;/code&gt; and here we will find the value of &lt;code&gt;varTwo&lt;/code&gt;. But again, the value of &lt;code&gt;varOne&lt;/code&gt;is not inside function &lt;code&gt;funcTwo&lt;/code&gt;(), so the lexical environment of &lt;code&gt;funcTwo&lt;/code&gt;will start searching for the value of &lt;code&gt;varOne&lt;/code&gt;of its parent i.e. inside &lt;code&gt;funcOne&lt;/code&gt;() and when the value is found it is assigned to its respective variables. The chain of these lexical environments is known as &lt;strong&gt;&lt;em&gt;Scope Chain&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: the lexical environment of global or window object is null. If we will try to access a variable or function which is not declared anywhere, then of course it will not be present in any of the lexical environments and we will get a beautiful error.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's enough for the lexical environments, now let's head to our final destination of this article i.e. the &lt;strong&gt;&lt;em&gt;block scope in JavaScript&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Block Scope in JavaScript
&lt;/h2&gt;

&lt;p&gt;Before learning block scope in JavaScript, first, let's understand what a &lt;strong&gt;BLOCK&lt;/strong&gt; is? So, a block in JavaScript is defined by the curly braces &lt;code&gt;{ }&lt;/code&gt;, also known as the compound statement. Now, you might be wondering what is the use of block, Right? So, a block is used to combine multiple JavaScript statements into a group. Now, you might have another question why do we need to group multiple JavaScript statements into a group? So, here is the answer, we need to group multiple JavaScript statements in a block so that we can use that block where JavaScript expects only one statement. If it sounds confusing don't worry, let's understand this by creating a block of statements step by step.&lt;/p&gt;

&lt;p&gt;Step 1:We can start our block with this &lt;code&gt;{ }&lt;/code&gt; curly braces. This curly braces &lt;code&gt;{ }&lt;/code&gt; below is a block and a valid JavaScript code.&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="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// I am a block&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Step 2: Now, we can write multiple JavaScript statements inside this block &lt;code&gt;{ }&lt;/code&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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am inside a 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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am also inside a block&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;Step 3: Let's used the above block with the &lt;code&gt;if&lt;/code&gt; where JavaScript expects only one statement, i.e. we can write an &lt;code&gt;if&lt;/code&gt; statement as &lt;code&gt;if (true) console.log('I am not inside a block')&lt;/code&gt;; when we need a single statement, but when we need multiple statements we can use block as shown below in the code.&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="kc"&gt;true&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am inside a 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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am also inside a block&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="c1"&gt;//Output : &lt;/span&gt;
&lt;span class="c1"&gt;//I am inside a block&lt;/span&gt;
&lt;span class="c1"&gt;//I am also inside a block&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now, as we have understood &lt;em&gt;block&lt;/em&gt;, let's dive into &lt;em&gt;block scope&lt;/em&gt; in JavaScript. To understand block scope, let's declare three variables using &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&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="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am var a&lt;/span&gt;&lt;span class="dl"&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am let b&lt;/span&gt;&lt;span class="dl"&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;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am const c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// inside block&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;a&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;b&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;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// outside block&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;a&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;b&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;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//Output : &lt;/span&gt;
&lt;span class="c1"&gt;//I am var a&lt;/span&gt;
&lt;span class="c1"&gt;//I am let b&lt;/span&gt;
&lt;span class="c1"&gt;//I am const c&lt;/span&gt;
&lt;span class="c1"&gt;//I am var a&lt;/span&gt;
&lt;span class="c1"&gt;//Uncaught ReferenceError: b is not defined&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now, when we run the above code we will get the error &lt;code&gt;Uncaught ReferenceError: b is not defined&lt;/code&gt; i.e. while accessing the variable &lt;code&gt;b&lt;/code&gt; outside the block. This is because the scope of variables &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; is inside that block only i.e. these variables are stored in the separate memory space, we cannot access variables declared with keyword &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; outside that block. And thus, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; is said to be &lt;strong&gt;block-scoped.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But, if we have written the same code inside a function, we will not be able to access a single variable outside the block i.e.&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;func&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am var a&lt;/span&gt;&lt;span class="dl"&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am let b&lt;/span&gt;&lt;span class="dl"&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;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am const c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// inside block&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;a&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;b&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;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// outside block&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;a&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;b&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;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//Output : &lt;/span&gt;
&lt;span class="c1"&gt;//I am var a&lt;/span&gt;
&lt;span class="c1"&gt;//I am let b&lt;/span&gt;
&lt;span class="c1"&gt;//I am const c&lt;/span&gt;
&lt;span class="c1"&gt;//Uncaught ReferenceError: a is not defined&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;And thus, &lt;code&gt;var&lt;/code&gt; is said to be &lt;code&gt;function/local scoped&lt;/code&gt; i.e. if we declare a variable using the var keyword inside a function, we will not be able to access that variable outside that function.&lt;/p&gt;

&lt;p&gt;So, that's it guys for in this blog. I will be very glad if you let me know any suggestions/corrections in any of my blog articles. If you find this article helpful, say hi to me on &lt;a href="https://www.linkedin.com/in/sobit-prasad/"&gt;LinkedIn&lt;/a&gt; 🌸&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>what is `this` in JavaScript?</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Sun, 13 Mar 2022 17:30:03 +0000</pubDate>
      <link>https://dev.to/sobitp59/what-is-this-in-javascript-2a2c</link>
      <guid>https://dev.to/sobitp59/what-is-this-in-javascript-2a2c</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When we run our code in JavaScript, a Global Execution Context (GBE) is created. But, along with GBE, a &lt;code&gt;Global&lt;/code&gt; object and &lt;code&gt;this&lt;/code&gt; keywords also get created. But, what are &lt;code&gt;Global&lt;/code&gt; object and &lt;code&gt;this&lt;/code&gt; keyword we are talking about here? So, let's explore, here &lt;code&gt;Global&lt;/code&gt; Object refers to the &lt;code&gt;window&lt;/code&gt; object and &lt;code&gt;this&lt;/code&gt; is a keyword that refers to this &lt;code&gt;window&lt;/code&gt; object at the global space i.e., in the global space &lt;code&gt;window === this&lt;/code&gt;. So, if we will type &lt;code&gt;window === this&lt;/code&gt; in the &lt;code&gt;console&lt;/code&gt; we will get &lt;code&gt;true&lt;/code&gt; as the output. But, the behavior of &lt;code&gt;this&lt;/code&gt; changes according to where it is being invoked. So, let's explore how it behaves at different places.&lt;/p&gt;

&lt;h2&gt;
  
  
  invoking &lt;code&gt;this&lt;/code&gt; in global space or alone
&lt;/h2&gt;

&lt;p&gt;When you write &lt;code&gt;console.log(this)&lt;/code&gt; in your javascript program and inspect it in the &lt;code&gt;console&lt;/code&gt;, you will see that this is referring to the &lt;code&gt;Window&lt;/code&gt; object. So, in the global space &lt;code&gt;this&lt;/code&gt; refers to &lt;code&gt;Window&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  invoking &lt;code&gt;this&lt;/code&gt; inside an function
&lt;/h2&gt;

&lt;p&gt;So, what do you think if we invoke &lt;code&gt;this&lt;/code&gt; inside a function? So, let's write a few lines and 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, this also refers to the Window object, but why? Because here function func is a property of &lt;code&gt;Global&lt;/code&gt; i.e. &lt;code&gt;Window&lt;/code&gt; object. So, here we can also say if we call &lt;code&gt;this&lt;/code&gt; in a function as - &lt;strong&gt;&lt;em&gt;&lt;code&gt;this&lt;/code&gt; refers to the object of which this function is a property of&lt;/em&gt;&lt;/strong&gt;. Don't worry if you didn't get it yet, believe me, you will understand it in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  invoking this in an object
&lt;/h2&gt;

&lt;p&gt;So, let's make an object and invoke this inside that.&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sobit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Prasad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;fullName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&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; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&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="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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// Output : Sobit Prasad&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, here output will be &lt;code&gt;Sobit Prasad&lt;/code&gt;. Here, inside object &lt;code&gt;data&lt;/code&gt; we have invoked this inside function &lt;code&gt;fullName&lt;/code&gt; and function &lt;code&gt;fullName&lt;/code&gt; is a property of object &lt;code&gt;data&lt;/code&gt; and that's why here &lt;code&gt;this&lt;/code&gt; refers to &lt;code&gt;data&lt;/code&gt; object and not the &lt;code&gt;Window&lt;/code&gt; object. Let's understand more deeply with the help of an 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sobit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Prasad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;fullName&lt;/span&gt;&lt;span class="p"&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&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; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&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;data2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mohit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sharma&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dataOne&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;data2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataOne&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;//Output : Mohit Sharma&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above program, in spite of declaring function &lt;code&gt;fullName&lt;/code&gt; inside object &lt;code&gt;data1&lt;/code&gt;, we get output as &lt;code&gt;Mohit Sharma&lt;/code&gt;, because at the time of invoking &lt;code&gt;dataOne&lt;/code&gt; is the property of object &lt;code&gt;data2&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; under &lt;code&gt;use strict&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;data&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//Output : undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we include &lt;code&gt;use strict&lt;/code&gt; in our program it doesn't allow default binding and thus we get &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, that's it guys for in this blog. I will be very glad if you let me know any suggestions/corrections in any of my blog articles. If you find this article helpful, say hi to me on &lt;a href="https://www.linkedin.com/in/sobit-prasad/"&gt;LinkedIn&lt;/a&gt; 🌸&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>let, const and the Temporal Dead Zone</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Thu, 24 Feb 2022 05:34:23 +0000</pubDate>
      <link>https://dev.to/sobitp59/let-const-and-the-temporal-dead-zone-2j8l</link>
      <guid>https://dev.to/sobitp59/let-const-and-the-temporal-dead-zone-2j8l</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;And as I promised in my &lt;a href="https://dev.to/sobitp59/hoisting-in-javascript-c3o"&gt;previous blog&lt;/a&gt; that we have a trip with Temporal Dead Zone, and here it is. So, if you haven't understood Temporal Dead Zone and how to play with variables using keywords &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; yet. Don't worry, this blog is totally dedicated to &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, and &lt;code&gt;temporal dead zone&lt;/code&gt;, and believe me if you will read this blog with the whole heart, this is going to be very beneficial for you in your future interviews. So without any further due let's start our trip.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting with basics: &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;As &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are almost similar I will explain this only with the help of &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;. So, below is our program with the same code but using variable keywords &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;. So, what do you think, what will be the answer?&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;//using `var` keyword&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;food&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;food&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cooking&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//using `let` keyword&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;food&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;food&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cooking&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;In our first program (using &lt;code&gt;var&lt;/code&gt;), of course, we will get &lt;code&gt;undefined&lt;/code&gt; and why is that, because of hoisting, Right? If you don't know what hoisting is, please read my &lt;a href="https://dev.to/sobitp59/hoisting-in-javascript-c3o"&gt;previous blog&lt;/a&gt;. Now, let's move to our next program i.e. using the &lt;code&gt;let&lt;/code&gt; keyword for variable declaration. Here, we will get an error as &lt;code&gt;Uncaught ReferenceError: Cannot access 'food' before initialization&lt;/code&gt;. Now let's understand why this error occurs with &lt;code&gt;let&lt;/code&gt;(and &lt;code&gt;const&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;First thing first, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are &lt;em&gt;block-scoped&lt;/em&gt; unlike &lt;code&gt;var&lt;/code&gt; which is &lt;em&gt;function-scoped&lt;/em&gt;. Now, let's understand the term function-scoped and block-scoped.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function-Scoped&lt;/strong&gt;: &lt;code&gt;var&lt;/code&gt; is a function-scoped which means if we declare a variable inside a function, we will not be able to access it from outside the function. So, &lt;code&gt;var&lt;/code&gt; is only limited to function i.e. if we declare variables inside &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;for&lt;/code&gt;, we can access it from outside of &lt;code&gt;if&lt;/code&gt; as well as of &lt;code&gt;for&lt;/code&gt; loop. Let's understand this with the help of an example -
&lt;/li&gt;
&lt;/ul&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;food&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cooking&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;food&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcOne&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;food&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;food&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// if&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;food&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;food&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// for-loop&lt;/span&gt;
&lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;food&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;food&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//OUTPUT : &lt;/span&gt;
&lt;span class="c1"&gt;//cooking&lt;/span&gt;
&lt;span class="c1"&gt;//undefined&lt;/span&gt;
&lt;span class="c1"&gt;//cooking&lt;/span&gt;
&lt;span class="c1"&gt;//cooking&lt;/span&gt;
&lt;span class="c1"&gt;//cooking&lt;/span&gt;
&lt;span class="c1"&gt;//cooking&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above program, we have declared four variables with the same name &lt;code&gt;food&lt;/code&gt;, one in the global scope, one inside function &lt;code&gt;funcOne&lt;/code&gt;, one inside &lt;code&gt;if&lt;/code&gt;, and the last one inside a &lt;code&gt;for&lt;/code&gt; loop. Also, we have initialized with value &lt;code&gt;cooking&lt;/code&gt; only at the top i.e. at Global Scope. Now, when we run this code we will get &lt;code&gt;undefined&lt;/code&gt; only when function &lt;code&gt;funcOne&lt;/code&gt; is invoked because the scope of variable food inside function &lt;code&gt;fucnOne&lt;/code&gt; lies only inside the &lt;code&gt;{ }&lt;/code&gt; curly braces of the function &lt;code&gt;funcOne&lt;/code&gt;. So, it cannot access the value &lt;code&gt;cooking&lt;/code&gt; we initialized at the top. And that's why it is known as &lt;em&gt;function-scoped&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Blocked-Scoped&lt;/strong&gt;: &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are blocked-scoped which means if we declare a variable using &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; inside any block( block means inside any &lt;code&gt;{ }&lt;/code&gt; braces), whether it is a &lt;code&gt;function&lt;/code&gt;, an &lt;code&gt;if&lt;/code&gt; or a &lt;code&gt;for&lt;/code&gt; loop, we will not be able to access it outside its scope. Now, let's understand this with the help of an example. Below we have the same program as above but replaced &lt;code&gt;let&lt;/code&gt; to &lt;code&gt;var&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&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;food&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cooking&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;food&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcOne&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;food&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;food&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// if&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;food&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;food&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// for-loop&lt;/span&gt;
&lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;food&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;food&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//OUTPUT :&lt;/span&gt;
&lt;span class="c1"&gt;//cooking&lt;/span&gt;
&lt;span class="c1"&gt;//undefined&lt;/span&gt;
&lt;span class="c1"&gt;//undefined&lt;/span&gt;
&lt;span class="c1"&gt;//undefined&lt;/span&gt;
&lt;span class="c1"&gt;//undefined&lt;/span&gt;
&lt;span class="c1"&gt;//undefined&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now you see we get &lt;code&gt;undefined&lt;/code&gt; to all the variables we have declared with &lt;code&gt;let&lt;/code&gt; inside function &lt;code&gt;funcOne&lt;/code&gt;, inside &lt;code&gt;if&lt;/code&gt;, and also inside &lt;code&gt;for&lt;/code&gt; loop. This is because, when we declare variables using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, JavaScript Engine assigns memory to it in a separate scope i.e. the variable is only accessible inside &lt;code&gt;{ }&lt;/code&gt; curly braces. And here also comes the concept of &lt;strong&gt;TEMPORAL DEAD ZONE&lt;/strong&gt;, so let's get immersed into it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Temporal Dead Zone
&lt;/h2&gt;

&lt;p&gt;So, &lt;em&gt;what is Temporal Dead Zone?&lt;/em&gt; Here is the answer, Temporal Dead Zone is the &lt;em&gt;time&lt;/em&gt; that starts from the beginning of its(variable) scope till the variable is declared. This is the time when a variable is &lt;em&gt;dead&lt;/em&gt; i.e. if you try to access a variable in this time zone, you will find it useless, you will not be able to access it. But you will find Temporal Dead Zone only when you declare variables using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;. So, let's understand this concept with the help of 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;// **********************************************************************&lt;/span&gt;
&lt;span class="c1"&gt;// ******************** Temporal Dead Zone *****************************&lt;/span&gt;
&lt;span class="c1"&gt;// *************************** for ***************************************&lt;/span&gt;
&lt;span class="c1"&gt;// ************************* variable ************************************&lt;/span&gt;
&lt;span class="c1"&gt;// ************************** `food1` ************************************&lt;/span&gt;
&lt;span class="c1"&gt;// ********************************************************************** &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;food1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;food is cooking at top&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;food1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// **********************************************************************&lt;/span&gt;
    &lt;span class="c1"&gt;// ******************** Temporal Dead Zone *****************************&lt;/span&gt;
    &lt;span class="c1"&gt;// *************************** for ***************************************&lt;/span&gt;
    &lt;span class="c1"&gt;// ************************* variable ************************************&lt;/span&gt;
    &lt;span class="c1"&gt;// *************************  `food2`  ***********************************&lt;/span&gt;
    &lt;span class="c1"&gt;// **********************************************************************&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;food2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;food is cooking inside funcOne&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;food2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note : *(asterisks) are given to visualize how (temporal dead zone)TDZ looks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here we have two variables &lt;code&gt;food1&lt;/code&gt; and &lt;code&gt;food2&lt;/code&gt; declared at top-level and inside function &lt;code&gt;funcOne&lt;/code&gt; respectively. The scope of variable &lt;code&gt;food1&lt;/code&gt; is Global because it is declared at the very top level. So, its temporal dead zone starts from the very beginning and ends when it is being declared. And variable &lt;code&gt;food2&lt;/code&gt; is declared inside function &lt;code&gt;funcOne&lt;/code&gt; so its scope will be only inside the curly braces &lt;code&gt;{ }&lt;/code&gt; of the function &lt;code&gt;funcOne&lt;/code&gt;. Now, its temporal dead zone starts from the start of curly brace &lt;code&gt;{&lt;/code&gt; and ends when it is being declared.&lt;br&gt;
Now, when we try to access variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; inside this temporal dead zone, you will not be able to access it, you are going to get a bunch of errors instead. Don't believe in me? let's do it then. Just move the &lt;code&gt;console.log()&lt;/code&gt; in the temporal dead zone and see what happens.&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;// **********************************************************************&lt;/span&gt;
&lt;span class="c1"&gt;// ******************** Temporal Dead Zone *****************************&lt;/span&gt;
&lt;span class="c1"&gt;// *************************** for ***************************************&lt;/span&gt;
&lt;span class="c1"&gt;// ************************* variable ************************************&lt;/span&gt;
&lt;span class="c1"&gt;// ************************** `food1` ************************************&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;food1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// OMG! inside TDZ&lt;/span&gt;
&lt;span class="c1"&gt;// ********************************************************************** &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;food1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;food is cooking at top&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="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// **********************************************************************&lt;/span&gt;
    &lt;span class="c1"&gt;// ******************** Temporal Dead Zone *****************************&lt;/span&gt;
    &lt;span class="c1"&gt;// *************************** for ***************************************&lt;/span&gt;
    &lt;span class="c1"&gt;// ************************* variable ************************************&lt;/span&gt;
    &lt;span class="c1"&gt;// *************************  `food2`  ***********************************&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;food2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//OMG! it is also inside TDZ&lt;/span&gt;
    &lt;span class="c1"&gt;// **********************************************************************&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;food2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;food is cooking inside funcOne&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="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Output in the console 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="nx"&gt;Uncaught&lt;/span&gt; &lt;span class="nx"&gt;ReferenceError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cannot&lt;/span&gt; &lt;span class="nx"&gt;access&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;food1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="nx"&gt;before&lt;/span&gt; &lt;span class="nx"&gt;initialization&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;You will get an error at the very first line because the variable is not accessible there. That's why it is preferred to use &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; in place of &lt;code&gt;var&lt;/code&gt; to avoid bugs. And now to save our variables from the TEMPORAL DEAD ZONE, we must always try to declare it at the very &lt;em&gt;top-level&lt;/em&gt; of its scope.&lt;/p&gt;

&lt;p&gt;So, that's it guys for in this blog. I will be very glad if you let me know any suggestions/corrections in any of my blog articles. If you find this article helpful, say &lt;em&gt;hi&lt;/em&gt; to me on &lt;a href="https://www.linkedin.com/in/sobit-prasad/"&gt;linkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>temporaldeadz</category>
    </item>
    <item>
      <title>hoisting in javascript</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Wed, 23 Feb 2022 02:59:05 +0000</pubDate>
      <link>https://dev.to/sobitp59/hoisting-in-javascript-c3o</link>
      <guid>https://dev.to/sobitp59/hoisting-in-javascript-c3o</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;First of all, let's understand the term 'HOISTING'. Hoisting means lifting up. For example, when we hoist our flag or when a crane lifts something, it's hoisting(lifting up) that thing, Right? Now, before proceeding further, I will recommend you to read my &lt;a href="https://dev.to/sobitp59/javascript-behind-the-scene-4hac"&gt;previous blog&lt;/a&gt;, because both the blogs are connected to each other and it will help you to understand the concept of this blog very easily. But let's revise a few concepts from the previous blog.&lt;/p&gt;

&lt;p&gt;In the previous blog, we have understood that whenever we run our code an Execution Context is created and inside that context, we have two phases - &lt;strong&gt;&lt;em&gt;Memory Creation Phase&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;Code Execution Phase&lt;/em&gt;&lt;/strong&gt;. Memory Creation Phase runs first and allocates memory/space to all the variables and functions and after that Code Execution Phase runs and it assigns the actual values to its corresponding variables. But why I am saying here variables only, don't worry we will understand it later in this blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variable Hoisting In JavaScript
&lt;/h2&gt;

&lt;p&gt;Now coming back again to the Memory Creation Phase. We know that when we run our code Javascript Engine scans the whole code and allocates memory to all the variables and functions. But, in the case of variables, it assigns with a default value of &lt;code&gt;undefined&lt;/code&gt; in Memory Creation Phase only, even before the execution of our code. Now let's understand all these concepts with the help of examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variable Hoisting: using the keyword &lt;code&gt;var&lt;/code&gt;
&lt;/h3&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;exOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am learning Hoisting in Javascript&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;exOne&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Output : I am learning Hoisting in Javascript&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;What do you think, what will be the output? It's simple, Right? It will give I am learning Javascript in the console. Now let's move to the next program.&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;exTwo&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;exTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Programming is fun&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;exTwo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now, what do you think, what will be the output? But before giving you the answer I want to remind you of something and that is 'Memory Creation Phase'. We know in Memory Creation Phase, Javascript Engine will allocate memory and the default value &lt;code&gt;undefined&lt;/code&gt; to its variable before executing the program. So, let's find out the answer.&lt;/p&gt;

&lt;p&gt;First, let's see the code above, here Javascript Engine will go to &lt;code&gt;line 1&lt;/code&gt; and find &lt;code&gt;console.log(exTwo);&lt;/code&gt;. Now, it finds the variable &lt;code&gt;exTwo&lt;/code&gt; but till now we haven't assigned any values to it and also trying to console log &lt;code&gt;exTwo&lt;/code&gt;, Right? So, it will assign the default value i.e. undefined to &lt;code&gt;exTwo&lt;/code&gt;. Now, JS Engine will move to &lt;code&gt;line 2&lt;/code&gt;. Here, we are assigning 'Programming is fun' to variable &lt;code&gt;exTwo&lt;/code&gt;, so it will hold this value. Now, JS Engine will move to &lt;code&gt;line 3&lt;/code&gt;, here we are again trying to console log variable &lt;code&gt;exTwo&lt;/code&gt;. Now, it will print 'Programming is fun', because here we have assigned &lt;code&gt;Programming is fun&lt;/code&gt; to variable &lt;code&gt;exTwo&lt;/code&gt;. So our output will look something like this,&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;//Output : &lt;/span&gt;
&lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="c1"&gt;// Programming is fun&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above program, we tried to console log variable &lt;code&gt;exTwo&lt;/code&gt; before declaring it. But, what if we try to console log the variable &lt;code&gt;exTwo&lt;/code&gt; without declaring it. So, what do think, what will be the output when we run the below code?&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;exTwo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So, here also JS Engine will scan the whole program and it finds &lt;code&gt;exTwo&lt;/code&gt; but there is no declaration for &lt;code&gt;exTwo&lt;/code&gt;, so JS Engine will not assign any memory to it and 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="nx"&gt;Uncaught&lt;/span&gt; &lt;span class="nx"&gt;ReferenceError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;exTwo&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;defined&lt;/span&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Difference between undefined and not defined. undefined: we will get undefined as an output when we try to access the variable before its being initialized or without initializing the variable. On the other hand, not defined is an error, we will face when we try to access the variable before declaring it.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Think it of as a stranger whom you don't know, will you let him/her in your house, No!, Right? Now let's understand hoisting through the 'let' and 'const' keywords.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variable Hoisting: using the keywords &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;
&lt;/h3&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;exThree&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;exThree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I love programming&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;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;exFour&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;exFour&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Javascript is a weird language&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;When we declare variables using keywords &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, it will work as same as the keyword var. But when we try to access the variable declared with keywords &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; before initializing it, it will throw 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="nx"&gt;Uncaught&lt;/span&gt; &lt;span class="nx"&gt;ReferenceError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cannot&lt;/span&gt; &lt;span class="nx"&gt;access&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;exThree&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="nx"&gt;before&lt;/span&gt; &lt;span class="nx"&gt;initialization&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nx"&gt;Uncaught&lt;/span&gt; &lt;span class="nx"&gt;ReferenceError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cannot&lt;/span&gt; &lt;span class="nx"&gt;access&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;exFour&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="nx"&gt;before&lt;/span&gt; &lt;span class="nx"&gt;initialization&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;But why do these happen? In the case of the keyword &lt;code&gt;var&lt;/code&gt;, when we try to access the variable before initializing it we get undefined, but with keywords &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; we get an error. This happens because of the '&lt;strong&gt;Temporal Dead Zone&lt;/strong&gt;'. I will not cover this topic in this blog but don't worry we are going to play more with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; and Temporal Dead Zone in the next blog. Now let's understand hoisting from the eyes of &lt;code&gt;function&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functional Hoisting In JavaScript
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcOne&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi! i am function one&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="c1"&gt;// Output  : hi! i am function one&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;What will be the output? Again, it's very simple, Right? We have used functions like it before so many times. But how does it possible to access a function even before declaring it. Have you remembered the last line of the second paragraph? So here is the answer, because in the case of functions, in Memory Creation Phase, JS Engine allocates the memory and the whole code inside that function as the value. Now when this function is invoked before or after the declaration of that function an Execution Context is created and in Memory Creation Phase inside this Execution Context, it will simply print the &lt;code&gt;hi! i am function one&lt;/code&gt; in the console and after that, this Execution Context will be deleted and the control will be transferred to Global Execution Context. Now if don't understand yet, just simply copy and paste the below code and open the console, you will find your answer.&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;funcOne&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcOne&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi, i am function one&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="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;funcOne&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;You will get output in the console 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="nx"&gt;ƒ&lt;/span&gt; &lt;span class="nx"&gt;funcOne&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi, i am function one&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="nx"&gt;ƒ&lt;/span&gt; &lt;span class="nx"&gt;funcOne&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi, i am function one&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, you know how we are able to access the function even before declaring/initializing it. But, what if we write the same function using the &lt;code&gt;fat arrow&lt;/code&gt; or &lt;code&gt;anonymous function&lt;/code&gt;. So, let's find out our answer. We have our code below,&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;// Using fat arrow function&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;funcOne&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;funcOne&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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi, i am function one&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="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;funcOne&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Using anonymous function&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;funcOne&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;funcOne&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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi, i am function one&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="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;funcOne&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;funcOne&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;And here I am asking for your answer again, so what will be your answer. Yes, you guessed it right, here &lt;code&gt;funcOne&lt;/code&gt; will act as a variable and it will output in the console 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="kc"&gt;undefined&lt;/span&gt;
&lt;span class="nx"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi, i am function one&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="nx"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So, in both(using fat arrow and anonymous functions) the programs above, we have &lt;code&gt;funcOne&lt;/code&gt; as a variable. So when JS Engine will scan these programs, it will allocate memory as well as the default value of &lt;code&gt;undefined&lt;/code&gt; for line one. Now it will move to the next line and will assign the whole function to variable &lt;code&gt;funcOne&lt;/code&gt;. In the second-last line, we are console logging the variable &lt;code&gt;funcOne&lt;/code&gt; which holds the entire function in it, so it will print the whole code inside that function. Now, in the last line we are invoking &lt;code&gt;funcOne&lt;/code&gt; which will now act as a function rather than a variable and again the whole cycle will repeat.&lt;/p&gt;

&lt;p&gt;So, that's it guys in this blog. In the next blog, we will talk about the variable declared using keywords &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, and also we will travel around the &lt;code&gt;Temporal Dead Zone&lt;/code&gt;. Don't worry we will not die there. So, till then see you in the next blog.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functions</category>
      <category>programming</category>
      <category>bigginer</category>
    </item>
    <item>
      <title>javascript : behind the scene</title>
      <dc:creator>Sobit Prasad</dc:creator>
      <pubDate>Mon, 21 Feb 2022 08:30:37 +0000</pubDate>
      <link>https://dev.to/sobitp59/javascript-behind-the-scene-4hac</link>
      <guid>https://dev.to/sobitp59/javascript-behind-the-scene-4hac</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I think we all like to watch BTS(Behind The Scene), whether it is a BTS of a movie or it could be BTS of your favorite show. But have you ever imagined if we could watch behind the scene of a programming language?&lt;/p&gt;

&lt;p&gt;Yes, you heard it right we can see BTS of a programming language. So in this blog, we will see how JavaScript actually works behind the scene?&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when we run our JavaScript program?
&lt;/h2&gt;

&lt;p&gt;So when you run your beautiful code in JavaScript, an Execution Context is created. So if you are wondering what the heck is this Execution Context, you can think it of like the movie set where all the shots of the movies are to be taken. So everything in JavaScript happens inside this Execution Context.&lt;/p&gt;

&lt;p&gt;In the Execution Context, there are two main components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memory Component (also known as Variable Environment)&lt;/li&gt;
&lt;li&gt;Code Component (also known as Thread of Execution)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Memory Component
&lt;/h3&gt;

&lt;p&gt;In Memory Component, all the values of the variables are assigned in a key-value pair as simple as that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Component
&lt;/h3&gt;

&lt;p&gt;In Code Component, our code gets executed line by line in a specific order. We will understand it more deeply when we will talk about the phases that occur when we run our code. But for now, just keep in mind that when we run our code, an Execution Context is created and it has two components - memory and code component.&lt;/p&gt;

&lt;h2&gt;
  
  
  What after the Execution Context?
&lt;/h2&gt;

&lt;p&gt;So far we know that when we run our code an Execution Context is created. Now, our code has to go through two beautiful phases and these phases are -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;PHASE 1: MEMORY CREATION PHASE&lt;br&gt;
In this phase, our codes get scanned and all the variables are assigned with a value of undefined but in the case of function, it stores the whole code inside that function name. We will understand it later with the help of an example.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PHASE 2: CODE EXECUTION PHASE&lt;br&gt;
In this phase, our codes run line by line in a specific order, and all the actual values of the variable get replaced by undefined which was held earlier as a placeholder.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  EXAMPLE: Understanding Execution Context with the help of an example
&lt;/h2&gt;

&lt;p&gt;I know it might be a little overwhelming, but don't worry let's understand the whole concept with the help of an example. Suppose this is the code we want to execute.&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;cube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;b&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;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we run this code an Execution Context is created and inside the Execution Context it has to go through two phases - MEMORY CREATION PHASE and CODE EXECUTION PHASE. First, it will go through Memory Creation Phase and assign the memory to all the variables. It will assign undefined to variable a and c and also it will assign the whole code inside function cube to cube.&lt;/p&gt;

&lt;p&gt;Now, here comes the Second Phase i.e. the Code Execution Phase. In this phase, our code will run line by line. So, when it goes to the first line, it will assign 7 to variable a. Now, it will move to the next line and founds that there is nothing to execute so it will jump to the sixth line. Now, the sixth line is invoking the function cube with parameter a which holds the value 7. Now, when a new function is invoked, another execution context is created inside the previous one and again it will follow the same procedure.&lt;/p&gt;

&lt;p&gt;Now in this execution context (i.e. the execution context inside the Global Execution Context), again it has to go through Memory Creation Phase and Code Execution Phase. In Memory Creation Phase, it will assign undefined to variables n and b. Now, it will move to the next phase i.e. the Code Execution Phase. In this phase, variable n will be assigned with value a because it was the parameter of the function cube with variable c, now a will be replaced by 7 which is taken from the Global Execution Context. Now finally the output of variable b i.e 343 will be returned to variable c in the Global Execution Context. Now after that, firstly the Local Execution Context will be deleted and after returning the value of variable c, the Global Execution Context will be deleted.&lt;/p&gt;

&lt;p&gt;Now, if you are wondering how JavaScript handles all this, I mean this is a lot. Suppose if we invoke a function inside another function and again inside another function, a number of 'Execution Context' will be created, but all this handles by JavaScript very beautifully. So how does it happen? Ladies and Gentlemen, give a big round of applauding to none other than our very dear CALLSTACK.&lt;/p&gt;

&lt;h2&gt;
  
  
  CallStack: The Game Changer
&lt;/h2&gt;

&lt;p&gt;We know that JavaScript is a synchronous single-threaded programming language and thus it has only one CALLSTACK which means that it can perform only one command at a time in a specific order. So, what basically CALLSTACK does do?&lt;/p&gt;

&lt;p&gt;CALLSTACK maintains the order of execution of EXECUTION CONTEXT which means that when we run our code the global execution context that is created at first will be pushed to the stack first and again if another function is invoked/called, it will be pushed at the top of CALLSTACK and again if another function is invoked it will be pushed to the top. Now when the task of the function at the top is completed it will be removed from the stack and at the end, the Global Execution Context is deleted.&lt;/p&gt;

&lt;p&gt;This is how it looks Behind The Scene of JavaScript. Isn't it amazing? If you are reading till here, thank you so much for that, I appreciate it. Please give your suggestions/corrections below in the comment section, I will be very happy to know your ideas as well. Till then, see you in the next blog.&lt;/p&gt;

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