<?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: Jasmeet Singh Bali</title>
    <description>The latest articles on DEV Community by Jasmeet Singh Bali (@jasmeetbali).</description>
    <link>https://dev.to/jasmeetbali</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%2F694856%2F0fc35329-a279-443b-9e49-1735528b7667.jpeg</url>
      <title>DEV Community: Jasmeet Singh Bali</title>
      <link>https://dev.to/jasmeetbali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jasmeetbali"/>
    <language>en</language>
    <item>
      <title>🚀 Nestjs BareBones Controllers &amp; ReqObjects</title>
      <dc:creator>Jasmeet Singh Bali</dc:creator>
      <pubDate>Sun, 02 Jan 2022 13:40:35 +0000</pubDate>
      <link>https://dev.to/jasmeetbali/nestjs-barebones-controllers-reqobjects-4lj7</link>
      <guid>https://dev.to/jasmeetbali/nestjs-barebones-controllers-reqobjects-4lj7</guid>
      <description>&lt;blockquote&gt;
&lt;h2&gt;
  
  
  🧩 features
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;typescript(disciplined)&lt;/li&gt;
&lt;li&gt;oops(SOLID principles)&lt;/li&gt;
&lt;li&gt;scalable/testable/loosely coupled production grade architecture boilerplate setup&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  👶 steps(understanding the blueprint)
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;npm i -g @nestjs/cli&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            # cmd line
            nest -v

            # config your nest backend
            nest new nest-basics-api

            # choices
            npm or yarn or pnpm
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;strong&gt;*app.module.ts is the root of the api**&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;*&lt;strong&gt;*everything is imported inside app.module and then this is exported and bootstrapped inside main.ts to create the nest backend**&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  🔧 building the api
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. 📩 Controllers
&lt;/h3&gt;

