<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shivaansh Agarwal</title>
    <description>The latest articles on Forem by Shivaansh Agarwal (@shiv1998).</description>
    <link>https://forem.com/shiv1998</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%2F242381%2Ffc94b4b6-fe43-4c17-9ced-de03558f628e.jpeg</url>
      <title>Forem: Shivaansh Agarwal</title>
      <link>https://forem.com/shiv1998</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shiv1998"/>
    <language>en</language>
    <item>
      <title>Use of package-lock.json</title>
      <dc:creator>Shivaansh Agarwal</dc:creator>
      <pubDate>Sat, 07 Jan 2023 17:40:03 +0000</pubDate>
      <link>https://forem.com/shiv1998/use-of-package-lockjson-1mn8</link>
      <guid>https://forem.com/shiv1998/use-of-package-lockjson-1mn8</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Let's create a new directory and initialize it with &lt;code&gt;npm init -y&lt;/code&gt;.

&lt;ol&gt;
&lt;li&gt;This will create a new &lt;code&gt;package.json&lt;/code&gt; file.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;Let’s say we’re in 2021 and we want to install moment.js in our project.&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;That time we would’ve done, &lt;code&gt;npm i moment&lt;/code&gt;, but to replicate the same let’s install the latest version of this package at that time which was 2.29.0 (Currently latest is 2.29.4). So the command for that will be &lt;code&gt;npm i moment@2.29.0&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This will add a new key for moment inside package.json’s dependencies key, with the value as the version number ^2.29.0.
It’ll also create &lt;code&gt;package-lock.json&lt;/code&gt; and node_modules folder. Inside node_modules folder we can see that the moment library has been added with the same version number, i.e. 2.29.0 &amp;amp; same is in the package-lock.json&lt;/li&gt;
&lt;/ol&gt;

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


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Push code to Github along with both &lt;code&gt;package.json&lt;/code&gt; &amp;amp; &lt;code&gt;package-lock.json&lt;/code&gt; (or simply delete the node_modules folder) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Take a clone of this repo (or simply delete the node_modules folder), and in that run &lt;code&gt;npm i&lt;/code&gt;. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now since the package-lock file is present it’ll simply install the version of moment that is mentioned in that file which is 2.29.0 inside node_modules. So both package.json &amp;amp; package-lock.json will be on the same package as in the screenshot shown above.&lt;/li&gt;
&lt;li&gt;This also means that the developer who commited the code, and someone who clones the project let’s say today, when the latest version of moment is 2.29.4, will also install the version 2.29.4 only because of package-lock.json &lt;/li&gt;
&lt;/ol&gt;


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

&lt;p&gt;If we had &lt;strong&gt;NOT commited&lt;/strong&gt; &lt;code&gt;package-lock.json&lt;/code&gt; file, but only &lt;code&gt;package.json&lt;/code&gt;, when &lt;code&gt;npm i&lt;/code&gt; was done, it would have found ^2.29.0 in package.json, but then it would’ve went to npm repository to find if there’s any new minor or patch release after 2.29.0, and since current latest version is 2.29.4, in node_modules this latest version will be installed and also in the newly created package-lock.json file, this version will be present.&lt;/p&gt;

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


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

</description>
      <category>node</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>backend</category>
    </item>
    <item>
      <title>MakeMyTrip Frontend Machine Coding Interview</title>
      <dc:creator>Shivaansh Agarwal</dc:creator>
      <pubDate>Sun, 03 Jul 2022 09:40:42 +0000</pubDate>
      <link>https://forem.com/shiv1998/makemytrip-frontend-machine-coding-interview-c7b</link>
      <guid>https://forem.com/shiv1998/makemytrip-frontend-machine-coding-interview-c7b</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Problem Statement&lt;/li&gt;
&lt;li&gt;Solution

&lt;ul&gt;
&lt;li&gt;Noting down the requirements&lt;/li&gt;
&lt;li&gt;Describing the components to be used&lt;/li&gt;
&lt;li&gt;Implementation

&lt;ol&gt;
&lt;li&gt;Start by creating the UI&lt;/li&gt;
&lt;li&gt;Describe the config to be passed&lt;/li&gt;
&lt;li&gt;Using config create the state for each box.&lt;/li&gt;
&lt;li&gt;Create a working UI by linking all 3 previous steps.&lt;/li&gt;
&lt;li&gt;Add logic for color change on button click.&lt;/li&gt;
&lt;li&gt;Add logic to store clickOrder in which buttons were clicked.&lt;/li&gt;
&lt;li&gt;Finally add, logic to reset the color of each box to white, once all boxes have been clicked and their color is green.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

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

&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Problem Statement
&lt;/h2&gt;

&lt;p&gt;Create a UI and functionality that looks exactly like shown in this &lt;a href="https://7m951t.csb.app/" rel="noopener noreferrer"&gt;Link&lt;/a&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hrlxods1ad2qfnc0d8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hrlxods1ad2qfnc0d8b.png" alt="Problem Screenshot"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Solution
&lt;/h2&gt;
&lt;h3&gt;
  
  
  i. Noting Down the Requirements:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The UI should look 'exactly' like shown in the image above with 7 boxes shaped in C shape.&lt;/li&gt;
&lt;li&gt;On click of the box, its color changes to green.&lt;/li&gt;
&lt;li&gt;When all boxes are green, they should again go back to previous color white, one by one, by 1 second delay, in the same order as they were first clicked.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  ii. Describing the components to be used
&lt;/h3&gt;

&lt;p&gt;For this I can break the UI into 3 components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;BoxesContainer&lt;/li&gt;
&lt;li&gt;BoxesRow&lt;/li&gt;
&lt;li&gt;Box&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  iii. Describing the implementation details
&lt;/h3&gt;
&lt;h4&gt;
  
  
  STEP-1
