<?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: Saloni Yadav</title>
    <description>The latest articles on DEV Community by Saloni Yadav (@salyadav).</description>
    <link>https://dev.to/salyadav</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%2F360350%2F5b94edc1-23c3-43b5-af89-a455180e9238.jpg</url>
      <title>DEV Community: Saloni Yadav</title>
      <link>https://dev.to/salyadav</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/salyadav"/>
    <language>en</language>
    <item>
      <title>Little shenanigans of JavaScript- the sly Map of ES6</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Sun, 19 Jul 2020 08:59:29 +0000</pubDate>
      <link>https://dev.to/salyadav/little-shenanigans-of-javascript-the-sly-map-of-es6-aj7</link>
      <guid>https://dev.to/salyadav/little-shenanigans-of-javascript-the-sly-map-of-es6-aj7</guid>
      <description>&lt;p&gt;We all know how quirky Javascript is, with all its sly ways and silent mischiefs. I encountered another such shenanigan of JS while using Maps. For those of you still living in ice age- YES, JavaScript supports Map (and Set) Data Structures ES6 on.&lt;/p&gt;

&lt;p&gt;Before we see what motivated me to write this post, let's quickly brush up what JS has to offer in Maps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is it? - Just an easier way to create an object of key-value pairs. Needless to say, it inherits &lt;em&gt;Object&lt;/em&gt; prototype. &lt;/li&gt;
&lt;li&gt;How is it any different from Objects? - We get several functions to make our life easier.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Find the documentation on Maps &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For the sake of this article, let me list out some basic functions that we will be using.&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;//creating a new Map&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourMap&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;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//setting key-value pairs&lt;/span&gt;
&lt;span class="nx"&gt;ourMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;firstName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Regina&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;ourMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lastName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Phalange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//getting value, given a key&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ourMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;firstName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;ourMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lastName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//fullName-&amp;gt; "Regina Phalange"&lt;/span&gt;

