<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mayankav</title>
    <description>The latest articles on DEV Community by mayankav (@mayankav).</description>
    <link>https://dev.to/mayankav</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%2F457774%2F27a0459e-cf3c-4e28-bbf6-9963d37d4b29.PNG</url>
      <title>DEV Community: mayankav</title>
      <link>https://dev.to/mayankav</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mayankav"/>
    <language>en</language>
    <item>
      <title>Why can't you break out of the forEach loop?</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Wed, 29 Sep 2021 16:26:09 +0000</pubDate>
      <link>https://dev.to/mayankav/why-you-can-t-break-out-of-the-foreach-loop-n5e</link>
      <guid>https://dev.to/mayankav/why-you-can-t-break-out-of-the-foreach-loop-n5e</guid>
      <description>&lt;p&gt;This is one of the many things I keep forgetting every now and then. Other things like taking out clothes from the washing machine, watering my plants, okay but this post is not about me being forgetful. I am sure lot many of you reading this post do know this but still its not until you try to break out of the "&lt;strong&gt;&lt;em&gt;forEach&lt;/em&gt;&lt;/strong&gt;" loop that you realize you made the same mistake once again. This post is just a reminder for most of us and maybe a short clarification for the rest of us.&lt;/p&gt;

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

&lt;p&gt;Moving ahead with an example because examples are the quickest way to explain stuff. Lets say we have &lt;strong&gt;&lt;em&gt;an array of flowers&lt;/em&gt;&lt;/strong&gt; and we want to collect all the flowers unto my favorite "&lt;strong&gt;&lt;em&gt;Tulip&lt;/em&gt;&lt;/strong&gt;" starting from the first one. Yes, please dont make it complicated by throwing in scenarios like what if there are multiple "&lt;strong&gt;&lt;em&gt;Tulips&lt;/em&gt;&lt;/strong&gt;". We have an array of flowers, where &lt;strong&gt;&lt;em&gt;each flower appears once&lt;/em&gt;&lt;/strong&gt; like so:&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const flowers = ["Rose", "Lily", "Marigold", "Jasmine", "Tulip", "Lotus", "Orchid", "Daffodil", "Sunflower", "Poppy"];

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;

&lt;p&gt;How would you do it? If I were you I'd rush to get the "&lt;strong&gt;&lt;em&gt;forEach&lt;/em&gt;&lt;/strong&gt;" loop down first but not this time. Lets use the &lt;strong&gt;&lt;em&gt;conventional for loop&lt;/em&gt;&lt;/strong&gt; and get this done first. Silly but helpful. Easy said, easy done! Then we shall also try that out with my favorite "&lt;strong&gt;&lt;em&gt;forEach&lt;/em&gt;&lt;/strong&gt;" loop. Take a look at the following snippets.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const flowers = ["Rose", "Lily", "Marigold", "Jasmine", "Tulip", "Lotus", "Orchid", "Daffodil", "Sunflower", "Poppy"];

let vase = [];
//collect all flowers unto "Tulip"
for(let i=0; i &amp;lt; flowers.length; i++) {
  vase.push(flowers[i]);
  if(flowers[i]==="Tulip") break;
}
console.log(vase); // ["Rose","Lily","Marigold","Jasmine","Tulip"]

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const flowers = ["Rose", "Lily", "Marigold", "Jasmine", "Tulip", "Lotus", "Orchid", "Daffodil", "Sunflower", "Poppy"];

let vase = [];
//collect all flowers unto "Tulip"
flowers.forEach(flower =&amp;gt; {
  vase.push(flower);
  if(flower==="Tulip") {
    break; //Illegal break statement
  }
});

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;

&lt;p&gt;Had you tried that on your own, you'd have come across this error that says "&lt;strong&gt;&lt;em&gt;Illegal break statement&lt;/em&gt;&lt;/strong&gt;". You can't break out of the forEach loop after you're done collecting "&lt;strong&gt;&lt;em&gt;Tulip&lt;/em&gt;&lt;/strong&gt;". You may have already figured out but don't worry if you haven't and let me tell you how does the forEach loop differ from all the conventional loops. "&lt;strong&gt;&lt;em&gt;forEach&lt;/em&gt;&lt;/strong&gt;" more than being a loop, is a function call and you know it right from the syntax. This function takes another &lt;strong&gt;&lt;em&gt;callback function&lt;/em&gt;&lt;/strong&gt; and mind it, the callback function has to be a &lt;strong&gt;&lt;em&gt;synchronous&lt;/em&gt;&lt;/strong&gt; function. The synchronous callback function essentially is then called in interation on the array on which you did call the "&lt;strong&gt;&lt;em&gt;forEach&lt;/em&gt;&lt;/strong&gt;" loop. I understand, that this may not sound very intuitive. So, let me try explaining this with a demo polyfill of the "&lt;strong&gt;&lt;em&gt;forEach&lt;/em&gt;&lt;/strong&gt;" function. You can easily make it go crazy throwing errors but for a quick example, it should suffice. For complete implementation you can &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#polyfill" rel="noopener noreferrer"&gt;see this&lt;/a&gt;.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
// forEach polyfill
Array.prototype.customForEach = function(callback) {
  const arr = this;
  // internal loop
  for(let i=0; i&amp;lt;arr.length; i++) {
    const x = arr[i];
    callback.call(arr, x);
    // only if we could add a break here, but we can't reach this place
  }
}

const callback = function(x) {
  console.log(x);
  // break; // Illegal break statement
}

// using custom forEach
const flowers = ["Rose", "Lily", "Marigold", "Jasmine", "Tulip", "Lotus", "Orchid", "Daffodil", "Sunflower", "Poppy"];
flowers.customForEach(callback);

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;

&lt;p&gt;Now when you look at the &lt;strong&gt;&lt;em&gt;poyfill&lt;/em&gt;&lt;/strong&gt; (customForEach), you'll be able to comprehend the situation much clear. You'd also agree how placing a "&lt;strong&gt;&lt;em&gt;break&lt;/em&gt;&lt;/strong&gt;" statement inside a function (named callback) is inappropriate. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break#:~:text=A%20break%20statement%2C%20with%20or%20without%20a%20following%20label%2C%20cannot%20be%20used%20within%20the%20body%20of%20a%20function%20that%20is%20itself%20nested%20within%20the%20current%20loop%2C%20switch%2C%20or%20label%20statement%20that%20the%20break%20statement%20is%20intended%20to%20break%20out%20of." rel="noopener noreferrer"&gt;Read what MDN docs say&lt;/a&gt;. Even if you assumed that something like a "&lt;strong&gt;&lt;em&gt;break&lt;/em&gt;&lt;/strong&gt;" statement was valid inside a function (which indeed isn't the case), you'd see that it would not be able to break out of the loop, since there's no way you can add a "&lt;strong&gt;&lt;em&gt;break&lt;/em&gt;&lt;/strong&gt;" statement inside the internal loop. Why? because &lt;strong&gt;&lt;em&gt;you simply cannot access it from outside&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Accomplishing the task we took as an example here for this post, is not a tough job. There are numerous ways this can be done but apart from this example, for your use-case checkout the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#:~:text=There%20is%20no%20way%20to%20stop%20or%20break%20a%20forEach()%20loop%20other%20than%20by%20throwing%20an%20exception.%20If%20you%20need%20such%20behavior%2C%20the%20forEach()%20method%20is%20the%20wrong%20tool" rel="noopener noreferrer"&gt;alternatives available&lt;/a&gt; at your disposal. Again, this is not an exhasutive list but then the purpose of this post is served and anything beyond this would only stretch the content unnecessarily I believe. Also since I have now written this post, I think I won't repeat this mistake &lt;strong&gt;&lt;em&gt;or will I&lt;/em&gt;&lt;/strong&gt;?&lt;/p&gt;

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



&lt;h2&gt;
  
  
  Originally Posted Here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/you-cant-break-the-foreach-loop" rel="noopener noreferrer"&gt;https://mayankav.webflow.io/blog/you-cant-break-the-foreach-loop&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>vue</category>
    </item>
    <item>
      <title>Array().fill is secretly broken</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Mon, 13 Sep 2021 23:48:18 +0000</pubDate>
      <link>https://dev.to/mayankav/array-fill-is-secretly-broken-28k4</link>
      <guid>https://dev.to/mayankav/array-fill-is-secretly-broken-28k4</guid>
      <description>&lt;p&gt;There's something weird about &lt;strong&gt;&lt;em&gt;Array().fill()&lt;/em&gt;&lt;/strong&gt;. Well, some of you would say &lt;strong&gt;&lt;em&gt;Array&lt;/em&gt;&lt;/strong&gt; itself is weird and confusing depending on how its implemented. You say &lt;strong&gt;&lt;em&gt;Array('5')&lt;/em&gt;&lt;/strong&gt; to get an array with '5' as its only element and then when you say &lt;strong&gt;&lt;em&gt;Array(5)&lt;/em&gt;&lt;/strong&gt;, you get an array with 5 empty slots waiting to be fed. If that's not confusing for you, let's not assume it to be the same for someone new to JavaScript. Anyway, cutting it short. Tell me when would you use the &lt;strong&gt;&lt;em&gt;fill()&lt;/em&gt;&lt;/strong&gt; method? You just created an empty array and you want to auto populate it with some data you already have. You want to overwrite your array from some index &lt;strong&gt;&lt;em&gt;"x"&lt;/em&gt;&lt;/strong&gt; to index &lt;strong&gt;&lt;em&gt;"y"&lt;/em&gt;&lt;/strong&gt; with a given value. I am sure a significant number of us would try and grab the &lt;strong&gt;&lt;em&gt;fill()&lt;/em&gt;&lt;/strong&gt; method to get this done. Nothing wrong, nothing bad! That's why we have "&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill" rel="noopener noreferrer"&gt;Array.prototype.fill()&lt;/a&gt;".&lt;/p&gt;

&lt;p&gt;Before I tell you where can it go wrong, let us ensure that everyone reading this is on the same page. Try going through the naive snippet that follows and see if you can guess the ouput right. Its so naive that you dont even need to open an editor and test stuff out. If you already feel confident and know how it works feel free to skip to the last paragraph.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const myArray = Array(5); // empty array with 5 slots
myArray.fill(); // myArray = [undefined, undefined, ...5 times]
myArray.fill(0); // [0,0,0,0,0]
myArray.fill('zero', 1, 4); // [0,"zero","zero","zero",0]

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;

&lt;p&gt;Everything looks fine, right? Yes! it does. We tried creating an empty array. We filled it with some number and then we also tried overwriting the array from index 1 to index 3 (4 is not included). What we did not try yet is filling our array with &lt;strong&gt;&lt;em&gt;non-primitives&lt;/em&gt;&lt;/strong&gt;. Lets try doing that now.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const myArray = Array(5); // empty array with 5 slots
myArray.fill({x:1, y:2}); // [{x:1,y:2}, ...5 times]
myArray.fill([1,2]); // [[1,2], [1,2], ...5 times]

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fct8s6qepmxu5wmh64vcz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fct8s6qepmxu5wmh64vcz.gif" alt="Something is burning gif"&gt;&lt;/a&gt;&lt;br&gt;&lt;/p&gt;


&lt;p&gt;So far there's no problem but hey! I am here to introduce you to a problem. So if you didn't already smell what's burning, let me show you. Lets say we created an empty array and filled it with 5 empty arrays. Imagine an array '&lt;strong&gt;&lt;em&gt;myArray&lt;/em&gt;&lt;/strong&gt;' with 5 empty arrays inside it. Now lets loop through '&lt;strong&gt;&lt;em&gt;myArray&lt;/em&gt;&lt;/strong&gt;' and try populating all the internal arrays one by one. Take a look at the snippet below and without having it run somewhere tell yourself what do you expect the code to do.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const myArray = Array(5); // empty array with 5 slots
myArray.fill([]); // myArray = [[], [], ...5 times]

myArray.forEach(arr =&amp;gt; {
  arr.push('x');
});
console.log(myArray); // check the output

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;

&lt;p&gt;You say, we'll get something like an array 'myArray' with 5 arrays inside it, each having one value '&lt;strong&gt;&lt;em&gt;x&lt;/em&gt;&lt;/strong&gt;' inside it? Something like so &lt;strong&gt;&lt;em&gt;[ [x], [x], [x], [x], [x] ]&lt;/em&gt;&lt;/strong&gt; ? There lies the problem. Try running the snippet now and see what happens. Weird, right? You get something like so  &lt;strong&gt;&lt;em&gt;[ [x,x,x,x,x], [x,x,x,x,x], [x,x,x,x,x], [x,x,x,x,x], [x,x,x,x,x] ]&lt;/em&gt;&lt;/strong&gt;. Can you guess what just happened? When you filled your empty '&lt;strong&gt;&lt;em&gt;myArray&lt;/em&gt;&lt;/strong&gt;' with 5 empty arrays, you may have thought that they will be 5 different arrays but that was not the case. Its one array (&lt;strong&gt;&lt;em&gt;one reference&lt;/em&gt;&lt;/strong&gt;) that's distributed all across '&lt;strong&gt;&lt;em&gt;myArray&lt;/em&gt;&lt;/strong&gt;'. This is gross. Why would someone want to fill an entire array with the same object? Anyway, this is clearly reflected in the MDN docs if you &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#polyfill" rel="noopener noreferrer"&gt;read the polyfill code to Array.prototype.fill&lt;/a&gt; (step 12). Generally, you wouldn't care doing so but then when you get stuck somewhere its always good to checkout what's hidden in the documentation. I was doing some &lt;a href="https://codepen.io/mayankav/pen/JjJJRXp" rel="noopener noreferrer"&gt;random experiments with radix sort&lt;/a&gt; and I enountered this. Even though there are so many other ways you can get this done, let me just share a simple work-around if you really want to use &lt;strong&gt;&lt;em&gt;fill&lt;/em&gt;&lt;/strong&gt; to achieve what we tried to achieve in our example.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const yourArray = new Array(5).fill().map(()=&amp;gt; []); // yourArray = [[], [], ...5 times]