&lt;/h4&gt;

&lt;p&gt;First I'll start by creating the UI. Create different files for all 3 components described above and write CSS for those.&lt;/p&gt;
&lt;h4&gt;
  
  
  STEP-2
&lt;/h4&gt;

&lt;p&gt;In order to create the UI, I'll pass a config in the following format to the BoxesContainer.&lt;br&gt;
    ```javascript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const config = [
  [true, true, true],
  [true, false, false],
  [true, true, true],
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The config is an array of array, with each nested array depicting the row, and each boolean value depicting whether the box is present at that location or not. 
&lt;h4&gt;
  
  
  STEP-3
&lt;/h4&gt;

&lt;p&gt;Based on this config, I'll create a global state, which contains state for each box, which has an &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;isClicked&lt;/code&gt; attribute. The state will look like this.&lt;br&gt;
    ```javascript&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const state = [
  [
    { id: "1", isClicked: false },
    { id: "2", isClicked: false },
    { id: "3", isClicked: false },
  ],
  [{ id: "4", isClicked: false }, null, null],
  [
    { id: "5", isClicked: false },
    { id: "6", isClicked: false },
    { id: "7", isClicked: false },
  ],
];
&lt;/code&gt;&lt;/pre&gt;

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

&lt;/div&gt;
&lt;h4&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  STEP-4&lt;br&gt;
&lt;/h4&gt;

&lt;p&gt;At this stage our code looks like as shown in the document below:&lt;br&gt;&lt;br&gt;
We've created 3 components BoxesContainer, BoxesRow, Box, and also written the CSS for that. And with the config we got as the input, we created the boxesState.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bxjk2n4raujfid8ax47.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bxjk2n4raujfid8ax47.png" alt="Code Snapshot 1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxz5v6y2qjg0ogm1i6od.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxz5v6y2qjg0ogm1i6od.png" alt="Code Snapshot 2"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2gmcaer5omg5o5tewfky.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2gmcaer5omg5o5tewfky.png" alt="Code Snapshot 3"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7kg6hg0b88g87y5je5af.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7kg6hg0b88g87y5je5af.png" alt="Code Snapshot 4"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8ko0hiooft6xqj3tmko.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8ko0hiooft6xqj3tmko.png" alt="Code Snapshot 5"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://codesandbox.io/s/mmt-question-blog-1-r8oisi" rel="noopener noreferrer"&gt;CodeSandbox Link 1&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  STEP-5
&lt;/h4&gt;

&lt;p&gt;Now since our UI is ready, let's add the logic, of color change on box click.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fikabs1kn2qsrqpbcit2n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fikabs1kn2qsrqpbcit2n.png" alt="Code Snapshot 6"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnh05b7w76q4qg6s5obep.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnh05b7w76q4qg6s5obep.png" alt="Code Snapshot 7"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzeloqjm4jhig16ftp669.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzeloqjm4jhig16ftp669.png" alt="Code Snapshot 8"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://codesandbox.io/s/mmt-question-blog-2-75rylh" rel="noopener noreferrer"&gt;CodeSandbox Link 2&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  STEP-6
&lt;/h4&gt;

&lt;p&gt;We also need to maintain the clickOrder of different boxes. This will be used when we need to reset the box color one by one, when when all boxes color has been changed to green.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F054k9jeeksn5yzssyklv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F054k9jeeksn5yzssyklv.png" alt="Code Snapshot 9"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://codesandbox.io/s/mmt-question-blog-3-31575s" rel="noopener noreferrer"&gt;CodeSandbox Link 3&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  STEP-7
&lt;/h4&gt;

&lt;p&gt;Now finally, we need to implement the logic, where once all the boxes are green, it should reset all boxes to white, in the same order in which they were clicked, one by one, by 1 second delay.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fricueu1v2d3rczadlrrz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fricueu1v2d3rczadlrrz.png" alt="Code Snapshot 10"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://codesandbox.io/s/mmt-question-blog-4-pgfzgg" rel="noopener noreferrer"&gt;CodeSandbox Link 4&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Learnings
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Although I was able to give a working solution to this problem within around 45-50 mins, my code lacked scalability and reusability.&lt;/li&gt;
&lt;li&gt;So initially, when I heard from the interviewer, that I need to create the exact same UI, I simply created divs for every box. I didn't think about the config. So even if the requirement is small, we should plan to write our code in such a way that later, it should be reusable and as configurable as possible.&lt;/li&gt;
&lt;li&gt;During the interview, I had to change my state logic several times. Initially I was unable to come up with what the final state of the app will look like, and which state will be maintained at the child level and which will be maintained at parent level. So, this wasted some precious minutes in the interview.&lt;/li&gt;
&lt;li&gt;I was finally able to come up with this code solution after 3 attempts. In this code too, there are some places where DRY principles can be used to improve the code quality.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>interview</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>What are CSS-Variables?</title>
      <dc:creator>Shivaansh Agarwal</dc:creator>
      <pubDate>Sat, 26 Jun 2021 06:59:28 +0000</pubDate>
      <link>https://forem.com/shiv1998/what-are-css-variables-3bc8</link>
      <guid>https://forem.com/shiv1998/what-are-css-variables-3bc8</guid>
      <description>&lt;h2&gt;
  
  
  Overview and History
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Initially CSS lacked support for native variables, which led developers turn to CSS Preprocessors like SAAS which solved this problem through the use of preprocessor variables.&lt;/li&gt;
&lt;li&gt;CSS Variables are used just like any other variable in a different language, i.e. to assign a certain value to then and reference that value later. &lt;em&gt;(The value is not actually stored )&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Also called Custom Properties or Cascading Variables. &lt;em&gt;(So, we may use them interchangeably in this blog post.)&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to Define and Use CSS Variables, &amp;amp; what are Scopes for CSS Variables?
&lt;/h2&gt;