&lt;span class="c1"&gt;//Size of the map - number of key-value pairs&lt;/span&gt;
&lt;span class="nx"&gt;ourMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//returns 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you try the above out on a browser's developer console, you will see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vmeuackG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mw9vl21njpd62yw7310t.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vmeuackG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mw9vl21njpd62yw7310t.JPG" alt="Alt Text" width="789" height="627"&gt;&lt;/a&gt;&lt;br&gt;
(Note the &lt;em&gt;arrow mark&lt;/em&gt; notation of Map's key-value pairs)&lt;/p&gt;

&lt;p&gt;Now, since Map is nothing but a data structure holding key value pairs, it feels quite intuitive to go ahead and set the key-value using the usual Object properties method. Something like this-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ourMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;profession&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Trader&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;ourMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//All these return true as the properties indeed get set.&lt;/span&gt;
&lt;span class="c1"&gt;//You can check as following:&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;ourMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;profession&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;ourMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;ourMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&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;p&gt;The above also sets a key-value pair in &lt;code&gt;ourMap&lt;/code&gt;. But what happens when you fetch the &lt;em&gt;size&lt;/em&gt; of the map???&lt;/p&gt;

&lt;p&gt;BAM! It is still 2! &lt;br&gt;
Then where on earth did &lt;code&gt;ourMap&lt;/code&gt; engulf the &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;profession&lt;/code&gt; keys? Where did it fetch these from when we console logged it??&lt;/p&gt;

&lt;p&gt;The answers to these are very similar to &lt;em&gt;Who murdered my array&lt;/em&gt; in my &lt;a href="https://dev.to/salyadav/little-shenanigans-of-javascript-2-2dkb"&gt;previous post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When we take a look at the map after all our fiddling, we see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YqyBF5yJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rxkndubpaw64jcjx8t1s.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YqyBF5yJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rxkndubpaw64jcjx8t1s.JPG" alt="Alt Text" width="757" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The key-value pair ONLY when set through the &lt;em&gt;getter-setter&lt;/em&gt;, populates the entry in the &lt;code&gt;[[Entries]]&lt;/code&gt; of the Map (note the arrowed key-value). When set through the object literals, it uses &lt;em&gt;Prototypal Chaining&lt;/em&gt; and accesses the methods offered by the &lt;em&gt;Object&lt;/em&gt; interface to set the key-values OUTSIDE the &lt;code&gt;[[Entries]]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Consequently, &lt;code&gt;ourMap.size&lt;/code&gt; which has access to only the Map Entries counts only the &lt;code&gt;firstName&lt;/code&gt; and the &lt;code&gt;lastName&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I have been working on developing a game. While using the Map to store Game Level Statistics, I used the shorthand for key-values and went bonkers when the map size kept returning 0. Only after further digging did I realise what was happening. I went back to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"&gt;documentation&lt;/a&gt; and realised, there was a clear disclaimer for such a behavior, specially for lazy developers like myself. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Lesson learnt?? ALWAYS read the documentation thoroughly!🥱🥱&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we know what's happening, why stop here? Let's have some fun with it...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vd87gdIJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bkq3yes1xltb2ax4tpfz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vd87gdIJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bkq3yes1xltb2ax4tpfz.gif" alt="Alt Text" width="220" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A trick you can play on JS newbies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&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;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;firstName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Regina&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lastName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Phalange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Phoebe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Buffey&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;firstName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//Regina&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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//Phoebe&lt;/span&gt;

&lt;span class="c1"&gt;//And the left Phalange of my brain short-circuits!! XD&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w4T2oz9S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/63krvr40b993xztd08td.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w4T2oz9S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/63krvr40b993xztd08td.JPG" alt="Alt Text" width="710" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TUn7VUlG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5m4yih2viemqzfo04ih2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TUn7VUlG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5m4yih2viemqzfo04ih2.gif" alt="Alt Text" width="360" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This can also be a potentially popular entry-level JS interview question to see if you got your fundas right.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;P.S. Remember that you cannot use any of the map functions on attributes that you set through object literals. &lt;/p&gt;

&lt;p&gt;Again, lesson learnt? Read the damn documentation well! Hope you learnt something out of this article. We can also extrapolate this behavior to other data structures in JS, including Sets and Arrays... The fundamental remains the same!&lt;/p&gt;

&lt;p&gt;Until next time...👋&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Who else misses DEV's old Unicorn n Heart icons?</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Wed, 17 Jun 2020 15:35:40 +0000</pubDate>
      <link>https://dev.to/salyadav/who-else-misses-dev-s-old-unicorn-heart-icons-2dd4</link>
      <guid>https://dev.to/salyadav/who-else-misses-dev-s-old-unicorn-heart-icons-2dd4</guid>
      <description>&lt;p&gt;I refreshed my Window's DEV app and saw the old colorful unicorn and heart replaced by a some not-so-fancy icon. :( &lt;/p&gt;

&lt;p&gt;Anybody else with me?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>ux</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Why it's important to strengthen core HTML/CSS and Vanilla JS before frameworks.</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Mon, 15 Jun 2020 07:18:44 +0000</pubDate>
      <link>https://dev.to/salyadav/why-it-s-important-to-strengthen-core-html-css-and-vanilla-js-before-frameworks-13od</link>
      <guid>https://dev.to/salyadav/why-it-s-important-to-strengthen-core-html-css-and-vanilla-js-before-frameworks-13od</guid>
      <description>&lt;p&gt;If you have been following my posts, you would know that I am not a full-time blogger who has any fixed agenda or timeline for her content. I write what I experiment. And I write to document my learnings on-the-fly, hoping it will help others who seek similar goals! So, the next question arises...&lt;/p&gt;

&lt;p&gt;In this era of powerful frameworks, why am I spending time on vanilla JS, HTML and CSS. Long answer short-&lt;/p&gt;

&lt;p&gt;1- While frameworks were busy keeping us away from our roots, the HTML5+CSS3+Vanilla JS combo have grown so powerful that it's mind-blowing how much catching-up we have to do. &lt;/p&gt;

&lt;p&gt;2- While the frameworks raging through the industry change with a blink, these roots will be our only constants and our forever companions.&lt;/p&gt;

&lt;p&gt;3- God forbid, but if ever these open-source frameworks start licensing themselves, companies will start relying on their home-grown frameworks (already many "big fishes" like Facebook, Oracle... prefer this over third-party- they want their revenue to stay indoors). This is when people with strong fundamentals will prove to be an irreplaceable asset!&lt;/p&gt;

&lt;p&gt;4- Every framework has its own set of limitations (no one's perfect). And, if you want to design a webpage out of the imagination of these framework developers, you have to rely on your own strength to create from scratch!&lt;/p&gt;

&lt;p&gt;I absolutely realise how controversial all points presented above could be! And that's why it's open for a friendly discussion. &lt;/p&gt;

&lt;p&gt;Again, I am not against frameworks. I am just against all the newbies jumping directly into the frameworks without knowing their roots. They end up thinking that certain features are specific to their frameworks while all the time it's offered by Vanilla JS and the framework is only gift-wrap around it. I have been there, and I don't want the newcomers to be in the same delusion. &lt;/p&gt;

&lt;p&gt;Any thoughts?&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>healthydebate</category>
    </item>
    <item>
      <title>Deep Clone of JS Objects with circular dependency</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Tue, 09 Jun 2020 05:45:50 +0000</pubDate>
      <link>https://dev.to/salyadav/deep-clone-of-js-objects-with-circular-dependency-4if7</link>
      <guid>https://dev.to/salyadav/deep-clone-of-js-objects-with-circular-dependency-4if7</guid>
      <description>&lt;p&gt;Deep cloning JS objects has a plethora of blog posts and articles over the internet. But as I see, most or rather all of them end up with a solution of stringifying the JSON object and parsing them back. Somehow I am really unsettled with this solution. Is there any other way of deep clone a JS object?&lt;/p&gt;

&lt;p&gt;Let's take some scenarios to discuss:&lt;br&gt;
1- Not just one level of nested object, how about atleast 10?&lt;br&gt;
2- What if after a certain point, there is circular dependency in the objects. (Ever heard of the tortoise-hare algorithm in linked list?) How will be approach cloning such an object?&lt;/p&gt;

&lt;p&gt;P.S. I do not want to &lt;code&gt;JSON.parse(JSON.stringify(obj))&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Edit:
&lt;/h4&gt;

&lt;p&gt;Why I don't what to use JSON.stringify?&lt;br&gt;
I came across &lt;a href="https://medium.com/@pmzubar/why-json-parse-json-stringify-is-a-bad-practice-to-clone-an-object-in-javascript-b28ac5e36521"&gt;this article&lt;/a&gt; on Medium.&lt;br&gt;
And it was pretty convincing of certain loopholes in using &lt;code&gt;JSON.stringify&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I agree that this is the most effective way to convert an object, but I am in a quest for a non-work-around solution. We will deal with performance of cloning later. For now, a base solution!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>discuss</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Behind the scenes: From the moment you enter a URL</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Wed, 27 May 2020 17:43:35 +0000</pubDate>
      <link>https://dev.to/salyadav/behind-the-scenes-from-the-moment-you-enter-a-url-1img</link>
      <guid>https://dev.to/salyadav/behind-the-scenes-from-the-moment-you-enter-a-url-1img</guid>
      <description>&lt;p&gt;Recently in a job interview, I was asked- "What happens from the moment you enter a URL in the browser?". Although, I had an overall idea, I was quite unable to construct the entire flow loquaciously. This article is meant to give you (and me) a seamless flow chart of what happens from top to bottom until you see the very webpage. It covers both, the browser components and server side resolution of domains. &lt;br&gt;
So without further ado, let's dive in...&lt;/p&gt;

&lt;h2&gt;
  
  
  The overall picture
&lt;/h2&gt;

&lt;p&gt;Although, a webpage loads in a matter of seconds, there is quite a lot going on in the background. For simplicity, we will split them into three major flows:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Get the IP address of the server that your domain name refers to
&lt;/h4&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%2Fi%2F0x0qzu9xi1av6ct2dnmm.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%2Fi%2F0x0qzu9xi1av6ct2dnmm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Hit the server to fetch what is to be rendered on the UI
&lt;/h4&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%2Fi%2Fpx0cu6wb80fh107gv40p.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%2Fi%2Fpx0cu6wb80fh107gv40p.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Construct, paint, and render the page
&lt;/h4&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%2Fi%2Fs25rhzjnd32gt9rpk968.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%2Fi%2Fs25rhzjnd32gt9rpk968.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is it. It's as simple as three steps. However, the complex part kicks in when we dive deeper into the two black boxes- &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;IP address resolution.&lt;/li&gt;
&lt;li&gt;Constructing and rendering webpage.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you are backend developer, the first will be of prime concern to you, and for frontend folks it's the browser rendering that takes precedence. &lt;br&gt;
Anyways, let's look into both of them. &lt;/p&gt;

&lt;h2&gt;
  
  
  Domain name to IP Address using Domain Name Server(DNS)
&lt;/h2&gt;

&lt;p&gt;Although this article is not meant for heavy theory (there are plenty on the internet), I will give a small summary why this block is important. &lt;/p&gt;

&lt;p&gt;We as human beings don't retain long numbers. And machines don't understand our sophisticated language. As a win-win solution, we give &lt;strong&gt;names&lt;/strong&gt; (domain name) to our servers while they have their identity as IP Addresses (numbers). So how do we bridge the gap and communicate? &lt;strong&gt;Domain Name Server&lt;/strong&gt; acts as our mediator! &lt;br&gt;
This is what goes behind the curtains: &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%2Fi%2Fnmgeburj0q5o1vazjkxr.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%2Fi%2Fnmgeburj0q5o1vazjkxr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Sorry if image is too small on your device, I had to fit in a lot of content in one flow chart. I urge you to download it and analyse each component. They are quite self-explanatory.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How the user finally gets a fancy webpage
&lt;/h2&gt;

&lt;p&gt;Once we have resolved the IP Address of the server that has our relevant data (the webpage), all there is left is to actually hit it and fetch what we wanted. Most of the time, we get an HTML page in response, but we also have instances when it is a PDF, or other content-types like image, JSON, XML etc.&lt;/p&gt;

&lt;p&gt;In this section, we will see how the browser converts an HTML file (bunch of nodes, scripts, and stylings) into a full-fledged viewable page.&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%2Fi%2Fyn1m5ni71ykq992o6z2b.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%2Fi%2Fyn1m5ni71ykq992o6z2b.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is only an overview. But, if you want to dig deeper into how each and every browser component works, refer &lt;a href="https://www.html5rocks.com/en/tutorials/internals/howbrowserswork/#Introduction" rel="noopener noreferrer"&gt;here&lt;/a&gt;. This site is pretty much the Magnum Opus of how browser renders the HTML with embedded scripts and painted stylings. &lt;/p&gt;

&lt;p&gt;However, I would like to mention a couple of important points here-&lt;br&gt;
1- Your &lt;strong&gt;Browser Engine&lt;/strong&gt; holds your JS environment like v8(for chrome) that has call stack, memory heap, event loop, Web API.. yada yada.&lt;br&gt;
2- It's the &lt;strong&gt;Render Engine&lt;/strong&gt; that parses the HTML nodes into a DOM tree and then further into a painted(CSS applied) render tree to display. &lt;br&gt;
3- Everytime your HTML parser encounters &lt;strong&gt;script&lt;/strong&gt; tag, it &lt;strong&gt;PAUSES PARSING DOM elements&lt;/strong&gt; (IMPORTANT!!!) and synchronously downloads all scripts first.&lt;/p&gt;

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

&lt;p&gt;Again, the agenda of this article was to help you articulate a big picture into a consolidated 3 min answer if anybody ever asks you- &lt;em&gt;"What happens when you enter a URL?"&lt;/em&gt;. Of course, there is a lot to explore here, and there are brilliant sources online to do so. Mentioning some of them in the references below. &lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://medium.com/@maneesha.wijesinghe1/what-happens-when-you-type-an-url-in-the-browser-and-press-enter-bb0aa2449c1a" rel="noopener noreferrer"&gt;The Big Picture&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=mpQZVYPuDGU" rel="noopener noreferrer"&gt;How DNS works&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.html5rocks.com/en/tutorials/internals/howbrowserswork/#Introduction" rel="noopener noreferrer"&gt;Rendering HTML into the browser&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading. Hope this helps! 🦄🦄🦄&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Little Shenanigans of JavaScript - Max is Negative Infinity? WHAT!</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Fri, 15 May 2020 18:22:04 +0000</pubDate>
      <link>https://dev.to/salyadav/little-shenanigans-of-javascript-2-2dkb</link>
      <guid>https://dev.to/salyadav/little-shenanigans-of-javascript-2-2dkb</guid>
      <description>&lt;p&gt;JavaScript is like the father who over-pampered and spoilt his son, and now the son blames his Dad for things he can't handle. When I was first introduced to JS, I was said, and I quote - &lt;em&gt;"JavaScript is a very forgiving language."&lt;/em&gt; This epithet stuck with me forever. Indeed, JS lets you do stuff you cannot imagine with other popular languages (I do not speak for the ones I don't know). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oaykQXTz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7jzf2f3mjsl4ne6vf3mo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oaykQXTz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7jzf2f3mjsl4ne6vf3mo.jpg" alt="Alt Text" width="828" height="780"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But before blaming JS think for a second, WHY would you want to add a string and number? What's the meaning of subtracting two strings? Why would you want to assign a negative index in an array? (Wait, did I just say negative indices??👻 Boo! I will get to it.) Some of the answers are obvious to ardent JS users when they use it day-in and day-out. But for the rest of you out there, just don't do it until you see why! &lt;/p&gt;

&lt;p&gt;Jokes aside, I present to you few more such playful insanities of JS. I had a fun time fiddling with them, hope you too. Enough said, let's dive in.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Math.min() and Math.max() Upside-down
&lt;/h3&gt;

&lt;p&gt;Most of us use these two functions in day-to-day basis. It's easiest way to compare a set of N numbers. Let's see what happens when you call them without feeding any parameters. What do you think the output to the following be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;POSITIVE_INFINITY&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//false&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="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NEGATIVE_INFINITY&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//false&lt;/span&gt;
&lt;span class="c1"&gt;//Damn Son!!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First reaction: You kidding me!&lt;br&gt;
But let's go ahead and see what values are we comparing at first place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;()&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-Infinity&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although this feels very counterintuitive to have &lt;code&gt;min()&lt;/code&gt; as &lt;code&gt;+Infinity&lt;/code&gt; and &lt;code&gt;max()&lt;/code&gt; as negative, I find an explanation very convincing...&lt;/p&gt;

&lt;p&gt;Let's take &lt;code&gt;Math.min()&lt;/code&gt;. &lt;br&gt;
We use it to find the minimum number from a set. When you call it with no parameters, it initializes a number with &lt;em&gt;Maximum Possible Value&lt;/em&gt;. Then, when you compare your first finite number with this, it will always pick that number as minimum. &lt;br&gt;
Imagine if &lt;code&gt;Math.min()&lt;/code&gt; was -Infinity. No matter what you compare this with, it will always pick your -Infinity. The function would fail! &lt;/p&gt;

&lt;p&gt;When I have to write an algorithm to find a minimum of something. Say, a minimum Sum, or the smallest number in a dynamic array, or what-so-ever, I always initialize my comparison variable with &lt;code&gt;Math.min()&lt;/code&gt;, so when I revisit it, I instantly know what I am intending to do! That is, find the MINIMUM. &lt;/p&gt;

&lt;p&gt;Doesn't sound so counterintuitive now, does it? Simple logic- Want to find the minimum of a lot? Initialize using min!&lt;/p&gt;

&lt;p&gt;Needless to explain, the same logic goes with &lt;code&gt;Math.max()&lt;/code&gt;. To check if a number is maximum, we start comparing it with -Infinity and climb all the way up. &lt;/p&gt;

&lt;p&gt;Now that you know, you will find yourself using this so very often! Been there. Done that.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Identity Crisis of NaN (I am Not-A-Number!!)
&lt;/h3&gt;

&lt;p&gt;What do you think is the type-of NaN?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//"number"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one got me rolling on the floor. 🤣&lt;br&gt;
Literally, the full-form of NaN is &lt;strong&gt;Not a number&lt;/strong&gt;. And yet, here we are!&lt;/p&gt;

&lt;p&gt;Contrary to the popular belief, &lt;code&gt;NaN&lt;/code&gt; is not a JS Keyword. It is merely a property of the global object &lt;em&gt;Number&lt;/em&gt;, just like several others you would be aware of:&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//NaN&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NEGATIVE_INFINITY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//-Infinity&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MIN_VALUE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//5e-324&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MAX_VALUE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//1.7976931348623157e+308&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NaN signifies quite a variety of scenarios. To enumerate a few:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You did an operation not explained by Math. 
Example- 0/0, Infinity/Infinity, Infinity * 0&lt;/li&gt;
&lt;li&gt;You are trying to convert a non-numeric value to Number. 
Example- &lt;code&gt;Number('foo')&lt;/code&gt; or &lt;code&gt;+"foo"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You challenged the finites of &lt;a href="https://www.sciencedirect.com/topics/mathematics/computer-arithmetic"&gt;Computer Arithmetic&lt;/a&gt; and executed an operation far bigger than the bits could hold. Example- 
&lt;code&gt;(3.3489324884 * 10e616) / (3.3489324884 * 10e616); //NaN&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, NaN does not always signify Not-a-number, rather it's a way of saying- &lt;br&gt;
&lt;em&gt;"You are trying to get a number beyond the understanding of your machine."&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;having understood NaN now, what would be output for this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.454&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="nx"&gt;e500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.454&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="nx"&gt;e500&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;x&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;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;YES! Although x is NaN, the comparison throws &lt;strong&gt;False&lt;/strong&gt;. Could you try reasoning out why? &lt;br&gt;
This question reminds me of a famous John Green quote: &lt;br&gt;
&lt;em&gt;"Some infinities are bigger than other infinities."&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Anyways, if you are searching for an exact answer why this doesn't work and how else will you check for NaN, you will find the answer &lt;a href="https://javascriptrefined.io/nan-and-typeof-36cd6e2a4e43"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Shhh, spoiler:&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;isNaN&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="c1"&gt;//true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Who murdered my Array?
&lt;/h3&gt;

&lt;p&gt;Prepare to get Bonkers! I urge you to open your Developers Console even before you start scrolling, you are going to need it.&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;x&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&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;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="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="mi"&gt;0&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="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="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="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;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;x&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did JS just let me add negative indices to an array!!! 🔪🔪🔪&lt;/p&gt;

&lt;p&gt;Before I start explaining, let's see what the output looks like?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YoftM3cE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8vhzbtloxivwmjrf4a3n.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YoftM3cE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8vhzbtloxivwmjrf4a3n.JPG" alt="Alt Text" width="417" height="288"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vATSgoGM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2gigatyy5d6mbgauukna.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vATSgoGM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2gigatyy5d6mbgauukna.JPG" alt="Alt Text" width="533" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you notice, even after adding two more data to the array, &lt;code&gt;x.length&lt;/code&gt; remains as 5, although the array shows the newly added data. &lt;/p&gt;

&lt;p&gt;Let's get our funda clear- &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any non-primitive data type in JS is treated as an Object. Let alone Arrays, Sets, Maps, even Functions are Objects. And any object is a set of key-value of pairs. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To understand what's happening in our use case here, let's for a second, forget that &lt;strong&gt;x&lt;/strong&gt; is an Array, and treat it as an Object (which it is, since arrays are also objects). Now, let's try adding a function definition, or a new attribute to &lt;strong&gt;x&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;in x foo&lt;/span&gt;&lt;span class="dl"&gt;'&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James Dean&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;x&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;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you are trying this out on-hands with me. You will get an &lt;strong&gt;x&lt;/strong&gt; that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q7WTU62T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6whwyfwzso6rtwt2hm8f.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q7WTU62T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6whwyfwzso6rtwt2hm8f.JPG" alt="Alt Text" width="744" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Are you able to guess what's happening? The array length still remains as 5 but all other attributes are present just like in an Object. If you open up the &lt;code&gt;__proto__&lt;/code&gt; of &lt;strong&gt;x&lt;/strong&gt; you will see &lt;em&gt;Array&lt;/em&gt;, open it up one more hierarchy and you see &lt;em&gt;Object&lt;/em&gt;. This is &lt;strong&gt;Prototype Chaining&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Since, only whole number indices are supported by the Array data structure, the rest of the &lt;em&gt;keys&lt;/em&gt; are simply treated as key-value pairs of an Object.&lt;/p&gt;

&lt;p&gt;You can simply invoke &lt;em&gt;foo&lt;/em&gt; using &lt;code&gt;x.foo()&lt;/code&gt; or access the &lt;code&gt;name&lt;/code&gt; attribute using one of the two ways of accessing a JSON Object:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;x.name&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;&lt;code&gt;x["name"]&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  So what's with the negative indices?
&lt;/h4&gt;

&lt;p&gt;They are also just a key-value pair like &lt;code&gt;name&lt;/code&gt;. Where the object &lt;strong&gt;x&lt;/strong&gt; has &lt;br&gt;
&lt;code&gt;x["-1"]&lt;/code&gt; attribute value. It is only an illusion of negative index where as it's only a part of the &lt;strong&gt;x&lt;/strong&gt; object and not the Array. &lt;/p&gt;

&lt;p&gt;And there's your answer!!! 🦄&lt;/p&gt;

&lt;p&gt;But before I end, I want you to try a couple more things and see what &lt;strong&gt;x&lt;/strong&gt; looks like and what happens to the &lt;em&gt;length&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;90&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="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//NOTE: We skipped index 6 and 7 altogether.&lt;/span&gt;
&lt;span class="c1"&gt;//What happens to the length?&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;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//9&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;x&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you skip the whole number indices (the legit indices of an array), JS just creates EMPTY spaces- an array index holding &lt;em&gt;undefined&lt;/em&gt; data type. But the space gets reserved as a part of the array. And that's why the length increases to the maximum index. This is what it looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xBgWofax--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7fmrhzfcaaog7nidx7sr.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xBgWofax--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7fmrhzfcaaog7nidx7sr.JPG" alt="Alt Text" width="722" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AMAZING!!! Isn't it?&lt;/p&gt;

&lt;p&gt;I absolutely adore JavaScript for it's Willy Wonka ways with data types and data structures. I have laughed out loud at some problems, I have pulled my brains out at others. But nevertheless, I hope this language never dies. Frameworks may come and frameworks may go, I hope Vanilla JS goes on forever. &lt;/p&gt;

&lt;p&gt;I do hope you had fun trying out some examples from above. And I also hope, I atleast inspired a handful of you to open up your computers and get scripting! Do let me know if I went wrong somewhere or what you think about the article. Your response inspires me to write more! 🧐 &lt;/p&gt;

&lt;p&gt;Find the previous article of this series &lt;a href="https://dev.to/salyadav/little-shenanigans-of-javascript-4e0j"&gt;here&lt;/a&gt;.&lt;br&gt;
See you soon! 👋&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Little shenanigans of JavaScript</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Fri, 08 May 2020 17:33:38 +0000</pubDate>
      <link>https://dev.to/salyadav/little-shenanigans-of-javascript-4e0j</link>
      <guid>https://dev.to/salyadav/little-shenanigans-of-javascript-4e0j</guid>
      <description>&lt;p&gt;Even after working with JavaScript for over three years now, it never fails to surprise me with its little tricks and sly outputs. But these very secrets and surprises makes me fall in love with this language, all over again. Everytime!🤩&lt;/p&gt;

&lt;p&gt;Some people don't find these shenanigans hindering their practical work. But believe me, I avoided a production bug (which would have made my fancy looking UI go berserk) by serendipitously testing a sample dataset that unveiled yet another sly trick of JS arithmetic. &lt;/p&gt;

&lt;p&gt;And so, finally, I decided to compile a few such tricks here in this post. Hope you have fun discovering something new. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Adding float numbers
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&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="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;0.2&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="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.7&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="mf"&gt;0.2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the above code in your developer console and be like WTF!! How can you mess up something as simple as this JS??&lt;/p&gt;

&lt;p&gt;If you are too lazy to check for yourself, this is what the output looks 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="mf"&gt;0.30000000000000004&lt;/span&gt;
&lt;span class="mf"&gt;0.10000000000000003&lt;/span&gt;
&lt;span class="mf"&gt;0.7999999999999999&lt;/span&gt;
&lt;span class="mf"&gt;0.8999999999999999&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason behind this behavior is the accuracy with which JS stores float values.&lt;/p&gt;

&lt;p&gt;I was working on a report visualization project where I had to display the total bounced email percentage by adding up soft-bounce and hard-bounce. Since this was to be displayed in a compact UI, displaying 0.30000000000000004% instead of 0.3% would make my page go insane (much to the amusement of the user). Fortunately, I realised it as soon as I tested it and fixed it.&lt;/p&gt;

&lt;p&gt;So, how do we fix this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toFixed&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toFixed&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation: &lt;br&gt;
&lt;code&gt;toFixed(num)&lt;/code&gt; converts the &lt;code&gt;float&lt;/code&gt; to &lt;code&gt;string&lt;/code&gt; with precision of &lt;code&gt;num&lt;/code&gt; after decimal point. The unary operator converts the &lt;code&gt;string&lt;/code&gt; back to &lt;code&gt;number&lt;/code&gt;. If you are using this to display somewhere in your UI, you are pretty much done after 'toFixed'. If you are going to use it for further calculations, go ahead and convert it to &lt;code&gt;number&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But note: &lt;br&gt;
"0.30" in string when converted to number becomes 0.3. So, don't be surprised.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Silent conversions
&lt;/h3&gt;

&lt;p&gt;I don't see why someone would write a code like this, but let's assume you somehow landed with these set of numbers (say from an API call response) and are performing an operation as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="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="mi"&gt;022&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;018&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;023&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;019&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;010&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;008&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//true&lt;/span&gt;
&lt;span class="c1"&gt;//if i keep going on, you will soon see the pattern&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Common, don't be lazy and give this a shot in your console. The best way to learn JS is to befriend that F12 of your browser. &lt;br&gt;
Most of you would have guessed what's happening here. But let me lay it down for you either way...&lt;br&gt;
When JS sees 0 in the start of a number, it converts it to Octa. That explains 022, 023, 010 (you can experiment more). But wait! 8 and 9 are not valid numbers of the Octa Number System. Well, that's why they are converted to Decimal. &lt;br&gt;
And this explains 022 (18 in Decimal) even strictly matches with 018. &lt;/p&gt;
&lt;h3&gt;
  
  
  3. Let's get Quirk-ier
&lt;/h3&gt;

&lt;p&gt;There is no real-world example of this crazy expression, but I loved it so much that I decided to pop it in anyways. I came across this in &lt;a href="https://wtfjs.com/"&gt;WTF-JS&lt;/a&gt; (quite apt!).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;!+&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;+!&lt;/span&gt;&lt;span class="p"&gt;[]).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// 9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, the answer is 9! Whaaaaa 🤐&lt;br&gt;
There was no way I was going to give up without finding out why! After a gazillion unsatisfying hypothesis, I finally made one that I am quite sure of, and here it is...&lt;/p&gt;

