<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Pranav</title>
    <description>The latest articles on Forem by Pranav (@pranav016).</description>
    <link>https://forem.com/pranav016</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%2F534701%2F837c1e58-edc9-469e-b023-69ae8826e330.jpg</url>
      <title>Forem: Pranav</title>
      <link>https://forem.com/pranav016</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pranav016"/>
    <language>en</language>
    <item>
      <title>What were your reasons to shift to Linux OS from a MAC/ Windows OS ?</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Thu, 02 Jun 2022 08:30:21 +0000</pubDate>
      <link>https://forem.com/pranav016/what-were-your-reasons-to-shift-to-linux-os-from-a-mac-windows-os--3f6n</link>
      <guid>https://forem.com/pranav016/what-were-your-reasons-to-shift-to-linux-os-from-a-mac-windows-os--3f6n</guid>
      <description>&lt;p&gt;A lot of my friends have suggested me to shift to Linux operating system due to a lot of privacy advantages and it being developer friendly as well as it being more useful for when we start working in the industry. I am currently using windows operating system and am able to get most of my work done with it. Plus I heard we need to manually install a lot of drivers for our devices for Linux OS which would make it complex for me to get started as a beginner.&lt;/p&gt;

&lt;p&gt;I do want to try it though but I am not convinced if I should really switch to it. I would love to know your thoughts/ advantages you felt there were on switching from a MAC/Windows OS to Linux. Also, please suggest some good/ beginner friendly ways to get started with Linux and which was the first Distro you used? I am sure other beginners who are trying to get started with it, will also benefit from this thread!&lt;/p&gt;

&lt;p&gt;Thank you if you commented 😃&lt;/p&gt;

</description>
      <category>linux</category>
      <category>discuss</category>
      <category>opensource</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 9: Constructor Functions, Object Oriented, `new` keyword</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Wed, 26 Jan 2022 10:18:54 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0</guid>
      <description>&lt;h2&gt;
  
  
  Constructor Functions-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In JavaScript, a &lt;strong&gt;constructor function&lt;/strong&gt; is used to create objects.&lt;/li&gt;
&lt;li&gt;It is considered good practice to name constructor functions with an &lt;strong&gt;upper-case first letter&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Phone = function (model, brand){
  this.model = model,
  this.brand = brand
}

phone.prototype.clickPicture = function(){
  console.log(`${this.brand} ${this.model} clicks picture!`)
}

const iphone = new Phone("Iphone 12", "Apple")
console.log(iphone.clickPicture())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the above example, function &lt;code&gt;Phone()&lt;/code&gt; is an object constructor function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To create an object from a constructor function, we use the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Apple Iphone 12 clicks picture!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Built-in constructors-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Example-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = new Object();    // A new Object object
let b = new String();    // A new String object
let c = new Number();    // A new Number object
let d = new Boolean();   // A new Boolean object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;All these are constructor functions provided to us by javascript.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Code-
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typeof Object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Output-
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'function'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Classes-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Another way of creating templates for JavaScript Objects are &lt;strong&gt;Classes&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;They are very similar to constructor functions. Classes have their own constructors that help initialize variables/values.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Constructor method-
&lt;/h3&gt;

&lt;p&gt;The constructor method is a special method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It has to have the exact name "constructor"&lt;/li&gt;
&lt;li&gt;It is executed automatically when a new object is created&lt;/li&gt;
&lt;li&gt;It is used to initialize object properties&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;If you do not define a constructor method, JavaScript will add an empty constructor method.&lt;/li&gt;
&lt;li&gt;Just like constructor functions, we can use the &lt;code&gt;new&lt;/code&gt; keyword to create new objects from this class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Phone{
  constructor(model, brand){
    this.model = model
    this.brand = brand
  }
  clickPicture() {
    console.log(`${this.brand} ${this.model} clicks picture!`)
  }
}

const iphone = new Phone("Iphone 12", "Apple")
console.log(iphone.brand)
console.log(iphone.clickPicture())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here &lt;code&gt;iphone&lt;/code&gt; is an instance of the class &lt;code&gt;Phone&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;There is a reason why we do not call the &lt;code&gt;clickPicture&lt;/code&gt; function from inside the constructor because, the constructor is called every time we instantiate a new object and that would initialize the function every time instead of sharing it among objects thus taking up more space in memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Apple"
"Apple Iphone 12 clicks picture!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Classes in JavaScript is just syntactical sugar but under the hood it still uses &lt;strong&gt;prototypal inheritance&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Inheritance in Classes-
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Inheritance enables you to define a class that takes all the functionality from a parent class and allows you to add more. Using class inheritance, a class can inherit all the methods and properties of another class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;super()&lt;/code&gt; method refers to the parent class.&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%2F56zl2zgjto5031ej6yki.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%2F56zl2zgjto5031ej6yki.png" alt="Inheritance " width="800" height="498"&gt;&lt;/a&gt; &lt;em&gt;Credits- &lt;a href="https://medium.com/@happymishra66" rel="noopener noreferrer"&gt;Rupesh Mishra&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example-
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Code-
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Phone{
  constructor(model, brand){
    this.model = model
    this.brand = brand
  }
  clickPicture() {
    console.log(`${this.brand} ${this.model} clicks picture!`)
  }
}

class Iphone extends Phone {
  constructor(model, brand, software){
    super(model, brand)
    this.software = software
  }
}

const tenPro = new Iphone("10 pro", "Apple", "IOS")
console.log(tenPro)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Output-
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  brand: "Apple",
  model: "10 pro",
  software: "IOS"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;new&lt;/code&gt; keyword in JS
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Car(make, model, year, owner) {
  this.make = make;
  this.model = model;
  this.year = year;
  this.owner = owner;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var car1 = new Car('Eagle', 'Talon TSi', 1993, rand);
var car2 = new Car('Nissan', '300ZX', 1992, ken);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  References-
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.programiz.com/javascript/constructor-function" rel="noopener noreferrer"&gt;https://www.programiz.com/javascript/constructor-function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_object_constructors.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_object_constructors.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_class_intro.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_class_intro.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.programiz.com/javascript/inheritance" rel="noopener noreferrer"&gt;https://www.programiz.com/javascript/inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_class_inheritance.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_class_inheritance.asp&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>codenewbie</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 8: The 2 Pillars~ Closures &amp; Prototypal Inheritance</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Sat, 22 Jan 2022 01:49:21 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n</guid>
      <description>&lt;h2&gt;
  
  
  Closures-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).&lt;/p&gt;
&lt;/blockquote&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%2F784dqhmvns1kc5yl25ui.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%2F784dqhmvns1kc5yl25ui.png" alt=" " width="620" height="400"&gt;&lt;/a&gt; &lt;em&gt;Credits- &lt;a href="https://edwardgunawan880.medium.com/" rel="noopener noreferrer"&gt;Edward Huang&lt;/a&gt;&lt;/em&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lets understand concept of closures with the help of examples.&lt;/li&gt;
&lt;li&gt;Closures have two major advantages.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. Memory efficient
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Example 1-
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;We want to build a counter function that keeps a track of counts and the count gets increased on calling the function. For that we'll need a &lt;code&gt;count&lt;/code&gt; variable initialized to zero.&lt;/li&gt;
&lt;li&gt;But we don't want it to be accessed by anyone else and alter it thus we &lt;strong&gt;don't want the &lt;code&gt;count&lt;/code&gt; variable to be in a global scope&lt;/strong&gt; for this very reason.&lt;/li&gt;
&lt;li&gt;We can't declare it inside the function either because whenever the function will be called, it will create a new execution context that creates &lt;strong&gt;a new local scope&lt;/strong&gt; for the function(we have learned this in previous parts of our series). Thus the &lt;code&gt;count&lt;/code&gt; variable gets reinitialized to zero &lt;strong&gt;every time we call the function&lt;/strong&gt;, hence we &lt;strong&gt;can't declare it in local/functional scope&lt;/strong&gt; either.&lt;/li&gt;
&lt;li&gt;We can also try to use nested functions just like this-
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add() {
  let counter = 0;
  function plus() {counter += 1;}
  plus();   
  return counter;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;But here we can't call the &lt;code&gt;plus()&lt;/code&gt; function from outside hence this is of no use.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here comes the concept of closures and self invoked functions(learned in earlier parts of the series).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).&lt;br&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 add = (function () {
  let counter = 0;
  return function () {counter += 1; return counter}
})();

add();
add();
add();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here as you can see the &lt;strong&gt;function that we return&lt;/strong&gt; from the self invoked function has a &lt;strong&gt;reference of a variable&lt;/strong&gt; that is outside its local environment just like we said in closures- &lt;code&gt;with references to its surrounding state&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;These references from external environment are stored in the memory even if &lt;strong&gt;we lose the function outside&lt;/strong&gt; because the particular reference is being used in the &lt;strong&gt;function we call&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;That's why closures are very a powerful concept.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example 2-
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Code-
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getHeavy = heavy();
console.log(getHeavy(699))
console.log(getHeavy(700))
console.log(getHeavy(701))