&lt;p&gt;In Javascript we usually declare and intialize a variable in one of the following ways:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in CSS, we don't use any such keyword like &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, &lt;code&gt;var&lt;/code&gt;; instead we just use 2 hyphens (dashes --) before the user-defined variable name and put them in a scope.&lt;br&gt;
Yes &lt;code&gt;scope&lt;/code&gt;, just like in Javascript we've scopes like &lt;strong&gt;function scope&lt;/strong&gt;, &lt;strong&gt;block scope&lt;/strong&gt;, &lt;strong&gt;global scope&lt;/strong&gt; we also have scopes in CSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--main-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&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;Here we've used the &lt;code&gt;:root&lt;/code&gt; selector. It targets the top element of the DOM and hence has a global scope. Similarly we can have other local scopes based on different CSS Selectors.&lt;br&gt;&lt;br&gt;
But then the question for specificity arises, like suppose we've the same CSS-property defined at the global level as well as the local level, then which value will be preferred?&lt;br&gt;&lt;br&gt;
For that consider the following code snippet.&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/shiv-1998/embed/qBmWdXE?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
Here we can notice a lot of things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to access a CSS- Variable.&lt;/li&gt;
&lt;li&gt;Local variables have higher precedence over global ones.&lt;/li&gt;
&lt;li&gt;We can use the property and declare it later, and CSS will still consider it, like in case of &lt;code&gt;div&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Accessing CSS Variables using &lt;code&gt;var()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;As seen above, we can access the CSS Variable defined above using the &lt;code&gt;var()&lt;/code&gt; function.&lt;br&gt;
&lt;code&gt;var()&lt;/code&gt; function takes the property name as argument, retrieves it's value from the stylesheet if it's present and replaces it with the actual CSS-Property.&lt;br&gt;
Hence unlike other languages variables in CSS, don't store values, they just replace it with the actual CSS value when used.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var()&lt;/code&gt; can also be used to provide a fallback value in case the property is missing.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--main-font-size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;16px&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;h2&gt;
  
  
  Using CSS Variables inside Javascript
&lt;/h2&gt;

&lt;p&gt;Consider the following Codepen for understanding how to access and change CSS-Variable in Javascript.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To get the value of a custom property at runtime, use the &lt;code&gt;getPropertyValue()&lt;/code&gt; method of the computed CSSStyleDeclaration object.&lt;/li&gt;
&lt;li&gt;To set the value of custom property at runtime, use the &lt;code&gt;setProperty()&lt;/code&gt; method of the CSSStyleDeclaration object.
&lt;iframe height="600" src="https://codepen.io/shiv-1998/embed/dyWbYYR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Benefits of using CSS Variables in your code:
&lt;/h2&gt;

&lt;p&gt;Consider the following CSS Code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--main-font-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#E9E9E9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--main-bg-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--main-font-color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--main-bg-color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;span&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#E9E9E9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, having looked at both the ways of using CSS i.e. with and without the use of CSS Variables, let's see what benefits we get using CSS-Variables over CSS without it.&lt;br&gt;
&lt;strong&gt;1. Ease of maintainability:&lt;/strong&gt;&lt;br&gt;
Let's say you've put in hours of work to select appropriate styles and colors and all of a sudden the requirement for that styling changes. You'll have to go through the entire project to change the colors and styling, but suppose if you've used CSS variables, you could've changed the value of the CSS property once and it would reflect at all places.&lt;br&gt;
&lt;strong&gt;2. Code becomes more readable:&lt;/strong&gt;&lt;br&gt;
As we can see above, the variables are user-defined so they can contain semantic meaning, and hence using these variables makes more sense as it is becomes much easier to understand and maintiain.&lt;/p&gt;

&lt;h2&gt;
  
  
  How are native CSS variables different from Preprocessor variables like SAAS?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;One of the main difference is Native CSS Variables can be modified at runtime dynamically.
&lt;/li&gt;
&lt;li&gt;Define CSS-Properties inside &lt;code&gt;media-queries&lt;/code&gt;.
&lt;em&gt;For other differences please look at the following section for an amazing article on CSS-Tricks&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Further Reading:
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;S.No.&lt;/th&gt;
&lt;th&gt;Link&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;a href="https://developers.google.com/web/updates/2016/02/css-variables-why-should-you-care"&gt;CSS Variables: Why Should You Care? [Google Developers]&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;a href="https://css-tricks.com/difference-between-types-of-css-variables/"&gt;What is the difference between CSS variables and preprocessor variables? [CSS-Tricks]&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties"&gt;Using CSS custom properties (variables) [MDN]&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>cssvariables</category>
      <category>cssproperties</category>
    </item>
    <item>
      <title>Understanding for...in vs for...of statements in Javascript</title>
      <dc:creator>Shivaansh Agarwal</dc:creator>
      <pubDate>Tue, 22 Jun 2021 04:54:08 +0000</pubDate>
      <link>https://forem.com/shiv1998/understanding-for-in-vs-for-of-statements-in-javascript-4dc3</link>
      <guid>https://forem.com/shiv1998/understanding-for-in-vs-for-of-statements-in-javascript-4dc3</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Differences