&lt;p&gt;*&lt;strong&gt;*Handles incoming request from client and sends back response**&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;*&lt;strong&gt;* ✏ never write the buisness logic inside the controllers**&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;In nest we define controllers via decorators that provide meta data about what functionality a particular code block will have&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;strong&gt;*the access decorators import @nestjs/common**&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            Controllers
                    |- student.controller.ts

            # student.controller.ts
            import { Controller } from @nestjs/common

            # Controller will tell nest that this is a controller based class

            @Controller('students')
            class StudentController {

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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;strong&gt;*every single route inside studentController is going to start as /students**&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;*&lt;strong&gt;*it can be specified with a &lt;a class="mentioned-user" href="https://dev.to/get"&gt;@get&lt;/a&gt;() decorator that this is a get request**&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            @Get()
            getStudents(){
                // return all students data
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;strong&gt;*make sure to import the student.controller.ts inside the app.module.ts**&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  ✔ running dev server first time
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                npm run start:dev
                # make sure the main.ts is just under the src directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;nested routes like /students/:studentId&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            @Get('/:studentId')
            getStudentById(){
                return "Get Student By Id"
            }
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;🐱‍🚀🐱‍🚀🐱‍🚀TIRED OF REPETATIVE CONTROLLERS SETUP🐱‍🚀🐱‍🚀🐱‍🚀&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating controllers through nestcli 🐱‍👤
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://docs.nestjs.com/controllers" rel="noopener noreferrer"&gt;https://docs.nestjs.com/controllers&lt;/a&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            nest g controller &amp;lt;controllerName&amp;gt; --no-spec
            # only add --no-spec flag if you dont need unit test file for this controller
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  best practice is to put similir prefix routes in seprate controller
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                # to run tests
                npm run test:watch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  2. Request Objects (extracting pieces of info from request like params)
&lt;/h2&gt;

&lt;p&gt;make use &lt;a class="mentioned-user" href="https://dev.to/param"&gt;@param&lt;/a&gt; () decorator in Nest for GET&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                # student object in student ID
                @Get('/:studentId')
                getStudentById(
                    @Param () params: {studentId: string}
                ) {
                    console.log(params)
                    return 'Get Student By Id';
                }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;further it can be simplified while the required params can be destructured at the time of decorator defination&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            # @Param('destructuredObjectFromParams')
            @Param ('studentId') studentId: string
            console.log(studentId)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;make use &lt;a class="mentioned-user" href="https://dev.to/body"&gt;@body&lt;/a&gt; () decorator in Nest for POST&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                @Post()
                createStudent(
                    @Body() body
                ) {
                    return `Create's New Student with details\n ${JSON.stringify(body)}`;
                }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;use &lt;a class="mentioned-user" href="https://dev.to/body"&gt;@body&lt;/a&gt; &amp;amp; @Parma together for PUT&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                @Put('/:studentId')
                updateStudentById(
                    @Param('studentId') studentId: string,
                    @Body() body
                ) {
                    return `Update's\n student id: ${JSON.stringify(studentId)}\n with new data ${body}`;
                }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Further their are @Query, @Session, @Next and &lt;a class="mentioned-user" href="https://dev.to/ip"&gt;@ip&lt;/a&gt;() that can be useful refer- &lt;a href="https://docs.nestjs.com/controllers#request-object" rel="noopener noreferrer"&gt;https://docs.nestjs.com/controllers#request-object&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                @Put('/:studentId')
                updateTeacherOfStudentById(
                    @Param('teachersId') teachersId: string,
                    @Param('studentId') studentId: string,
                    @Ip() clientIP: string 
                ) {
                    return `clientIP: ${clientIP}\nUpdate's Teacher with ID: ${teachersId}\n Associated To Student With Id: ${studentId}`;
                }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  📚 refferences
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;codeSnippet : &lt;a href="https://github.com/JasmeetSinghBali/TNG/tree/master/nest-basics-api" rel="noopener noreferrer"&gt;https://github.com/JasmeetSinghBali/TNG/tree/master/nest-basics-api&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;⛑ &lt;a href="https://docs.nestjs.com/" rel="noopener noreferrer"&gt;https://docs.nestjs.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🧪 &lt;a href="https://docs.nestjs.com/fundamentals/testing" rel="noopener noreferrer"&gt;https://docs.nestjs.com/fundamentals/testing&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Closures in javascript...</title>
      <dc:creator>Jasmeet Singh Bali</dc:creator>
      <pubDate>Sat, 04 Sep 2021 08:25:11 +0000</pubDate>
      <link>https://dev.to/jasmeetbali/closures-in-javascript-41c9</link>
      <guid>https://dev.to/jasmeetbali/closures-in-javascript-41c9</guid>
      <description>&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Function bind together with its lexical environment/scope(its local+parent scope)&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function x(){
    var a = 7;
    function y(){
      console.log(a)
    }
    y(); 
  }
  x();

  # output
  7

  # debugger for line 
  # console.log(a) 
  Closure(x)
  a:7
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;returning a function inside function&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function x(){
    var a = 7;
    function y(){
      console.log(a)
    }
    return y; 
  }
  let res = x();
  console.log(res);

  # output
  F:y(){
    console.log(a);
  }
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;though after returning from x() x is completely vannished from call stack, still y() will remember the variables and functions associated to it in closure&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# the inner function remembers the binding variables and the functions due to closure and even after the function that calls inner function gets vanished from call stack the inner fnction will remember the refferences to the outer function.

function x(){
    var a = 7;
    function y(){            
      console.log(a)     
    }
    return y; 
  }
  let res = x();
  res();

  OR

  function x(){
    var a = 7;
    return function y(){            
      console.log(a);     
    } 
  }
  let res = x();
  res();

  # output
  7
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;so whenever the inner function is returned within the other function then it is actually returning the closure of inner function+its lexical scope and it remembers the refferences to its parent&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Some output prediction questions on closures&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function x(){
    var a = 7;
    function y(){            
      console.log(a);     
    }
    a = 100;
    return y; 
  }
  let res = x();
  res();

  # output
  100
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;remember the inner function on returning also returns the refference(original address) of the parent scope/lexical scope variables, and changing the value will direclty change that parent scoped variable also as shown above code&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A multilevel closure&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function z(){
    var b = 900;  
    function x(){
      var a = 7;            
      function y(){
        console.log(a,b);
      }     
    y();
    }
    x();
  }
  z();

  # debugger at console.log(a,b)
  Closure (x)
  a:7
  Closure (z)
  b:900
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Uses of Closures
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Module Design Pattern&lt;/li&gt;
&lt;li&gt;Currying&lt;/li&gt;
&lt;li&gt;Functions like once&lt;/li&gt;
&lt;li&gt;memoize&lt;/li&gt;
&lt;li&gt;maintaining state in async world&lt;/li&gt;
&lt;li&gt;setTimeouts&lt;/li&gt;
&lt;li&gt;Iterators&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  setTimeout + Closures Interview Questions
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        function x(){
          var i = 1;
          setTimeout(function(){
            console.log(i);
          },3000)
        }
        x();

        # output
        # prints 1 after 3 second from 
        # the time the x is called
        1

        function x(){
        var i = 1;
        setTimeout(function(){
          console.log(i);
        },3000);
        console.log("olaaa");
      }
      x();

      # output
      olaaa
      #after 3 seconds
      1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Remember tide,time and javascript dont wait for anyone&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Tricky question problem is to print number from 1 to 5 like 1 should print after 1 second 2 should print in 2 second and so on&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    function x(){
    for(var i=1;i&amp;lt;=5;i++){
      setTimeout(function(){
      console.log(i);
    },i * 1000);
    }
    console.log("olaaa");
  }
  x();

  # output
  olaaa
  6
  6
  6
  6
  6
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;the above code gives such output due to closure since the setTimeout callback is reffering to the memory space of i by the time the console.log(i) is executed the loop has already incremented to 6 and thus each time the console.log(i) now executes it prints 6.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;to solve this problem we can use let instead of var, as let has block scope and each time loop runs i is a new variable altogether i.e new copy of i is made i.e different memory location is used for each changed value of i when using let&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function x(){
    for(let i=1; i &amp;lt;= 5; i++){
      setTimeout(function(){
      console.log(i);
    },i * 1000);
}
    console.log("olaaa");
}
x();

# output
olaaa
1
2
3
4
5
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT Solution without using let via help of closures i.e making a function and putting the setTimeout inside of it so that each time the innerfunction is called it creates new memeory location for i&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function x(){
  for(var i=1; i &amp;lt;= 5; i++){
    # making a explicit closure for setTimeout
    function close(i){
      setTimeout(function(){
          console.log(i);
        },i * 1000);
    }
  # we are calling the close()  function with new copy of i  different memory location
  close(i);
  }
  console.log("olaaa");
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;}&lt;br&gt;
  x();&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  # output
  olaaa
  1
  2
  3
  4
  5
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You My Friend Have A Great Day!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>core</category>
    </item>
    <item>
      <title>Block,Scope &amp; Shadowing In javascript...</title>
      <dc:creator>Jasmeet Singh Bali</dc:creator>
      <pubDate>Tue, 31 Aug 2021 08:03:02 +0000</pubDate>
      <link>https://dev.to/jasmeetbali/block-scope-shadowing-in-javascript-392a</link>
      <guid>https://dev.to/jasmeetbali/block-scope-shadowing-in-javascript-392a</guid>
      <description>&lt;h3&gt;
  
  
  Block?
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                  # an empty block
                  {
                    //Compound statements
                  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT Block combines multiple statements as a single unit so as javascript can execute multiple lines of code as multiple lines inside block act as single unit&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                  # no use of block single line possible only
                  if(true) true;

                  # with use of block
                  # multiple lines can be written as a single unit where javascript expects only single statement
                  if(true){
                    const a =100; // line1
                    console.log(100); //line 2
                  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Block Scope?
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                {
                  // variables or
                  //functions that can be accessed inside this block
                  // is called BLOCK SCOPE
                }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;When we run the  code snippet-12, A Block scope is created where  b and c are present  while a is inside global scope even if it is declared inside a block&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            # code snippet -12
            {
              var a = 10;
              let b = 100;
              const c = 1000;
            }
            console.log(a); // 10
            console.log(b); //  referrence error
            console.log(c); //  referrence error
            # Inside debugger

            # Block
            b: undefined
            c: undefined

            # Global
            a: undefined
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;that is the reason let and const are in block scope i.e seperate scope, and we can only access a in global scope while b and c in the block scope.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Shadowing in javascript
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The variable declared inside the block takes preference i.e shadows the same named variable declared outside the block&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              var a= 100;
              {
                var a=10;
                console.log(a);

              }

              # output
              10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;In addition to shadowing the var keyword variable will modify the same name variable value declared in the global scope&lt;/strong&gt;&lt;br&gt;
                  var a = 100;&lt;br&gt;
                  {&lt;br&gt;
                    var a=10&lt;br&gt;
                    console.log(a);&lt;br&gt;
                  }&lt;br&gt;
                  console.log(a)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              # output
              10
              10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Case : Let keyword
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Shadowing happens in let also&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However In contrast to var the let keyword declared variable do not alter the value of the same let keyword variable in the global scope&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              let b = 100;
              {
                let b = 20;
                console.log(b);
              }
              console.log(b);

              # output
              20
              100

              # debugger

              # Script
              b: 100

              # Block
              b: 20

              # Global
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT - Shadowing works the similar way for the function scope also&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              const c=100;
              function x(){
                const c = 30;
                console.log(c);
              }
              x();
              console.log(c);

              # output
              30
              100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  I'llegal Shadowing
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              # illegal shadowing
              let a= 20;
              {
                var a =10;
              }

              # output
              SyntaxError a has already been declared

              # legal shadowing
              # case-1
              let b =20;
              {
                let b=2;
              }
              # output
              No error runs perfectly

              # case-2
              var c =20;
              {
                let c=2;
              }
              # output
              No error runs perfectly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT From the above code snippet the point to note is that shadowing can only happen if a particular variable do not cross the boundry for the case of illegal shadowing declaring a var a inside a block will give error as the var keyword will be in the global scope and not in the block scope&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              let a= 20;
              function x(){
                var a =10;
              }

              # output
              No error perfectly runs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT the arrow function behaves the same as normal function when Block Scope rules &amp;amp; shadowing  are considered.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You My Friend Have A Great Day!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>core</category>
      <category>blockscope</category>
      <category>shadowing</category>
    </item>
    <item>
      <title>Scope Chain,Scope &amp; Lexical Environment in javascript...</title>
      <dc:creator>Jasmeet Singh Bali</dc:creator>
      <pubDate>Mon, 30 Aug 2021 06:35:21 +0000</pubDate>
      <link>https://dev.to/jasmeetbali/scope-chain-scope-lexical-environment-in-javascript-53id</link>
      <guid>https://dev.to/jasmeetbali/scope-chain-scope-lexical-environment-in-javascript-53id</guid>
      <description>&lt;h4&gt;
  
  
  Scope is the area where a variable or function can be accessed.
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                # code -snippet - 8
                function a(){
                  console.log(b);
                }
                var b = 10;
                a();

                # output
                10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;though the function a() has its own local execution context&lt;br&gt;
it can still access the global scope variable b inside of it.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            # code snippet -9
            function a(){
              c();
              function c(){
                console.log(b)
              }
            }
            var b = 10
            a();

            # output
            10
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;So a global scope variable can be accessed by a nested function also as shown in code snippet-9&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            # code snippet -10
            function a(){
              var b= 10;
              c();
              function c(){

              }
            }
            a();
            console.log(b);

            # output
            b is not Not defined
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Local scoped variables cannot be accesed inside global scope however global scoped variables can be accesed within local scope anywhere even in nested functions.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Scope and lexical environment
&lt;/h3&gt;

&lt;p&gt;To understand this section you need to have the understanding of &lt;a href="https://dev.to/jasmeetbali/execution-context-exploring-the-core-concepts-of-javascript-3kcb"&gt;Execution Context&lt;/a&gt; proceed accordingly.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    # for code snippet -10
                    # Call Stack

                    |    LEC for c()                             |
                    |   Memory |  Code                           |
                    |          |                                 |
                    |          |                                 |
                    |          |                                 |
                    |          |                                 |
                    | $lexical env of c()--&amp;gt; lexical env of   a()                                          |
                    |----------------------------------------
                    |    LEC for a()                             |
                    |   Memory |  Code                           |
                    |  b:10    |                                 |
                    |  c:{...} |                                 |
                    | $lexical env of a() --&amp;gt;lexical env of GEC  |                                            |
                    |------------------------------------------- |                                            |
                    |      GEC                                   |
                    |                                            |
                    |   Memory |  Code                           |
                    |  a:{...} |                                 |
                    |          |                                 |
                    | $lexical env of GEC ---&amp;gt;null               |
                    |------------------------------------------- |          
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;IMPORTANT- Each Time a execution context is created a lexical environment is created this lexical environment comprise of local memory+ its parent lexical environment&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lexical means a heirarchy i.e a sequence.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              lexical env of z = local memory of z + lexical env of z's parent
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;IMPORTANT - For the code snippet-10 we can say that  function c() is sitting in the lexical scope of a function of a() i.e c() is lexically inside a(),while a() is lexically inside global scope.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;So for the LEC c() its lexical env is pointing i.e reffering to the lexical env of a(), so c() will have access to lexical env of a() also.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Scope Chain
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;When inside of local execution context for a nested function and we are trying to access a variable that is defined inside the global scope then first the search begins inside of the local env and memory if not found it goes to the parent lexical env and searches their and so on until it either finds that variable or reaches to lexical env as null(parent of global execution context) this process or way of accessing/finding variable or function is called Scope Chain.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion -&amp;gt; The Combination of lexical env. and parent references constitute for Scope Chain Mechanism which in fact  is used in accessing variable and functions in different scopes throughout the javascript program.
&lt;/h3&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;say z is a function invocation then a execution context is created with memory and code along with this a lexical env is also created for z.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lexical env. of z = local memory of z + lexical env. of z's parent&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scope Chain Mechanism = lexical env. + references to  parent&lt;/strong&gt;**&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scope chain mechanism is used to understand where a particular variable or function is accessible.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You My Friend Have A Great Day!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>core</category>
    </item>
    <item>
      <title>Undefined Vs Not defined in javascript...</title>
      <dc:creator>Jasmeet Singh Bali</dc:creator>
      <pubDate>Sun, 29 Aug 2021 08:51:55 +0000</pubDate>
      <link>https://dev.to/jasmeetbali/undefined-vs-not-defined-in-javascript-4ja6</link>
      <guid>https://dev.to/jasmeetbali/undefined-vs-not-defined-in-javascript-4ja6</guid>
      <description>&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Whenever we define a variable then the execution context in the first phase i.e memory allocation phase will provide a placeholder named as undefined to that variable&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Not defined is error thrown out by the JS Engine when we are trying to access any variable that has not been assigned any memory by the execution context&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          # code snippet-4
          console.log(a); // undefined (for the memory allocation phase)
          var a = 7;

          console.log(x); // not defined
          console.log(a); // 7 (after code execution phase)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h4&gt;
  
  
  Important Conclusions/Takeaways
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;for the code snippet-4 the line console.log(a); represents hoisting in javascript that means accessing variable before they are defined, and the reason  why a hoisted variable gives undefined as output is because of the execution context memory allocation phase as every javascript program on execution will create a memory even before the actual execution of the javascript code happens. If you want to understand what execution context is then refer my  &lt;a href="https://dev.to/jasmeetbali/execution-context-exploring-the-core-concepts-of-javascript-3kcb"&gt;Execution Context Article&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;An Interesting thing to note is that the state of the variable changes from undefined to not undefined if we have initialized some value to it or provided some value to it in later part of the code&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              # code snippet-5
              var a;
              console.log(a); //undefined
              a=10;

              if(a===undefined){
                consle.log("a is undefined");
              }
              else{
                console.log("a is not undefined");
              }

              # output:

              undefined
              // for uninitialized variable a

              a is not undefined
              // after initializing variable a as 10
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You My Friend Have A Great Day!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>core</category>
    </item>
    <item>
      <title>Execution Context ? Exploring The Core Concepts Of javascript...</title>
      <dc:creator>Jasmeet Singh Bali</dc:creator>
      <pubDate>Sat, 28 Aug 2021 07:21:15 +0000</pubDate>
      <link>https://dev.to/jasmeetbali/execution-context-exploring-the-core-concepts-of-javascript-3kcb</link>
      <guid>https://dev.to/jasmeetbali/execution-context-exploring-the-core-concepts-of-javascript-3kcb</guid>
      <description>&lt;p&gt;Everything in javascript happens inside of &lt;strong&gt;Global Execution Context(GEC).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can visualize this execution context as a big magical box.&lt;/p&gt;

&lt;h4&gt;
  
  
  2 Main Components Of GEC are -
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Memory also called &lt;strong&gt;Variable Environment&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Code also called &lt;strong&gt;Thread Of Execution&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    # code snippet-1
    var n =2;
    function square(num){
      var ans = num *num;
      return ans;
    }
    var square2 = square(n);
    var square4 = square(4);

    # after running the above code snippet
    # a global context execution is created with two components Memory and Code.

    Memory | Code
           |
           |
           |
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Execution Context is created in two phases &amp;gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  a) Memory Creation Phase
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Allocation of memory space to all variables and functions.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      # for the code snippet-1
      # global execution context first phase

      Memory                 |  Code
      n: undefined           |
      square: {entire        |
         body of             |
         the function}       |
      square2: undefined     |
      square4: undefined     |
                             |
                             |
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;undefined is a placeholder for variables declared in the js code, while whole body  of the function act as placeholder for functions in memory allocation.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  b) Code Execution Phase
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      # for the code snippet-1
      # global execution context Second phase

      Memory                 |  Code
      n: 2                   | initializes value of n and change happens in memory  
      square: {entire        |  Nothing Happens
         body of             |
         the function}       |
      square2: undefined     |   function invocation is understood a new Execution Context is created
      square4: undefined     |
                             |
                             |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;When a function invocation is encountered a new execution context is created consider it like a nested context execution inside of global execution context&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Same two phases i.e memory creation phase and code execution phase are followed for local/nested context execution for a function invocation&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  b.1) Local/nested execution context on function invocation
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      # for the code snippet-1
      # global execution context second phase square2 with local execution context phase 1 memory creation

      Memory                 |  Code
      square2: undefined     |    Memory               | Code
                             |   num :undefined        |
                             |   ans: undefined        |
                             |                         |

     # global execution context second phase square2 with local execution context phase 2 Code execution

     Memory                 |  Code
     square2: returns 4     |    Memory               | Code
                            |   num :2                | initializes n to 2
                            |   ans: 4                | computation happens ans = 2*2
                            |                         |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;control of the program returned back to global context execution from local context execution&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Similar procedure happens for square4 function invocation&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;All the Execution Context creation,deletion and control transfer happens via Stack i.e a call stack&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;At the bottom of the stack is the Global  Execution Context, and when function is invoked and new local execution context is created that local execution context is placed in top of stack.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              # LEC- local execution context created during function invokation
              # GEC - Global execution context created in the beginning of javascript source code.

              # Call stack managing Execution Context
              # creation for execution context it is pushed into call stack
              # deletion for execution context it is
              pop of the stack

              |             |
              |             |
              | LEC1/LEC2   |
              | GEC         |
              ---------------
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;LEC 1 two phases happens then control goes to GEC&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Similarly then LEC2 is pushed into top of stack it complete its two phases memory creation and code execution and then give control back to GEC&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Understanding Global Execution Context  helps in determining why javascript behaves weird as compared to other programming languages.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;You my friend have a great day!!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>coreconcepts</category>
      <category>globalexecutioncontext</category>
    </item>
  </channel>
</rss>