yourArray.forEach(arr =&amp;gt; {
  arr.push('y');
});

console.log(yourArray); // check the output

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;center&gt;&lt;h6&gt;
&lt;a href="https://codepen.io/mayankav/pen/xxrrRJK" rel="noopener noreferrer"&gt;Try on Codepen&lt;/a&gt;&lt;h6&gt;&lt;/h6&gt;
&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;



&lt;h2&gt;
  
  
  Originally Posted Here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/array-fill-is-secretly-broken" rel="noopener noreferrer"&gt;https://mayankav.webflow.io/blog/array-fill-is-secretly-broken&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>vue</category>
      <category>node</category>
    </item>
    <item>
      <title>Why care about Immutability ?</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Sun, 22 Aug 2021 20:37:23 +0000</pubDate>
      <link>https://dev.to/mayankav/why-care-about-immutability-119g</link>
      <guid>https://dev.to/mayankav/why-care-about-immutability-119g</guid>
      <description>&lt;p&gt;We unavoidingly tap into function purity, point free syle, recursion, immutability etc.. when discussing functional programming. You may not necessarily practice all aspects of functional programming in your run-of-the-mill job but if you're someone who works extensively with JavaScript libraries like RxJs, Cycle or state management tools like Flux (Redux, Vuex), I am sure you'd come across immutable objects more often than anything else functional. Immutability infact is so crucial to the reactive world of programming that you can count it into the basics of it. We are not going to talk about strings and other primitives in JavaScript which by design are always immutable.&lt;/p&gt;

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


&lt;p&gt;For a library that's reactive, it needs to &lt;strong&gt;&lt;em&gt;preserve the state&lt;/em&gt;&lt;/strong&gt; throughout the execution of the program, why? How else would you detect change in the state? Think of it like this, given that JS objects are &lt;strong&gt;&lt;em&gt;ephemeral&lt;/em&gt;&lt;/strong&gt; (non persistent), once you modify some property its value gets changed, &lt;strong&gt;&lt;em&gt;the object being the same&lt;/em&gt;&lt;/strong&gt;. If you compare the object before modifcation to the one after modification, well they are the same. Obviously you know why, modifying a property wont generate a new object! To understand this I expect you know that &lt;strong&gt;&lt;em&gt;a variable holding an object in JavaScript actually holds the reference to the memory block where the object's properties are stored as key value pairs&lt;/em&gt;&lt;/strong&gt;. Now you may say that you can detect a change by employing a recursive comparison on the data ? Not a performant idea when your state keeps changing every now and then! Immutability suggests &lt;strong&gt;&lt;em&gt;shallow copying&lt;/em&gt;&lt;/strong&gt; the object and making new modifications on the new copy of the object. Thinking of the copying step as a &lt;strong&gt;&lt;em&gt;signal&lt;/em&gt;&lt;/strong&gt; that something changed in the state, wont be wrong. Now that's a much faster and performance compliant way to tell whether or not the state changed. That may also trigger another doubt, how do you believe that making copies of your state is more performant than a recursive check on the property which changed? Well, that's a good question. I will try to catch up with this towards the end of this post, for now I'd say that there's something called &lt;strong&gt;&lt;em&gt;structural sharing&lt;/em&gt;&lt;/strong&gt; that makes this possible. &lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
// hidden setup JavaScript code goes in this preamble area

  &lt;/code&gt;
  &lt;code&gt;
    
const obj = {
    x: 1,
    y: 2
}

const copiedObj = obj;
copiedObj.x = 10;

console.log(obj.x); // 10
console.log(copiedObj.x); // 10 
// Referential equality check doesn't care about the contents, only the reference
console.log(obj === copiedObj); // true

const immutablyCopiedObj = {...obj};
console.log(obj === immutablyCopiedObj); //false

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;center&gt;&lt;h6&gt;
&lt;a href="https://codepen.io/mayankav/pen/xxrKggJ" rel="noopener noreferrer"&gt;Try on Codepen&lt;/a&gt;&lt;h6&gt;&lt;/h6&gt;
&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;



&lt;h2&gt;
  
  
  In essence immutability has the following benefits
&lt;/h2&gt;

&lt;p&gt;1- &lt;strong&gt;&lt;em&gt;&lt;u&gt;Reactivity through change tracking&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt; - We already discussed this. Using immutable state can make identifying changes quick and effortless both for the machine and us developers. This is what tools like redux, vuex or even &lt;a href="https://codeburst.io/when-to-use-component-or-purecomponent-a60cfad01a81#:~:text=objects%20and%20arrays.-,Never%20MUTATE,-You%E2%80%99ve%20probably%20been" rel="noopener noreferrer"&gt;parts of react&lt;/a&gt; and vue themselves build their reactivity upon. As soon as something in the state changes, be it based on some asynchronous background activity or a result of user interaction with the UI, a reference equality check instantly signals that it may be the correct time to rerender.&lt;/p&gt;

&lt;p&gt;2- &lt;strong&gt;&lt;em&gt;&lt;u&gt;Predictability and better debugging&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt; - Predictability is very frequently linked with function purity. Given a function which does not cause any &lt;a href="https://subscription.packtpub.com/book/application-development/9781789800104/5/ch05lvl1sec38/side-effects" rel="noopener noreferrer"&gt;side effect&lt;/a&gt; within itself, the ouput will always be the same for the same set of inputs no matter how many times you call the function. With this restriction that no function can modify the shared state, we now have tools like Vuex and Redux that let you modify the state but in a way that fulfils their criteria. For example, you can only make changes to the Vuex store through functions listed as &lt;strong&gt;&lt;em&gt;mutations&lt;/em&gt;&lt;/strong&gt; in the store. You also have access to methods like &lt;strong&gt;&lt;em&gt;Vue.set()&lt;/em&gt;&lt;/strong&gt; &amp;amp; &lt;strong&gt;&lt;em&gt;Vue.delete()&lt;/em&gt;&lt;/strong&gt; to register your changes immutably. This makes debugging more easy and outputs/errors more predictable.&lt;/p&gt;

&lt;p&gt;3- &lt;strong&gt;&lt;em&gt;&lt;u&gt;Versioning&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt; - Isn't it obvious that if you can preserve states you can go back and look at the old ones whenever needed? Quite similar to how you still have access to your old piece of code in Git even after merging several times on top of that. Redux implements a feature they call "&lt;a href="https://github.com/zalmoxisus/redux-devtools-extension/blob/master/docs/Features/Trace.md" rel="noopener noreferrer"&gt;action replay&lt;/a&gt;", wherein you can see the state change and the user interaction side by side in the browser. You think its helpful? Ofcourse! cool and helpful. Now you know how important it is to preserve the state.&lt;/p&gt;

&lt;p&gt;4- &lt;strong&gt;&lt;em&gt;&lt;u&gt;Performance&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt; - I took this as the last thing only because I did not talk about &lt;strong&gt;&lt;em&gt;structural sharing&lt;/em&gt;&lt;/strong&gt; when we were discussing performance. You may still be wondering how would creating new objects for every simple change be more performance complaiant than a &lt;a href="https://www.npmjs.com/package/deep-equal" rel="noopener noreferrer"&gt;deep equality check&lt;/a&gt; on the objects. While talking about immutability I also used the term &lt;strong&gt;&lt;em&gt;shallow copy&lt;/em&gt;&lt;/strong&gt;, that should have given out some hint. If not, its still nothing to worry about. As easy as it is, when making copies its important to be aware that the object you're copying may have nested objects as values to its properties. We &lt;strong&gt;&lt;em&gt;shallow copy&lt;/em&gt;&lt;/strong&gt; (just copy the reference without creating a new object) those objects which are not to be changed and only &lt;strong&gt;&lt;em&gt;deep clone&lt;/em&gt;&lt;/strong&gt; the nested object that actually needs to be changed. That's what we call structure sharing between 2 objects. You share the entire structure by internal references and only re create the node that needs modification. This may take an example for you to wrap your head around it.&lt;br&gt;&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
// hidden setup JavaScript code goes in this preamble area

  &lt;/code&gt;
  &lt;code&gt;
    
const tea = {
  tbspSugar: 1,
  type: 'beverage',
  manufacturer: {
    name: 'Assam Tea Company',
    licensed: true
  }
}

// making a copy of tea but with an extra tbsp of sugar
const sweetestTea = {
  ...tea,
  tbspSugar: 2
}

// making a copy of tea but with another manufacturer name
const chineseTea = {
  ...tea,
  manufacturer: {
   ...tea.manufacturer,
    name: 'Chinese Tea Company'
  }
}

console.log(sweetestTea);
console.log(chineseTea);

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;center&gt;&lt;h6&gt;
&lt;a href="https://codepen.io/mayankav/pen/powzeGY" rel="noopener noreferrer"&gt;Try on Codepen&lt;/a&gt;&lt;h6&gt;&lt;/h6&gt;
&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;




&lt;p&gt;You see its not that difficult but only until it reaches like thousands of properties in an object and then when you need to modify some very very deeply nested object, it sure will break your fingers. If that wasn't enough trouble, a thought of mistakenly altering some other nested object may start bothering you. To avoid hassle when dealing with large chunks of objects, you may opt for libraries like &lt;a href="https://www.npmjs.com/package/immutable" rel="noopener noreferrer"&gt;immutable.js&lt;/a&gt; or &lt;a href="https://github.com/immerjs/immer" rel="noopener noreferrer"&gt;immer&lt;/a&gt;. I would highly recommed &lt;a href="https://dev.to/viebel/structural-sharing-with-7-lines-of-javascript-2dnh"&gt;this article by Yehonathan&lt;/a&gt; if you'd like to learn more about structural sharing. If you'd like to explore more on functional programming, &lt;a href="https://dev.to/mayankav/think-recursive-272h"&gt;give this a read&lt;/a&gt; to understand recursion from my point of view.‍&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Originally Posted Here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/why-immutability" rel="noopener noreferrer"&gt;https://mayankav.webflow.io/blog/why-immutability&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>vue</category>
      <category>webdev</category>
    </item>
    <item>
      <title>&lt; template &gt; vs &lt; div &gt;</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Sun, 15 Aug 2021 20:08:14 +0000</pubDate>
      <link>https://dev.to/mayankav/template-vs-div-3b32</link>
      <guid>https://dev.to/mayankav/template-vs-div-3b32</guid>
      <description>&lt;p&gt;You'd relate to this if you've been working with likes of Vue and React. The &amp;lt;template&amp;gt; tag also finds use in web components development. You may have considered this trivial enough to let it go without trying to find out '&lt;strong&gt;why use &amp;lt;template&amp;gt; and why not &amp;lt;div&amp;gt; or even &amp;lt;span&amp;gt; which we tend to throw in wherever&lt;/strong&gt;'. If you fit in that unsubstantial description, probably you'll get to learn something.&lt;/p&gt;

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




&lt;p&gt;Before we start I'd throw in a few points to remember&lt;/p&gt;

&lt;p&gt;1- &amp;lt;template&amp;gt; is &lt;strong&gt;inert&lt;/strong&gt;&lt;br&gt;
2- &amp;lt;template&amp;gt; has only one &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement#properties" rel="noopener noreferrer"&gt;property&lt;/a&gt; of its own i.e. 'content'&lt;br&gt;
3- &amp;lt;template&amp;gt; accepts &lt;strong&gt;global attrbiutes&lt;/strong&gt; ('class', 'id', etc..) available on almost anything in HTML.&lt;br&gt;
4- A template's &lt;strong&gt;content&lt;/strong&gt; is not a valid target for events&lt;/p&gt;

&lt;p&gt;When I say &amp;lt;template&amp;gt; is inert. It simply means that if you put a template tag in your HTML code, it will &lt;strong&gt;sit there doing nothing&lt;/strong&gt; by itself. The template tag along with its content/children won't be rendered as a part of the DOM, not even on the next &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Reflow" rel="noopener noreferrer"&gt;reflow&lt;/a&gt;. Obvious question, how is that useful? To answer whys and hows, lets first get over the whats. So, the template tag won't reveal itself in the DOM tree unless you explicitly handle it in your JavaScript and make it do so. Its important to note that the &lt;strong&gt;DOMParser&lt;/strong&gt; parses everything including the temlpate tag but in this case only to check &lt;strong&gt;syntax validity&lt;/strong&gt;. Alright, to get the markup inside &amp;lt;template&amp;gt;&amp;lt;/template&amp;gt; you need to&lt;/p&gt;