&lt;ol&gt;
&lt;li&gt;Definition&lt;/li&gt;
&lt;li&gt;Syntax&lt;/li&gt;
&lt;li&gt;When to use for...in or for...of?&lt;/li&gt;
&lt;li&gt;Examples&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Further Reading&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Differences
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Definition
&lt;/h3&gt;

&lt;h4&gt;
  
  
  for...in:
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;for...in&lt;/code&gt; loop statement iterates over the enumerable properties of an object including the inherited enumerable properties in an arbitrary order except the keys that are of type &lt;code&gt;Symbol&lt;/code&gt;.&lt;br&gt;
&lt;em&gt;&lt;code&gt;Enumerable properties&lt;/code&gt; are those properties whose internal &lt;code&gt;enumerable&lt;/code&gt; flag is set to true.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;The following properties a,b,c are enumerable by default:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;a&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="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The property defined using Object.defineProperty by default are not enumerable:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&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;defineProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&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="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  for...of:
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;for...of&lt;/code&gt; loop statement iterates over the values that the iterable object defines to be iterated over like &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;. etc.&lt;br&gt;
&lt;em&gt;Some built in types such as &lt;code&gt;Array&lt;/code&gt; and &lt;code&gt;Map&lt;/code&gt; have a default iteration behaviour while &lt;code&gt;Object&lt;/code&gt; does not.&lt;/em&gt;&lt;/p&gt;

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

&lt;h4&gt;
  
  
  for...in:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;statement&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  for...of:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;statement&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. When to use for...in or for...of?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  for...in:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;for...in&lt;/code&gt; can used in scenarios where you want to iterate over properties of an Object to check the keys and their corresponding values.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;for...in&lt;/code&gt; loop statement iterates over enumerable properties of an object in an &lt;strong&gt;arbitrary&lt;/strong&gt; order it is not recommended to use &lt;code&gt;for..in&lt;/code&gt; with an Array where index order is important. It is because there is no guarantee that &lt;code&gt;for...in&lt;/code&gt; will return indexes in any particular order.&lt;/li&gt;
&lt;li&gt;If you want to loop over values of an array consider using &lt;code&gt;for&lt;/code&gt; with numeric index, &lt;code&gt;Array.prototype.forEach&lt;/code&gt;, &lt;code&gt;for...of&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  for...of:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;To be used to iterate over iterable objects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for...of&lt;/code&gt; can be iterated over &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;TypedArray&lt;/code&gt;, &lt;code&gt;Map&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt;, arguments object of a function, &lt;code&gt;DOM Collection&lt;/code&gt;, &lt;code&gt;generators&lt;/code&gt;, other iterable objects.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KvsvW0wu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kxzsh6yk1ffpgjki9td.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KvsvW0wu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kxzsh6yk1ffpgjki9td.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VVz-kOcn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdu4pijs1lmyzzq1vxtt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VVz-kOcn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdu4pijs1lmyzzq1vxtt.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2B2S_WB5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mn0adglb15mu3cacdfm9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2B2S_WB5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mn0adglb15mu3cacdfm9.png" alt="image"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading:
&lt;/h2&gt;

&lt;p&gt;Following are the list of resources for further deep exploration: &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;S.No&lt;/th&gt;
&lt;th&gt;Link&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in"&gt;for...in [MDN Docs]&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of"&gt;for...of [MDN Docs]&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;a href="https://stackoverflow.com/questions/29285897/what-is-the-difference-between-for-in-and-for-of-statements"&gt;What is the difference between ( for… in ) and ( for… of ) statements? [Stackoverflow]&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
    </item>
    <item>
      <title>Why not to use index as key in React Lists (Practical Example)</title>
      <dc:creator>Shivaansh Agarwal</dc:creator>
      <pubDate>Sun, 06 Jun 2021 08:18:55 +0000</pubDate>
      <link>https://forem.com/shiv1998/why-not-to-use-index-as-key-in-react-lists-practical-example-3e66</link>
      <guid>https://forem.com/shiv1998/why-not-to-use-index-as-key-in-react-lists-practical-example-3e66</guid>
      <description>&lt;p&gt;We all have heard that using index as key in a react list is an anti-pattern and should be avoided.&lt;br&gt;&lt;br&gt;
The answer to this lies in the concepts of:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;React Virtual DOM&lt;/strong&gt;: It's a lightweight representation of actual DOM, stored in memory and is never rendered.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reconciliation in React&lt;/strong&gt;: The process of syncing Virtual DOM with the real DOM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Diffing Algorithm&lt;/strong&gt;: The algorithm to find the minimum number of steps needed to update the real DOM.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assumptions for using the Diffing Algorithm&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Two elements of different types will produce different trees.&lt;/li&gt;
&lt;li&gt;The developer can hint at which child elements may be stable across different renders with a &lt;code&gt;key&lt;/code&gt; attribute.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is just a very brief overview of these concepts. For more details definitely checkout React Documentation.&lt;br&gt;
For now let's just keep in mind the 2nd assumption used for React's diffing algorithm and proceed further.&lt;/p&gt;

&lt;p&gt;Coming back to our topic of why a &lt;code&gt;key&lt;/code&gt; attribute is necessary.&lt;br&gt;&lt;br&gt;
React uses &lt;code&gt;key&lt;/code&gt; attribute to track the changes in the list.&lt;br&gt;&lt;br&gt;
We might face following issues when we use index value as &lt;code&gt;key&lt;/code&gt; attribute when creating a list:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Performance Issues due to unnecessary re-renders.&lt;/li&gt;
&lt;li&gt;Issues in data mapping in case list items are sorted, filtered, or deleted.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's understand the performance issue with the following example.&lt;/p&gt;