// we don't want to pollute global namespace
function heavy() {
  const bigArray = new Array(7000).fill('hello')
  return function(item) {
    return bigArray[item]
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we are returning a function that can access the required index whenever called, &lt;strong&gt;without polluting our global namespace&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Here the &lt;strong&gt;reference to the array&lt;/strong&gt; &lt;code&gt;bigArray&lt;/code&gt; &lt;strong&gt;stays in memory&lt;/strong&gt; even though the outer function is popped from the call stack and its context is removed because of the concept &lt;strong&gt;of closures&lt;/strong&gt; and we are able to use the &lt;code&gt;getHeavy&lt;/code&gt; function to access required indexes from it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello"
"hello"
"hello"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Encapsulation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We can make variables that are not accessible in the global scope by anyone or any function.&lt;/li&gt;
&lt;li&gt;We can also make variables that are &lt;strong&gt;accessible via a function without it being in its local scope&lt;/strong&gt; such that it gets destroyed when its execution context is popped of the call stack.&lt;/li&gt;
&lt;li&gt;We can make variables encapsulated and safe with the help of closures.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example-
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Code-
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getHeavy = heavy();
console.log(getHeavy(699))
console.log(getHeavy(700))
console.log(getHeavy(701))

// we don't want to pollute global namespace
function heavy() {
  const bigArray = new Array(7000).fill('hello')
  return function(item) {
    return bigArray[item]
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;bigArray&lt;/code&gt; cannot be accessed from anywhere in the function except for the function that we return to the &lt;code&gt;getHeavy&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;In this way the array is encapsulated, we can access it anytime, from anywhere without it being declared in the global namespace/scope and this is property is very useful in different scenarios.&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%2Fq3gn8neke3498qnci68f.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%2Fq3gn8neke3498qnci68f.png" alt=" " width="650" height="360"&gt;&lt;/a&gt; &lt;em&gt;Credits- &lt;a href="https://www.c-sharpcorner.com/members/neelesh-vishwakarma" rel="noopener noreferrer"&gt;Neelesh Vishwakarma&lt;/a&gt;&lt;/em&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Prototypal Inheritance-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Prototypal Inheritance is a method by which an object can inherit the properties and methods of another object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;All JavaScript objects inherit properties and methods from a prototype.&lt;/li&gt;
&lt;li&gt;Date objects inherit from &lt;code&gt;Date.prototype&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Array objects inherit from &lt;code&gt;Array.prototype&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Person objects inherit from &lt;code&gt;Person.prototype&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;Object.prototype&lt;/code&gt; is on the top of the prototype inheritance chain:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Date objects, Array objects, and Person objects inherit from &lt;code&gt;Object.prototype&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And if we check the prototype of the &lt;code&gt;Object&lt;/code&gt; then we see &lt;code&gt;null&lt;/code&gt; being returned by JavaScript since Object is root element in JS.&lt;/p&gt;&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%2Fv69ewr3k3sv9sq5w2u8u.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%2Fv69ewr3k3sv9sq5w2u8u.png" alt="Inheritance" width="800" height="421"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://www.educative.io/blog/understanding-and-using-prototypal-inheritance-in-javascript" rel="noopener noreferrer"&gt;Ryan Thelin&lt;/a&gt;&lt;/em&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;__proto__&lt;/code&gt; is another keyword that can help us determine the parent/prototype of any object(even array or function) in javascript. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see this with help of an example-&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's make an object for a phone that would have all the basic properties that a phone should have.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we would make an Object for an iPhone, that would inherit the properties from the generic phone object to specify all the basic features and then add its own specific features to the iPhone object(itself).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We also have a &lt;code&gt;isPrototypeOf()&lt;/code&gt; method that checks if an object exists in another object's prototype chain.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;hasOwnProperty()&lt;/code&gt; method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const phone = {
  calling: true,
  camera: true,
  touchscreen: true,
}

const iphone = {
  software: "IOS",
  security: "Face Unlock",
}

iphone.__proto__ = phone
console.log(iphone.calling)
console.log(phone.isPrototypeOf(iphone))
console.log(phone.hasOwnProperty(camera))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In this example, when run &lt;code&gt;console.log(iphone.calling)&lt;/code&gt;, the JS engine checks iphone's properties and looks for the key &lt;code&gt;calling&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When we use &lt;strong&gt;prototypal inheritance&lt;/strong&gt;, the properties don't get added to the child object itself. That is why, when we access a property that is not there in the child object, the JS engine continues its &lt;strong&gt;searches up the prototype chain in parent object's&lt;/strong&gt; properties and returns if found.&lt;/li&gt;
&lt;li&gt;If not found, &lt;code&gt;undefined&lt;/code&gt; is logged on the console.&lt;/li&gt;
&lt;li&gt;This above is the reason, false is returned, when we run &lt;code&gt;console.log(phone.hasOwnProperty(camera))&lt;/code&gt; because the iphone object doesn't have the &lt;code&gt;camera&lt;/code&gt; property natively, instead it is inherited from the prototytpe.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true
true
false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 2-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;__proto__&lt;/code&gt; always &lt;strong&gt;returns the parent object of our current object&lt;/strong&gt; that it &lt;strong&gt;inherits&lt;/strong&gt; its properties from.&lt;/li&gt;
&lt;li&gt;If we take an array or a function and access &lt;code&gt;__proto__&lt;/code&gt; property of either one, firstly we'll see their respective objects in the output.&lt;/li&gt;
&lt;li&gt;But if we further access the &lt;code&gt;__proto__&lt;/code&gt; property of their outputs then we get the constructor object "Object" that is the base unit of arrays, functions, objects etc in JavaScript.&lt;/li&gt;
&lt;li&gt;We cannot go any further back than the Object property. Behind that we only receive &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const phone = {
  calling: true,
  camera: true,
  touchscreen: true,
}

const iphone = {
  software: "IOS",
  security: "Face Unlock",
}

iphone.__proto__ = phone
console.log(iphone.__proto__) // we recieve phone object
console.log(iphone.__proto__.__proto__) // we get the base constructor object
console.log(iphone.__proto__.__proto__.__proto__) // we get null here since we cannot go further back than an Object which is base unit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&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%2F5mc06oqma8fa0hck9gna.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%2F5mc06oqma8fa0hck9gna.png" alt="Output1" width="520" height="629"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;prototype&lt;/code&gt; keyword in JavaScript is always present in the parent object that holds all the properties that would be inherited down to its child. It also holds the parent object's own &lt;code&gt;__proto__&lt;/code&gt; property to access its parent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example to help understand-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const phone = {
  calling: true,
  camera: true,
  touchscreen: true,
}

const iphone = {
  software: "IOS",
  security: "Face Unlock",
}

iphone.__proto__ = phone
console.log(iphone.prototype)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Traditionally, in order to get and set the &lt;code&gt;[[Prototype]]&lt;/code&gt; of an object, we use &lt;code&gt;Object.getPrototypeOf&lt;/code&gt; and &lt;code&gt;Object.setPrototypeOf&lt;/code&gt;. Nowadays, in modern language, it is being set using &lt;code&gt;__proto__&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;One reason to use the built-in prototype object is if you'll be duplicating an object multiple times that will share common functionality. By attaching methods to the prototype, you can save on duplicating methods being created per each new instance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;__proto__&lt;/code&gt; is an object in every class instance that points to the prototype it was created from.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The only true difference between &lt;code&gt;prototype&lt;/code&gt; and &lt;code&gt;__proto__&lt;/code&gt; is that the &lt;strong&gt;former is a property of a class constructor&lt;/strong&gt;, while the latter is a &lt;strong&gt;property of a class instance&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;__proto__&lt;/code&gt; is the actual object that is used in the lookup chain to resolve methods, etc. &lt;code&gt;prototype&lt;/code&gt; is the object that is used to build &lt;code&gt;__proto__&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Updating the &lt;code&gt;__proto__&lt;/code&gt; property is not a good practice, instead a good way to inherit properties is by using &lt;code&gt;Object.create()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Another way of creating a prototype chain &lt;code&gt;Object.create()&lt;/code&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;ECMAScript 5 introduced a new method: &lt;code&gt;Object.create()&lt;/code&gt;. Calling this method creates a new object. The prototype of this object is the first argument of the function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const phone = {
  calling: true,
  camera: true,
  touchscreen: true,
}

const iphone = Object.create(phone)

iphone.software= "IOS",
iphone.security= "Face Unlock"

console.log(iphone.calling)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some useful articles-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tricky example to test knowledge about prototype chain-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiply = function(a, b){
  return a*b
}

console.log(multiply.__proto__)
console.log(Function.prototype)
console.log(multiply.__proto__.__proto__)
console.log(Object.prototype)

console.log(typeof Object)
console.log(typeof Object.prototype)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function constructor
Function constructor
Object constructor
Object constructor
'function'
'object'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Object&lt;/code&gt; is an inbuilt function in JavaScript. It also has a prototype of its own like all the other functions in JS.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Object.prototype&lt;/code&gt; returns an &lt;code&gt;'object'&lt;/code&gt; as output since the base element/parent of a function is the &lt;strong&gt;object constructor in JavaScript&lt;/strong&gt;. (as we learned before)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  References-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/prototypal-inheritance-using-__proto__-in-javascript/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/prototypal-inheritance-using-__proto__-in-javascript/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://javascript.plainenglish.io/proto-vs-prototype-in-js-140b9b9c8cd5" rel="noopener noreferrer"&gt;https://javascript.plainenglish.io/proto-vs-prototype-in-js-140b9b9c8cd5&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/4736910/javascript-when-to-use-prototypes" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/4736910/javascript-when-to-use-prototypes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 7: First Class Citizens &amp; Higher Order Functions</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Thu, 20 Jan 2022 17:46:03 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda</guid>
      <description>&lt;h2&gt;
  
  
  First Class Citizens
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;First-class citizenship simply means &lt;strong&gt;being able to do what everyone else can do.&lt;/strong&gt; In JavaScript, functions are objects (hence the designation of first-class object).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JavaScript has all those abilities or features that are required to be a language having First Class Functions, hence functions are treated as First Class Citizens. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let’s look at all the abilities of functions being a First Class Citizen.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. Ability to treat functions as values-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var hello = function(){
  return "hello world"
}

console.log(hello())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello world"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Ability to Pass functions as arguments-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function hello(fn){
  fn()
}

hello(function() { console.log("hello world") })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello world"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Ability return a function from another function-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function hello(){
  return function() {
    return "hello world"
  }
}

var hi=hello()
console.log(hi())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello world"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Because this behavior of JS functions as first class citizens, we are also able to do functional programming that we will learn more about in further parts of our series.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Higher Order Functions-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A higher order function is a function that takes a function as an argument, or returns a function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Simplified Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiplyBy = (num1) =&amp;gt; {
  return function (num2) {
    return num1 * num2;
  }
}