&lt;p&gt;1- Get the content from template. The content is an instance of &lt;strong&gt;DocumentFragment&lt;/strong&gt;&lt;br&gt;
2- Since DocumentFragment is an implementation of the &lt;strong&gt;Node interface&lt;/strong&gt;, you can use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode" rel="noopener noreferrer"&gt;cloneNode()&lt;/a&gt; to get a copy of the template's content to then append it somewhere in the DOM. Alternatively, you can select a part of the content using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment#:~:text=new%C2%A0DocumentFragment%20object.-,Properties,-This%20interface%20has" rel="noopener noreferrer"&gt;properties available on the DocumentFragment&lt;/a&gt; interface or even push the entire DocumentFragment into the DOM tree. &lt;strong&gt;&lt;em&gt;If you choose not to clone the content&lt;/em&gt;&lt;/strong&gt;, you'll notice that the part of the document fragment once appended to some node in the DOM tree, is not to be found in the fragment anymore.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="o"&gt;----------------------------&lt;/span&gt; &lt;span class="nx"&gt;HTML&lt;/span&gt; &lt;span class="o"&gt;----------------------------&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Basket&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/b&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;inert&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Apples&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Oranges&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Milk&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/template&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="o"&gt;-------------------------&lt;/span&gt; &lt;span class="nx"&gt;JavaScript&lt;/span&gt; &lt;span class="o"&gt;-------------------------&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;inert&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;documentFragment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;documentFragment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childElementCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;

&lt;span class="c1"&gt;// const deepClone = true;&lt;/span&gt;
&lt;span class="c1"&gt;// const nodeToAppend = documentFragment.cloneNode(deepClone);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nodeToAppend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;documentFragment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstElementChild&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nodeToAppend&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;documentFragment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childElementCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;


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

&lt;/div&gt;