&lt;p&gt;Suppose we've a list of elements, with &lt;code&gt;key&lt;/code&gt; attribute as index.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Milk&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Eggs&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;3&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Bread&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;Now, in case of any state change in the list, React just iterates over each list item in both the lists (React compares the Virtual DOM snapshot before the update and after the update), looks for changes and finally updates the RealDOM with only those changes.  &lt;/p&gt;

&lt;p&gt;If we add an item to the end of the list, React no longer needs to re-render the first 3 list items which are same. It will just add a new list item at the end.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Milk&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Eggs&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;3&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Bread&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;4&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Butter&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;But suppose we add the new item at the beginning of the list.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Butter&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Milk&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Eggs&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Bread&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;Now, the key of remaining list items also changes, which makes React re-render all the elements again, instead of just adding a new item at the end.&lt;/p&gt;

&lt;p&gt;This can be avoided if we use some unique id as a key rather than index.&lt;br&gt;
Let's again consider the same previous example but this time by using a unique id as &lt;code&gt;key&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"12abc"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Milk&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"23bcd"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Eggs&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"34cde"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Bread&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;Now even if we add element to the beginning or the end, we won't face an issue since keys are different.&lt;br&gt;
Since React tracks all list items with their key attribute, after adding a new element it would not re-render the previous list items.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"45htl"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Milk&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"12abc"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Milk&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"23bcd"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Eggs&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"34bcd"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Bread&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"85kgt"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Milk&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;In the below example, the first list is created using &lt;code&gt;index&lt;/code&gt; as &lt;code&gt;key&lt;/code&gt; and in the second using &lt;code&gt;id&lt;/code&gt; as key.&lt;br&gt;
On deleting the item from the first list, we can see the whole list is getting re-rendered, while in the second list the original list remains intact only the targeted element is removed.&lt;br&gt;
So, in case the list is large with complex components it might cause a huge performance issue.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8pnq9wjbkq4ejlyf8bva.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8pnq9wjbkq4ejlyf8bva.gif" alt="screen-capture-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/loving-wilbur-enj6v"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  When is it safe to use index as key in a list?
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Data is static.&lt;/li&gt;
&lt;li&gt;When you know reordering of lists: Sorting, Filtering is not going to happen.&lt;/li&gt;
&lt;li&gt;In the absence of an id.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  SUMMARY
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Always prefer using a &lt;code&gt;unique id&lt;/code&gt; as value for the key attribute in a list and avoid using &lt;code&gt;index&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;index&lt;/code&gt; might result in performance issues and data binding issues in case reordering in the form of sorting, filtering might happen.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  References:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/docs/reconciliation.html" rel="noopener noreferrer"&gt;Reconciliation - React&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://codepen.io/pen?&amp;amp;editors=0010" rel="noopener noreferrer"&gt;An example of the issues that can be caused by using indexes as keys on CodePen&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://codepen.io/pen?&amp;amp;editors=0010" rel="noopener noreferrer"&gt;an updated version of the same example showing how not using indexes as keys will fix these reordering, sorting, and prepending issues.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/faq-internals.html" rel="noopener noreferrer"&gt;Virtual DOM and Internals - React&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=i793Qm6kv3U" rel="noopener noreferrer"&gt;Understanding React's UI Rendering Process&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://programmingwithmosh.com/react/why-do-i-need-keys-in-react-lists/" rel="noopener noreferrer"&gt;Why do I need Keys in React Lists?&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>interview</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Understanding undefined, null, undeclared in Javascript</title>
      <dc:creator>Shivaansh Agarwal</dc:creator>
      <pubDate>Sun, 23 May 2021 13:38:19 +0000</pubDate>
      <link>https://forem.com/shiv1998/understanding-undefined-null-undeclared-in-javascript-220f</link>
      <guid>https://forem.com/shiv1998/understanding-undefined-null-undeclared-in-javascript-220f</guid>
      <description>&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;undefined&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's a primitive data type in Javascript.&lt;/li&gt;