&lt;p&gt;But before scrolling down for the solution, I urge you to try figuring out for yourself. Believe me, it's Merlin's Beard Satisfying!&lt;/p&gt;

&lt;p&gt;Explanation:&lt;br&gt;
First let's see what are we finding the length of, at first place?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!+&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;+!&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="c1"&gt;// Ans: "truefalse"&lt;/span&gt;
&lt;span class="c1"&gt;//HOLY CHRIST!!!!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How on earth did JS do this??&lt;br&gt;
Let's chop it down to three parts:&lt;br&gt;
(!+[]) + ([]) + (![])&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;!&lt;/code&gt; alone is really nothing in JS. God knows why it took me so long to figure out... And soooo, &lt;br&gt;
&lt;code&gt;+[]&lt;/code&gt; works as a unary operator on an empty object, which basically converts &lt;code&gt;[]&lt;/code&gt; into Number. &lt;br&gt;
Since &lt;code&gt;[]&lt;/code&gt; is an empty array, its numeric value is &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
So this is equivalent to &lt;code&gt;!0&lt;/code&gt; == &lt;code&gt;true&lt;/code&gt; (because Boolean of 0 is TRUE). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;[]&lt;/code&gt; is an empty array. Adding this with a boolean &lt;code&gt;true&lt;/code&gt; converts both of them to string. So the stringified value of &lt;code&gt;[]&lt;/code&gt; is an empty string &lt;code&gt;""&lt;/code&gt;. Hence, so far we have &lt;code&gt;"true"&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, the last one might be get a little tricky considering the first one in picture and if you are not aware of &lt;em&gt;falsy values&lt;/em&gt; in JS. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here, &lt;code&gt;![]&lt;/code&gt; is not the same as &lt;code&gt;!0&lt;/code&gt;.&lt;br&gt;
This time &lt;code&gt;[]&lt;/code&gt; is NOT converted to number, but directly applied to &lt;code&gt;Boolean(![])&lt;/code&gt; which is FALSE. &lt;br&gt;
There are only 8 falsy values in JS, find them &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So all-in-all, this expression is equivalent to:&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="c1"&gt;//"truefalse"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know here on. string + boolean = string. So the result is &lt;code&gt;"truefalse"&lt;/code&gt;. And the length of it is 9. &lt;/p&gt;