&lt;center&gt;&lt;h6&gt;[Try on Codepen](https://codepen.io/mayankav/pen/VwbNxjQ)&lt;h6&gt;&lt;/h6&gt;
&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;


That said, what does it mean when I say a template's content is not a valid target for events? We now know that content of a template is an instance of DocumentFragment which inturn can be considered a light weight version of [Document](https://developer.mozilla.org/en-US/docs/Web/API/Document). DocumentFragment doesn't make it into the real DOM tree. If you know this you won't mistake it for a target of any sort of event or you may still end up doing something like so

```javascript


const template = document.getElementById("inert");
const fragment = template.content;

const nodeToAppend = fragment.cloneNode(true); // just another instance of DocumentFragment
nodeToAppend.addEventListener("click", () =&amp;gt; {...}) // Mistake


```
&lt;center&gt;&lt;h6&gt;[Easy to make mistake](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template#avoiding_documentfragment_pitfall)&lt;h6&gt;&lt;/h6&gt;
&lt;/h6&gt;&lt;/center&gt;




&lt;p&gt;That alone clarifies how &amp;lt;template&amp;gt; differs from &amp;lt;div&amp;gt;, &amp;lt;span&amp;gt; or any other tag for that matter. Template does exactly what its name suggests, its meant to be used as a temporary case for your unfinished, yet to be processed markup. You may argue that you can use the hidden property on &amp;lt;div&amp;gt; and use it like a &amp;lt;template&amp;gt; but then your unfinished markup will still be a part of the DOM causing &lt;strong&gt;side effects&lt;/strong&gt; (n/w requests for images, scripts etc..) which you may not want. The template element and its contents are inert i.e. not rendered in the DOM, until you grab a reference to it with JavaScript and then append its content to some node in the DOM. You can use a &amp;lt;template&amp;gt; tag when you want to load something into the DOM after processing it in JavaScript, &lt;a href="https://vuejs.org/v2/guide/syntax.html#:~:text=and%20HTML%20parsers.-,Under%20the%20hood,-%2C%20Vue%20compiles%20the" rel="noopener noreferrer"&gt;which is what Vue does&lt;/a&gt; (processes the elements and makes them reactive). Try replacing &amp;lt;template&amp;gt; with &amp;lt;div&amp;gt; in one of your SFCs and see how Vue reacts with &lt;strong&gt;&lt;em&gt;[Vue warn]: Failed to mount component: template or render function not defined&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You could have done well without reading this but I feel it never hurts to know a little more of everything you come across at work. This shall defnitely add value in some way or the other. If you like paying attention to details, I would recommend an old post I wrote on &lt;a href="https://dev.to/mayankav/the-key-element-to-responsive-websites-31e5"&gt;key to responsive websites&lt;/a&gt;. You may find it useful. If you want to read more on html templates, &lt;a href="https://www.html5rocks.com/en/tutorials/webcomponents/template/" rel="noopener noreferrer"&gt;go ahead&lt;/a&gt;.&lt;/p&gt;



&lt;h2&gt;
  
  
  Originally Posted Here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/template-vs-div" rel="noopener noreferrer"&gt;https://mayankav.webflow.io/blog/template-vs-div&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>webdev</category>
      <category>html</category>
    </item>
    <item>
      <title>Think Recursive</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Sun, 08 Aug 2021 18:29:38 +0000</pubDate>
      <link>https://dev.to/mayankav/think-recursive-272h</link>
      <guid>https://dev.to/mayankav/think-recursive-272h</guid>
      <description>&lt;p&gt;I am not so good at cooking stuff but I am an all time admirer of the traditional Polish dish '&lt;a href="https://www.youtube.com/watch?v=GQ0GiTKzu38"&gt;Pierogi&lt;/a&gt;'. I took a few days off from work last week, all determined not to let the days fly by without getting my hands on the polish delicacy. Now I do realize that I don't even know where to start from. Well, what are friends for? &lt;strong&gt;&lt;em&gt;Now read this with patience!&lt;/em&gt;&lt;/strong&gt; I made a call to "Darek" back in Warsaw and asked him if he could give me some direction. Darek, being just another geek, told me that he knows how to do the veggies (&lt;a href="https://www.polonist.com/pierogi-filling-ideas/"&gt;the filling&lt;/a&gt;) but then he shall ask another friend, how to prepare the wrap. &lt;strong&gt;&lt;em&gt;He put me on hold&lt;/em&gt;&lt;/strong&gt; and went ahead to call his friend, Marek. Marek tells Darek that he does indeed know how to do the wrap but then he shall call Alek, who lives nearby to find out how to do the dressing. 'Sauce', my friends, is important. &lt;strong&gt;&lt;em&gt;He puts Darek on hold as well&lt;/em&gt;&lt;/strong&gt;. Silly! Alright so Alek, the neighbor finally &lt;strong&gt;&lt;em&gt;does't call another friend&lt;/em&gt;&lt;/strong&gt; but gives away the recipe of the sauce. Marek combines his recipe of the wrap with what Alek told him about the sauce and conveys it back to Darek, who was simply waiting to combine this information with the recipe of the filling only to deliver the complete information back to me. Long day but I finally have what I needed.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
Lets switch the context now. Did you already visualise the call stack? Only if you don't know, JavaScript runtime uses a call stack to track the execution of functions. Its nothing but a stack that orderly arranges the execution contexts of functions in memory making sure that the currently executing function stays on the top. Going by my example, look how it can actually be portrayed. Think of it as a recurring call to the function &lt;strong&gt;&lt;em&gt;getRecipeHelp()&lt;/em&gt;&lt;/strong&gt;.

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pWfCH71D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lixt78a3qrt90nozmnlv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pWfCH71D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lixt78a3qrt90nozmnlv.png" alt="recursion example"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;alek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alek&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;ingr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sauce&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;marek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Marek&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;alek&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;ingr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;wrap&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;darek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Darek&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;marek&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;ingr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;filling&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getRecipeHelp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// bail out condition&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ingr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ingr&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; + &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;getRecipeHelp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Here we call Darek to get help with the recipe who then calls his friend Marek and Marek finally calls his friend Alek&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getRecipeHelp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;darek&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "filling + wrap + sauce"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/poPZyEb"&gt;Try on Codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;Assuming you digested the example really well, let me now ask you, how do you think you'd define '&lt;strong&gt;&lt;em&gt;recursion&lt;/em&gt;&lt;/strong&gt;'? The academic definition says '&lt;strong&gt;&lt;em&gt;A non leaf function calling itself&lt;/em&gt;&lt;/strong&gt;'. On a personal note, I understand recursion as &lt;strong&gt;&lt;em&gt;a quest to meet the bail out condition so that the return values can sequentially be resolved into the final output&lt;/em&gt;&lt;/strong&gt;. This may confuse you a little unless you understand that every recursive function you define has to have a bail out condition. Broadly, I'd recommend you to remember three things about any recursive function. What are those three things?&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;You should start with thinking of the bail out condition&lt;/li&gt;
&lt;li&gt;Understand that the values start resolving in LIFO fashion to finally render the output&lt;/li&gt;
&lt;li&gt;Recursively call the function itself but with a different argument ofcourse ( Make sure you'e moving towards the bail out condition)&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Though the bail out condition is quite visible in the example, to make it even more clear, if you do not have this check to stop your recursive calls, you may end up with a stack overflow where functions keep piling up on the stack without returning. By value resolution in LIFO fashion, all I mean is that the functions lower in the stack keep waiting until the final function (that satisfies the bail out condition) returns some decreed value, post which the return values start getting resolved top to bottom in the stack. With so much of this information at hand, go ahead and try implementing the classic factorial function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// bail out condition&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; 
  &lt;span class="c1"&gt;// make sure you're moving towards the bail out condition and not away from it&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/NWjByjP"&gt;Try on Codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EPdti35W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52k2y7frc1obwkvjmieo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EPdti35W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52k2y7frc1obwkvjmieo.gif" alt="Call Stack javaScript"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;An illustration borrowed from Codeacademy&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;I think the illustration is self explanatory. If not, lets cover another example. Lets get the fibonacci series in. Hardly anyone in the wild, would be unaware of the fibinacci series but still it goes like this 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.. Every other number starting from the third in series is the sum of the previous two. Fibonacci is magical, go ahead and read &lt;a href="https://insteading.com/blog/fibonacci-sequence-in-nature/#:~:text=18%20Amazing%20Examples%20of%20the%20Fibonacci%20Sequence%20in%20Nature"&gt;this&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... starts with 0 and 1 and then each number is the sum of previous two numbers in the series&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; 
  &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="c1"&gt;// bail out condition&lt;/span&gt;
  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// make sure you're moving towards the bail out condition and not away from it&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 55&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/NWjBYxp"&gt;Try on Codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;Conceptually, not much different from what we did for factorials. Think of every recursive function as a mathematical function. Perhaps then it shall become more obvious. We've got our bail out condition at (&lt;strong&gt;&lt;em&gt;n &amp;lt;= 1&lt;/em&gt;&lt;/strong&gt;) , where we simply return any argument that's less than 1. Otherwise we go ahead and make recursive calls to the &lt;strong&gt;&lt;em&gt;fib&lt;/em&gt;&lt;/strong&gt; function for &lt;strong&gt;&lt;em&gt;n-1&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;n-2&lt;/em&gt;&lt;/strong&gt;. Well, that only gives me the &lt;strong&gt;&lt;em&gt;nth&lt;/em&gt;&lt;/strong&gt; fibonacci member. How'd you print the entire series? Try not to use loops and create a recursive function &lt;strong&gt;&lt;em&gt;showFib(n) {..}&lt;/em&gt;&lt;/strong&gt; that prints the series all at once. &lt;a href="https://codepen.io/mayankav/pen/NWjBYxp"&gt;Here's the code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Alright! now try calling the &lt;strong&gt;&lt;em&gt;fib(n)&lt;/em&gt;&lt;/strong&gt; function like &lt;strong&gt;&lt;em&gt;fib(999999)&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;fib(9999999)&lt;/em&gt;&lt;/strong&gt;. Do you see the result already? As you could say just by looking at it, its going to be a huge huge number, your browser may give up on this and start crawling or you may even get a stack overflow exception depending on the content in the call stack. Switch back to the illustration that shows the stack for the factorial program. Can you imagine 999999 functions being piled up all waiting for their successor to return some value? How do you get around this? There's actually a way out but that's kind of a trade off. We call it &lt;a href="https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-in-webkit/"&gt;Proper Tail Calls (PTC)&lt;/a&gt;. Checkout the last line in the function. For the factorial function its a return statement. The return statement has two parts if you see&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;a multiplier i.e. n&lt;/li&gt;
&lt;li&gt;a recursive call i.e. factorial(n-1)&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since we have the multiplier waiting for the recursive call to return some value, the function cannot be offboarded from the stack. It has this pending work (&lt;strong&gt;&lt;em&gt;multiply by n&lt;/em&gt;&lt;/strong&gt;) to finish after the recursive call returns. What if we pass the &lt;strong&gt;&lt;em&gt;product&lt;/em&gt;&lt;/strong&gt; to the recursive call instead of waiting with the multiplier? Well, since the pending work will get delegated to the recursive call everytime , the engine won't need to keep the execution stack crowded with functions in stand by.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;
  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 9.332621544394415e+155&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;999&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Infinity&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;999999&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Error- Maximum call stack size exceeded &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/vYmazxZ"&gt;Try on Codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;You see it works better now. Unlimited frames and you can call a function recursively as many times as you want? Before mentioning PTC, I said it was a tradeoff. A tradeoff with the stack trace. You no longer have easy debugging for your function. Since the function frame is lost to create space in the execution stack, they won't show up even while tracing your error. &lt;a href="https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-in-webkit/#:~:text=The%20calling%20function%E2%80%99s%20frame%20is%20called%20a%20tail%20deleted%20frame%20as%20it%20is%20no%20longer%20on%20the%20stack%20once%20it%20makes%20a%20tail%20call.%20This%20means%20that%20the%20tail%20deleted%20function%20will%20not%20show%20up%20in%20a%20stack%20trace."&gt;Read more here&lt;/a&gt;. So hold your horses and think before you opt for an optimized recursive solution. Now you're thinking, won't it misfire everytime you place a function call in the tail of a function? You don't want to lose the stack trace. Good news and Bad news, all that I told you about Proper Tail Calls simply won't work with JS engines other than JavaScriptCore (by Apple). Apple likes to call it Tail Call Optimization (TCO). TCO goes one step ahead of PTC to actually optimize your function execution.V8 did infact support this for a while but then for the same reason and possibly some bugs, took it down. If you are on chrome, you can test this out in your debugger. Alternatively you can go through &lt;a href="https://stackoverflow.com/questions/42788139/es6-tail-recursion-optimisation-stack-overflow"&gt;this&lt;/a&gt;. V8 creates frames for all function calls and keeps them in the stack regardless of the way you write your code. So you still get the stack overflow exception if you take your recursions off limit. An explicit version of PTC is under discusstion (seems abandoned though). They call it &lt;a href="https://github.com/tc39/proposal-ptc-syntax#:~:text=Syntactic%20Tail%20Calls%20(STC)"&gt;Syntactic Tail Calls (STC)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aOnnNygl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aw9k2wc9wu7m0o9o1vkf.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aOnnNygl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aw9k2wc9wu7m0o9o1vkf.PNG" alt="V8 recursion fibonacci example"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;V8 stacking up function calls&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;
&lt;h2&gt;
  
  
  Originally Posted Here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/think-recursive"&gt;https://mayankav.webflow.io/blog/think-recursive&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>functional</category>
    </item>
    <item>
      <title>The "new" blunder in JavaScript</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Fri, 30 Jul 2021 16:00:37 +0000</pubDate>
      <link>https://dev.to/mayankav/the-new-blunder-in-javascript-1lee</link>
      <guid>https://dev.to/mayankav/the-new-blunder-in-javascript-1lee</guid>
      <description>&lt;p&gt;No, I am not even talking about why JavaScript tries to replicate classical inheritance. As much as that is an easy target on JavaScript, lets leave that upto anti evangelists. Just pondering over the &lt;strong&gt;"new"&lt;/strong&gt; keyword in isolation is what I aim for right now. &lt;a href="https://codepen.io/mayankav/pen/zYwZOZJ"&gt;Do we know all the possible ways of creating objects in JavaScript&lt;/a&gt;? Assuming that we do, two of the four ways available to us, make use of the &lt;strong&gt;"new"&lt;/strong&gt; operator. The first one being &lt;strong&gt;constructor functions&lt;/strong&gt; and yes you guessed it, &lt;strong&gt;ES6 classes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When I talk of classes, I am somehow driven by my conscience to talk about all the problems classical inheritance brings along but for now I will hold on to the &lt;strong&gt;"new"&lt;/strong&gt; keyword. The question you should ask is, why did we feel the need to have &lt;strong&gt;"new"&lt;/strong&gt;, when we could actually use object literals in the first place? I mean, there must be some benefit of saying &lt;strong&gt;new SomeFunction()&lt;/strong&gt; over your old pal &lt;strong&gt;{ }&lt;/strong&gt;. Make a guess. Did someone say &lt;strong&gt;"creating similar objects"&lt;/strong&gt;? So when you have a class &lt;strong&gt;"YourClass"&lt;/strong&gt; with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields"&gt;class fields&lt;/a&gt; &lt;strong&gt;"x"&lt;/strong&gt; and &lt;strong&gt;"y"&lt;/strong&gt; and then when you say &lt;strong&gt;"new YourClass(1,2)"&lt;/strong&gt;, you're assured that everytime you do a &lt;strong&gt;"new"&lt;/strong&gt; object creation you will get a similar object, right? We can do that using &lt;strong&gt;object concatenation&lt;/strong&gt; or &lt;strong&gt;factory functions&lt;/strong&gt; but alright, not bad. What else? Maybe it also feels way simpler, no? So far so good. Smart people probably won't even talk about classes and constuctor functions, leave alone the &lt;strong&gt;"new"&lt;/strong&gt; operator. &lt;a href="https://medium.com/javascript-scene/common-misconceptions-about-inheritance-in-javascript-d5d9bab29b0a#:~:text=Smart%20people%20will%20do%20whatever%20they%20want."&gt;Smart people will do whatever they want&lt;/a&gt;. I personally don't favor using classes but that only makes sense when everything is under my control. Being a part of some team, that's not always the case. We need to deal with code whether we like it or not. Assuming that the &lt;strong&gt;"new"&lt;/strong&gt; operator makes it intuitive for you specially when you're coming from OOP, can you figure out the difference here?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Fri Jul 30 2021 20:08:55 GMT+0530 (India Standard Time)&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Fri Jul 30 2021 20:08:55 GMT+0530 (India Standard Time)&lt;/span&gt;
&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// "Fri Jul 30 2021 20:08:55 GMT+0530 (India Standard Time)"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is &lt;strong&gt;Date&lt;/strong&gt; a class, a constructor function or a factory function? If you don't know what a &lt;strong&gt;factory function&lt;/strong&gt; is, its just another normal function that returns an object. So, if a function explicitly returns an object and is apparently not a constructor function, you can call it an object factory function. So what do you think Date in JavaScript is? I'll leave that on you to experiment with. If you can't reckon, think of how &lt;strong&gt;"new String()"&lt;/strong&gt; &amp;amp; &lt;strong&gt;"String()"&lt;/strong&gt; behave. The former gives you a new object whereas simply calling &lt;strong&gt;String(..)&lt;/strong&gt; over some primitive does cast the value's type to string. The question is how do you define a function that can be safely called with and without the &lt;strong&gt;"new"&lt;/strong&gt; operator? A factory function returns you the same object irrespective of whether you call it with or without a &lt;strong&gt;"new"&lt;/strong&gt; operator. A constructor function on the other hand unless and until called with a &lt;strong&gt;"new"&lt;/strong&gt; prefix returns undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;factoryFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ConstructorFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;factoryFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// {x:1, y:2}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;factoryFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// {x:1, y:2}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ConstructorFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ConstructorFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// {x:1, y:2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/VwbxmEY"&gt;Try on Codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;Now, I am kind of more interested in the &lt;strong&gt;constructor function&lt;/strong&gt;. Notice that when you simply call your constructor function without the &lt;strong&gt;"new"&lt;/strong&gt; keyword, it returns undefined? Visibly so because there's nothing to return. Interestingly, unless you're in strict mode, you've now also created properties &lt;strong&gt;"x"&lt;/strong&gt; and &lt;strong&gt;"y"&lt;/strong&gt; on the global object. I understand, there's hardly someone in the wild who'd instantiate a constructor function without &lt;strong&gt;"new"&lt;/strong&gt;. Anyway, we know how a constructor function otherwise implicitly returns "this" (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new#:~:text=Creates%20a%20blank%2C%20plain%20JavaScript%20object."&gt;an anonymous object created using the "new" keyword&lt;/a&gt;). What if I put a blunt &lt;strong&gt;return statement&lt;/strong&gt; right inside the constuctor function? Take a look.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ConstructorFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a blunder&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;AnotherConstructorFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;not a blunder&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ConstructorFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "a blunder"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AnotherConstructorFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// {x:1, y:2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/WNjJRNz"&gt;Try on Codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;Uncannily, if you return an object or an array, it seems to block the implicit nature of the constructor function that returns the &lt;strong&gt;"this"&lt;/strong&gt; object, upon being instantiated with the &lt;strong&gt;"new"&lt;/strong&gt; operator whereas returning an atomic string makes no difference as such. How do you think it is possible to make a constructor function work safe without the &lt;strong&gt;"new"&lt;/strong&gt; operator? Why would you even want to do that? Well, you may have your own reasons, I just want to prevent the users of my constructor function from mistakenly trying to invoke it without the &lt;strong&gt;"new"&lt;/strong&gt; operator. I know you can simply use an ES6 class but for some reason I want to stick to the old functions style and yes I am not using the strict mode as well. Strict mode inside the function can alert you from creating implicit globals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;StrictConstructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;StrictConstructor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;StrictConstructor should only be instantiated with 'new' operator&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;StrictConstructor&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// {x:1, y:2}&lt;/span&gt;
&lt;span class="nx"&gt;StrictConstructor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/xxdWoxx"&gt;Try on Codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;So the conditional filter we used to throw the error depends on how the &lt;strong&gt;"new"&lt;/strong&gt; operator creates a new object and assigns it a constructor under the hood. If you want to get deep into this you should definitely go check out the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new#:~:text=Creates%20a%20blank%2C%20plain%20JavaScript%20object."&gt;MDN reference&lt;/a&gt; and then &lt;a href="https://dev.to/mayankav/javascript-inside-story-more-about-prototypes-and-inheritance-3a9l"&gt;my last blog post&lt;/a&gt;. As a matter of fact, instead of throwing an error you can even return an object to eliminate the need of calling the function using &lt;strong&gt;"new"&lt;/strong&gt; like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;StrictConstructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;StrictConstructor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;StrictConstructor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;StrictConstructor&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// {x:1, y:2}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;StrictConstructor&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// {x:1, y:2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





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

&lt;p&gt;For a while, if you forget about JavaScript, its not very intuitive to instantiate functions with the &lt;strong&gt;"new"&lt;/strong&gt; operator. Probably that's why we name our constructor functions in &lt;a href=""&gt;PascalCase&lt;/a&gt;. Due to the fact that the &lt;strong&gt;"new"&lt;/strong&gt; operator and constructor functions may behave eerie at times (specially when you forget the &lt;strong&gt;"new"&lt;/strong&gt; operator), you can choose a combination of options from the available list to keep your code safe from surprises.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An ES6 class shall help you spot when someone forgets the &lt;strong&gt;"new"&lt;/strong&gt; keyword, by throwing an error&lt;/li&gt;
&lt;li&gt;Following the convention of naming constructor functions in Pascal.&lt;/li&gt;
&lt;li&gt;Placing a check within your constructor function to either throw an error on skipping the &lt;strong&gt;"new"&lt;/strong&gt; operator or to silently fix the implicit behavior of the constructor function.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Originally Posted Here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/the-new-blunder-in-javascript"&gt;https://mayankav.webflow.io/blog/the-new-blunder-in-javascript&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Inside Story : More about Prototypes and Inheritance</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Sat, 17 Jul 2021 23:39:10 +0000</pubDate>
      <link>https://dev.to/mayankav/javascript-inside-story-more-about-prototypes-and-inheritance-3a9l</link>
      <guid>https://dev.to/mayankav/javascript-inside-story-more-about-prototypes-and-inheritance-3a9l</guid>
      <description>&lt;p&gt;I almost picked the title "&lt;strong&gt;&lt;em&gt;JavaScript's Factory for Everything&lt;/em&gt;&lt;/strong&gt;", but then I changed my mind after reading my older post. Just trying to push my &lt;a href="https://dev.to/mayankav/is-everything-in-javascript-an-object-1alg"&gt;last post&lt;/a&gt; here. I don't feel very comfortable writing this, not because I don't understand the stuff but because its something that pretends to be something it really is not. Protesting that prototypal inheritance is not inheritance at all does not change anything. Perhaps if it were not for the comfort of OOPers, it could have been better called &lt;strong&gt;&lt;em&gt;prototypal wiring&lt;/em&gt;&lt;/strong&gt; or maybe &lt;strong&gt;&lt;em&gt;prototypal delegation&lt;/em&gt;&lt;/strong&gt;. If you're coming from Java, how would you answer the question, "&lt;strong&gt;&lt;em&gt;Why does Java not suppport multiple class inheritance?&lt;/em&gt;&lt;/strong&gt;". Well, you'd point out &lt;a href="https://www.cs.cornell.edu/courses/JavaAndDS/abstractInterface/05diamond.pdf" rel="noopener noreferrer"&gt;the Diamond Problem&lt;/a&gt;, won't you? Given that JavaScript doesn't know this problem how would you explain it not being able to support such a thing? PS: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model#no_multiple_inheritance" rel="noopener noreferrer"&gt;It can be done but it has its own issues&lt;/a&gt;. Despite its appearance resembling to that of classical inheritance, JavaScript has its own reasons for (not) being able to do certain things. Not asking you to unlearn the classical inheritance take but then to understand JavaScript's way of handling prototypal inheritance you need to drop those intuitive assumptions atleast for the time being. ‍‍&lt;/p&gt;

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

&lt;br&gt;
I dont want to recall the time when I started logging objects in the console only to check what's really inside of them. I mean I knew objects had their properties but I also found stuff like __proto__, constructor, prototype, __proto__ again. It would not stop. I kept on digging and it kept on throwing more and then I realized I was willingly stuck in a loop of references. Let me take help of an analogy to explain this to you. Lets assume that any function you create in JavaScript is a House to be put on rent. The House then comes with a bunch of keys ( &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt; ). The bunch of keys has another bunch of small master keys ( &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; ) inside it and has a label ( &lt;strong&gt;&lt;em&gt;constructor&lt;/em&gt;&lt;/strong&gt; ) with the name of the house on it. This bunch of keys is given to the tenants ( &lt;strong&gt;&lt;em&gt;objects created from the function&lt;/em&gt;&lt;/strong&gt; ) and the tenants then keep it with them and they like to call it guess what ? &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt;. Heck! confusing. Its not that easy to build analogies here. Just take a look at the diagram I came up with.

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1b8b48mhm1v6ap0qa21u.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1b8b48mhm1v6ap0qa21u.gif" alt="Prototypes in JavaScript"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;&lt;a href="https://www.slideshare.net/Mayankav1/javascript-function-prototype" rel="noopener noreferrer"&gt;Check out the Ppt&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;What you see is what I am going to pen down now. Consider a class &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt; or simply a function &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt;. The function when created, gets two properties by default namely &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt;. &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; is a property that is available on everything in JS be it a primitive or an object. Go ahead! try that in your console. It contains some information from the function that was responsible for creating the primitive/object in the first place. Since a function is nothing more than a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#:~:text=Every%20JavaScript%20function%20is%20actually%20a%20Function%20object.%20This%20can%20be%20seen%20with%20the%20code%20(function()%7B%7D).constructor%20%3D%3D%3D%20Function%2C%20which%20returns%20true." rel="noopener noreferrer"&gt;Function object&lt;/a&gt;, the &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; property on each and every function takes its value from &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#instance_methods" rel="noopener noreferrer"&gt;Function.prototype&lt;/a&gt;. What about the &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt; property? In contrast with &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; which is available on anything and everything in JS, &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt; is only available on JavaScript functions. The &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt; property is an object ( only exception being Function.prototype which is a native function ) with two default properties namely &lt;strong&gt;&lt;em&gt;constructor&lt;/em&gt;&lt;/strong&gt; ( refers to the Function/Class itself to which prototype belongs ) and &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; . The purpose of &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; on &lt;strong&gt;&lt;em&gt;A.prototype&lt;/em&gt;&lt;/strong&gt; is nothing different from that on the function &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt; itself. &lt;strong&gt;&lt;em&gt;A.prototype.__proto__&lt;/em&gt;&lt;/strong&gt; contains information about the function that was responsible for the creation of &lt;strong&gt;&lt;em&gt;A.prototype&lt;/em&gt;&lt;/strong&gt;. Since this object (A.prototype) was automatically created, the function/class responsible for its creation was &lt;strong&gt;&lt;em&gt;Object&lt;/em&gt;&lt;/strong&gt;. No wonder why every &lt;strong&gt;&lt;em&gt;SomeFunction.prototype.__proto__&lt;/em&gt;&lt;/strong&gt; gets a default value of &lt;strong&gt;&lt;em&gt;Object.prototype&lt;/em&gt;&lt;/strong&gt;. To verify this, try creating an object using the object lietral syntax like so. ‍&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;randomObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;randomObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/VwbpMVY" rel="noopener noreferrer"&gt;Try on Codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;&lt;br&gt;

&lt;br&gt;
Moving on to instantiating the constructor function, lets do &lt;strong&gt;&lt;em&gt;let objA = new A();&lt;/em&gt;&lt;/strong&gt; to create an object from the function &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt;. &lt;strong&gt;&lt;em&gt;objA&lt;/em&gt;&lt;/strong&gt; gets a &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; . We just discussed how everything in JS gets this default property with a value of &lt;strong&gt;&lt;em&gt;SomeFunction.prototype&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;SomeFunction&lt;/em&gt;&lt;/strong&gt; being the function/class responsible for its creation. No points for guessing its value in this case. Its &lt;strong&gt;&lt;em&gt;A.prototype&lt;/em&gt;&lt;/strong&gt;. ‍&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;objA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;objA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true &lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;objA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;center&gt;&lt;h6&gt;.prototype only exists on functions. &lt;a href="https://codepen.io/mayankav/pen/VwbpMVY" rel="noopener noreferrer"&gt;Try on Codepen&lt;/a&gt;.&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;



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

&lt;p&gt;All this while what I've been trying to tell you is that &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; is just the pen name of &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt;. A &lt;strong&gt;&lt;em&gt;constructor's prototype&lt;/em&gt;&lt;/strong&gt; becomes it's &lt;strong&gt;&lt;em&gt;object's __proto__&lt;/em&gt;&lt;/strong&gt; . How does this help? Well since its not the copy but the reference to a function's &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt; which is shared amongst objects created using the function, any new function/property on the &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt; of a function is readily available on the object's &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; as well. Though its not a good practice to monkey patch properties on the &lt;strong&gt;&lt;em&gt;protoype&lt;/em&gt;&lt;/strong&gt; of a constructor function. &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes#:~:text=You%20will%20rarely%20see%20properties%20defined%20on%20the%20prototype%20property%2C%20because%20they%20are%20not%20very%20flexible%20when%20defined%20like%20this.%20For%20example%20you%20could%20add%20a%20property%20like%20so%3A" rel="noopener noreferrer"&gt;Read more about that here&lt;/a&gt;. Interestingly you dont even need to access the patched property via &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; . You just access it on the object like &lt;strong&gt;&lt;em&gt;objA.somePatchedFunction()&lt;/em&gt;&lt;/strong&gt; and it gets resolved from &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; in the chain. This sounds fun but can very quickly get on the nerves when some object starts patching functions/properties on its &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; property causing a &lt;strong&gt;&lt;em&gt;prototype namespace pollution&lt;/em&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Anyway, did you think of this? What happens when you manually alter the &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; property on &lt;strong&gt;&lt;em&gt;objA&lt;/em&gt;&lt;/strong&gt; like &lt;strong&gt;&lt;em&gt;objA.__proto__ = { random : 10 }&lt;/em&gt;&lt;/strong&gt; ? Apparently, the link from the function &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt; breaks and you can no more access the functions patched on &lt;strong&gt;&lt;em&gt;A.prototype&lt;/em&gt;&lt;/strong&gt; and then you get access to properties of the newly set object (&lt;strong&gt;&lt;em&gt;{ random : 10 }&lt;/em&gt;&lt;/strong&gt;) like &lt;strong&gt;&lt;em&gt;objA.random&lt;/em&gt;&lt;/strong&gt;. Apart of assigning a value directly to &lt;strong&gt;&lt;em&gt;objA.__proto__&lt;/em&gt;&lt;/strong&gt; there exist lawful functions (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf" rel="noopener noreferrer"&gt;Object.setPrototypeOf&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create" rel="noopener noreferrer"&gt;Object.create&lt;/a&gt;) in JavaScript to help you do this some of which are &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain#:~:text=Ill-performing.%20Should%20be%20deprecated." rel="noopener noreferrer"&gt;on the verge of being deprecated&lt;/a&gt; but that's not my concern for this blog post. This does not even seem very helpful. Why don't we try altering the &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; property on &lt;strong&gt;&lt;em&gt;A.prototype&lt;/em&gt;&lt;/strong&gt;? Well, that sounds like a plan.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;patchedP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//  Object.create sets (Child.prototype).__proto__ = Parent.prototype&lt;/span&gt;
&lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Resetting the child constructor may/may not be needed&lt;/span&gt;
&lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;patchedC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 50  //undefined if no Parent.call(this)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;patchedP&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/qBmRzzj" rel="noopener noreferrer"&gt;Try on Codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;For the ease of understanding lets try to alter &lt;strong&gt;&lt;em&gt;Child.prototype&lt;/em&gt;&lt;/strong&gt;. Let me tell you what am I trying to do. When I create a new object using the Child constructor function, I can say something like &lt;strong&gt;&lt;em&gt;new Child().c&lt;/em&gt;&lt;/strong&gt; and get the expected value i.e. &lt;strong&gt;&lt;em&gt;200&lt;/em&gt;&lt;/strong&gt;. What I want is to get a valid value on doing so &lt;strong&gt;&lt;em&gt;new Child().patchedP&lt;/em&gt;&lt;/strong&gt; ie. &lt;strong&gt;&lt;em&gt;100&lt;/em&gt;&lt;/strong&gt;. Do you think I should simply make an assignment like so &lt;strong&gt;&lt;em&gt;Child.prototype = Parent.prototype&lt;/em&gt;&lt;/strong&gt; ? Well , no because then when you want to patch some functions on &lt;strong&gt;&lt;em&gt;Child.prototype&lt;/em&gt;&lt;/strong&gt;, you will end up patching the orignal &lt;strong&gt;&lt;em&gt;Parent.prototype&lt;/em&gt;&lt;/strong&gt;. Changes on Child should not impact Parent or you can't call it inheritance. I better use an &lt;strong&gt;&lt;em&gt;intermediary object&lt;/em&gt;&lt;/strong&gt; to set the &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt; of child. That's why we do this &lt;strong&gt;&lt;em&gt;Child.prototype = Object.create(Parent.prototype)&lt;/em&gt;&lt;/strong&gt;. Now when you patch the Child's &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt;, it won't impact the Parent (you only patch the intermediary object). &lt;/p&gt;

&lt;p&gt;Did you notice the call to Parent function within the Child function (kind of super if you're coming from Java) ? Try commenting it out in the &lt;a href="https://codepen.io/mayankav/pen/qBmRzzj" rel="noopener noreferrer"&gt;pen&lt;/a&gt;. That shall leave you with no access to Parent's instance properties i.e. &lt;strong&gt;&lt;em&gt;p&lt;/em&gt;&lt;/strong&gt; here. When you call the Parent with '&lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt;' (&lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; refers to the newly created object when you say &lt;strong&gt;&lt;em&gt;new Child()&lt;/em&gt;&lt;/strong&gt;), the Parent function executes to add the property &lt;strong&gt;&lt;em&gt;p&lt;/em&gt;&lt;/strong&gt; on &lt;strong&gt;&lt;em&gt;new Child()&lt;/em&gt;&lt;/strong&gt;. Now in every new instance you create from the Child function, you get access to instance properties of Parent &amp;amp; Child both along with patched properties of both &lt;strong&gt;&lt;em&gt;Parent.prototype&lt;/em&gt;&lt;/strong&gt; &amp;amp; &lt;strong&gt;&lt;em&gt;Child.prototype&lt;/em&gt;&lt;/strong&gt;. Additionally, now patching &lt;strong&gt;&lt;em&gt;Child.prototype&lt;/em&gt;&lt;/strong&gt; will not impact Parent. Now that's something we can kind of call inheritance. Just to touch upon the concept of &lt;strong&gt;&lt;em&gt;prototype chaining&lt;/em&gt;&lt;/strong&gt;, it goes without saying that if you try to access &lt;strong&gt;&lt;em&gt;aChild.randomProperty&lt;/em&gt;&lt;/strong&gt; given &lt;strong&gt;&lt;em&gt;aChild = new Child();&lt;/em&gt;&lt;/strong&gt; , it shall be first looked up in the property list of &lt;strong&gt;&lt;em&gt;aChild&lt;/em&gt;&lt;/strong&gt; itself, if not found it should be searched for in &lt;strong&gt;&lt;em&gt;aChild.__proto__&lt;/em&gt;&lt;/strong&gt; (the intermediary object we discussed earlier), next in &lt;strong&gt;&lt;em&gt;aChild.__proto__.__proto__&lt;/em&gt;&lt;/strong&gt; until the search reaches &lt;strong&gt;&lt;em&gt;Object.prototype&lt;/em&gt;&lt;/strong&gt; which is the last man standing in the prototype chain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qmf4qp1wssxc3iu5jjc.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qmf4qp1wssxc3iu5jjc.PNG" alt="A look at new Child()‍"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;A look at new Child()‍&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;



&lt;h2&gt;
  
  
  Take Away
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Every function's &lt;strong&gt;&lt;em&gt;.prototype&lt;/em&gt;&lt;/strong&gt; property is of type - object except function Function. (It's &lt;strong&gt;&lt;em&gt;.prototype&lt;/em&gt;&lt;/strong&gt; property is of type - function)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every function's &lt;strong&gt;&lt;em&gt;.__proto__&lt;/em&gt;&lt;/strong&gt; property is always equal to &lt;strong&gt;&lt;em&gt;Function.prototype&lt;/em&gt;&lt;/strong&gt; and hence of the type - Function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Objects don't have &lt;strong&gt;&lt;em&gt;.prototype&lt;/em&gt;&lt;/strong&gt; property.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every object's &lt;strong&gt;&lt;em&gt;.__proto__&lt;/em&gt;&lt;/strong&gt; property is of type object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An object's &lt;strong&gt;&lt;em&gt;.__proto__&lt;/em&gt;&lt;/strong&gt; property takes its value from the &lt;strong&gt;&lt;em&gt;.prototype&lt;/em&gt;&lt;/strong&gt; property of the Function from which it was created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If an object was not created using any particular function (created using object literal or using Object.create(Object.prototype)), the value of it's &lt;strong&gt;&lt;em&gt;.__proto__&lt;/em&gt;&lt;/strong&gt; property will be &lt;strong&gt;&lt;em&gt;Object.prototype&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create an object from a class &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt; or a function &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt; : let &lt;strong&gt;&lt;em&gt;objA = Object.create(A.prototype);&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;let objA = new A();&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In ES5, inheritance looks like so : &lt;strong&gt;&lt;em&gt;let anObjectFromParent = Object.create(Parent.prototype);&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;Child.prototype = anObjectFromParent;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In ES6, the &lt;strong&gt;&lt;em&gt;extends&lt;/em&gt;&lt;/strong&gt; keyword plays the role of &lt;strong&gt;&lt;em&gt;Object.create(Parent.prototype)&lt;/em&gt;&lt;/strong&gt; and the &lt;strong&gt;&lt;em&gt;super&lt;/em&gt;&lt;/strong&gt; keyword invokes the constructor of the parent.‍‍&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessing &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; directly on an object is not as optimal as using the &lt;strong&gt;&lt;em&gt;new&lt;/em&gt;&lt;/strong&gt; keyword, &lt;strong&gt;&lt;em&gt;Object.create&lt;/em&gt;&lt;/strong&gt; (to set) and &lt;strong&gt;&lt;em&gt;Object.getPrototypeOf&lt;/em&gt;&lt;/strong&gt; (to get).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt; is just a way to programmatically access an object's &lt;strong&gt;&lt;em&gt;[[Prototype]]&lt;/em&gt;&lt;/strong&gt; internal slot which is otherwise not accessible in code.&lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Originally Posted Here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/javascript-prototypal-inheritance" rel="noopener noreferrer"&gt;https://mayankav.webflow.io/blog/javascript-prototypal-inheritance&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Is everything in JavaScript an Object?</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Sat, 10 Jul 2021 19:59:53 +0000</pubDate>
      <link>https://dev.to/mayankav/is-everything-in-javascript-an-object-1alg</link>
      <guid>https://dev.to/mayankav/is-everything-in-javascript-an-object-1alg</guid>
      <description>&lt;p&gt;Well well! The motivation for this post comes from my &lt;a href="https://dev.to/mayankav/are-all-javascript-functions-closures-27kp"&gt;last post&lt;/a&gt; where I had a comment from one of my readers that I couldn't agree with. I was talking about functions and closures &lt;a href="https://dev.to/mayankav/are-all-javascript-functions-closures-27kp"&gt;(also how closures work internally)&lt;/a&gt; and how every function in JavaScript is indeed a closure on its surrouding scope but then somebody said, &lt;strong&gt;&lt;em&gt;aren't all entities in JavaScript objects?&lt;/em&gt;&lt;/strong&gt; I didn't see that coming but this wasn't the first time somebody put this up. So you will find me trying to break this down practically in this post.&lt;/p&gt;

&lt;p&gt;If you can, bring up your browser's console and try creating a simple variable and give it a string value. How did you do it? Did you write something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;That's my string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 or did you choose to go with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;That's my string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 For a fact, I know you didn't choose the last method. I mean no body does and if you want to prove me wrong and say that you did infact choose the second one, well somebody will then probably ask you, why? MDN says "&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?redirectlocale=en-US&amp;amp;redirectslug=JavaScript/Reference/Global_Objects/String#:~:text=String%20primitives%20and%20string%20objects%20can%20be%20used%20interchangeably%20in%20most%20situations."&gt;String primitives and string objects can be used interchangeably in most situations&lt;/a&gt;". MDN then also says "&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#:~:text=a%20primitive%20(primitive%20value%2C%20primitive%20data%20type)%20is%20data%20that%20is%20not%20an%20object"&gt;A Primitive is data that is not an Object&lt;/a&gt;". The second statement makes the answer clear "&lt;strong&gt;&lt;em&gt;Not everything in JavaScript is an Object&lt;/em&gt;&lt;/strong&gt;" and this can easily be verified by using the "&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof"&gt;typeof operator&lt;/a&gt;". We still need to clarify the pin size difference. With an example?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;That's my string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;yourString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;That's your string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;myString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs "string"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;yourString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs "object"&lt;/span&gt;

&lt;span class="c1"&gt;//MDN says primitives and objects can SOMETIMES be interchangeably used&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;yourBigString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;yourString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;yourBigString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs "THAT'S YOUR STRING"&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myBigString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myBigString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs "THAT'S MY STRING"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/vYmGYpo"&gt;Try this on codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;Does that ring a bell? We do often use primitives and objects interchangeably because JavaScript makes it possible for primitives to somehow use the methods designed into real Objects. Using a primitve value shall give you the benefit of writing concise without losing on the comfort of using easy methods to manipulate and process the values. When you call a String object's method on a primitive string value, to make this work your primitve is first wrapped into an appropriate Wrapper class (String in this case). The method you want to call is called on the transient object which is discarded as soon as the result is returned. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?redirectlocale=en-US&amp;amp;redirectslug=JavaScript/Reference/Global_Objects/String#:~:text=JavaScript%20automatically%20converts%20primitives%20to%20String%20objects%2C%20so%20that%20it%27s%20possible%20to%20use%20String%20object%20methods%20for%20primitive%20strings"&gt;Read on MDN&lt;/a&gt;. The following codepen shall present a demo. &lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/mayankav/embed/OJmNPqv?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;You may have two question now&lt;/u&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If this is taken care of internally, what may go wrong if we assume that everything in JavaScript is an Object?&lt;/li&gt;
&lt;li&gt;If the JS engine does this autoboxing internally everytime we use a primitive, isn't this more expensive than simply using String Objects?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To answer the first question, let me throw another example which is an extension to the very first example. The example shows a case where someone would try assigning a property to some primitive expecting it to be retrievable. If you assign some property to a string primitive with an intention of getting the value back at some point, you will only be returned '&lt;strong&gt;&lt;em&gt;undefined&lt;/em&gt;&lt;/strong&gt;' because the temporary String Object was discarded then and there. Similarly such assumptions can misfire &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?redirectlocale=en-US&amp;amp;redirectslug=JavaScript/Reference/Global_Objects/String#:~:text=String%20primitives%20and%20String%20objects%20also%20give%20different%20results%20when%20using%20eval()"&gt;when using eval&lt;/a&gt; (which indeed should not be used). These examples may not be very relatable but I guess they are enough to warn us from assumptions. &lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/mayankav/embed/vYmGYpo?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I believe the second question is no less important. Even though this seems like a tedious process of coercing a primitive into an Object and then calling the method on the temporary object, the JavaScript engine highly optimizes this process. It may even skip the creation of the extra object altogether. If you still wonder why do we even have primitives, you better ask Brendan Eich as T.J Crowder says in &lt;a href="https://stackoverflow.com/questions/61427733/why-is-the-string-literal-considered-a-primitive-type-in-javascript#:~:text=So%20why%20have%20primitive%20strings%3F%20You%27d%20have%20to%20ask%20Brendan%20Eich"&gt;this answer&lt;/a&gt;. &lt;/p&gt;



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

&lt;p&gt;To conclude I'd just highlight that MDN says "&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?redirectlocale=en-US&amp;amp;redirectslug=JavaScript/Reference/Global_Objects/String#:~:text=String%20primitives%20and%20string%20objects%20can%20be%20used%20interchangeably%20in%20most%20situations."&gt;String primitives and string objects can be used interchangeably in most situations&lt;/a&gt;". We probably know why they say "&lt;strong&gt;most situations&lt;/strong&gt;" and not "&lt;strong&gt;always&lt;/strong&gt;". &lt;/p&gt;

&lt;h2&gt;
  
  
  Originally Posted Here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/is-everything-in-javascript-an-object"&gt;https://mayankav.webflow.io/blog/is-everything-in-javascript-an-object&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Are all JavaScript functions Closures ?</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Sun, 04 Jul 2021 18:43:59 +0000</pubDate>
      <link>https://dev.to/mayankav/are-all-javascript-functions-closures-27kp</link>
      <guid>https://dev.to/mayankav/are-all-javascript-functions-closures-27kp</guid>
      <description>&lt;p&gt;Probably this is one of the most dreaded topics to talk about. Quite a few readers won't even bother to read on but hey! hang in there and we will keep things simple. To explain what closures are, I won't begin with the classic ubiquitous example of a silly function within another function (keeping it for later though). Well, closures also play a very obvious role when implementing event handlers (DOM Level 2). Lets talk with an example, shall we?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzwg29gu0ozgqy0gu62io.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzwg29gu0ozgqy0gu62io.PNG" alt="Event Handler"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;&lt;a href="https://codepen.io/mayankav/pen/QWvLgpP" rel="noopener noreferrer"&gt;Try this out on codepen&lt;/a&gt;&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;There's nothing much to the code shown above, its as simple as a function that takes in an integer and sets event listeners on two different buttons. The first event listener calls a function (callback) when the first button is clicked. The callback function in turn increments the counter and shows it in an alert box. The second event handler does nothing different apart from the fact that its callback function decrements the same counter. Do we understand that even after the function &lt;strong&gt;&lt;em&gt;setClickHandler&lt;/em&gt;&lt;/strong&gt; has finished execution (offboarded from the execution stack with all its variables and arguments), the two event listeners are actively listening for a click on their respective buttons? Well, that's how event listeners work, right? You set them up once and they stay there for you unless you call &lt;strong&gt;&lt;em&gt;"removeEventListener"&lt;/em&gt;&lt;/strong&gt; on the node. There's another interesting thing to observe in the small code snippet. When the function &lt;strong&gt;&lt;em&gt;"setClickHandler"&lt;/em&gt;&lt;/strong&gt; has already left the call stack (no arguments and local variables in memory anymore), why do the callback functions not throw a &lt;strong&gt;&lt;em&gt;"ReferenceError"&lt;/em&gt;&lt;/strong&gt; when trying to access the argument &lt;strong&gt;&lt;em&gt;"counter"&lt;/em&gt;&lt;/strong&gt; which effectively lies in the lexical scope of &lt;strong&gt;&lt;em&gt;"setClickHandler"&lt;/em&gt;&lt;/strong&gt; ? Well, if you want to attribute this to the scope chain, you won't be completely wrong but then when I ask you how does something up in the scope chain remain active in memory after the owner function has offboarded the execution stack? That's when you need to say &lt;strong&gt;&lt;em&gt;CLOSURES&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;



&lt;h2&gt;
  
  
  What is a closure?
&lt;/h2&gt;

&lt;p&gt;Lets try to gain some clarity on stuff we discussed so far. The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures" rel="noopener noreferrer"&gt;MDN&lt;/a&gt; says a function along with references to its surrounding scopes is a closure for that function. Well, won't you then protest that literally every function in JavaScript by default gets access to everything in its respective lexical (surrounding) scope? If that's true then every function has to have a closure as soon as its created. Bingo! guess who is correct again? &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#:~:text=Functions%20created%20with%20the%20Function%20constructor%20do%20not%20create%20closures%20to%20their%20creation%20contexts%3B%20they%20always%20are%20created%20in%20the%20global%20scope." rel="noopener noreferrer"&gt;Exception being functions created with the Function constructor&lt;/a&gt;. We usually tend to ignore a function's ability to fom closures when the function finishes its execution well within its lexical scope. What if the same function gets returned from its lexical scope and is refrenced somewhere else later in time? The function will still have access to the references (memory location of variables and functions) in its lexical scope as it would even if it were not returned. Wait! how? Didn't the scope chain die when the outer function finished execution? Infact, it did! but before it died, right when the inner function was being returned, the scope chain was saved as a property ( or an &lt;a href="https://tc39.es/ecma262/#:~:text=ECMAScript%20function%20objects%20also%20have%20the%20internal%20slots" rel="noopener noreferrer"&gt;internal slot&lt;/a&gt; as ECMAScript quotes ) of the inner function itself. The specification calls it &lt;strong&gt;&lt;em&gt;[[Environent]]&lt;/em&gt;&lt;/strong&gt;, however Google Chrome uses something like &lt;strong&gt;&lt;em&gt;( [[Scopes]]: { closure } )&lt;/em&gt;&lt;/strong&gt;. It seems chrome did this back in time following &lt;a href="https://262.ecma-international.org/5.1/#:~:text=Set%20the%20%5B%5BScope%5D%5D%20internal%20property%20of%20F%20to%20the%20value%20of%20Scope." rel="noopener noreferrer"&gt;the old specification from 2011&lt;/a&gt;. Just to mention, Firefox and IE don't even show a function's inner slot like chrome does. Alright, so this saved scope chain is nothing but &lt;strong&gt;&lt;em&gt;Closure&lt;/em&gt;&lt;/strong&gt;. That's how an inner function closes over the references in its surrounding scope.&lt;/p&gt;

&lt;p&gt;If you try to inspect such a function's execution in devtools, you shall notice that as soon as the function is pushed on the top of the execution stack, its closure shows up in the scope chain as if the function carried it throughout. If you're 5, a closure is a function who took notes &lt;strong&gt;&lt;em&gt;( [[Environent]] )&lt;/em&gt;&lt;/strong&gt; to remember what has been taught in the school, outside the school.&lt;/p&gt;

&lt;p&gt;‍&lt;br&gt;
&lt;a href="https://media.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%2F8nyv0ycdhraiizu676rr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8nyv0ycdhraiizu676rr.png" alt="Do not Forget"&gt;&lt;/a&gt;&lt;br&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  Detect a Closure
&lt;/h2&gt;

&lt;p&gt;If you really want to visualize this, the easiest way is to use &lt;a href="https://ui.dev/javascript-visualizer/" rel="noopener noreferrer"&gt;an online visualizer&lt;/a&gt;. I will keep it simple and use chrome devtools instead.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuz28ne89z2iexqj4b79j.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuz28ne89z2iexqj4b79j.PNG" alt="Inner function returned from the Outer funtion"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;Inner function returned from the Outer funtion&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frlxii878bty6sr561i2o.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frlxii878bty6sr561i2o.PNG" alt="Inner function not returned"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;It's not necessary that the inner function be returned to form a closure&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3xp4revpfjh8qfn5lvr.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3xp4revpfjh8qfn5lvr.PNG" alt="Chrome does not show the inner function in the Local scope"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;Chrome does not show the inner function in the Local scope?&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;



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

&lt;p&gt;Well, now you know how closures are formed. The last example can be a bit confusing. So to clarify, since Chrome does not show the inner function in the outer function's local scope, it does not mean that it doesn't exist (You can see that on Firebox). Chrome probably lists the function only when it has been invoked, stored or returned. Similarly, every function when created forms a closure with its surrounding scope but we only care about closures when the function escapes its lexical scope to execute at some point later in time after its surrounding function is dead. In the end, it shouldn't matter much whether you say all JavaScript functions are closures or all JavaScript functions have closures. You get the gist of it. Here's an &lt;a href="https://codepen.io/mayankav/pen/BaRBxMz" rel="noopener noreferrer"&gt;interesting codepen &lt;/a&gt;that demonstrates function currying, a good practical use-case of closures. I would also recommend reading &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures#performance_considerations" rel="noopener noreferrer"&gt;the performance considerations&lt;/a&gt; on MDN.&lt;/p&gt;

&lt;h2&gt;
  
  
  Originally Posted Here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/all-functions-are-closures" rel="noopener noreferrer"&gt;https://mayankav.webflow.io/blog/all-functions-are-closures&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript's Broken Mathematics ? 💔💔</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Sun, 27 Jun 2021 20:49:57 +0000</pubDate>
      <link>https://dev.to/mayankav/javascript-s-broken-mathematics-304m</link>
      <guid>https://dev.to/mayankav/javascript-s-broken-mathematics-304m</guid>
      <description>&lt;p&gt;Does mathematics already scare you? Well, if it does take a deep breath and read on and by any chance if it does not, I will try my best to scare you now but ofcourse with a promise that we shall fill all the gaps before ending this post. Doesn't matter what programming langauge you code in, you shall still be able to relate to this post. For my convenience I will kill it with JavaScript.&lt;/p&gt;

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

&lt;p&gt;So long back, I was working on a game that added &lt;b&gt;1 point&lt;/b&gt; to the user's score for every correct answer and deducted &lt;b&gt;0.1 points&lt;/b&gt; for a wrong one. The game starts with 1 point in your balance and then the score gets calculated based on your choices. Prima facie it worked fine (kind of) but then something caught me by dismay. I started the game (score = 1) and submitted three wrong answers back to back. What do you expect? A score of &lt;b&gt;1 - 0.1 - 0.1 - 0.1 = 0.7&lt;/b&gt; ? Got you! Try that right away in your browser's console. It works alright you say? I bet you did &lt;b&gt;1 - 0.3&lt;/b&gt;, that indeed shall give you &lt;b&gt;0.7&lt;/b&gt; but when you do it incrementally like I did, you shall see that&lt;/p&gt;

&lt;p&gt;✔️ 1 - 0.1 = 0.9&lt;/p&gt;

&lt;p&gt;✔️ 0.9 - 0.1 = 0.8&lt;/p&gt;

&lt;p&gt;❌ 0.8 - 0.1 = 0.7000000000000001&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjsumn4sizgffk57mv6t0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjsumn4sizgffk57mv6t0.png" alt="confusion"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;Confused? Check out &lt;a href="https://codepen.io/mayankav/pen/mdWNqqz" rel="noopener noreferrer"&gt;this codepen&lt;/a&gt;
&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;Why is &lt;b&gt;0.8 - 0.1&lt;/b&gt; not &lt;b&gt;0.7&lt;/b&gt;? Well, it is so in real world mathematics. &lt;b&gt;So, is JavaScript's mathematics broken?&lt;/b&gt; Co-readers who also code in python would now tell you that even Python failed in its maths class. What's happening? Well, if you want a short blunt answer its the binary system making floating point calculations unpredictable. So yes its not your favorite programming language. We shall ofcourse discuss how to get around with this limitation but I cannot hold myself from digging a little more into the root cause.&lt;/p&gt;




&lt;p&gt;Do we all understand that our computers store all and any kind of information in binary? Assuming you said 'YES', how is a decimal number (which we input) converted into binary before it gets stored? Do you know that after the number is converted into binary, to get stored in the register (memory), the binary should be first arranged in some appropriate format? &lt;b&gt;"Binary Floating Point Representation"&lt;/b&gt; is the term we use for those formats. Call it &lt;b&gt;FPR&lt;/b&gt; for simplicity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2qu1t4teggfg3ajp3ctd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2qu1t4teggfg3ajp3ctd.png" alt="FPR"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;Floating Point Representation&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;
&lt;h2&gt;
  
  
  Binary Floating Point Representation can be of 3 types :
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Half Precision Format&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;available memory for a given number = &lt;strong&gt;16 bits&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dclausen.net/projects/microfloat/" rel="noopener noreferrer"&gt;microFloat&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;least precise &amp;amp; least wasteful&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Single Precision Format&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;available memory for a given number = &lt;strong&gt;32 bits&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;float data-type in Java&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Double Precision Format&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;available memory for a given number = &lt;strong&gt;64 bits&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;double data-type in Java&lt;/li&gt;
&lt;li&gt;most accurate representation of bigger numbers‍‍&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;&lt;br&gt;Taking you back to school? No, please take a quick look (1.5x speed) at &lt;a href="https://www.youtube.com/watch?v=LScLyZRBpk8" rel="noopener noreferrer"&gt;this video&lt;/a&gt; if you're not sure what did I just say. Now that you know we have limited space in the memory to store the binary representation, what if the binary of some number you input doesn't fit in 64 bits? Well, we round it up and make it fit in 64 bits somehow and hence we introduce the famous &lt;a href="https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#:~:text=Squeezing%20infinitely%20many%20real%20numbers,bits%20requires%20an%20approximate%20representation.&amp;amp;text=Therefore%20the%20result%20of%20a,feature%20of%20floating%2Dpoint%20computation." rel="noopener noreferrer"&gt;Rounding Error&lt;/a&gt;. This rounding error is the characteristic feature of floating-point computation and obviously when you input some number X, it may or may not stay exactly X after binary round off.&lt;/p&gt;

&lt;p&gt;So what could be the examples of numbers whose binary won't fit even in 64 bits? A very obvious case can be of a number whose binary representation is non-terminating. &lt;strong&gt;0.1&lt;/strong&gt; ? Seriously? Yes, lets see how this simple small decimal number has got a binary equivalent that never terminates (like the value of &lt;strong&gt;π&lt;/strong&gt; in decimal).&lt;/p&gt;

&lt;p&gt;‍&lt;br&gt;
&lt;a href="https://media.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%2F0d436eebzfc63xaafi6p.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0d436eebzfc63xaafi6p.jpeg" alt="Decimal To Binary Conversion"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;Not my best handwriting though&lt;/h6&gt;&lt;/center&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fauvapb4hoq98tadube8e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fauvapb4hoq98tadube8e.png" alt="Non Terminating Binary"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;That's how the decimal 0.1 looks in binary&lt;br&gt;
&lt;/h6&gt;&lt;/center&gt;



&lt;br&gt;
&lt;em&gt;There's a &lt;b&gt;&lt;u&gt;simple rule&lt;/u&gt;&lt;/b&gt; to find out if the given decimal number will have a non terminating binary or not. A decimal has an equivalent terminating binary if and only if the decimal, written as a proper fraction in lowest terms, has a denominator that is a power of two. &lt;b&gt;Example&lt;/b&gt; : 0.1 has an infinite binary : 0.1 = 1/10, and 10 is not a power of two. Also 0.5, on the other hand, terminates: 0.5 = 5/10 = 1/2.&lt;/em&gt;&lt;br&gt;


&lt;p&gt;Apart from such numbers with non terminating binaries there can also be numbers with terminating but too big to fit in 64 bits binaries. Such numbers can also result in rounding errors. Now when I ask you to debug my game, you shall be able to atleast say (after looking at the output) that 0.8 - 0.1 is not 0.7 because somewhere in the binary round-off 0.8, 0.1 or 0.7 or maybe all of them got introduced to the rounding error. So what do we learn from this? We learn that FPR of the decimal numbers we input can make calculations unpredictable. How do we deal with this? Well, I shall tell you how, atleast how in JavaScript.&lt;/p&gt;

&lt;p&gt;‍&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution to the round-off error in JavaScript
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;b&gt;Math.round((0.8-0.1)*factor)/factor&lt;/b&gt; shall give &lt;b&gt;0.7&lt;/b&gt;, where &lt;b&gt;factor = 10&lt;/b&gt; for rounding to single digit, &lt;b&gt;100&lt;/b&gt; for rounding the result to 2 digits after decimal and so on.&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;(0.8-0.1).toFixed(1)&lt;/b&gt; shall give &lt;b&gt;"0.7"&lt;/b&gt; but in string format. Mostly irrelevant now but "toFixed" may show inconsistencies amongst older versions of some browsers. &lt;a href="https://stackoverflow.com/questions/566564/math-roundnum-vs-num-tofixed0-and-browser-inconsistencies" rel="noopener noreferrer"&gt;Read more&lt;/a&gt;&lt;a&gt;&lt;/a&gt;.&lt;/li&gt;

&lt;li&gt;There can be many more solutions. For example the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor" rel="noopener noreferrer"&gt;"floor"&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil" rel="noopener noreferrer"&gt;"ceil"&lt;/a&gt; functions of the Math object depending on the use-case or even &lt;a href="https://codepen.io/mayankav/pen/RwVbbqg" rel="noopener noreferrer"&gt;custom functions like so&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;



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

&lt;p&gt;Most decimals have infinite representations in binary. Due to limitation of memory, rounding errors may get introduced in numbers whose binary equivalent does not fit even the Double Precision Format. So do not be surprised the next time you see an anomaly in floating point calculations. Its good to use one of the above mentioned solutions or a custom tailored solution that fits your requirement.&lt;/p&gt;

&lt;p&gt;‍&lt;/p&gt;

&lt;h2&gt;
  
  
  Originally posted here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/javascripts-broken-mathematics" rel="noopener noreferrer"&gt;https://mayankav.webflow.io/blog/javascripts-broken-mathematics&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript's Broken Mathematics ? 💔</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Sun, 27 Jun 2021 19:30:38 +0000</pubDate>
      <link>https://dev.to/mayankav/javascript-s-broken-mathematics-5al6</link>
      <guid>https://dev.to/mayankav/javascript-s-broken-mathematics-5al6</guid>
      <description>&lt;p&gt;Does mathematics already scare you? Well, if it does take a deep breath and read on and by any chance if it does not, I will try my best to scare you now but ofcourse with a promise that we shall fill all the gaps before ending this post. Doesn't matter what programming langauge you code in, you shall still be able to relate to this post. For my convenience I will kill it with JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zhIgfDKJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvgz39b9jraelnjs49na.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zhIgfDKJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvgz39b9jraelnjs49na.png" alt="start"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So long back, I was working on a game that added &lt;b&gt;1 point&lt;/b&gt; to the user's score for every correct answer and deducted &lt;b&gt;0.1 points&lt;/b&gt; for a wrong one. The game starts with 1 point in your balance and then the score gets calculated based on your choices. Prima facie it worked fine (kind of) but then something caught me by dismay. I started the game (score = 1) and submitted three wrong answers back to back. What do you expect? A score of &lt;b&gt;1 - 0.1 - 0.1 - 0.1 = 0.7&lt;/b&gt; ? Got you! Try that right away in your browser's console. It works alright you say? I bet you did &lt;b&gt;1 - 0.3&lt;/b&gt;, that indeed shall give you &lt;b&gt;0.7&lt;/b&gt; but when you do it incrementally like I did, you shall see that&lt;/p&gt;

&lt;p&gt;✔️ 1 - 0.1 = 0.9&lt;/p&gt;

&lt;p&gt;✔️ 0.9 - 0.1 = 0.8&lt;/p&gt;

&lt;p&gt;❌ 0.8 - 0.1 = 0.7000000000000001&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g0X4-eKw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsumn4sizgffk57mv6t0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g0X4-eKw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsumn4sizgffk57mv6t0.png" alt="confusion"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;Confused? Check out &lt;a href="https://codepen.io/mayankav/pen/mdWNqqz"&gt;this codepen&lt;/a&gt;
&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;Why is &lt;b&gt;0.8 - 0.1&lt;/b&gt; not &lt;b&gt;0.7&lt;/b&gt;? Well, it is so in real world mathematics. &lt;b&gt;So, is JavaScript's mathematics broken?&lt;/b&gt; Co-readers who also code in python would now tell you that even Python failed in its maths class. What's happening? Well, if you want a short blunt answer its the binary system making floating point calculations unpredictable. So yes its not your favorite programming language. We shall ofcourse discuss how to get around with this limitation but I cannot hold myself from digging a little more into the root cause.&lt;/p&gt;




&lt;p&gt;Do we all understand that our computers store all and any kind of information in binary? Assuming you said 'YES', how is a decimal number (which we input) converted into binary before it gets stored? Do you know that after the number is converted into binary, to get stored in the register (memory), the binary should be first arranged in some appropriate format? &lt;b&gt;"Binary Floating Point Representation"&lt;/b&gt; is the term we use for those formats. Call it &lt;b&gt;FPR&lt;/b&gt; for simplicity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ryq_69J_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qu1t4teggfg3ajp3ctd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ryq_69J_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qu1t4teggfg3ajp3ctd.png" alt="FPR"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;Floating Point Representation&lt;/h6&gt;&lt;/center&gt;
&lt;br&gt;
&lt;h2&gt;
  
  
  Binary Floating Point Representation can be of 3 types :
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Half Precision Format&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;available memory for a given number = &lt;strong&gt;16 bits&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dclausen.net/projects/microfloat/"&gt;microFloat&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;least precise &amp;amp; least wasteful&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;&lt;strong&gt;Single Precision Format&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;available memory for a given number = &lt;strong&gt;32 bits&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;float data-type in Java&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;&lt;strong&gt;Double Precision Format&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;available memory for a given number = &lt;strong&gt;64 bits&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;double data-type in Java&lt;/li&gt;
&lt;li&gt;most accurate representation of bigger numbers‍‍&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;&lt;br&gt;Taking you back to school? No, please take a quick look (1.5x speed) at &lt;a href="https://www.youtube.com/watch?v=LScLyZRBpk8"&gt;this video&lt;/a&gt; if you're not sure what did I just say. Now that you know we have limited space in the memory to store the binary representation, what if the binary of some number you input doesn't fit in 64 bits? Well, we round it up and make it fit in 64 bits somehow and hence we introduce the famous &lt;a href="https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#:~:text=Squeezing%20infinitely%20many%20real%20numbers,bits%20requires%20an%20approximate%20representation.&amp;amp;text=Therefore%20the%20result%20of%20a,feature%20of%20floating%2Dpoint%20computation."&gt;Rounding Error&lt;/a&gt;. This rounding error is the characteristic feature of floating-point computation and obviously when you input some number X, it may or may not stay exactly X after binary round off.&lt;/p&gt;

&lt;p&gt;So what could be the examples of numbers whose binary won't fit even in 64 bits? A very obvious case can be of a number whose binary representation is non-terminating. &lt;strong&gt;0.1&lt;/strong&gt; ? Seriously? Yes, lets see how this simple small decimal number has got a binary equivalent that never terminates (like the value of &lt;strong&gt;π&lt;/strong&gt; in decimal).&lt;/p&gt;

&lt;p&gt;‍&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J7najfkV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d436eebzfc63xaafi6p.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J7najfkV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d436eebzfc63xaafi6p.jpeg" alt="Decimal To Binary Conversion"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;Not my best handwriting though&lt;/h6&gt;&lt;/center&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n9ZD9WAx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/auvapb4hoq98tadube8e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n9ZD9WAx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/auvapb4hoq98tadube8e.png" alt="Non Terminating Binary"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;That's how the decimal 0.1 looks in binary&lt;br&gt;
&lt;/h6&gt;&lt;/center&gt;



&lt;br&gt;
&lt;em&gt;There's a &lt;b&gt;&lt;u&gt;simple rule&lt;/u&gt;&lt;/b&gt; to find out if the given decimal number will have a non terminating binary or not. A decimal has an equivalent terminating binary if and only if the decimal, written as a proper fraction in lowest terms, has a denominator that is a power of two. &lt;b&gt;Example&lt;/b&gt; : 0.1 has an infinite binary : 0.1 = 1/10, and 10 is not a power of two. Also 0.5, on the other hand, terminates: 0.5 = 5/10 = 1/2.&lt;/em&gt;&lt;br&gt;


&lt;p&gt;Apart from such numbers with non terminating binaries there can also be numbers with terminating but too big to fit in 64 bits binaries. Such numbers can also result in rounding errors. Now when I ask you to debug my game, you shall be able to atleast say (after looking at the output) that 0.8 - 0.1 is not 0.7 because somewhere in the binary round-off 0.8, 0.1 or 0.7 or maybe all of them got introduced to the rounding error. So what do we learn from this? We learn that FPR of the decimal numbers we input can make calculations unpredictable. How do we deal with this? Well, I shall tell you how, atleast how in JavaScript.&lt;/p&gt;

&lt;p&gt;‍&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution to the round-off error in JavaScript
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;b&gt;Math.round((0.8-0.1)*factor)/factor&lt;/b&gt; shall give &lt;b&gt;0.7&lt;/b&gt;, where &lt;b&gt;factor = 10&lt;/b&gt; for rounding to single digit, &lt;b&gt;100&lt;/b&gt; for rounding the result to 2 digits after decimal and so on.&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;(0.8-0.1).toFixed(1)&lt;/b&gt; shall give &lt;b&gt;"0.7"&lt;/b&gt; but in string format. Mostly irrelevant now but "toFixed" may show inconsistencies amongst older versions of some browsers. &lt;a href="https://stackoverflow.com/questions/566564/math-roundnum-vs-num-tofixed0-and-browser-inconsistencies"&gt;Read more&lt;/a&gt;&lt;a&gt;&lt;/a&gt;.&lt;/li&gt;

&lt;li&gt;There can be many more solutions. For example the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor"&gt;"floor"&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil"&gt;"ceil"&lt;/a&gt; functions of the Math object depending on the use-case or even &lt;a href="https://codepen.io/mayankav/pen/RwVbbqg"&gt;custom functions like so&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;



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

&lt;p&gt;Most decimals have infinite representations in binary. Due to limitation of memory, rounding errors may get introduced in numbers whose binary equivalent does not fit even the Double Precision Format. So do not be surprised the next time you see an anomaly in floating point calculations. Its good to use one of the above mentioned solutions or a custom tailored solution that fits your requirement.&lt;/p&gt;

&lt;p&gt;‍&lt;/p&gt;

&lt;h2&gt;
  
  
  Originally posted here -
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/javascripts-broken-mathematics"&gt;https://mayankav.webflow.io/blog/javascripts-broken-mathematics&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Key Element to Responsive Websites</title>
      <dc:creator>mayankav</dc:creator>
      <pubDate>Sun, 20 Jun 2021 18:56:37 +0000</pubDate>
      <link>https://dev.to/mayankav/the-key-element-to-responsive-websites-31e5</link>
      <guid>https://dev.to/mayankav/the-key-element-to-responsive-websites-31e5</guid>
      <description>&lt;p&gt;What is the first thing you think of when someone says responsive websites? What comes rushing to your mind when you think of the most important piece of code related to responsive websites? CSS? relative units (%)? Media queries? Well, we won't talk about that here. This post aims to pin point the most essential stuff without which none of your responsive techniques would ever work the way you expect them to. We are talking about the  html tag. Well, but you know meta tags have got nothing to do with what's being rendered, right? They just carry information regarding the page content. They do not even appear on the page themselves. How do they matter when it comes to making our web page responsive? &lt;/p&gt;

&lt;p&gt;There's a special meta tag called the meta viewport, which actually has a lot to do with how well your page adapts to the screen it is rendered on. Lets dig deep.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Meta Viewport Example&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=80, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at the code snippet that shows a meta tag enclosed within the Head section. The name attribute says viewport. That should be enough to indicate that this tag is supposed to add some kind of information related to the viewport - the space on the screen on which your web page is being rendered. Lets not confuse the viewport width with screen width. They may be the same when the rendering device is a mobile phone (we shall talk about this later in the same post) but on desktops and PCs, you can play with the size of your browser. &lt;b&gt;&lt;u&gt;Check out the following piece of code.&lt;/u&gt;&lt;/b&gt; Paste it in your browser console and resize your window to see how the viewport width changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;resize&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientWidth&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we hopefully understand the difference between the physical screen width and the viewport width, let me break it to you that mobile phones generally (if not all of them) do not map the viewport to the exact dimensions of the screen (width). You can check out some real examples &lt;a href="https://experienceleague.adobe.com/docs/target/using/experiences/vec/mobile-viewports.html?lang=en" rel="noopener noreferrer"&gt;here&lt;/a&gt;. So for example if a mobile phone's screen is say 320px wide, it may consider a virtual viewport that is 800px, 900px wide or whatever and then minimize (zoom out) the rendered output to fit the screen size. Why would they do so you ask? This comes from back in time when responsive websites were not that common maybe. Even today not all websites are made mobile friendly (which indeed isn't what we want to do). If the mobile browser vendors mapped the viewport width of the browser to the screen width, the elements in the real website (big images/big buttons etc..) would break when rendered on such a small viewport. So they render it on a bigger viewport and then scale down the result to show it to you on a smaller screen. You can then zoom in on the part you want. You want an example? Checkout &lt;a href="https://www.youtube.com/" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt; but do it on your desktop and then resize your window to make sure you do not go to m.youtube.com which is another version meant specifically for mobile phones. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkusib41gn67ubya7q04.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkusib41gn67ubya7q04.PNG" alt="YouTube Desktop"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;YouTube Desktop (not optimized for mobile experience)&lt;/h6&gt;&lt;/center&gt;




&lt;p&gt;This however, is not an ideal experience for websites which are made mobile friendly. Can you guess what sort of issues can this virtual viewport cause? You guessed it, didn't you? The media queries will start breaking. If you put in place a media query that should fire at 320px, it simply won't get triggered because factually the screen width is 900px which was just zoomed out to show it to you on a 320px wide mobile screen. What do such mobile browser vendors do for developers like us who write media queries to make our websites look good on all screen sizes? Well, they gave us this meta viewport tag which we can now use on our web page and tell the browser, to set the virtual viewport's width in accordance with our needs. So now when you say&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;html &amp;lt;meta name="viewport" content="width=device-width" /&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
, the mobile browser knows that you want to render your page with a virtual viewport that is as wide as the device's screen. Of course, you can set this "width" value to random pixels but that does not bring us joy. You can see the examples on &lt;a href="https://www.w3schools.com/css/css_rwd_viewport.asp" rel="noopener noreferrer"&gt;w3school&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Interestingly, if you use boilerplate builder tools like create-react-app, vue create etc.. you may or may not have bothered to go through the index.html file completely. If you do a cross check, you shall definitely find the meta viewport tag included in the head section. Get rid of it for a while and see how your web app behaves on different screen sizes. Hopefully now, everyone can relate to the virtual viewport of mobile browsers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5yt9nm69wxjgapeth08u.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5yt9nm69wxjgapeth08u.jpg" alt="Virtual Viewports"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;h6&gt;Learn more about Virtual Viewports &lt;a href="https://developers.google.com/web/updates/2015/01/What-the-Viewport" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/h6&gt;&lt;/center&gt;




&lt;h2&gt;Originally posted here -&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mayankav.webflow.io/blog/key-to-responsive-websites" rel="noopener noreferrer"&gt;https://mayankav.webflow.io/blog/key-to-responsive-websites&lt;/a&gt;&lt;/p&gt;

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