&lt;li&gt;Any variable which has been declared but has not been assigned any value has the value undefined. 
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--agkwSGQa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9g0g17re7lzgc2olz23b.png" alt="image"&gt; &lt;/li&gt;
&lt;li&gt;During the memory creation phase of the execution context, every variable is assigned the value of &lt;code&gt;undefined&lt;/code&gt; by the JS Engine.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;null&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's a primitive data type in Javascript.&lt;/li&gt;
&lt;li&gt;A variable can be assigned the value &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Although &lt;code&gt;null&lt;/code&gt; is itself a data type, but on checking it's type using &lt;code&gt;typeof&lt;/code&gt; we get &lt;code&gt;"object"&lt;/code&gt;.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qCHUSOJ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g596u5w21t0wg6f6jz7l.png" alt="image"&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/18808226/why-is-typeof-null-object"&gt;Why is typeof null “object”?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;undeclared (not defined)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This occurs when we try to access a variable which hasn't been declared or initalized yet.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XxMFzbLb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sh48snqrcmtqzy0grc7v.png" alt="image"&gt;
&lt;/li&gt;
&lt;li&gt;If we try to &lt;code&gt;console.log(undeclaredVar)&lt;/code&gt; the error will be raised as shown above, so especially for this a try{...} catch(e){...} block has to be written. But if we instead use &lt;code&gt;"use strict"&lt;/code&gt; pragma we can fix these errors during compile phase itself rather than during runtime.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;typeof&lt;/code&gt; operator is the only operator that can reference a variable that hasn't been declared and not throw an error.
&lt;code&gt;typeof undeclaredVar //"undefined"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;uninitialized (TDZ)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It was introduced with ES6.&lt;/li&gt;
&lt;li&gt;It's based on the concept of Temporal Dead Zone.&lt;/li&gt;
&lt;li&gt;Variables which are block scoped (declared using &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt;) are initially &lt;em&gt;uninitialized&lt;/em&gt;. They can't be accessed unless they're in the temporal dead zone or intialized.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KQMolchL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/boibbzk87j0r7px2pw09.png" alt="image"&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;References &amp;amp; Further Reading:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://tc39.es/ecma262/#sec-ecmascript-data-types-and-values"&gt;6 ECMAScript Data Types and Values&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/64406377/how-to-check-if-a-variable-is-undefined-versus-it-is-undeclared-in-javascript"&gt;How to check if a variable is undefined versus it is undeclared in javascript?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://johnresig.com/blog/ecmascript-5-strict-mode-json-and-more/"&gt;ECMAScript 5 Strict Mode, JSON, and More&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>interview</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Value VS Reference in Javascript</title>
      <dc:creator>Shivaansh Agarwal</dc:creator>
      <pubDate>Tue, 19 Jan 2021 20:36:59 +0000</pubDate>
      <link>https://forem.com/shiv1998/value-vs-reference-in-javascript-22d7</link>
      <guid>https://forem.com/shiv1998/value-vs-reference-in-javascript-22d7</guid>
      <description>&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Javascript has 6 primitive data types: &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/string"&gt;&lt;code&gt;string&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/number"&gt;&lt;code&gt;number&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/bigint"&gt;&lt;code&gt;bigint&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/boolean"&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/undefined"&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/symbol"&gt;&lt;code&gt;symbol&lt;/code&gt;&lt;/a&gt;. Although &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/null"&gt;&lt;code&gt;null&lt;/code&gt;&lt;/a&gt; is also considered as a primitive data type because of it's behavior, But in certain cases, &lt;code&gt;null&lt;/code&gt; is not as "primitive" as it first seems! since every object is derived from &lt;code&gt;null&lt;/code&gt; by the &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance"&gt;&lt;code&gt;Prototypal Chain&lt;/code&gt;&lt;/a&gt; and therefore &lt;code&gt;typeof&lt;/code&gt; operator returns object for it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The primitive data types are copied by value.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Javascript also provides another data structure like &lt;code&gt;Object&lt;/code&gt;, which itself is used for creating other non-primitive data-types like &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Function&lt;/code&gt;, &lt;code&gt;Map&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt;, &lt;code&gt;WeakMap&lt;/code&gt;, &lt;code&gt;WeakSet&lt;/code&gt;, &lt;code&gt;Date&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;These non-primitive data types are copied by reference.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Primitive Data Types Examples
&lt;/h3&gt;

&lt;p&gt;Let's take the examples of copying primitive data types.&lt;br&gt;
Here we can see that the values are copied as it is to other variables.&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;a1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b1&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a1&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;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b1&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;a1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// Javascript 10 Javascript 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we assign something else to the previously declared &lt;code&gt;a2&lt;/code&gt; and &lt;code&gt;b2&lt;/code&gt; variables, we can see that the value stored inside &lt;code&gt;a1&lt;/code&gt; and &lt;code&gt;b1&lt;/code&gt; didn't get impacted.&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;a1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b1&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a1&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;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;a2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Java&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&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;a1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// Javascript 10 Java 20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Non-Primitive Data Types Examples
&lt;/h3&gt;

&lt;p&gt;Now suppose we've a non-primitive data type and we copy it to another variable.&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;arr1&lt;/span&gt; &lt;span class="o"&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;1&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;2&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;3&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;4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr1&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;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// ["1", "2", "3", "4"]&lt;/span&gt;
&lt;span class="c1"&gt;// ["1", "2", "3", "4"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But now if we make some change to arr2.&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;arr2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&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;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// ["1", "2", "5", "4"]&lt;/span&gt;
&lt;span class="c1"&gt;// ["1", "2", "5", "4"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that the change made to the copied array &lt;code&gt;arr2&lt;/code&gt; also reflects in the original array &lt;code&gt;arr1&lt;/code&gt;. So what happens when we did &lt;code&gt;arr2 = arr1&lt;/code&gt; was, we assigned the reference of the value stored inside arr1 to arr2. And this is the case with all non-primitive data types.&lt;/p&gt;



&lt;p&gt;So what can be done if suppose we want to copy a non-primitive data type, say array for example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&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;1&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;2&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;3&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;4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;// Option-1: Using Array.prototype.slice() method. [Shallow Copy]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arrCopy1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Option-2: Using Array.prototype.concat() method. [Shallow Copy]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arrCopy2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[].&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Option-3: Using es6 spread operator. [Shallow Copy]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arrCopy3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;// Option-4: Using Array.from() method [Shallow Copy]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arrCopy4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now if we change anything inside these new copied arrays, the original values inside &lt;code&gt;arr&lt;/code&gt; won't change.&lt;br&gt;
For shallow copying of &lt;code&gt;Objects&lt;/code&gt; use Object.assign()&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;car&lt;/span&gt; &lt;span class="o"&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;brand&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;BMW&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;wheels&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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;bike&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;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&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;wheels&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;safety&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="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;car&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bike&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// {brand: "BMW", wheels: 4} {brand: "BMW", wheels: 2, safety: 3}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Shallow VS Deep Copy (Array)
&lt;/h3&gt;