&lt;p&gt;DAMN! This felt GOOD! 🤸‍♂️&lt;/p&gt;

&lt;p&gt;I will end the list for now. But I have way too many such examples. If you liked it, let me know and I will make this into a series. Also, if you have such examples which got you like- Whaaaaaa 👀 , do share it in comment section. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Re-introducing JavaScript Objects using Object constructor</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Tue, 05 May 2020 14:06:05 +0000</pubDate>
      <link>https://dev.to/salyadav/re-introducing-javascript-objects-using-object-constructor-1bmk</link>
      <guid>https://dev.to/salyadav/re-introducing-javascript-objects-using-object-constructor-1bmk</guid>
      <description>&lt;p&gt;If you have been with me since the last two posts, first of all, thank you for coming back! And second, the contents of this post is a learning curve for me as well. So there are a few questions that I am still seeking answers to, see if you can help me out.&lt;/p&gt;

&lt;p&gt;Let's begin...&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/salyadav/re-introducing-javascript-objects-2-h63"&gt;previous post&lt;/a&gt;, we had discussed &lt;em&gt;Constructor Function&lt;/em&gt; way of creating objects and I had left you wondering about memory optimization using &lt;em&gt;Prototypes&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;But before we get into the vast concept of &lt;em&gt;prototypes&lt;/em&gt;, I want to discuss two more ways of creating objects-&lt;br&gt;
1- Using &lt;strong&gt;Object Constructor&lt;/strong&gt;&lt;br&gt;
2- Using &lt;strong&gt;Object.create(..)&lt;/strong&gt; function (This one has a little surprise in it.🎁)&lt;/p&gt;