const multiplyByTwo = multiplyBy(2);
multiplyByTwo(4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  References-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.developintelligence.com/blog/2016/10/javascript-functions-as-first-class-objects/" rel="noopener noreferrer"&gt;https://www.developintelligence.com/blog/2016/10/javascript-functions-as-first-class-objects/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/what-is-first-class-citizen-in-javascript/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/what-is-first-class-citizen-in-javascript/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/javascript-scene/higher-order-functions-composing-software-5365cf2cbe99" rel="noopener noreferrer"&gt;https://medium.com/javascript-scene/higher-order-functions-composing-software-5365cf2cbe99&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 6.2: Pass by Value &amp; Pass by Reference, Shallow &amp; Deep Copy, Type Coercion</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Tue, 18 Jan 2022 22:28:04 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3</guid>
      <description>&lt;h2&gt;
  
  
  Pass by Value-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In JavaScript &lt;strong&gt;pass by value&lt;/strong&gt;, the function is called by directly passing the &lt;strong&gt;value of the variable&lt;/strong&gt; as the argument. Therefore, even changing the argument inside the function doesn’t affect the variable passed from outside the function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;It is important to note that in JavaScript, &lt;strong&gt;all function arguments are always passed by value&lt;/strong&gt;. That is, JavaScript &lt;strong&gt;copies the values&lt;/strong&gt; of the passing variables into arguments inside of the function.&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%2Fy1zr1i8nuk6y3sjz1dlo.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%2Fy1zr1i8nuk6y3sjz1dlo.png" alt="DataTypes" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://levelup.gitconnected.com/pass-by-value-vs-pass-by-reference-in-javascript-82fa8736c9f7" rel="noopener noreferrer"&gt;Reina Mitchell&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a=5
let b=a

b++;
console.log(b)
console.log(a)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Pass by Reference-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In &lt;strong&gt;Pass by Reference&lt;/strong&gt;, a function is called by &lt;strong&gt;directly passing the reference/address&lt;/strong&gt; of the variable as the argument. Changing the argument inside the function affects the variable passed from outside the function.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;ul&gt;
&lt;li&gt;In JavaScript, &lt;strong&gt;objects and arrays&lt;/strong&gt; are always passed by reference.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj1={
  a: 'a',
  b: 'b',
  c: 'c'
}

// shallow copying the object using the spread operator
let obj2={...obj1}
obj2.b='d'

console.log(obj2)
console.log(obj1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj1={
  a: 'a',
  b: 'b',
  c: 'c'
}

// another way of copying the object
let obj2=Object.assign({}, obj1)
obj2.b='d'

console.log(obj2)
console.log(obj1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  a: "a",
  b: "d",
  c: "c"
}
{
  a: "a",
  b: "b",
  c: "c"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;But this is only a &lt;strong&gt;shallow copy&lt;/strong&gt; of the original object.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;deep copy&lt;/strong&gt; means that all of the values of the new variable are copied and disconnected from the original variable. A &lt;strong&gt;shallow copy&lt;/strong&gt; means that certain (sub-)values are still connected to the original variable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Lets understand this with the help of an example.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj1 = {
  a: 'a',
  b: 'b',
  c: {
    d: 'd'
  }
};

let obj2={...obj1}
obj2.c.d='f'

console.log(obj2)
console.log(obj1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  a: "a",
  b: "b",
  c: {
    d: "f"
  }
}
{
  a: "a",
  b: "b",
  c: {
    d: "f"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;As you can see that the new object is still connected to the original object it got the value from.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The problem with &lt;strong&gt;shallow copy&lt;/strong&gt; is that, if the user makes changes to the complex object (update street property of address object) of the source object &lt;code&gt;userName&lt;/code&gt;, it is also reflected in the Destination Object, since it points to the same memory address and vice-versa.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thus we turn towards &lt;strong&gt;Deep Copying&lt;/strong&gt;. Deep copying means that value of the new variable is disconnected from the original variable while a shallow copy means that some values are still connected to the original variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lets understand deep copying with the help of an example.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj1 = {
  a: 'a',
  b: 'b',
  c: {
    d: 'd'
  }
};

// converts obj1 to string and then parses it into a new object
let obj2 = JSON.parse(JSON.stringify(obj1))
obj2.c.d = 'f'

console.log(obj2)
console.log(obj1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  a: "a",
  b: "b",
  c: {
    d: "f"
  }
}
{
  a: "a",
  b: "b",
  c: {
    d: "d"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we can see that changes made in &lt;code&gt;obj2&lt;/code&gt; are not reflected in &lt;code&gt;obj1&lt;/code&gt; thus we can say that its a deep copy and the &lt;strong&gt;two objects are disconnected&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to compare two objects having different memory location but same value-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To answer this question, I have found this Stack Overflow thread that answer this question flawlessly and I couldn't explain it any better than thus adding the link to this thread: &lt;a href="https://stackoverflow.com/questions/1068834/object-comparison-in-javascript" rel="noopener noreferrer"&gt;Stack Overflow thread&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the link due to any reason doesn't open, here is the fastest and limited way of comparing values of objects at different memory locations-&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JSON.stringify(obj1) === JSON.stringify(obj2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fi1rj9r35k0y90qpejh1v.gif" 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%2Fi1rj9r35k0y90qpejh1v.gif" alt="Example" width="500" height="270"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- Mathwarehouse&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Tricky question to test knowledge-
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const number = 100
const string = "Jay"
let obj1 = {
  value: "a"
}
let obj2 = {
  value: "b"
}
let obj3 = obj2;

function change(number, string, obj1, obj2) {
    number = number * 10;
    string = "Pete";
    obj1 = obj2;
    obj2.value = "c";
}

change(number, string, obj1, obj2);

// guess which variables will get updated
console.log(number); 
console.log(string);
console.log(obj1.value);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output-
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100
Jay
a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Type Coercion-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers).&lt;/p&gt;
&lt;/blockquote&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%2F3snzvcjric0z27bk5bwx.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%2F3snzvcjric0z27bk5bwx.png" alt="Table" width="800" height="942"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons" rel="noopener noreferrer"&gt;Bill&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const value1 = '5';
const value2 = 9;
let sum = value1 + value2;

console.log(sum);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"59"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the above example, JavaScript has coerced the &lt;strong&gt;9 from a number into a string&lt;/strong&gt; and then &lt;strong&gt;concatenated&lt;/strong&gt; the two values together, resulting in a string of 59. JavaScript had a choice between a string or a number and decided to use a string.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The compiler could have coerced the 5 into a number and returned a sum of 14, but it did not. To return this result, you'd have to &lt;strong&gt;explicitly convert&lt;/strong&gt; the 5 to a number using the &lt;code&gt;Number()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sum = Number(value1) + value2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As an example of type coercion in practice, look at the &lt;a href="https://dorey.github.io/JavaScript-Equality-Table/" rel="noopener noreferrer"&gt;JavaScript Comparison Table&lt;/a&gt;, which shows how the loose equality &lt;code&gt;==&lt;/code&gt; operator behaves for different types.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implicit vs. explicit coercion
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Type coercion can be explicit and implicit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When a developer expresses the intention to convert between types by writing the appropriate code, like Number(value), it’s called &lt;strong&gt;explicit type coercion&lt;/strong&gt; (or type casting).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since JavaScript is a &lt;a href="https://en.wikipedia.org/wiki/Strong_and_weak_typing" rel="noopener noreferrer"&gt;weakly-typed language&lt;/a&gt;, values can also be converted between different types automatically, and it is called &lt;strong&gt;implicit type coercion&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  References-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://flexiple.com/javascript-pass-by-reference-or-value/" rel="noopener noreferrer"&gt;https://flexiple.com/javascript-pass-by-reference-or-value/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/" rel="noopener noreferrer"&gt;https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 6.1: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Mon, 17 Jan 2022 21:21:14 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c</guid>
      <description>&lt;h2&gt;
  
  
  Introduction-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;There are two types of datatypes in JavaScript namely the primitive and non-primitive data-types.&lt;/li&gt;
&lt;li&gt;Primitive data types means that are immutable, cannot be further broken down since they are the smallest unit any data can be in. Non-primitive are opposite to this and can consist of different primitive types.&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%2Ftiq8n9z3hsbimcdy17kb.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%2Ftiq8n9z3hsbimcdy17kb.png" alt="DataTypes" width="591" height="516"&gt;&lt;/a&gt; &lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://www.naukri.com/learning/articles/javascript-data-types-with-examples/" rel="noopener noreferrer"&gt;Deepali&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Primitive type includes-
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Null&lt;/li&gt;
&lt;li&gt;Undefined&lt;/li&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;BigInt&lt;/li&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Symbol&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Non-Primitive Types includes-
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Objects&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;You must be wondering what about arrays and functions? Well, in JavaScript both arrays and functions are a form of object even though when we do &lt;code&gt;typeof&lt;/code&gt; on a function it returns &lt;code&gt;function&lt;/code&gt; but it is an object. Check these examples to understand better.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Examples-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code 1-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function a(){
  console.log("hello world")
}

a.hi = "hi"
console.log(a.hi)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output 1-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hi"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;You can see here how a function is behaving like an object. How we were able to add a new property to the function.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Code 2-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typeof []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output 2-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'object'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;You can see here how an array is returning &lt;code&gt;object&lt;/code&gt; as its type.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;But in real, everything in JavaScript behaves as an object. Check out this &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; and see how &lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt; and many more are listed as built-in objects in JavaScript.&lt;/li&gt;
&lt;li&gt;Let's see this with the help of an example.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(true.toString())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'true'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can see how a boolean value is acting like an object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is because behind the scenes, JS adds a wrapper to it and the code becomes &lt;code&gt;console.log(Boolean(true).toString())&lt;/code&gt; and as we know everything acts like an object hence we are able to call the &lt;code&gt;toString()&lt;/code&gt; function from &lt;code&gt;Boolean&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  If an array is an object, how would we differentiate incase we need to-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;There are many different functions available in JS that help us differentiate the types from one anther.&lt;/li&gt;
&lt;li&gt;For example, in JS a new function was introduced that helps differentiate array from objects. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x=[1,2,3]
Array.isArray(x)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  References-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Rare &amp; useful Git commands summarized + solution to difficult scenarios while using Git</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Mon, 17 Jan 2022 00:44:53 +0000</pubDate>
      <link>https://forem.com/pranav016/rare-useful-git-commands-summarized-solution-to-difficult-scenarios-while-using-git-3m4i</link>
      <guid>https://forem.com/pranav016/rare-useful-git-commands-summarized-solution-to-difficult-scenarios-while-using-git-3m4i</guid>
      <description>&lt;h2&gt;
  
  
  Git commands
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git restore .&lt;/code&gt; - restores the files to the previous commit/ undos all the local changes that haven't been committed.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git restore index.html&lt;/code&gt; - restores only that particular file to the recent commit/ undos all the local/uncommitted changes for that file.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git reset --hard &amp;lt;hash code of the commit&amp;gt;&lt;/code&gt; - removes commits and goes back to the commit for that hash code&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git checkout &amp;lt;hash code of the commit&amp;gt; -- &amp;lt;path/to/file&amp;gt;&lt;/code&gt;- goes back to the commit with that hash code only for that particular file. You need to create a new commit after the rollback is complete.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git commit --amend -m 'Your message'&lt;/code&gt;- helps re-write messages&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git revert &amp;lt;hash code&amp;gt;&lt;/code&gt;- helps to roll back to a previous commit by creating a new commit for it. Doesn't removes those commits from the &lt;code&gt;log&lt;/code&gt; like &lt;code&gt;git reset&lt;/code&gt; does.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git reflog&lt;/code&gt;- this can be useful to bring back deleted commits/files/changes. Use &lt;code&gt;git reset &amp;lt;hash code of lost commit from reflog&amp;gt;&lt;/code&gt; to bring back rolled changes.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git reset HEAD~2&lt;/code&gt;- Helps roll back by 2 commits and unstage all the changes in those 2 removed commits.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git reset HEAD~2 --hard&lt;/code&gt; - Helps roll back by 2 commits but permanently removes all the changes in those 2 removed commits.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git rebase&lt;/code&gt; (most useful command)- Reapply commits on top of another base tip. ex. &lt;code&gt;git rebase master&lt;/code&gt; sets the branch at the tip of master branch&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Solution to difficult scenarios while using Git:
&lt;/h2&gt;



&lt;h3&gt;
  
  
  1. Moving committed changes to a new branch: (scenario: you accidentally worked on master)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  -   Use &lt;code&gt;git checkout -b new-feature&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  -   Then roll back commits on master using &lt;code&gt;git reset HEAD~1 --hard&lt;/code&gt;: (this command will roll back 1 commit)

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

&lt;h3&gt;
  
  
  2. Stashing (example scenario: you need to work/switch on a new branch without making a commit on the current branch)
&lt;/h3&gt;



&lt;h5&gt;
  
  
  All about git stashing:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;  Use &lt;code&gt;git stash&lt;/code&gt; when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The modifications stashed away by this command can be listed with &lt;code&gt;git stash list&lt;/code&gt;, inspected with &lt;code&gt;git stash show&lt;/code&gt;, and restored (potentially on top of a different commit) with &lt;code&gt;git stash apply&lt;/code&gt;. Calling &lt;code&gt;git stash&lt;/code&gt; without any arguments is equivalent to &lt;code&gt;git stash push&lt;/code&gt;.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;git stash&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  stashes/ saves the changes in the back of the project/ another directory of the project and the control moves back to the last working copy of the last commit.&lt;/li&gt;
&lt;li&gt;  saves the changes as a draft and moves back to code of the last commit.

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


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;git stash push -m "Message"&lt;/code&gt;- Adds a message for the stash to the stash list&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;git stash list&lt;/code&gt; - lists all the draft changes in the back of the project&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Tip-&lt;/strong&gt; The stash list stores all the stashes and each stashed feature/code has a unique index number to it. The last added stash always appears at the top with index 0.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;git stash apply&lt;/code&gt; - applies the last stashed draft to our current working directory&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;git stash apply &amp;lt;index number&amp;gt;&lt;/code&gt; - applies the particular indexed stash to our current working directory&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;git stash drop &amp;lt;index number&amp;gt;&lt;/code&gt; - drops the stash out of the stash list with the particular index&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;git stash pop&lt;/code&gt;- pops the last draft changes back into the working directory/ on the working branch and that draft is then removed from the stash list&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;git stash pop &amp;lt;index number&amp;gt;&lt;/code&gt;- pops the draft change with the particular index back into the working directory/ on the working branch and that draft is then removed from the stash list&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;git stash clear&lt;/code&gt;- clears/ deletes all the draft changes stored&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Final Solution using stashing-
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  -   First add all changes to staging using &lt;code&gt;git add .&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  -   Stash changes using &lt;code&gt;git stash&lt;/code&gt; command.&lt;/li&gt;
&lt;li&gt;  -   Go to your new branch and use command &lt;code&gt;git stash apply&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;



&lt;h3&gt;
  
  
  3. Moving committed changes to an already existing branch using cherry-pick(scenario: you accidently worked on master when there is already a dedicated branch for the feature):
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  -   &lt;code&gt;git checkout feature-branch&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  -   &lt;code&gt;git cherry-pick &amp;lt;hash code of that commit on master&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  -   &lt;code&gt;git checkout master&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  -   &lt;code&gt;git reset HEAD~1 --hard&lt;/code&gt; (rolls back 1 commit)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note-&lt;/strong&gt;&lt;br&gt;
This scenario can also be solved using the stashing method explained in #2. You can stash changes and apply those changes to new branch using &lt;code&gt;git stash apply&lt;/code&gt;.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  4. Squashing commits (scenario: you made a bunch of extra commits because of errors and you wan to combine commits into one with a new commit message)-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;git rebase -i &amp;lt;hash code of the commit above which all the commits need to be squashed&amp;gt;&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;  i stands for interactive squash&lt;/li&gt;
&lt;li&gt;  opens up squashing in vim editor where you can pick or squash and update commit messages.

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  5. Bisect (scenario: there is a bug introduced in the code but you don't know which commit introduced the bug)-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Uses binary search to find the commit that introduced a bug.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Procedure-
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git bisect start&lt;/code&gt;- starts the searching procedure to find the bad commit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;(Optional) Use the command &lt;code&gt;git bisect good &amp;lt;hash-code of the commit that you are sure doesn't have the bug&amp;gt;&lt;/code&gt;- usually this is the first or the second commit and tells git to start from the middle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;git will start checking out commits on its own, you have to test the commit and let git know if the commit has the bug or not by using commands, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git bisect good&lt;/code&gt;- if the commit is fine&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git bisect bad&lt;/code&gt;- if the commit has the bug.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After giving feedbacks, git has a feedback tree that it uses to return the commit that introduced the bug/ the first bad commit, you can copy its hash code. The reference &lt;code&gt;refs/bisect/bad&lt;/code&gt; will be left pointing at that commit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After a bisect session, to clean up the bisection state and return to the original HEAD, issue the following command:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git bisect reset&lt;/code&gt;- By default, this will return your tree to the commit that was checked out before git bisect start.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git bisect reset &amp;lt;commit&amp;gt;&lt;/code&gt;- For example, &lt;code&gt;git bisect reset bisect/bad&lt;/code&gt; will check out the first bad revision, while &lt;code&gt;git bisect reset HEAD&lt;/code&gt; will leave you on the current bisection commit and avoid switching commits at all.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git revert &amp;lt;hash-code of the bad commit&amp;gt;&lt;/code&gt;- used to revert the changes done in the bad commit that introduced the bug in the code.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  6. Filter branch (scenario: You want to remove a particular file, whether a large file or any api key accidently uploaded, from you previous commits)-
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git filter-branch --index-filter 'git rm --cached --ignore-unmatch filepath' HEAD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Rewrites whichever commit has the particular file present in it while removing it from the commit.&lt;/li&gt;
&lt;li&gt;Starts searching from &lt;code&gt;HEAD&lt;/code&gt; commit.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--ignore-unmatch&lt;/code&gt; helps avoiding errors if file is not found.&lt;/li&gt;
&lt;li&gt;Commit time and other properties remain same, only SHA/ Hash of the commit may update if the particular file is found and removed from that commit.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  7. Your commits aren't appearing on your GitHub graph:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This mostly happens when either the username or email set in the &lt;code&gt;.gitconfig&lt;/code&gt; file in your system is different than the username and email on your GitHub account.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;strong&gt;Solution-&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git filter-branch --env-filter '
if [ "$GIT_COMMITTER_EMAIL" = "&amp;lt;Old Email&amp;gt;" ];
        then
                GIT_COMMITTER_NAME="&amp;lt;New Name&amp;gt;";
                GIT_AUTHOR_NAME="&amp;lt;New Name&amp;gt;";
                GIT_COMMITTER_EMAIL="&amp;lt;New Email&amp;gt;";
                GIT_AUTHOR_EMAIL="&amp;lt;New Email&amp;gt;";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h3&gt;
  
  
  8. Resetting a particular file to HEAD/ removing the new changes added to a particular file.
&lt;/h3&gt;



&lt;ul&gt;
&lt;li&gt;Use command &lt;code&gt;git checkout HEAD -- &amp;lt;file-name&amp;gt;&lt;/code&gt; to reset the changes done in a particular file back to its state in the last commit.&lt;/li&gt;
&lt;/ul&gt;






&lt;h2&gt;
  
  
  Bonus command-
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Creating zip files using git:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git archive --format=zip --output project.zip master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will output a zip file for master branch and will not include files mentioned in &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>github</category>
      <category>devops</category>
      <category>git</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 5: IIFE &amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Sun, 16 Jan 2022 20:47:07 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c</guid>
      <description>&lt;h2&gt;
  
  
  IIFE
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;An IIFE (&lt;strong&gt;Immediately Invoked Function Expression&lt;/strong&gt;) is a JavaScript function that &lt;strong&gt;runs as soon as it is defined&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Use cases-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Helps avoid polluting the global namespace-
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Since our application may incorporate a large number of functions and global variables from various source files, it's critical to keep the number of &lt;strong&gt;global variables&lt;/strong&gt; to a minimum.&lt;/li&gt;
&lt;li&gt;We could utilize the &lt;strong&gt;IIFE&lt;/strong&gt; pattern if we have some initiation code that we don't need to use again. Because &lt;strong&gt;we won't be reusing&lt;/strong&gt; the code, IIFE is preferable than a function declaration or a function expression &lt;strong&gt;in this scenario&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function () {
  // some initiation code
  let firstVariable;
  let secondVariable;
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;code&gt;firstVariable&lt;/code&gt; and &lt;code&gt;secondVariable&lt;/code&gt; will be discarded after the function is executed.&lt;/em&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%2Ftldp18yttibgq6mxxjcs.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%2Ftldp18yttibgq6mxxjcs.png" alt="IIFE" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The module pattern-
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;We would also use IIFE &lt;strong&gt;to create private and public&lt;/strong&gt; variables and methods.&lt;/li&gt;
&lt;li&gt;These patterns &lt;strong&gt;were more useful before the introduction of ES6&lt;/strong&gt;, when we didn't have the &lt;code&gt;let&lt;/code&gt; and the &lt;code&gt;const&lt;/code&gt; keywords. Back then when we imported all the JavaScript files into one, then there were a lot of conflicts in variable names since all variables were global because of declaration using &lt;code&gt;var&lt;/code&gt;. Thus &lt;strong&gt;developers used IIFE module patterns&lt;/strong&gt; where the variables were made and only those required inside module were left in global scope and others &lt;strong&gt;were discarded because of property of Scope&lt;/strong&gt; using IIFEs. This also overlaps with the first use case of IIFEs mentioned above. Consider this example to understand better-&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Example-
&lt;/h5&gt;

&lt;blockquote&gt;
&lt;p&gt;As you know that a function in JavaScript creates the local scope. So, you can define variables and function inside a function which &lt;strong&gt;cannot be access outside of that function&lt;/strong&gt;. However, sometime you accidently &lt;strong&gt;pollute the global variables&lt;/strong&gt; or functions by unknowingly giving same name to variables &amp;amp; functions as global variable &amp;amp; function names. &lt;/p&gt;

&lt;p&gt;For example, there are multiple &lt;code&gt;.js&lt;/code&gt; files in your application written by multiple developers over a period of time. Single JavaScript file includes many functions and so these multiple &lt;code&gt;.js&lt;/code&gt; files will result in large number of functions. There is a good chance of &lt;strong&gt;having same name of function&lt;/strong&gt; exists in different &lt;code&gt;.js&lt;/code&gt; files written by multiple developer and if these files included in a single web page then it &lt;strong&gt;will pollute the global scope&lt;/strong&gt; by having two or more function or variables with the same name.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Consider the following example of &lt;code&gt;MyScript1.js&lt;/code&gt; and &lt;code&gt;MyScript2.js&lt;/code&gt; with same variable &amp;amp; function name.&lt;/em&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  MyScript1.js
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var userName = "Bill";

function display(name)
{
    alert("MyScript1.js: " + name);
}

display(userName);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  MyScript2.js
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var userName = "Steve";

function display(name)
{
    alert("MyScript2.js: " + name);
}

display(userName);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Importing both files-
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width" /&amp;gt;
    &amp;lt;title&amp;gt;JavaScript Demo&amp;lt;/title&amp;gt;
    &amp;lt;script src="/MyScript1.js"&amp;gt;&amp;lt;/&amp;lt;script&amp;gt; 
    &amp;lt;script src="/MyScript2.js"&amp;gt;&amp;lt;/&amp;lt;script&amp;gt; 
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt; IIFE Demo&amp;lt;/h1&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you run above example, you will find that every time it call &lt;code&gt;display()&lt;/code&gt; function in &lt;code&gt;MyScript2.js&lt;/code&gt; because &lt;code&gt;MyScript2.js&lt;/code&gt; included after &lt;code&gt;MyScript1.js&lt;/code&gt; in a web page. So JavaScript considers last definition of a function if two functions have the same name.&lt;/p&gt;

&lt;p&gt;IEFE solves this problem by having its &lt;strong&gt;own scope and restricting functions and variables to become global&lt;/strong&gt;. The functions and variables declare inside IIFE will &lt;strong&gt;not pollute&lt;/strong&gt; global scope &lt;strong&gt;even they have same name as global&lt;/strong&gt; variables &amp;amp; functions. So let's see what is an IIFE is.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Advantages of IIFE:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Helps avoid creating unnecessary global variables and functions.&lt;/li&gt;
&lt;li&gt;Functions and variables defined in IIFE do not conflict with other functions &amp;amp; variables even if they have same name.&lt;/li&gt;
&lt;li&gt;Organize JavaScript code.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make JavaScript code maintainable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Even though the information above is more than enough and well explained to grasp the concept but you can still check out this &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; and this &lt;a href="https://benalman.com/news/2010/11/immediately-invoked-function-expression/" rel="noopener noreferrer"&gt;article&lt;/a&gt; to read more in-depth about IIFEs.*&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; keyword-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;this&lt;/code&gt; represents the &lt;strong&gt;object that the function is a property of&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or simply&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;this&lt;/code&gt; helps &lt;strong&gt;refer to the object it belongs to&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;In a method, &lt;code&gt;this&lt;/code&gt; refers to the owner object.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = {
  firstName: "Pranav",
  lastName : "Mendiratta",
  fullName : function() {
    // here `this` keyword refers to our object `person` 
    return this.firstName + " " + this.lastName;
  }
};
console.log(person.fullName())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Pranav Mendiratta"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Alone, &lt;code&gt;this&lt;/code&gt; refers to the global object (called the window object in the browser).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(this)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In a function, &lt;code&gt;this&lt;/code&gt; refers to the global object.&lt;/li&gt;
&lt;li&gt;In a function, in strict mode, &lt;code&gt;this&lt;/code&gt; is undefined.&lt;/li&gt;
&lt;li&gt;In an event, &lt;code&gt;this&lt;/code&gt; refers to the element that received the event.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onclick="this.style.display='none'"&amp;gt;
  Click to Remove Me!
&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Tricky example on &lt;code&gt;this&lt;/code&gt; keyword 1
&lt;/h3&gt;

&lt;p&gt;&lt;iframe src="https://jsfiddle.net/rkjdpuez/2//embedded/js//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window
window
c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Both &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are functions of the global/ window object, thus as per the definition, the &lt;code&gt;window&lt;/code&gt; object gets returned.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;this&lt;/code&gt; represents the object that the function is a property of.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The third &lt;code&gt;console.log&lt;/code&gt; returns the &lt;code&gt;c&lt;/code&gt; object because that's what has called the &lt;code&gt;hi()&lt;/code&gt; function in &lt;code&gt;c.hi()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;One trick to solve these easily&lt;/strong&gt; is to check what is on the &lt;strong&gt;left side of the function call&lt;/strong&gt;. If there is nothing then it returns the &lt;strong&gt;window object&lt;/strong&gt;. If some object is calling it like &lt;code&gt;c.hi()&lt;/code&gt; then the &lt;code&gt;this&lt;/code&gt; keyword in the function points to the object &lt;code&gt;c&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tricky example on &lt;code&gt;this&lt;/code&gt; keyword 2
&lt;/h3&gt;

&lt;p&gt;&lt;iframe src="https://jsfiddle.net/gfvjLo2b//embedded/js//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;obj
window
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;On calling the &lt;code&gt;sing()&lt;/code&gt; function, the &lt;code&gt;console.log(this)&lt;/code&gt; on line 4 returns the &lt;code&gt;obj&lt;/code&gt; object since &lt;code&gt;obj&lt;/code&gt; is calling the function.&lt;/li&gt;
&lt;li&gt;Whereas the &lt;code&gt;console.log(this)&lt;/code&gt; on line 6 returns the &lt;code&gt;window&lt;/code&gt; object because its &lt;strong&gt;function call is not attached to any object&lt;/strong&gt;, and those not attached are always under the global/ window object.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tricky example on &lt;code&gt;this&lt;/code&gt; keyword 3
&lt;/h3&gt;

&lt;p&gt;&lt;iframe src="https://jsfiddle.net/yut8g7La//embedded/js//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;b
window
d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Using the trick we learned in tricky example 1, we see that &lt;code&gt;b.say()&lt;/code&gt; should return the &lt;code&gt;b&lt;/code&gt; object and it does exactly that.&lt;/li&gt;
&lt;li&gt;Arrow functions are lexically scoped where as regular anonymous functions are dynamically scoped.&lt;/li&gt;
&lt;li&gt;That is why when calling &lt;code&gt;c.say()()&lt;/code&gt;, it returns the window object because it uses anonymous functions that are lexically scoped (we've learned in earlier part of the series). 
&amp;gt; Lexical scope care where a function was declared, but dynamic scope cares where a function was called from.&lt;/li&gt;
&lt;li&gt;This statement will help in understanding the difference.&lt;/li&gt;
&lt;li&gt;The final output on calling &lt;code&gt;d.say()()&lt;/code&gt; returns the object &lt;code&gt;d&lt;/code&gt; that is the correct output because it used the arrow functions that are dynamically scoped and bind the &lt;code&gt;this&lt;/code&gt; keyword with the object calling the function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tricky example on &lt;code&gt;this&lt;/code&gt; keyword 4
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const phone = function (model, brand){
  this.model = model,
  this.brand = brand
}

// regular anonymous  function used
phone.prototype.clickPicture = function(){
  console.log(`${this.brand} ${this.model} clicks picture!`)
}

// arrow function used here
phone.prototype.powerOn = () =&amp;gt; {
  console.log(`${this.brand} ${this.model} boots up!`)
}

const iphone = new phone("Iphone 12", "Apple")
console.log(iphone.clickPicture())
console.log(iphone.powerOn())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Apple Iphone 12 clicks picture!"
"undefined undefined boots up!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arrow functions are lexically scoped&lt;/strong&gt; where as &lt;strong&gt;regular anonymous functions are dynamically scoped&lt;/strong&gt; that is why the arrow functions that are dynamically scoped and bind the &lt;code&gt;this&lt;/code&gt; keyword with the object calling the function and the other function doesn't thus logging undefined on using &lt;code&gt;this.brand&lt;/code&gt; or &lt;code&gt;this.model&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A lot of the weird behavior of the &lt;code&gt;this&lt;/code&gt; keyword is mainly because it is &lt;strong&gt;dynamically scoped and not lexically scoped&lt;/strong&gt; like everything else in JavaScript meaning that it is not important where it is written but how it is called.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Solution to weird behavior-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;One way to solve these issues is the use of &lt;strong&gt;arrow functions&lt;/strong&gt; that were introduced in ES6.&lt;/li&gt;
&lt;li&gt;If we use an arrow function in the &lt;strong&gt;previous example&lt;/strong&gt; then our function gives us the &lt;strong&gt;desired output&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Another way is to bind the &lt;code&gt;this&lt;/code&gt; keyword to the object. We will learn more about &lt;code&gt;bind&lt;/code&gt; keyword ahead.&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%2Fmzdepajp4aap56bugsww.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%2Fmzdepajp4aap56bugsww.png" alt="This" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://medium.com/@iqbal.ipel" rel="noopener noreferrer"&gt;Iqbal M Ipel&lt;/a&gt;&lt;/em&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%2Fn4i1abr1nmt4962robct.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%2Fn4i1abr1nmt4962robct.png" alt="Function" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://dev.to/thesanjeevsharma"&gt;Sanjeev Sharma&lt;/a&gt;&lt;/em&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  call()
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;With the &lt;code&gt;call()&lt;/code&gt; method, you can write a method that can be used on different objects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const wizard = {
  name: 'Pranav',
  health: 100,
  heal: function(num1, num2) {
    this.health += num1 + num2;
  }
}

const archer = {
  name: 'Robin',
  health: 50
}

wizard.heal.call(archer, 50, 60)
console.log(archer)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  health: 160,
  name: "Robin"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  apply()
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;With the apply() method, you can write a method that can be used on different objects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;It is very similar to the &lt;code&gt;call&lt;/code&gt; keyword, only difference is that the arguments are passed as an array when we are using &lt;code&gt;apply&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const wizard = {
  name: 'Pranav',
  health: 100,
  heal: function(num1, num2) {
    this.health += num1 + num2;
  }
}

const archer = {
  name: 'Robin',
  health: 50
}

wizard.heal.apply(archer, [20, 30])
console.log(archer)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  health: 100,
  name: "Robin"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  bind()
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The bind() method creates a new function that, when called, has its this keyword set to the provided value. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It let’s us explicitly define the value of this when calling a function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It returns a new function that we can call.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const wizard = {
  name: 'Pranav',
  health: 100,
  heal: function(num1, num2) {
    this.health += num1 + num2;
  }
}

const archer = {
  name: 'Robin',
  health: 50
}

const healArcher = wizard.heal.bind(archer, 50, 60);
healArcher()
console.log(archer)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The js engine is creating a new instance of the heal function and binding its &lt;code&gt;this&lt;/code&gt; object to archer.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  health: 160,
  name: "Robin"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F87k9zcky6o4siop6jw5x.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%2F87k9zcky6o4siop6jw5x.png" alt="CallApply" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://hdsatija.medium.com/" rel="noopener noreferrer"&gt;Himanshu Satija&lt;/a&gt;&lt;/em&gt; &lt;/p&gt;




&lt;h2&gt;
  
  
  Currying-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Currying is a technique of evaluating function with multiple arguments, into sequence of functions with single argument.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Example 1-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function volume(length) {
      return function(width) {
         return function(height) {
            return height * width * length;
         }
      }
   }
console.log(volume(11)(2)(3))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;66
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 2-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(a, b) {
    return a+b;
}

var sumWithThree = sum.bind(this, 3);
console.log(sumWithThree(4));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Partial Application-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Partial application starts with a function. We take this function and create a new one with one or more of its arguments already “set” or &lt;strong&gt;partially applied&lt;/strong&gt;. It will help reduce the number of parameters needed for our functions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Both currying and partial application are patterns that &lt;strong&gt;allow us to call functions&lt;/strong&gt; with some of their parameters, and provide the rest later.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;They both are important concepts in &lt;strong&gt;Functional programming&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiply = (a, b, c) =&amp;gt; a * b * c
const partialMultiplyBy5 = multiply.bind(null, 5)
partialMultiplyBy5(10, 20)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Difference b/w Currying and Partial Application-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Partial application is more or less a pattern of &lt;strong&gt;calling&lt;/strong&gt; a function. You can &lt;strong&gt;partially apply any function&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Currying is more &lt;strong&gt;about a form of the function&lt;/strong&gt;. To be able to use currying, you have to &lt;strong&gt;explicitly create a new function&lt;/strong&gt; that is a curried version of the original one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Advantages of using Currying or Partial Application-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;They both help us &lt;strong&gt;create specialized versions of generic functions&lt;/strong&gt;, thus &lt;strong&gt;removing duplication&lt;/strong&gt; and making the code easier to compose.&lt;/li&gt;
&lt;li&gt;Another benefit of using partial application and currying is that they can help us create more &lt;strong&gt;readable code&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  References-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Glossary/IIFE&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialsteacher.com/javascript/immediately-invoked-function-expression-iife" rel="noopener noreferrer"&gt;https://www.tutorialsteacher.com/javascript/immediately-invoked-function-expression-iife&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_this.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_this.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_function_call.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_function_call.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_function_apply.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_function_apply.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@omergoldberg/javascript-call-apply-and-bind-e5c27301f7bb" rel="noopener noreferrer"&gt;https://medium.com/@omergoldberg/javascript-call-apply-and-bind-e5c27301f7bb&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/what-is-currying-in-javascript" rel="noopener noreferrer"&gt;https://www.tutorialspoint.com/what-is-currying-in-javascript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/how-to-use-partial-application-to-improve-your-javascript-code-5af9ad877833/" rel="noopener noreferrer"&gt;https://www.freecodecamp.org/news/how-to-use-partial-application-to-improve-your-javascript-code-5af9ad877833/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/dailyjs/functional-js-5-partial-application-currying-da30da4e0cc3" rel="noopener noreferrer"&gt;https://medium.com/dailyjs/functional-js-5-partial-application-currying-da30da4e0cc3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://medium.com/@osmanakar_65575/javascript-lexical-and-dynamic-scoping-72c17e4476dd#:%7E:text=The%20Key%20Contrast%20Between%20Lexical,a%20function%20was%20called%20from" rel="noopener noreferrer"&gt;https://medium.com/@osmanakar_65575/javascript-lexical-and-dynamic-scoping-72c17e4476dd#:~:text=The%20Key%20Contrast%20Between%20Lexical,a%20function%20was%20called%20from&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 4.2: Scope Chains and their working, Lexical and Variable Environments</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Sun, 16 Jan 2022 20:25:36 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5</guid>
      <description>&lt;h2&gt;
  
  
  What is a Scope Chain?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The Scope Chain is the hierarchy of scopes that will be searched in order to find a function or variable.&lt;/p&gt;
&lt;/blockquote&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%2Fkz8mt8i5eo0plhotv8j1.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%2Fkz8mt8i5eo0plhotv8j1.png" alt="Scope Chain" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://hashnode.com/@anuradha" rel="noopener noreferrer"&gt;Anuradha Aggarwal&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a variable is used in JavaScript, the &lt;strong&gt;JavaScript engine&lt;/strong&gt; will try to find the variable’s value in the current scope. If it could not find the variable, it will look into the outer scope and will continue to do so until it finds the variable or reaches &lt;strong&gt;global scope&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If it’s still could not find the variable, it will either &lt;strong&gt;implicitly declare the variable&lt;/strong&gt; in the global scope (if not in strict mode) or return an &lt;strong&gt;error&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The Scope Chain is used to &lt;strong&gt;resolve variables&lt;/strong&gt;. When asked to resolve a variable, JavaScript always starts at the &lt;strong&gt;innermost level&lt;/strong&gt; of the code nest and keeps jumping back to the parent scope until it finds the variable or any other resource it is looking for. &lt;/li&gt;
&lt;li&gt;The scope chain can simply be defined as an &lt;strong&gt;object&lt;/strong&gt; that contains a bunch of other objects. Each object has the &lt;strong&gt;variable-to-value mapping&lt;/strong&gt; for its particular execution context.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let c = 10
function a() {
  let b = 25;  
  console.log('Inside function a()');
}
a();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Sample Scope chain object for the function &lt;code&gt;a&lt;/code&gt;-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;functionLexicalEnvironment = {
  environmentRecord: {
      b    : 25,
  }
  outer: {
    c  : 10,
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Lexical Environment-
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A lexical environment is a structure that holds &lt;strong&gt;identifier-variable mapping&lt;/strong&gt;. &lt;br&gt;
(here identifier refers to the name of variables/functions, and the variable is the reference to actual object [including function object and array object] or primitive value).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simply put, a lexical environment is a place where &lt;strong&gt;variables and references to the objects&lt;/strong&gt; are stored.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A lexical environment conceptually looks like this:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lexicalEnvironment = {
  environmentRecord: {
    &amp;lt;identifier&amp;gt; : &amp;lt;value&amp;gt;,
    &amp;lt;identifier&amp;gt; : &amp;lt;value&amp;gt;
  }
  outer: &amp;lt; Reference to the parent lexical environment&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;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 plaintext"&gt;&lt;code&gt;let language = 'JS';
function a() {
  let b = 25;  
  console.log('Inside function a()');
}
a();
console.log('Inside global execution context');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The JavaScript engine creates a new &lt;strong&gt;lexical environment&lt;/strong&gt; to store the variables and functions defined in the global scope when it establishes a &lt;strong&gt;global execution context&lt;/strong&gt; to execute global code. As a result, the lexical environment for the global scope will be as follows:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;globalLexicalEnvironment = {
  environmentRecord: {
      language    : 'JS',
      a : &amp;lt; reference to function object &amp;gt;
  }
  outer: null
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Because there is no outer &lt;strong&gt;lexical environment&lt;/strong&gt; for the global scope, the outer lexical environment is set to &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the engine establishes an &lt;strong&gt;execution context&lt;/strong&gt; for the &lt;code&gt;a()&lt;/code&gt; function, it also creates a lexical environment in which variables defined in the function can be stored while the function is being executed. As a result, the function's &lt;strong&gt;lexical environment&lt;/strong&gt; will look like this:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;functionLexicalEnvironment = {
  environmentRecord: {
      b    : 25,
  }
  outer: &amp;lt;globalLexicalEnvironment&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Because the function is surrounded by the &lt;strong&gt;global scope&lt;/strong&gt; in the source code, the function's outer lexical environment is set to the global lexical environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When a function finishes executing, its execution context is removed from the stack, but its lexical environment &lt;strong&gt;may or may not be erased from memory&lt;/strong&gt;, depending on whether it is referenced by any other lexical environments in their &lt;strong&gt;outer lexical environment property&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Variable Environment-
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;The variable environment is a representation of the &lt;strong&gt;lexical environment’s local memory&lt;/strong&gt;. In the environment record, the lexical environment stores variables as well as other information such as the infamous this.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We've previously used one variable environment, the &lt;strong&gt;global environment's memory&lt;/strong&gt;, which holds variables that are universally available throughout the script. While the lexical environment refers to this global environment, the variable environment only refers &lt;strong&gt;to variables created within the scope&lt;/strong&gt; of the provided function within the lexical environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The variable environment &lt;strong&gt;maps the local scope&lt;/strong&gt; of a given environment. In other words, the variable environment stores those variables defined within the given working code block &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;&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%2Folf5ymfxixmr88y5i2jf.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%2Folf5ymfxixmr88y5i2jf.png" alt="Img" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Credits-  &lt;a href="https://stackoverflow.com/users/1348195/benjamin-gruenbaum" rel="noopener noreferrer"&gt;Benjamin Gruenbaum&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  References-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://anuradha.hashnode.dev/scope-chain-and-lexical-environment-in-javascript" rel="noopener noreferrer"&gt;https://anuradha.hashnode.dev/scope-chain-and-lexical-environment-in-javascript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.bitsrc.io/understanding-scope-and-scope-chain-in-javascript-f6637978cf53" rel="noopener noreferrer"&gt;https://blog.bitsrc.io/understanding-scope-and-scope-chain-in-javascript-f6637978cf53&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@bdov_/javascript-typescript-execution-vs-lexical-vs-variable-environment-37ff3f264831" rel="noopener noreferrer"&gt;https://medium.com/@bdov_/javascript-typescript-execution-vs-lexical-vs-variable-environment-37ff3f264831&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/20721626/value-of-variable-and-lexical-environment-after-creating-execution-context" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/20721626/value-of-variable-and-lexical-environment-after-creating-execution-context&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
      <category>react</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 4.1: Global, Function and Block Scope, Lexical vs Dynamic Scoping</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Sun, 09 Jan 2022 17:25:03 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg</guid>
      <description>&lt;h2&gt;
  
  
  What is Scope?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In simple terms, &lt;strong&gt;Scope&lt;/strong&gt; can be defined as the space in which variables and statements &lt;strong&gt;are accessible&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scope determines &lt;strong&gt;the accessibility&lt;/strong&gt; of variables, objects, and functions from different parts of the code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Let's understand this definition with an example- &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 2

function myFunc(){
  console.log(x)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;myFunc function is able to access the variable &lt;code&gt;x&lt;/code&gt; thus we can say that &lt;code&gt;x&lt;/code&gt; is in scope of myFunc.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Before ES6 (2015), there were only 2 types of scope (Global and Function) but in ES6, a new scope was introduced, namely the &lt;strong&gt;Block Scope&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3 types of Scopes:
&lt;/h2&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%2F2xounixbcpualerc4k8z.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%2F2xounixbcpualerc4k8z.png" alt="Scope" width="724" height="724"&gt;&lt;/a&gt;&lt;em&gt;Credits-&lt;a href="https://dev.to/nemo011"&gt;Nemo&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Global Scope-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Variables declared globally/ in the global execution context have a global scope.&lt;/li&gt;
&lt;li&gt;They can be &lt;strong&gt;accessed from anywhere&lt;/strong&gt; in the program.&lt;/li&gt;
&lt;li&gt;No matter whether they are declared using &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt;, variables declared in global scope behave similarly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 2

function myFunc(){
  console.log(x)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Here the variable &lt;code&gt;x&lt;/code&gt; is declared in global scope thus it is available for use across the entire program.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As explained in &lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;Part 3&lt;/a&gt; of this Advanced JavaScript series, if a variable is declared without &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword, it always gets declared in the &lt;strong&gt;global scope&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunc(){
  x = 1
}
console.log(x)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Here the code gives output &lt;code&gt;1&lt;/code&gt; since the variable &lt;code&gt;x&lt;/code&gt; gets declared in Global scope.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Function/Local Scope-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Variables declared within a JavaScript function, become &lt;strong&gt;LOCAL&lt;/strong&gt; to the function.&lt;/li&gt;
&lt;li&gt;These variables can only be &lt;strong&gt;accessible from within&lt;/strong&gt; the function.&lt;/li&gt;
&lt;li&gt;These variables are &lt;strong&gt;removed from the memory&lt;/strong&gt; when the execution of the function is completed thus the variable names can be reused in some other functions.&lt;/li&gt;
&lt;li&gt;All &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; work similarly in Function scope. &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunc(){
  let x = 1
  console.log(x)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Here the variable &lt;code&gt;x&lt;/code&gt; is declared in the function/local scope thus accessible only from inside the function.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Block Scope-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The two new keywords for variable declaration, that is &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; that were introduced in ES6 are Block Scoped.&lt;/li&gt;
&lt;li&gt;Any variable that is declared within a pair of braces { } or a block and cannot be accessed from outside it are said to be &lt;strong&gt;Block Scoped&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 1
if(x){
  var y = 2
  let z = 3
  console.log("hello world")
}
console.log(y)
console.log(z)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
Uncaught ReferenceError: z is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Here, the variable &lt;code&gt;y&lt;/code&gt; cannot be accessed from outside the &lt;code&gt;if block&lt;/code&gt; because variables declared using &lt;code&gt;let&lt;/code&gt; are Block Scoped whereas variables declared using &lt;code&gt;var&lt;/code&gt; are not.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical vs Dynamic Scoping-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;strong&gt;Lexical(Static) Scoping&lt;/strong&gt;, the structure of the program source code determines what variables you are referring to.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;Dynamic Scoping&lt;/strong&gt;, the runtime state of the program stack determines what variable you are referring to.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Lexical scoping&lt;/strong&gt; refers to the variables in the location of a function's definition, whereas &lt;strong&gt;dynamic scoping&lt;/strong&gt; refers to the variables in the location of a function's invocation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Let's understand with the help of an example.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function a() { 
    console.log(i);
}

function b() {
    var i = 1;
    a();
}

var i = 0;

b();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation-
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;We have two function, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;. &lt;code&gt;a&lt;/code&gt; logs out the value of &lt;code&gt;i&lt;/code&gt; which is set globally to &lt;code&gt;0&lt;/code&gt;. &lt;code&gt;b&lt;/code&gt; sets its value to &lt;code&gt;1&lt;/code&gt;, and calls &lt;code&gt;a&lt;/code&gt;. If we call &lt;code&gt;b&lt;/code&gt;, it will log out &lt;code&gt;0&lt;/code&gt;. This is because — while &lt;code&gt;a&lt;/code&gt; doesn't have a variable called &lt;code&gt;i&lt;/code&gt; — &lt;code&gt;a&lt;/code&gt; has access to the enclosing scope where the function is defined. And in the global scope, we have an &lt;code&gt;i&lt;/code&gt; which is set to &lt;code&gt;0&lt;/code&gt;. This is called lexical scoping.&lt;/p&gt;

&lt;p&gt;In dynamic scoping, the value of &lt;code&gt;i&lt;/code&gt; will be &lt;code&gt;1&lt;/code&gt;. This is because instead of looking at where the function is defined, it looks at where it is called from. It sees from the call stack that it has been called from &lt;code&gt;b&lt;/code&gt;, which sets the value of &lt;code&gt;i&lt;/code&gt; to be &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;As you can see, lexical scope looks at where a &lt;strong&gt;function is declared&lt;/strong&gt;, where dynamic scope refers to where a &lt;strong&gt;function is called&lt;/strong&gt; from.&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%2Fr4ix3u3wqemix3cfi2sj.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%2Fr4ix3u3wqemix3cfi2sj.png" alt="Scoping" width="800" height="400"&gt;&lt;/a&gt;&lt;em&gt;Credits-&lt;a href="https://www.slideshare.net/thangtd90/javascript-under-the-hood-1" rel="noopener noreferrer"&gt;Thang Tran Duc&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  References-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_scope.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_scope.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/22394089/static-lexical-scoping-vs-dynamic-scoping-pseudocode" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/22394089/static-lexical-scoping-vs-dynamic-scoping-pseudocode&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.webtips.dev/webtips/javascript-interview/lexical-vs-dynamic-scoping" rel="noopener noreferrer"&gt;https://www.webtips.dev/webtips/javascript-interview/lexical-vs-dynamic-scoping&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
      <category>react</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 3: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Fri, 07 Jan 2022 09:58:51 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3</guid>
      <description>&lt;h2&gt;
  
  
  Weird JS Behavior
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code-
&lt;/h3&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%2Fimiqqgcpwue8uyrd13gq.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%2Fimiqqgcpwue8uyrd13gq.png" alt="Code" width="800" height="400"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Output-
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;50
50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;In the code sample, we didn't even explicitly declare the variable but we are able to use without any error and it is available in global scope&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Explanation-
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Older versions of JS allowed us to create variables &lt;strong&gt;without explicitly declaring&lt;/strong&gt; them using the &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; or the &lt;code&gt;const&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;There are a lot of downfalls to this, some of them being-&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Downfalls-
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;JS creates these variables in &lt;strong&gt;global scope&lt;/strong&gt; by default thus anyone can access them from outside the function and change them.&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;mistype&lt;/strong&gt; a variable name and JS won't even give an &lt;strong&gt;error&lt;/strong&gt;, instead it will create a new variable in the global scope because of this behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Solution: &lt;em&gt;Strict Mode&lt;/em&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Introduction-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The "use strict" directive was new in ECMAScript version 5 indicating use of &lt;strong&gt;strict mode&lt;/strong&gt; while running the code.&lt;/li&gt;
&lt;li&gt;It is supported by all the modern browsers and since it is only a string, &lt;strong&gt;even older versions&lt;/strong&gt; that don't understand it won't throw any error.&lt;/li&gt;
&lt;li&gt;It prevents all the &lt;strong&gt;bad code practices&lt;/strong&gt; in previous JS versions from turning into actual errors.&lt;/li&gt;
&lt;li&gt;If declared at the beginning of a script, it &lt;strong&gt;has global scope&lt;/strong&gt; whereas if it is used inside the function then it's scope is only for that block/&lt;strong&gt;block scope&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Declaration example-&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";
x = 3.14;  // this will cause error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Issues that "use strict" fixes-
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;If you mistakenly mistype a variable, if ran in strict mode, it will &lt;strong&gt;throw an error&lt;/strong&gt; instead of creating a new global variable.&lt;/li&gt;
&lt;li&gt;It prevents us from assigning values to &lt;strong&gt;non-writable properties&lt;/strong&gt; by throwing an error. This wasn't the same in previous versions.&lt;/li&gt;
&lt;li&gt;Keywords reserved for future JavaScript versions can not be used as variable names in strict mode.&lt;/li&gt;
&lt;li&gt;Prevents us from &lt;strong&gt;duplicating parameter names&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Prevents us from writing to a read-only properties.&lt;/li&gt;
&lt;li&gt;Prevents us from writing to a get-only property.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";
const obj = {get x() {return 0} };

obj.x = 3.14;            // This will cause an error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7.Prevents us from &lt;strong&gt;deleting an undeletable property&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";
delete Object.prototype; // This will cause an error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;8.Prevents us from using &lt;strong&gt;Octal&lt;/strong&gt; numerical literals and Octal escape characters. Example-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";
let x = 010; // gives error
let x = "\010"; // gives error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Check this &lt;a href="https://www.w3schools.com/js/js_strict.asp" rel="noopener noreferrer"&gt;article&lt;/a&gt; for all the things that are not allowed in "use strict".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note- The "use strict" directive is only recognized at the beginning of a script or a function.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Hoisting-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Hoisting is JavaScript's default behavior of moving all the declarations at the &lt;strong&gt;top of the scope&lt;/strong&gt; before code execution.&lt;/li&gt;
&lt;li&gt;It could be &lt;strong&gt;variable&lt;/strong&gt; declarations or &lt;strong&gt;function&lt;/strong&gt; declarations or even class declarations.
&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%2Fgxhki733f22qn6ttrbzn.png" alt="Hoisting" width="317" height="147"&gt;
&lt;em&gt;Credits-&lt;a href="https://www.tutorialsteacher.com/javascript/javascript-hoisting" rel="noopener noreferrer"&gt;tutorialsteacher&lt;/a&gt;&lt;/em&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example of variable hoisting-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = 5 // doesn't give any error because of hoisting
console.log(x)

var x // this gets hoisted to the top of the scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example of function hoisting-
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(hello()) // doesn't give any error because of hoisting

function hello(){ // this gets hoisted to the top of the scope
    return "hello world"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello world"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are also hoisted but, unlike &lt;code&gt;var&lt;/code&gt;, are not initialized with a default value such as &lt;code&gt;undefined&lt;/code&gt;. A &lt;code&gt;ReferenceError&lt;/code&gt; exception will be thrown if a variable declared with &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; is read before it is initialized. This is because they stay in a &lt;strong&gt;Temporal Dead Zone&lt;/strong&gt; before they are explicitly declared. We will learn more about Temporal Dead Zone ahead. &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(x)

let x
x = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught ReferenceError: Cannot access 'x' before initialization
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(x)

const x = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught ReferenceError: Cannot access 'x' before initialization
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;All JavaScript &lt;strong&gt;declarations are hoisted&lt;/strong&gt; but not for initialization. &lt;strong&gt;Initialization&lt;/strong&gt; in variables using &lt;code&gt;var&lt;/code&gt; keyword are &lt;strong&gt;partially hoisted&lt;/strong&gt; but those using &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword are not hoisted at all and give error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Partial hoisting&lt;/strong&gt; means that the JS engine before running the code line by line already knows that the &lt;strong&gt;variable exists&lt;/strong&gt; and has some memory allocated (because of hoisting) but the value for it hasn't been set/ stored yet (it gets set when we actually &lt;strong&gt;reach that line of code&lt;/strong&gt;) thus a default value of &lt;code&gt;undefined&lt;/code&gt; is set and returned. This partial hoisting happens in case of variable &lt;strong&gt;initialization using &lt;code&gt;var&lt;/code&gt;&lt;/strong&gt; keyword.&lt;br&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%2F28a00s8npi7rjy930p66.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%2F28a00s8npi7rjy930p66.png" alt="Partial Hoisting" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://medium.com/@sabih811" rel="noopener noreferrer"&gt;Sabih Rehman&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 1
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(x)

var x = 5 // this is initialization, not a declaration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This code does not work because initializations are not hoisted. It returns &lt;code&gt;undefined&lt;/code&gt; because we have used &lt;code&gt;var&lt;/code&gt; here that leads to partial hoisting as discussed above.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(x)

let x = 5 // this is initialization, not a declaration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught ReferenceError: Cannot access 'x' before initialization"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This is because variable initialization using &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; don't get hoisted.&lt;/em&gt;&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;The variables declared using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are not accessible before they are initialized with some value, and the phase between the starting of the execution of block in which the variable is declared using &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; &lt;strong&gt;till that variable is being initialized&lt;/strong&gt; is called Temporal Dead Zone for the variable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Accessing the variable before the initialization results in a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError" rel="noopener noreferrer"&gt;ReferenceError&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(x)

let x
x = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught ReferenceError: Cannot access 'x' before initialization
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The term "temporal" is used because the &lt;strong&gt;zone depends on the order of execution&lt;/strong&gt; (time) rather than the order in which the code is written (position). For example, the code below works because, even though the function that uses the &lt;code&gt;let&lt;/code&gt; variable appears before the variable is declared, the function is called outside the TDZ.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Code-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    // TDZ starts at beginning of scope
    const func = () =&amp;gt; console.log(letVar); // OK

    // Within the TDZ letVar access throws `ReferenceError`

    let letVar = 3; // End of TDZ (for letVar)
    func(); // Called outside TDZ!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output-
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Temporal Dead Zone tricky example-
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function test(){
   var foo = 33;
   if(foo) {
      let foo = (foo + 55); // ReferenceError
   }
}
test();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;if&lt;/code&gt; block is evaluated because the outer &lt;code&gt;var foo&lt;/code&gt; has a value. However due to lexical scoping this value is not available inside the block: the identifier &lt;code&gt;foo&lt;/code&gt; inside the &lt;code&gt;if&lt;/code&gt; block is the &lt;code&gt;let foo&lt;/code&gt;. The expression &lt;code&gt;(foo + 55)&lt;/code&gt; throws a &lt;code&gt;ReferenceError&lt;/code&gt; because initialization of &lt;code&gt;let foo&lt;/code&gt; has not completed — it is still in the &lt;strong&gt;temporal dead zone&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  References-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_strict.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_strict.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_hoisting.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_hoisting.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Glossary/Hoisting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.geeksforgeeks.org/what-is-the-temporal-dead-zone-in-es6/#:%7E:text=The%20let%20and%20const%20variables,Dead%20Zone%20for%20the%20variable" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/what-is-the-temporal-dead-zone-in-es6/#:~:text=The%20let%20and%20const%20variables,Dead%20Zone%20for%20the%20variable&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;All codes implemented using JS Fiddle&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
      <category>react</category>
    </item>
    <item>
      <title>Advanced JavaScript Series - Part 1: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)</title>
      <dc:creator>Pranav</dc:creator>
      <pubDate>Wed, 05 Jan 2022 09:46:16 +0000</pubDate>
      <link>https://forem.com/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj</link>
      <guid>https://forem.com/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction-
&lt;/h2&gt;

&lt;p&gt;JavaScript is a single-threaded, synchronous programming language. It means that when a script is run, the JS engine runs the code line by line, beginning at the top and working its way down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Behind the scenes-
&lt;/h2&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%2Ftb8ol66g9infpetxsgiz.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%2Ftb8ol66g9infpetxsgiz.png" alt="Flowchart1" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/" rel="noopener noreferrer"&gt;Yair Cohen&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. JavaScript Engine
&lt;/h3&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%2F94ww1vgzutji2qoke4q7.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%2F94ww1vgzutji2qoke4q7.png" alt="Flowchart2" width="700" height="374"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/" rel="noopener noreferrer"&gt;Yair Cohen&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every JavaScript program requires a specific environment to execute because our computers and other machines do not understand JavaScript syntax.&lt;/li&gt;
&lt;li&gt;They only understand &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/Machine_code" rel="noopener noreferrer"&gt;Machine Code&lt;/a&gt;&lt;/em&gt; thus every environment has an engine that converts this JS human understandable syntax to Machine Code.&lt;/li&gt;
&lt;li&gt;There are many different engines available out there with the most popular being the Google Chrome's V8 engine, Firefox SpiderMonkey, JavaScriptCore by Safari etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/ECMAScript" rel="noopener noreferrer"&gt;ECMAScript&lt;/a&gt;&lt;/strong&gt; is a JavaScript standard that helps ensure interoperability of JS web pages by keeping a check on how all the different engines interpret the JavaScript language.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Parser/ Syntax Parser
&lt;/h3&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%2Fs42zl32gr7x5oraf5mqm.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%2Fs42zl32gr7x5oraf5mqm.png" alt="Flowchart3" width="700" height="374"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/" rel="noopener noreferrer"&gt;Yair Cohen&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every JS engine has a parser in it which knows all the JS syntax rules and checks for any syntax or grammar errors.&lt;/li&gt;
&lt;li&gt;If found, it gives out an error otherwise the Parser generates an &lt;strong&gt;Abstract Syntax Tree&lt;/strong&gt; that is then passed on to aid the code execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Abstract Syntax Tree (AST)
&lt;/h3&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%2F74js34tooi6v63t1hftr.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%2F74js34tooi6v63t1hftr.png" alt="Flowchart4" width="700" height="374"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/" rel="noopener noreferrer"&gt;Yair Cohen&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is a tree like structural representation of the JS code.&lt;/li&gt;
&lt;li&gt;The main purpose for creating an AST is that it helps understand the code better and helps make the translation to machine code much easier.&lt;/li&gt;
&lt;li&gt;You can see how an AST is formed &amp;amp; represented on &lt;a href="https://astexplorer.net/" rel="noopener noreferrer"&gt;AST Explorer&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Interpreter
&lt;/h3&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%2Fht9ofhqklm5lifq7y1au.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%2Fht9ofhqklm5lifq7y1au.png" alt="Flowchart5" width="700" height="374"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/" rel="noopener noreferrer"&gt;Yair Cohen&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The interpreter takes the AST and parses and transform it into an &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Intermediate_representation#:~:text=An%20intermediate%20representation%20%28IR%29%20is,such%20as%20optimization%20and%20translation." rel="noopener noreferrer"&gt;Intermediate Representation&lt;/a&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Intermediate Representation-
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Intermediate Representation acts as an intermediate step between translation from an abstract language such as JS to machine code.&lt;/li&gt;
&lt;li&gt;The most famous Intermediate representation amongst JS engines is &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Bytecode" rel="noopener noreferrer"&gt;Bytecode&lt;/a&gt;&lt;/strong&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%2Fy5y78aw418qng2k8zszi.png" alt="IR" width="300" height="190"&gt;
&lt;em&gt;Credits- &lt;a href="https://auth.geeksforgeeks.org/user/Satyabrata_Jena/articles" rel="noopener noreferrer"&gt;Satyabrata Jena&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Need for Intermediate Representation(IR)-
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Unlike Machine code that are hardware dependent, IRs are universal thus allowing more mobility and easier conversions.&lt;/li&gt;
&lt;li&gt;It is easier to optimize code when it is in IR than it being in machine code. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  5. Compiler
&lt;/h3&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%2F10jk02xso6zdd34mehpx.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%2F10jk02xso6zdd34mehpx.png" alt="Flowchart6" width="700" height="374"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/" rel="noopener noreferrer"&gt;Yair Cohen&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The main purpose of a compiler is to take the Intermediate representation received from the previous step, perform optimizations and then convert it to Machine code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Difference between Interpreter and a Compiler
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An interpreter and a compiler are different in the way that an interpreter translates your code and runs it line by line, whereas a compiler instantaneously converts all code into machine code before executing it.&lt;/li&gt;
&lt;li&gt;Each has advantages and disadvantages; a &lt;strong&gt;compiler is quick but complex&lt;/strong&gt; and difficult to start, whereas an &lt;strong&gt;interpreter is slower but simpler&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;With that in mind, there are &lt;strong&gt;three methods for converting high-level code to machine code&lt;/strong&gt; and running it:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Interpretation&lt;/strong&gt; – this technique uses an interpreter to go through the code line by line and execute it (not so efficient).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ahead of Time Compilation (AOT)&lt;/strong&gt; - entails a compiler first compiling and then executing the complete code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Just-In-Time Compilation (JIT)&lt;/strong&gt; — A hybrid of the AOT and interpretation strategies, a JIT compilation approach aims to combine the best of both worlds by performing dynamic compilation while also allowing for optimizations, resulting in a compilation process that is significantly sped up.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;A JIT compiler is used by the majority of JS engines, although not all of them.&lt;/li&gt;
&lt;li&gt;Checkout this &lt;a href="https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/" rel="noopener noreferrer"&gt;article&lt;/a&gt; for a more complete explanation on the topic.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Extras-
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Hidden Classes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;As we all know, JavaScript is a dynamic programming language.&lt;/li&gt;
&lt;li&gt;While this is a benefit of JavaScript's dynamic nature, it also has a disadvantage. In memory, JS objects are stored in what is known as a &lt;strong&gt;HASH TABLE&lt;/strong&gt;. When compared to the contiguous buffer method used in non-dynamic programming languages, retrieving a property on an object with hash tables is substantially slower.&lt;/li&gt;
&lt;li&gt;Hidden classes, a mechanism provided by the V8 engine, gives the answer. &lt;strong&gt;Hidden classes&lt;/strong&gt; are used to reduce the time it takes to retrieve a property from an object. This is &lt;strong&gt;accomplished by sharing hidden classes across similar-looking objects.&lt;/strong&gt;
When a JavaScript object is created, a hidden class is assigned to it.&lt;/li&gt;
&lt;li&gt;The length of an offset to reach the hidden class can easily be determined based on the property’s type, whereas this is not possible in JavaScript where a property’s type can change &lt;strong&gt;during runtime&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Hidden Classes are attached at &lt;strong&gt;runtime&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;When a property is introduced to an object, a &lt;strong&gt;"class transition"&lt;/strong&gt; occurs, in which the previous hidden class is replaced by a new hidden class that includes the new property. Let's look at an example to help you understand.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function cupcake(frosting,sprinkles) {
    this.frosting = frosting;
    this.sprinkles = sprinkles;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We have a constructor function cupcake that takes as argument the frosting type and the sprinkles type and whenever this function is invoked; we get an object that is our new Cupcake!&lt;/li&gt;
&lt;li&gt;V8 creates a hidden class called Class0 when it sees our cupcake function is declared. When V8 notices that frosting has been added as a property on the cupcake on line 2, it changes class0 with the new frosting property and switches from class0 to a new hidden class called class1. The same happens when sprinkles are added to the cupcake and class transition occurs from class1 to class2.&lt;/li&gt;
&lt;li&gt;Checkout this &lt;a href="https://medium.com/swlh/writing-optimized-code-in-js-by-understanding-hidden-classes-3dd42862ad1d" rel="noopener noreferrer"&gt;article&lt;/a&gt; for a more in-depth explanation on hidden classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Inline Caching
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Inline caching&lt;/strong&gt; relies upon the observation that repeated calls to the same method tend to occur on the same type of object. [2]&lt;/li&gt;
&lt;li&gt;V8 keeps a cache of the types of objects that have been &lt;strong&gt;supplied as parameters&lt;/strong&gt; in recent method calls and uses that data to guess what type of object will be passed as a parameter in the future. &lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;V8&lt;/strong&gt; can make a good guess about the type of object that will be provided to a method, it can skip the process of finding out how to access the object's properties and instead &lt;strong&gt;rely on previously stored&lt;/strong&gt; information from lookups to the hidden class.
&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%2Fb485rs95wr3sn6lzhfap.png" alt="Objects" width="390" height="200"&gt;
&lt;em&gt;Credits- &lt;a href="https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/" rel="noopener noreferrer"&gt;Yair Cohen&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Relation between Hidden classes and Inline caching
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;When a method on a specific object is called, the &lt;strong&gt;V8 engine&lt;/strong&gt; must look up the hidden class of that object to calculate the offset for accessing a specific attribute. V8 skips the hidden class lookup after two successful calls to the same hidden class and simply adds the offset of the property to the object pointer itself. The V8 engine thinks that the &lt;strong&gt;hidden class&lt;/strong&gt; hasn't changed for all subsequent calls to that method, and leaps directly into the memory address for a given field using offsets recorded from prior lookups, considerably &lt;strong&gt;increasing execution performance&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The importance of objects of the same type &lt;strong&gt;sharing hidden classes&lt;/strong&gt; is due to inline caching. V8 will not be able to use &lt;strong&gt;inline caching&lt;/strong&gt; if you create two objects of the same type but with different hidden classes (as we did in the previous example). This is because, despite the fact that the two objects are of the same type, their corresponding hidden classes assign different offsets to their properties.&lt;/li&gt;
&lt;li&gt;JS being &lt;strong&gt;dynamically typed&lt;/strong&gt;, occasionally the hidden class assumption about the object might be wrong. In that case V8 goes for the original call that is searching from the Hash Table that makes the data fetching slower.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Optimizations to take advantage of Hidden Classes and Inline Caching-
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Try to assign all properties of an object in its constructor.&lt;/li&gt;
&lt;li&gt;If still(for some reason), you are dynamically adding new properties to the objects, always instantiate them in the &lt;strong&gt;SAME ORDER so that hidden classes may be shared&lt;/strong&gt; among them because then the V8 engine is able to predict them thus assigning the same hidden class to both the objects.&lt;/li&gt;
&lt;li&gt;Below is an example of a good and a bad practice for this use case-&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Bad Practice-
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1  function Point(x,y) {
2    this.x = x;
3    this.y = y;
4  }
5 
7  var obj1 = new Point(1,2);
8  var obj2 = new Point(3,4);
9
10 obj1.a = 5;
11 obj1.b = 10;
12
13 obj2.b = 10;
14 obj2.a = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Up until line 9, obj1 and obj2 shared the same hidden class. However, since properties a and b were added in opposite orders, obj1 and obj2 end up with different hidden classes.&lt;/p&gt;

&lt;h5&gt;
  
  
  Good Practice-
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1  function Point(x,y) {
2    this.x = x;
3    this.y = y;
4  }
5 
7  var obj1 = new Point(1,2);
8  var obj2 = new Point(3,4);
9
10 obj1.a = 5;
11 obj2.a = 5;
12
13 obj1.b = 10;
14 obj2.b = 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Garbage Collection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript is a &lt;strong&gt;Garbage collected language&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Meaning that if we allocate some memory inside a function, JavaScript will automatically deallocate that memory once the function finishes executing or is out of scope.&lt;/li&gt;
&lt;li&gt;But the issue of &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Memory_leak" rel="noopener noreferrer"&gt;Memory Leak&lt;/a&gt;&lt;/strong&gt; still prevails in JS like in other languages. Thus it is important to ensure good memory management on our part.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;JS collects garbage with a &lt;strong&gt;&lt;a href="https://www.geeksforgeeks.org/mark-and-sweep-garbage-collection-algorithm/" rel="noopener noreferrer"&gt;mark and sweep&lt;/a&gt;&lt;/strong&gt; method.&lt;br&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%2Fqiq0w2a6uuhz8lrvoaae.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%2Fqiq0w2a6uuhz8lrvoaae.png" alt="Mark and sweep" width="294" height="446"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Credits- &lt;a href="https://zerotomastery.io/cheatsheets/javascript-cheatsheet-the-advanced-concepts/?utm_source=udemy&amp;amp;utm_medium=coursecontent#call-stack-memory-heap" rel="noopener noreferrer"&gt;Andrei Neagoie&lt;/a&gt;&lt;/em&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%2Fi71muydk6hy8toeuj4rw.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%2Fi71muydk6hy8toeuj4rw.png" alt="Code" width="756" height="558"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;a href="https://jsfiddle.net/j7569dfh/" rel="noopener noreferrer"&gt;Open code in JS Fiddle&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In this example, a &lt;strong&gt;memory leak&lt;/strong&gt; is created. By changing the value of &lt;code&gt;person&lt;/code&gt;, we leave the previous value in the memory heap thus causing a leak.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Best practice against memory leaks are to avoid global instantiation, instead we should instantiate only inside functions, where required.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Connect with me-
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Pranav016" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/pranav-mendiratta/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Appendix-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-behind-the-scenes-javascript-engine-ats-hidden-classes-garbage-collection-3ajj"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 1&lt;/strong&gt;: Behind the scenes (JavaScript Engine, ATS, Hidden Classes, Garbage Collection)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-1-execution-context-and-call-stack-l1o"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 2&lt;/strong&gt;: Execution Context and Call Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-3-weird-js-behavior-strict-mode-and-hoisting-26a3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 3&lt;/strong&gt;: Weird JS behavior, Strict Mode and Hoisting, Temporal Dead Zone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-41-global-function-and-block-scope-lexical-vs-dynamic-scoping-20pg"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.1&lt;/strong&gt;: Global, Function and Block Scope, Lexical vs Dynamic Scoping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-42-scope-chains-and-their-working-lexical-and-variable-environments-19d5"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 4.2&lt;/strong&gt;: Scope Chains and their working, Lexical and Variable Environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-5-iife-this-keyword-in-jstricky-eg-call-apply-bind-curryingfunctional-prog-98c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 5&lt;/strong&gt;: IIFE &amp;amp; 'this' keyword in JS(tricky Eg.), call(), apply(), bind(), Currying(Functional Prog)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-61-everything-in-js-is-an-object-primitive-non-primitive-types-1d8c"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.1&lt;/strong&gt;: Everything in JS is an Object? Weird JS behaviors revealed, Primitive Non-Primitive Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-62-pass-by-value-pass-by-reference-shallow-deep-copy-type-coercion-49f3"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 6.2&lt;/strong&gt;: Pass by Value &amp;amp; Pass by Reference, Shallow &amp;amp; Deep Copy, Type Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-7-first-class-citizens-higher-order-functions-3cda"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 7&lt;/strong&gt;: First Class Citizens &amp;amp; Higher Order Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-8-the-2-pillars-closures-prototypal-inheritance-4m5n"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 8&lt;/strong&gt;: The 2 Pillars~ Closures &amp;amp; Prototypal Inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/pranav016/advanced-javascript-series-part-9-constructor-functions-object-oriented-new-keyword-1gg0"&gt;&lt;strong&gt;Advanced JavaScript Series - Part 9&lt;/strong&gt;: Constructor Functions, Object Oriented, &lt;code&gt;new&lt;/code&gt; keyword&lt;/a&gt;

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

&lt;h2&gt;
  
  
  References-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/" rel="noopener noreferrer"&gt;https://coralogix.com/blog/how-js-works-behind-the-scenes%E2%80%8A-%E2%80%8Athe-engine/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://richardartoul.github.io/jekyll/update/2015/04/26/hidden-classes.html" rel="noopener noreferrer"&gt;https://richardartoul.github.io/jekyll/update/2015/04/26/hidden-classes.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/difference-between-source-code-and-byte-code/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/difference-between-source-code-and-byte-code/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://zerotomastery.io/cheatsheets/javascript-cheatsheet-the-advanced-concepts/?utm_source=udemy&amp;amp;utm_medium=coursecontent#call-stack-memory-heap" rel="noopener noreferrer"&gt;https://zerotomastery.io/cheatsheets/javascript-cheatsheet-the-advanced-concepts/?utm_source=udemy&amp;amp;utm_medium=coursecontent#call-stack-memory-heap&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/swlh/writing-optimized-code-in-js-by-understanding-hidden-classes-3dd42862ad1d" rel="noopener noreferrer"&gt;https://medium.com/swlh/writing-optimized-code-in-js-by-understanding-hidden-classes-3dd42862ad1d&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

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