&lt;p&gt;But a thing to remember here is that all these techniques perform &lt;code&gt;shallow copy&lt;/code&gt; instead of a &lt;code&gt;deep copy&lt;/code&gt;, i.e. If the array is nested or multi-dimensional or contains objects, and if we change anything inside those it won't work.&lt;br&gt;
Let me explain with an example:&lt;br&gt;
Here I'm taking &lt;code&gt;Array.prototype.slice()&lt;/code&gt; for copying but any of the others can also be used.&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;obj1&lt;/span&gt; &lt;span class="o"&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;name&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;shivaansh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&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;name&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;agarwal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;obj2&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;arrCopy1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;arrCopy1&lt;/span&gt;&lt;span class="p"&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&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;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arrCopy1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*
[{"name":"shivaansh", "age":22}, {"name":"agarwal"}]
[{"name":"shivaansh", "age":22}, {"name":"agarwal"}]
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see here in case of deep copy the above technique fails.&lt;/p&gt;

&lt;p&gt;So to avoid this some developers usually prefer using the JSON methods.&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;obj1&lt;/span&gt; &lt;span class="o"&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;name&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;shivaansh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&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;name&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;agarwal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;obj2&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;arrCopy1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;arrCopy1&lt;/span&gt;&lt;span class="p"&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&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;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arrCopy1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*
[{"name":"shivaansh"}, {"name":"agarwal"}]
[{"name":"shivaansh", "age":22}, {"name":"agarwal"}]
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
But as pointed out by &lt;a href="https://dev.to/samanthaming"&gt;Samantha Ming&lt;/a&gt; in her &lt;a href="https://dev.to/samanthaming/how-to-deep-clone-an-array-in-javascript-3cig"&gt;blog&lt;/a&gt;, even &lt;code&gt;JSON&lt;/code&gt; technique might fail as it'll not work with values not compatible with &lt;code&gt;JSON&lt;/code&gt; like suppose if we've a function being assigned to an object property inside an array.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--53-Llgzs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vkjs9uqcbm1jsifgoq81.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--53-Llgzs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vkjs9uqcbm1jsifgoq81.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, consider the following example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;nestedCopy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// undefined are converted to nulls&lt;/span&gt;
&lt;span class="nx"&gt;nestedCopy&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="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; [1, null, 2]&lt;/span&gt;

&lt;span class="c1"&gt;// DOM nodes are converted to empty objects&lt;/span&gt;
&lt;span class="nx"&gt;nestedCopy&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; [{}, {}]&lt;/span&gt;