&lt;p&gt;So without further ado, let's dive in:&lt;/p&gt;
&lt;h3&gt;
  
  
  Using Object Constructor
&lt;/h3&gt;

&lt;p&gt;JavaScript offers a class called &lt;em&gt;Object&lt;/em&gt;, and every object we create, no matter which way, is an instance of this class. And so, we can simply call this class constructor to create an instance of it-&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;person3&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;Object&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;Maria&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hola! Mi nombre es &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We basically pass the JSON into the &lt;em&gt;Object Constructor&lt;/em&gt; to get an object instance.&lt;/p&gt;

&lt;p&gt;If you working on-hands with the above examples (including the one in &lt;a href="https://dev.to/salyadav/re-introducing-javascript-objects-1-3m4h"&gt;#1&lt;/a&gt; using &lt;em&gt;Object Literals&lt;/em&gt;), you will be wondering WHAT IS THE DIFFERENCE? As far as Object properties and prototypes are concerned, there is absolutely NO difference. And if you are a beginner, let's have it this way.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note (NOT for beginners)- &lt;br&gt;
Well, after much Googling, I am still boggled as to what indeed is the difference between creating using Object Literal and Object Constructor! May be "nothing" is answer. May be not. I have my suspicions on memory usage. Since, an object is allocated space in HEAP instead of the CALL STACK (in case primitives), I am not really sure of how these two ways are different. &lt;strong&gt;If you have any idea, do comment below or DM me.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's see what our developer console shows for the two cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using &lt;strong&gt;Object Literal&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RA7YIdSp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jz1xd7btlj2rc5iuxhlm.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RA7YIdSp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jz1xd7btlj2rc5iuxhlm.JPG" alt="Alt Text" width="527" height="407"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using &lt;strong&gt;Object Constructor&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tDvb_5Dq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bf6j2j2c2odhfwcz32jf.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tDvb_5Dq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bf6j2j2c2odhfwcz32jf.JPG" alt="Alt Text" width="527" height="408"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Using Object.create function
&lt;/h3&gt;

&lt;p&gt;Before we move on to compare all the ways of creating objects and which way do we go about for different scenarios, we have one last way of creating an object, which I personally find the most exciting one! (Told you, there is a 🎁)...&lt;/p&gt;

&lt;p&gt;If you refer the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create"&gt;Mozilla developer docs&lt;/a&gt;, you will see a plethora of methods housed by this &lt;em&gt;Object Class&lt;/em&gt;. One of which is &lt;em&gt;.create(...)&lt;/em&gt;. Let's see how to use it. &lt;/p&gt;

&lt;p&gt;We created &lt;em&gt;person3&lt;/em&gt; in the above example. Here, we pass this object reference to create a new object.&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;person4&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;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//try calling the greeting function in person3&lt;/span&gt;
&lt;span class="c1"&gt;//to check if person4 actually got created. &lt;/span&gt;
&lt;span class="nx"&gt;person4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;//What output do you see?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above functioned well, did it not?&lt;br&gt;
Well, you will be surprised when you call &lt;code&gt;console.log(person4)&lt;/code&gt;. What do you see! &lt;br&gt;
An EMPTY OBJECT! What! 😨&lt;br&gt;
Again, try &lt;code&gt;person4.greeting()&lt;/code&gt;. Works fine! &lt;/p&gt;

&lt;p&gt;Then where on earth is this function getting called from?&lt;/p&gt;

&lt;p&gt;The secret lies in its &lt;strong&gt;prototype&lt;/strong&gt;. &lt;br&gt;
Now open &lt;code&gt;__proto__&lt;/code&gt;. What do you see?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--okRrBdPv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/50mzrddgm4z7dzg16mtu.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--okRrBdPv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/50mzrddgm4z7dzg16mtu.JPG" alt="Alt Text" width="342" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reason for this behavior is clearly mentioned in the mozilla docs I referred you to earlier. Let me lay it for you:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"What we pass is the object that should be the prototype of the newly-created object."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And so, the &lt;em&gt;person4&lt;/em&gt; is actually an empty object with a reference to &lt;em&gt;person3&lt;/em&gt; in its prototype. This leads us to the next important concept in JS- &lt;strong&gt;Prototype Chain and Inheritance&lt;/strong&gt;. Also, this queer behavior makes a great source of many JS Interview questions (which I will share towards the end).&lt;/p&gt;

&lt;p&gt;But before we move on, let's play around with this object a bit-&lt;/p&gt;

&lt;p&gt;Try these out:&lt;br&gt;
1- We know that &lt;em&gt;person4&lt;/em&gt; is an empty object. But try &lt;code&gt;person4.name&lt;/code&gt;. What do you see? Where is this value coming from?&lt;br&gt;
2- Now run &lt;code&gt;person4.name = "Maria Junior"&lt;/code&gt;. And see what the object holds using &lt;code&gt;console.log(person4)&lt;/code&gt;. &lt;br&gt;
3- Run &lt;code&gt;person4.greeting()&lt;/code&gt; and what do you see? Since this function was not in the object when we printed it earlier, where is it getting fetched from? And what is it printing?&lt;br&gt;
4- Now try &lt;code&gt;person4.__proto__.greeting()&lt;/code&gt;. Can you explain the result?&lt;/p&gt;
&lt;h3&gt;
  
  
  Try this actual interview question!
&lt;/h3&gt;

&lt;p&gt;What will be the output? (Answer without running the code)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;muffin_order_1&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;Object&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;muffin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;quantity&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="na"&gt;delivery&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Days to deliver: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;quantity&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="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;muffin_order_1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;delivery&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;muffin_order_2&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;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;muffin_order_1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;muffin_order_2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;muffin_order_2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;delivery&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By now you should have an abstract idea about what are prototypes and what are we aiming for with them. Nevertheless, I will formalize them for you in the future posts. Meanwhile...&lt;/p&gt;

&lt;p&gt;You have made it so far! You deserve a 🍰. Go ahead, take a break! &lt;/p&gt;

&lt;p&gt;Reference: &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
      <category>beginners</category>
      <category>explainlikeimfive</category>
    </item>
    <item>
      <title>Re-introducing JavaScript Objects using Constructor Function</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Mon, 04 May 2020 12:34:07 +0000</pubDate>
      <link>https://dev.to/salyadav/re-introducing-javascript-objects-2-h63</link>
      <guid>https://dev.to/salyadav/re-introducing-javascript-objects-2-h63</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/salyadav/re-introducing-javascript-objects-1-3m4h"&gt;Previously&lt;/a&gt; we had seen JavaScript's Object Literal method of creating an object. In this post, we will explore how to create an object using the &lt;strong&gt;Constructor Function&lt;/strong&gt;. Yes, that's right. You can create an object using a function! &lt;/p&gt;

&lt;p&gt;For those of you familiar with Classes and instantiating an object from a class, this concept is very similar. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Then why don't we just create a class??&lt;/em&gt;&lt;br&gt;
Surprisingly, classes were introduced in JS only with ES6 (ECMAScript2015), which is quite recent. Sigh! Until then, Constructor Function was our savior. (Don't worry if you don't know what I am talking about.)&lt;/p&gt;

&lt;p&gt;Having said that, let's dive in:&lt;/p&gt;

&lt;p&gt;We will use the same object format we used in &lt;a href="https://dev.to/salyadav/re-introducing-javascript-objects-1-3m4h"&gt;#1 post&lt;/a&gt; for our example. Here it is,&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;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;thename&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;thename&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;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hola! Mi nombre es &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;PS. I have dropped the age and country attributes just to keep it simple.&lt;/p&gt;

&lt;p&gt;This creates a function "object" with a namespace &lt;em&gt;Person&lt;/em&gt;, within which there are two attributes called &lt;em&gt;name (of string type)&lt;/em&gt; and &lt;em&gt;greeting (of function type)&lt;/em&gt;. &lt;br&gt;
Internally, JS converts this code into an &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE" rel="noopener noreferrer"&gt;Immediately-Invoked Function Expression&lt;/a&gt; and allocates a variable &lt;em&gt;Person&lt;/em&gt; of type - function object. We will learn about IIFE later, let's keep it simple for now.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note-&lt;/strong&gt; The CAPITAL 'P' used while declaring 'Person' is nothing but a common naming convention for anything that resembles a Class. If you are using any modern code editor like VSCode with ES6 JS version, you will notice a warning like:&lt;br&gt;
&lt;em&gt;"This constructor function may be converted to a class declaration."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Remember, we haven't created an object yet. We have only defined what the object should look like. Now, to create an object, simply call the function like a constructor:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;person1 = new Person('Sanchez');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This creates a new object called &lt;code&gt;person1&lt;/code&gt;. Go ahead and print &lt;em&gt;person1&lt;/em&gt; in your console using &lt;code&gt;console.log(person1)&lt;/code&gt; and dissect it. Open it until you have reached trivia. You will see something like this (and a lot more): &lt;/p&gt;

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

&lt;span class="cm"&gt;/**
 * {
 *  name: 'Sanchez',
 *  greeting: f (), --&amp;gt; no name to the function - anonymous function
 *  __proto__: {
 *      constructor: f Person(thename), --&amp;gt; name of the function "Person"
 *      __proto__: Object
 *  }
 * }
 */&lt;/span&gt;


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

&lt;/div&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%2Fi%2F097n927duuvmq0o4if71.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%2Fi%2F097n927duuvmq0o4if71.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is actually happening here?
&lt;/h3&gt;

&lt;p&gt;We can break it down to three steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;new&lt;/strong&gt; creates a new empty object and assigns it to &lt;strong&gt;this&lt;/strong&gt; -&amp;gt; 
&lt;code&gt;this = {}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The function then runs and adds new key-value pairs to &lt;strong&gt;this&lt;/strong&gt; object -&amp;gt; 
&lt;code&gt;this.name = thename&lt;/code&gt; and  &lt;code&gt;this.greeting = function() {...}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finally, the value of &lt;strong&gt;this&lt;/strong&gt; is returned and assigned to person1 -&amp;gt;
&lt;code&gt;person1 = function () {...  return this; }&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note-&lt;/strong&gt; In this case, your constructor function should NOT have any return statement. JS won't stop you from adding one. If you do, that return statement will be executed and the corresponding object will be returned instead of &lt;strong&gt;this&lt;/strong&gt; and the instantiation will work differently than you anticipate. So be careful!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But what is this ugly wormy thing that creeped into your object called &lt;code&gt;__proto__&lt;/code&gt;!! This is JavaScript's &lt;em&gt;Object Prototypes&lt;/em&gt;. It's an amazing and equally important concept for us to master Objects in JS. We will get to them soon!&lt;br&gt;
For now, stash them in your memory palace.&lt;/p&gt;

&lt;h3&gt;
  
  
  When do we use Constructor Functions?
&lt;/h3&gt;

&lt;p&gt;Take a scenario where you just know you are going to have Objects of 'Person' type, but don't know HOW MANY. Here, it is simply convenient to call &lt;code&gt;var newguy = new Person('His Name')&lt;/code&gt; everytime you encounter a need for new object, instead of creating them using the verbose method of &lt;em&gt;Object Literal&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;But this can't be all so perfect, can it? &lt;/p&gt;

&lt;p&gt;Think about this...&lt;br&gt;
Everytime you create a new object from Person, a new variable is getting assigned in the memory with an object reference. And EVERY object contains a function called &lt;em&gt;greeting&lt;/em&gt; which is essentially doing the same thing in all the objects. If you have 10 such object, 50... 100... How many times do we define the same function for every object!! &lt;/p&gt;

&lt;p&gt;We have solution to this extravagant waste of space. 🎶🎵 #SuspenseMusic ...&lt;br&gt;
&lt;strong&gt;Prototypes&lt;/strong&gt;. &lt;br&gt;
Whaaaaaa.... 😱 &lt;br&gt;
Anyways, let's not overload ourselves. If you have made it this far, treat yourself with some 🍦. Take a break. Then move on to &lt;a href="https://dev.to/salyadav/re-introducing-javascript-objects-using-object-constructor-1bmk"&gt;#3&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: Before you move on to the next chapter. I would suggest you play around with this a bit. Create new functions. Instantiate new objects. Open them up. Open what's openable inside of them. Keep going. I promise, it's fun! Comment here some interesting things you see.&lt;/p&gt;

&lt;p&gt;Also, I will soon add some JS Interview kind of questions to help you grasp the usage of these concepts soon. Hang in there!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Reference: &lt;br&gt;
&lt;a href="https://javascript.info/constructor-new#constructor-function" rel="noopener noreferrer"&gt;https://javascript.info/constructor-new#constructor-function&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
      <category>beginners</category>
      <category>explainlikeimfive</category>
    </item>
    <item>
      <title>Re-introducing JavaScript Objects using Object Literals</title>
      <dc:creator>Saloni Yadav</dc:creator>
      <pubDate>Mon, 04 May 2020 08:02:59 +0000</pubDate>
      <link>https://dev.to/salyadav/re-introducing-javascript-objects-1-3m4h</link>
      <guid>https://dev.to/salyadav/re-introducing-javascript-objects-1-3m4h</guid>
      <description>&lt;p&gt;The number of ways one can create Objects in JavaScript can be quite overwhelming. JavaScript is a unique language where almost EVERYTHING is an Object. It is not an exaggeration when I say, even Functions in JavaScript are Objects. This article is a detailed re-introduction to Objects, to refresh one's basics if already familiar, or introduce the newbies to Object Oriented Programming in JS.&lt;/p&gt;

&lt;p&gt;Having said that, let's dive in:&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an object?
&lt;/h3&gt;

&lt;p&gt;Simply put, it's a group of anything! Usually, a group of some related data and functionalities put together make an object. There is nothing more or less to it. So I will not get into the bookish definitions.&lt;/p&gt;

&lt;p&gt;For example, details about a person can be represented as an object: &lt;br&gt;
Name&lt;br&gt;
Age&lt;br&gt;
Country&lt;/p&gt;

&lt;p&gt;In JS we have a particular way of representing this object- Curly Braces with key-value pairs! It's nothing but JavaScript's Object Notation or JSON. (Oh! I have heard this!)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person0&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;Juan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mexico&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;And there you have your first object. 😊&lt;br&gt;
But remember I told you, some data and functions. So let's add a 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;var&lt;/span&gt; &lt;span class="nx"&gt;person0&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;Juan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mexico&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hola! Mi nombre es Juan&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eazy-Peezy!&lt;/p&gt;

&lt;p&gt;Wondering why are you able to add a function inside an object? Well, remember even Functions in JS is an Object! So this is simply a nested object. An object inside an object. &lt;/p&gt;

&lt;p&gt;What happens when you call &lt;code&gt;person0.greeting()&lt;/code&gt;? You guessed it right, &lt;code&gt;"Hola! Mi nombre es Juan"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This way of declaring an object in JS is called &lt;strong&gt;Object Literal&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But, what happens if you change &lt;em&gt;person0&lt;/em&gt;'s name?&lt;br&gt;
i.e. &lt;code&gt;person0.name = 'Maria';&lt;/code&gt;&lt;br&gt;
Then, calling &lt;code&gt;person0.greeting()&lt;/code&gt; still prints &lt;code&gt;"Hola! Mi nombre es Juan"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's fix this. Instead of hard-coding "Juan", we simply fetch the current 'name' in the object-&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;person0&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;Juan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mexico&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hola! Mi nombre es &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;this&lt;/code&gt; holds the current scope where the function is called, which in our case in &lt;em&gt;person0&lt;/em&gt;. Now, if you change &lt;em&gt;person0&lt;/em&gt;'s name in future, the &lt;code&gt;greeting()&lt;/code&gt; function will update its output accordingly.&lt;/p&gt;

&lt;p&gt;Unfortunately, the concept of scope is 'out of scope' of this article. Pun Intended! 😂 I promise to cover it later.&lt;/p&gt;

&lt;p&gt;You have done well 🤗. Take a break. Digest this. Then move on to &lt;a href="https://dev.to/salyadav/re-introducing-javascript-objects-2-h63"&gt;#2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Reference: &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics"&gt;https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
      <category>beginners</category>
      <category>explainlikeimfive</category>
    </item>
  </channel>
</rss>