&lt;span class="c1"&gt;// JS dates are converted to strings&lt;/span&gt;
&lt;span class="nx"&gt;nestedCopy&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()])&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; ["2019-03-04T10:09:00.419Z"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  deepClone by lodash or custom function
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JSON.stringify/parse&lt;/strong&gt; only work with Number and String and Object literal without function or Symbol properties.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;deepClone&lt;/strong&gt; works with all types, function and Symbol are copied by reference.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example of Lodash Solution by &lt;a href="https://dev.to/alfredosalzillo"&gt;Alfredo Salzillo&lt;/a&gt;,
&lt;/h4&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;lodashClonedeep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lodash.clonedeep&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrOfFunction&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="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="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;

&lt;span class="c1"&gt;// deepClone copy by refence function and Symbol&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;lodashClonedeep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrOfFunction&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// JSON replace function with null and function in object with undefined&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrOfFunction&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

&lt;span class="c1"&gt;// function and symbol are copied by reference in deepClone&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;lodashClonedeep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrOfFunction&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;lodashClonedeep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrOfFunction&lt;/span&gt;&lt;span class="p"&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;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;lodashClonedeep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrOfFunction&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;lodashClonedeep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrOfFunction&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;h4&gt;
  
  
  Example of Recursive function Solution by &lt;a href="https://medium.com/@zubaidi/there-is-other-simple-and-more-performant-solution-for-this-problem-8c8bda77d042"&gt;Tareq Al-Zubaidi&lt;/a&gt;
&lt;/h4&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;clone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&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;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  References:
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.educative.io/courses/step-up-your-js-a-comprehensive-guide-to-intermediate-javascript/7nAZrnYW9rG"&gt;https://www.educative.io/courses/step-up-your-js-a-comprehensive-guide-to-intermediate-javascript/7nAZrnYW9rG&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value"&gt;https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/understanding-by-reference-vs-by-value-d49139beb1c4/"&gt;https://www.freecodecamp.org/news/understanding-by-reference-vs-by-value-d49139beb1c4/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://javascript.info/object-copy"&gt;https://javascript.info/object-copy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/samanthaming/how-to-deep-clone-an-array-in-javascript-3cig"&gt;https://dev.to/samanthaming/how-to-deep-clone-an-array-in-javascript-3cig&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Javascript30 Course by WesBros&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Maps (Hashmaps) in Javascript</title>
      <dc:creator>Shivaansh Agarwal</dc:creator>
      <pubDate>Mon, 28 Dec 2020 04:08:11 +0000</pubDate>
      <link>https://forem.com/shiv1998/maps-hashmaps-in-javascript-4cdi</link>
      <guid>https://forem.com/shiv1998/maps-hashmaps-in-javascript-4cdi</guid>
      <description>&lt;h3&gt;
  
  
  MAPS
&lt;/h3&gt;

&lt;p&gt;A map is a data structure which maps values (keys) with other values.&lt;br&gt;&lt;br&gt;
In Javascript we can use an Object for this purpose.&lt;/p&gt;

&lt;p&gt;Suppose we've to store student's marks, we can use an object for this purpose.&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;marks&lt;/span&gt;&lt;span class="o"&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;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;98&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Arun&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;87&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Robert&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;91&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Arun scored &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Arun&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s2"&gt; marks.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// → Arun scored 87 marks.&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="s2"&gt;Are Julie's marks known?&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;Julie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// → Are Julie's marks known? 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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Are toString's marks known?&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;toString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// → Are toString's marks known? true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although the first 2 results were expected, we certainly didn't expect the 3rd result as we didn't add any key with the name of 'toString' to our marks object.&lt;br&gt;
But this is the downside of using an &lt;code&gt;Object&lt;/code&gt; as a &lt;code&gt;Map&lt;/code&gt;.&lt;br&gt;
Along with the key-value pairs we define the plain object also derives properties from &lt;code&gt;Object.prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8KrjVHiC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ov9b9b43yqjvnttuk57.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8KrjVHiC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ov9b9b43yqjvnttuk57.PNG" alt="Example 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hence using plain objects as maps could be dangerous. So suppose we want an object without it inheriting from &lt;code&gt;Object.prototype&lt;/code&gt;, we can do this in many ways:&lt;br&gt;
One of the way is to pass &lt;code&gt;null&lt;/code&gt; to &lt;code&gt;Object.create()&lt;/code&gt; method. In this way the resulting object will not derive from &lt;code&gt;Object.prototype&lt;/code&gt; and can be safely used as a map.&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&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="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// → false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also if we're using a plain object as our map, we can avoid using &lt;code&gt;in&lt;/code&gt; keyword for finding whether key is present in object or not; instead we can use &lt;code&gt;hasOwnProperty()&lt;/code&gt; method which ignores the object's prototype.&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="na"&gt;x&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="nx"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;x&lt;/span&gt;&lt;span class="dl"&gt;"&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="na"&gt;x&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="nx"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// → false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's another problem with using plain objects. It only accepts strings as it's keys. And if suppose we want suppose an object as a key, we cannot do this.&lt;/p&gt;

&lt;p&gt;Don't worry, Javascript also provides us with &lt;code&gt;Map&lt;/code&gt; class which is written for this exact purpose.&lt;br&gt;
The methods &lt;code&gt;set&lt;/code&gt;, &lt;code&gt;get&lt;/code&gt;, and &lt;code&gt;has&lt;/code&gt; are part of the interface of the Map 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;let&lt;/span&gt; &lt;span class="nx"&gt;marks&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;marks&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;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;98&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;marks&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;Arun&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;87&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;marks&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;Robert&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;91&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Arun scored &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;marks&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;Arun&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; marks.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// → Júlia scored 87 marks.&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="s2"&gt;Are Julie's marks known?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Julie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// → Are Julie's marks known? 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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Are toString's marks known?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// → Are toString's marks known? false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;References:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://eloquentjavascript.net/06_object.html"&gt;https://eloquentjavascript.net/06_object.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://adripofjavascript.com/blog/drips/creating-objects-without-prototypes.html"&gt;http://adripofjavascript.com/blog/drips/creating-objects-without-prototypes.html&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Closures in Javascript PART-1</title>
      <dc:creator>Shivaansh Agarwal</dc:creator>
      <pubDate>Mon, 14 Dec 2020 18:24:46 +0000</pubDate>
      <link>https://forem.com/shiv1998/closures-in-javascript-part-1-28i4</link>
      <guid>https://forem.com/shiv1998/closures-in-javascript-part-1-28i4</guid>
      <description>&lt;p&gt;&lt;em&gt;DISCLAIMER: I've written this blog to share with others what I've understood after going through several blogs, articles, videos, etc. So the following written blog might contain some ideas &amp;amp; language influenced by those.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;Closures are one of the most asked interview topics &amp;amp; are frequently used in JavaScript for object data privacy, in event handlers and callback functions, and in partial applications, currying, and other functional programming patterns.&lt;/p&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A closure is a function bundled together with it's lexical environment. &lt;/li&gt;
&lt;li&gt;In simple words a closure gives you access from an outer function's scope from an inner function.&lt;/li&gt;
&lt;li&gt;In Javascript, a closure is created every time a function is created, at function creation time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Lexical Scoping
&lt;/h3&gt;

&lt;p&gt;The following is an example of &lt;em&gt;lexical scoping&lt;/em&gt;, where &lt;code&gt;getName()&lt;/code&gt; has 2 local variables &lt;code&gt;firstName&lt;/code&gt; &amp;amp; &lt;code&gt;lastName&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
It also has a nested method which is accessing these variables of it's other function.&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0wobVp6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/52g5mw7fzrgelzjzo1m8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0wobVp6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/52g5mw7fzrgelzjzo1m8.png" alt="carbon"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://jsfiddle.net/shivaansh/vLy1b8eh/1/"&gt;JSFiddle Link&lt;/a&gt;&lt;br&gt;
Lexical Scoping describes how a parser resolves variable names when functions are nested.&lt;/p&gt;

&lt;h3&gt;
  
  
  Closures
&lt;/h3&gt;

&lt;p&gt;Now what happens if we return this nested method and call it from outside?&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3oXRqSF7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uhm7uo3z0x7d5ebn9uby.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3oXRqSF7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uhm7uo3z0x7d5ebn9uby.png" alt="carbon (1)"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://jsfiddle.net/shivaansh/vLy1b8eh/5/"&gt;JS Fiddle Link&lt;/a&gt;&lt;br&gt;
It works exactly the same as in previous example.&lt;br&gt;
The catch here is when we return the function, it is returned along with its lexical environment. The lexical environment of the inner function &lt;code&gt;getFullName&lt;/code&gt; consists of any local variables that were in-scope at the time when closure was created.&lt;/p&gt;

&lt;h4&gt;
  
  
  References:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://javascript.info/closure"&gt;https://javascript.info/closure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=qikxEIxsXco"&gt;https://www.youtube.com/watch?v=qikxEIxsXco&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=71AtaJpJHw0"&gt;https://www.youtube.com/watch?v=71AtaJpJHw0&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

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