<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amit Khonde</title>
    <description>The latest articles on DEV Community by Amit Khonde (@amitkhonde).</description>
    <link>https://dev.to/amitkhonde</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%2F264435%2F4163f175-05c6-409e-94cf-f70704c348fa.jpg</url>
      <title>DEV Community: Amit Khonde</title>
      <link>https://dev.to/amitkhonde</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amitkhonde"/>
    <language>en</language>
    <item>
      <title>Creating Skeleton Loading using HTML and CSS</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Sat, 30 Oct 2021 08:03:35 +0000</pubDate>
      <link>https://dev.to/amitkhonde/creating-skeleton-loading-using-html-and-css-1a2b</link>
      <guid>https://dev.to/amitkhonde/creating-skeleton-loading-using-html-and-css-1a2b</guid>
      <description>&lt;h2&gt;
  
  
  TLDR;
&lt;/h2&gt;

&lt;p&gt;Final version of working code: &lt;a href="https://codepen.io/amitkhonde/pen/XWgjNeR" rel="noopener noreferrer"&gt;https://codepen.io/amitkhonde/pen/XWgjNeR&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React CodeSandbox Link: &lt;a href="https://codesandbox.io/s/skeleton-loading-demo-6dwee" rel="noopener noreferrer"&gt;https://codesandbox.io/s/skeleton-loading-demo-6dwee&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React Github Link: &lt;a href="https://github.com/amitkhonde/skeleton-loading-demo" rel="noopener noreferrer"&gt;https://github.com/amitkhonde/skeleton-loading-demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In today's web development world, making sure that the users are hooked to your website is the most important thing. And to achieve that, designers and developers come up with the most aesthetically pleasing UIs.&lt;/p&gt;

&lt;p&gt;But, another thing that goes hand in hand with great UI is relevant information. With so much increase in the amount of data to process today, even the most powerful servers take a second to send back the data.&lt;/p&gt;

&lt;p&gt;And this is the tricky part of engaging user experience. &lt;strong&gt;NOBODY LIKES TO WAIT&lt;/strong&gt;. &lt;a href="https://www.websitebuilderexpert.com/building-websites/website-load-time-statistics/" rel="noopener noreferrer"&gt;A recent study&lt;/a&gt; found out that a 1-second delay in the page load reduces customer satisfaction by 16%. And &lt;strong&gt;46% of users do not revisit the websites with poor loading time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To overcome this problem, UX engineers came up with the solution of &lt;strong&gt;Skeleton Loading&lt;/strong&gt;. As the name suggests, skeleton loading shows the user a skeleton of what is going to come on the screen and how it is structured.&lt;/p&gt;

&lt;p&gt;This serves 2 purposes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user is engaged via some cool animation.&lt;/li&gt;
&lt;li&gt;Users know where to expect information because the skeleton shows where the content would appear.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this post, we are going to look at how to create the skeleton loading effect using CSS animations. And as a bonus, I will link a CodeSandbox project and a Github repository on how I have used it in React. Let us get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Skeleton Loading with HTML and CSS
&lt;/h2&gt;

&lt;p&gt;First of all, let us see what our result will look like and then get to it step by step. For the purpose of learning, we will develop a user card which shows skeleton loading until the data is received.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7140tc0502jzjn0hr06.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%2Fx7140tc0502jzjn0hr06.gif" alt="skeleton-demo.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Adding HTML Markup
&lt;/h3&gt;

&lt;p&gt;In this step, we are just adding the basic HTML markup you see in the above image. We have one parent &lt;code&gt;div&lt;/code&gt; with a class &lt;code&gt;user-card&lt;/code&gt;. This serves as the container for all content. Inside &lt;code&gt;user-card&lt;/code&gt;, we have 3 elements.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;div&lt;/code&gt; for user’s profile picture.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;paragraph&lt;/code&gt; for user’s name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;paragraph&lt;/code&gt; for user’s email.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is it. No fancy stuff here. Let us look at the code to understand it better.&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;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"user-card"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"profile-pic"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"status"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 2: Adding Skeleton class CSS
&lt;/h3&gt;

&lt;p&gt;In this step, we will be adding the CSS for our &lt;code&gt;user-card&lt;/code&gt; and its children elements. For the &lt;code&gt;user-card&lt;/code&gt;, we are adding some basic paddings and borders. And for each child, we are adding &lt;code&gt;min-width&lt;/code&gt; and &lt;code&gt;min-height&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Adding &lt;code&gt;min-width&lt;/code&gt; and &lt;code&gt;min-height&lt;/code&gt; properties to the children is very important because if the content is not present in any of the elements, they will remain empty.&lt;/p&gt;

&lt;p&gt;Also in this step, we are adding basic styling for the &lt;code&gt;skeleton&lt;/code&gt; class. This is the class that will take care of showing the loading animation. We are giving our &lt;code&gt;skeleton&lt;/code&gt; class the &lt;code&gt;background-color&lt;/code&gt; #A5A5A5. And adding an animation property &lt;code&gt;animation: skeleton-animation 1s infinite ease-in-out&lt;/code&gt; which signifies 4 things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The name of the animation.&lt;/li&gt;
&lt;li&gt;Duration of the animation.&lt;/li&gt;
&lt;li&gt;Frequency of the animation.&lt;/li&gt;
&lt;li&gt;The transition timing function &lt;code&gt;ease-in-out&lt;/code&gt; signifies that our animation will run slow at start and end.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;On whichever element we want to show the skeleton loading animation, we have to add the class &lt;code&gt;skeleton&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us take a look at what our current CSS looks like and in the next step, we will add the actual animation.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;

&lt;span class="nc"&gt;.user-card&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt; &lt;span class="m"&gt;16px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#a5a5a5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.user-card&lt;/span&gt; &lt;span class="nc"&gt;.profile-pic&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.user-card&lt;/span&gt; &lt;span class="nc"&gt;.name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;.user-card&lt;/span&gt; &lt;span class="nc"&gt;.status&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;min-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;24px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.user-card&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16px&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.skeleton&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="m"&gt;#a5a5a5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;skeleton-animation&lt;/span&gt; &lt;span class="m"&gt;1s&lt;/span&gt; &lt;span class="n"&gt;infinite&lt;/span&gt; &lt;span class="n"&gt;ease-in-out&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;h3&gt;
  
  
  Step 3: Adding the animation
&lt;/h3&gt;

&lt;p&gt;This is the final and the most important step. We are going the add the actual animation in this step. The name of our animation is &lt;code&gt;skeleton-animation&lt;/code&gt; (as defined in the previous step). And in that animation, we are going to define the stages 0%, 50%, and 100%. The CSS in these stages defines how our element looks at that particular stage.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;At 0%, we are setting the &lt;code&gt;opacity&lt;/code&gt; to 0.2.&lt;/li&gt;
&lt;li&gt;At 50% we increase the &lt;code&gt;opacity&lt;/code&gt; to 0.5.&lt;/li&gt;
&lt;li&gt;And at 100% we again bring back the &lt;code&gt;opacity&lt;/code&gt; to 0.2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Doing this will give our element a look like it is getting removed and added continuously. Which is the exact skeleton loading effect we want. Now enough with the theory. Let us check the code and jump to what the final version of our code will look like.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;

&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="n"&gt;skeleton-animation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="err"&gt;50&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Note: If you are unaware of the CSS animations, I highly recommend going through &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations/Using_CSS_animations" rel="noopener noreferrer"&gt;this MDN tutorial&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Code
&lt;/h3&gt;

&lt;p&gt;Yay!! We are now done with all the steps and our skeleton loading is ready. Let us look at the final code and the result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/amitkhonde/pen/XWgjNeR" rel="noopener noreferrer"&gt;https://codepen.io/amitkhonde/pen/XWgjNeR&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: How I used Skeleton in an Actual React Project
&lt;/h2&gt;

&lt;p&gt;Now that we have learned the basics of how we can add the loading animation, here is a react example project where I have used this animation. In this project, I fetch the list of users from a dummy API and until that is done, skeleton loading is shown for an engaging user experience. Feel free to edit and improve the code.&lt;/p&gt;

&lt;p&gt;React CodeSandbox Link: &lt;a href="https://codesandbox.io/s/skeleton-loading-demo-6dwee" rel="noopener noreferrer"&gt;https://codesandbox.io/s/skeleton-loading-demo-6dwee&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Well I know there are a lot of ways we can achieve the skeleton loading effect. And there are also many libraries available we can use for achieving this. But, in my opinion, it is very important that we know the basics of what we are using. Comment down below on how you have approached this problem in your projects. And if you want to learn more about Skeleton loading, check out the links provided at the end. Until then, Happy Coding!!&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Readings and Videos
&lt;/h2&gt;

&lt;p&gt;Web Dev Simplified's &lt;a href="https://www.youtube.com/watch?v=ZVug65gW-fc&amp;amp;ab_channel=WebDevSimplified" rel="noopener noreferrer"&gt;How To Create Skeleton Loading Animation With CSS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The IT Guy's &lt;a href="https://www.youtube.com/watch?v=YTUuZg7NpZU&amp;amp;ab_channel=TheITGuy" rel="noopener noreferrer"&gt;Loading Placeholder Effect&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kevin Mehta's &lt;a href="https://blog.prototypr.io/skeleton-loader-an-overview-purpose-usage-and-design-173b5340d0e1#:~:text=Why%20to%20use%20%3F,progress%20instead%20of%20waiting%20time." rel="noopener noreferrer"&gt;Skeleton Loader: An overview, purpose, usage and design&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was &lt;a href="https://www.amitkhonde.com/blog/create-skeleton-loading-with-html-css" rel="noopener noreferrer"&gt;originally published here&lt;/a&gt;. For more such interesting reads, &lt;a href="https://www.amitkhonde.com/blog" rel="noopener noreferrer"&gt;visit my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>codenewbie</category>
      <category>webdev</category>
      <category>ux</category>
    </item>
    <item>
      <title>What is flatMap in JavaScript? And how made my code cleaner</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Sat, 18 Sep 2021 08:32:27 +0000</pubDate>
      <link>https://dev.to/amitkhonde/what-is-flatmap-in-javascript-and-how-made-my-code-cleaner-3nbn</link>
      <guid>https://dev.to/amitkhonde/what-is-flatmap-in-javascript-and-how-made-my-code-cleaner-3nbn</guid>
      <description>&lt;p&gt;Have you ever looked at a JavaScript in built function and said to yourself: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I understand what this is but I do not know where I can use this.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, this exact thought came to mind when I read about &lt;code&gt;Array.flatMap()&lt;/code&gt;. So I started looking for its applications in my day-to-day coding and viola!! I found a use case. In this post, we will first see what &lt;code&gt;Array.flatmap()&lt;/code&gt; is and then look at how I used it in a real problem. &lt;/p&gt;

&lt;h2&gt;
  
  
  What exactly is &lt;code&gt;Array.flatMap&lt;/code&gt;?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;FlatMap is a mix of two words which are derived from latin words blah blah blah...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Just kidding.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Array.flatMap&lt;/code&gt; is a mixture of two array methods &lt;code&gt;Array.flat&lt;/code&gt; and &lt;code&gt;Array.map&lt;/code&gt;. Individually,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Array.flat&lt;/code&gt; is used for flattening nested arrays. For example
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;names&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="s1"&gt;Amit&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="s1"&gt;Karishma&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="s1"&gt;Kunal&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pankaj&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="s1"&gt;Rahee&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;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// expected output: ["Amit", "Karishma", "Kunal", "Pankaj", "Rahee"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Array.map&lt;/code&gt; is used for creating a new array by performing operations on each array element and returning a value. For example
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;names&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="s1"&gt;Amit&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="s1"&gt;Karishma&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="s1"&gt;Kunal&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="s1"&gt;Pankaj&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="s1"&gt;Rahee&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;everyOneLovesDogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;names&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;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; loves dogs`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;everyOneLovesDogs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// expected output: ["Amit loves dogs", "Karishma loves dogs", "Kunal loves dogs", "Pankaj loves dogs", "Rahee loves dogs"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So as you can guess, &lt;code&gt;Array.flatMap&lt;/code&gt; maps an array and then flattens the nested arrays in the returned array. A quick example of flatMap looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;usersWithInterests&lt;/span&gt; &lt;span class="o"&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Amit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;interests&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="s1"&gt;Cricket&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="s1"&gt;Music&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Karishma&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;interests&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="s1"&gt;Drawing&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="s1"&gt;Reading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pranav&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;interests&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="s1"&gt;Crafting&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="s1"&gt;Biking&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;usersWithInterests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flatMap&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interests&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interests&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: ["Cricket", "Music", "Drawing", "Reading", "Crafting", "Biking"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How I used &lt;code&gt;Array.flatMap&lt;/code&gt; in a real-world problem?
&lt;/h2&gt;

&lt;p&gt;I was working on a feature where I had to create a dropdown for categories for products. The actual response from &lt;code&gt;getProducts&lt;/code&gt; API looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&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;status&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;success&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;products&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="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="s1"&gt;One Plus 7T&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;categories&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Technology&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="s1"&gt;Mobile&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="s1"&gt;SmartPhone&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;description&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="s1"&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&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;rating&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="s1"&gt;4.5&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="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="s1"&gt;MacBook Pro 2018&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;categories&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Technology&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="s1"&gt;Computer&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="s1"&gt;Laptop&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;description&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="s1"&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&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;rating&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="s1"&gt;4.8&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="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="s1"&gt;LG Monitor 221G&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;categories&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Technology&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="s1"&gt;Monitor&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;description&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="s1"&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&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;rating&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="s1"&gt;4.3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The product object has categories as a key for which the values are stored in the array. &lt;/p&gt;

&lt;p&gt;So I had to do 3 things&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get the categories from each product&lt;/li&gt;
&lt;li&gt;Flatten that array&lt;/li&gt;
&lt;li&gt;Get unique values from the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of using 2 separate functions for first two tasks, i used one function: &lt;code&gt;Array.flatmap&lt;/code&gt;. And then applied the set operation on that array to get unique values. &lt;/p&gt;

&lt;p&gt;The final code snippet looked something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&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;allCategories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flatMap&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;uniqueCategories&lt;/span&gt; &lt;span class="o"&gt;=&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;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;allCategories&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;uniqueCategories&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: ["Technology", "Mobile", "SmartPhone", "Computer", "Laptop", "Monitor"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conclusion &lt;/p&gt;

&lt;p&gt;I hope now you folks can also apply &lt;code&gt;Array.flatMap&lt;/code&gt; in cases where they fit and produce a cleaner code. For more such interesting tidbits, stay tuned in. Until then, Happy Coding!!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was &lt;a href="https://www.amitkhonde.com/blog/what-is-flatmap-in-javascript"&gt;originally published here&lt;/a&gt;. For more such interesting reads, &lt;a href="https://www.amitkhonde.com/blog"&gt;visit my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>JavaScript Date Object Simplified</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Mon, 05 Jul 2021 18:46:31 +0000</pubDate>
      <link>https://dev.to/amitkhonde/javascript-date-object-simplified-453m</link>
      <guid>https://dev.to/amitkhonde/javascript-date-object-simplified-453m</guid>
      <description>&lt;p&gt;You must have heard many times that &lt;strong&gt;Objects represent real-world entities&lt;/strong&gt; when learning about object-oriented programming. I think the JavaScript Date object is a great testament to this statement. Because date and time are some of the most fundamental entities of the world.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Date object in javascript represents a given point in time.&lt;/strong&gt; It has everything you can associate with time. For example day, year, month, hour, minutes...&lt;/p&gt;

&lt;p&gt;Although this striking connection with our daily lives, &lt;code&gt;Date&lt;/code&gt; in JavaScript remains one of the most complicated and thus, feared topics. So much so that we have to rely on external libraries like &lt;a href="https://momentjs.com/"&gt;Moment.js&lt;/a&gt; for the simplest tasks.&lt;/p&gt;

&lt;p&gt;In this post, I have tried to simplify the &lt;code&gt;Date&lt;/code&gt; object as per my understanding and the most common functionalities I use in day-to-day development. Let us get started...&lt;/p&gt;

&lt;h2&gt;
  
  
  Ways we can create the &lt;code&gt;Date&lt;/code&gt; object
&lt;/h2&gt;

&lt;p&gt;There are four ways we can create &lt;code&gt;Date&lt;/code&gt; in JavaScript. All are useful in their own ways. Let us look at them one by one and when you will most likely use them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using empty constructor
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;today&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;Date&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;today&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Expected output: Tue Jun 29 2021 23:30:27 GMT+0530 (India Standard Time)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using an empty constructor will create the current date and time. This is the most common way of creating a &lt;code&gt;Date&lt;/code&gt; object. This will create the &lt;code&gt;Date&lt;/code&gt; in the client's local timezone.&lt;/p&gt;

&lt;p&gt;I generally use this way of creating the date when adding a new object. Like when creating a new TODO, I pass &lt;code&gt;createdDate&lt;/code&gt; as a &lt;code&gt;new Date()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using milliseconds
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;someDate&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;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1122334455&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;someDate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Expected output: Wed Jan 14 1970 05:15:34 GMT+0530 (India Standard Time)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the date constructor to create a Date object based on the number of milliseconds passed since Jan 1st of 1970 UTC+0. Jan 1st of 1970 UTC+0 is considered as &lt;strong&gt;Unix Epoch Time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So if you pass &lt;code&gt;24 * 60 * 60 * 1000&lt;/code&gt; (which is 24 hours in milliseconds), you will get the date as Jan 2nd of 1970 UTC+0.&lt;/p&gt;

&lt;p&gt;In most cases, this is the standard way of creating &lt;code&gt;Date&lt;/code&gt; because it is easier to communicate with the backend. When you want to save or retrieve dates, the backend prefers Unix Epoch Time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Passing each date entity separately
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;someDate&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;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2020&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someDate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Expected output: Sat Dec 12 2020 18:12:50 GMT+0530 (India Standard Time)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also create a &lt;code&gt;Date&lt;/code&gt; object by passing individual values of year, month, day, hours, minutes, seconds, and milliseconds.&lt;/p&gt;

&lt;p&gt;If we omit any values, the &lt;code&gt;Date&lt;/code&gt; constructor will take the default values for those parameters.&lt;/p&gt;

&lt;p&gt;I rarely use this method of creating the &lt;code&gt;Date&lt;/code&gt; object. Because I would prefer passing milliseconds to the constructor most of the time.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: &lt;code&gt;month&lt;/code&gt; Integer value representing the month, beginning with 0 for January to 11 for December.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Passing the date string
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;someDate&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;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2020-04-11T10:20:30Z&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;someDate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Expected output: Sat Apr 11 2020 15:50:30 GMT+0530 (India Standard Time)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The date constructor also accepts a string as a parameter and creates a &lt;code&gt;Date&lt;/code&gt; object by parsing the date string. This I feel is the most dangerous way of creating the date. Because we have to be careful of the format of the date string.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: If you still want to use this method, as JavaScript officially supports a simplification of the ISO 8601 Extended Format, stick with &lt;code&gt;YYYY-MM-DDTHH:mm:ss.sssZ&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Helpful methods in the &lt;code&gt;Date&lt;/code&gt; object
&lt;/h2&gt;

&lt;p&gt;Now that we know how to create the &lt;code&gt;Date&lt;/code&gt; object, let us understand different methods on the date object. These common methods are useful in displaying dates or manipulating the dates. &lt;/p&gt;

&lt;p&gt;There are also some static methods on the date constructor that are used as shorthands for common tasks.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getFullYear()&lt;/code&gt; returns the year part from the date. With &lt;code&gt;setFullYear()&lt;/code&gt; you can set the year part. Setting the year will not affect any other part of the date.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getMonth()&lt;/code&gt; returns month part from the date object. And with its counterpart &lt;code&gt;setMonth()&lt;/code&gt; we can set the month in the date.&lt;/p&gt;

&lt;p&gt;Similarly, &lt;code&gt;getDate()&lt;/code&gt; returns the current date and &lt;code&gt;setDate()&lt;/code&gt; sets the date in the date object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;someDate&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;Date&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;someDate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: Wed Jun 30 2021 09:02:03 GMT+0530 (India Standard Time)&lt;/span&gt;

&lt;span class="c1"&gt;// Gets the day of the week&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;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getDay&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: 3&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;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getFullYear&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: 2021&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;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getMonth&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: 5&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;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getDate&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: 30&lt;/span&gt;

&lt;span class="nx"&gt;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setFullYear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2020&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;someDate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: Tue Jun 30 2020 09:13:00 GMT+0530 (India Standard Time)&lt;/span&gt;

&lt;span class="nx"&gt;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setMonth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&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;someDate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: Thu Jul 30 2020 09:13:33 GMT+0530 (India Standard Time)&lt;/span&gt;

&lt;span class="nx"&gt;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;31&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;someDate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: Fri Jul 31 2020 09:13:55 GMT+0530 (India Standard Time)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, &lt;code&gt;getHours()&lt;/code&gt;,  &lt;code&gt;setHours()&lt;/code&gt;, &lt;code&gt;getMinutes()&lt;/code&gt;, &lt;code&gt;setMinutes()&lt;/code&gt;, &lt;code&gt;getSeconds()&lt;/code&gt;, &lt;code&gt;setSeconds()&lt;/code&gt;, &lt;code&gt;getMilliseconds()&lt;/code&gt; and &lt;code&gt;setMilliseconds()&lt;/code&gt; are used for getting and setting hours, minutes, seconds and milliseconds respectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Date.now()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This method is used to get the current date and time as the number of milliseconds passed since Unix epoch time. The number of milliseconds returned by this method is referred to as the timestamp.&lt;/p&gt;

&lt;p&gt;This is the most used method in my personal projects. This timestamp might even serve as id in some cases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;someDate&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;Date&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;someDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getTime&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="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output:&lt;/span&gt;
&lt;span class="c1"&gt;// 1625024936606&lt;/span&gt;
&lt;span class="c1"&gt;// 1625024936606&lt;/span&gt;

&lt;span class="c1"&gt;// Date.now() is same as saying someDate.getTime()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Converting &lt;code&gt;Date&lt;/code&gt; to another timezone
&lt;/h2&gt;

&lt;p&gt;Most of us work on applications that are used by a global audience. And it is our duty as web developers to make our content accessible for all users. Localizing the date is a big part of this and it makes for a great user experience. This is why converting dates to different timezones is one of the common tasks we perform on the &lt;code&gt;Date&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Let us understand this through an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;today&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;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1625061658610&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;timeZone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;America/New_York&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;dateOptions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;timeZone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;timeZone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;weekday&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;long&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;month&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;long&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;day&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;numeric&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;hour12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;hour&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2-digit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;minute&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2-digit&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;today&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLocaleString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en-US&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dateOptions&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
&lt;span class="c1"&gt;// Expected output: Wednesday, June 30, 10:00 AM&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to read more about &lt;code&gt;Date.toLocalString()&lt;/code&gt;, please check &lt;a href="https://www.w3schools.com/jsref/jsref_tolocalestring.asp"&gt;this W3 School link&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Well, this is mostly all that we need to know about working with the JavaScript Date object. Of course, there is a lot more to it and I hope this basic understanding helps you to learn more complex stuff.&lt;/p&gt;

&lt;p&gt;If you have anything more than what I have covered, comment down below. Until then, Happy Coding!!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Photos by &lt;a href="https://unsplash.com/@esteejanssens?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Estée Janssens&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was &lt;a href="https://www.amitkhonde.com/blog/javascript-date-object-simplified"&gt;originally published here&lt;/a&gt;. For more such interesting reads, &lt;a href="https://www.amitkhonde.com/blog"&gt;visit my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is a closure in JavaScript and when to use one?</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Mon, 21 Jun 2021 18:37:44 +0000</pubDate>
      <link>https://dev.to/amitkhonde/what-is-a-closure-in-javascript-and-when-to-use-one-3bln</link>
      <guid>https://dev.to/amitkhonde/what-is-a-closure-in-javascript-and-when-to-use-one-3bln</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Can you explain what is a closure in JavaScript?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is without a doubt one of the most popular questions in front-end interviews. There is a 99% chance you have faced this question in your JavaScript interviews round.&lt;/p&gt;

&lt;p&gt;And it is justified.&lt;/p&gt;

&lt;p&gt;The closure is such a non-trivial concept that you might have already used it without realising it.&lt;/p&gt;

&lt;p&gt;We will see some real development scenarios that involve closures,  but first, let us understand what closure actually is.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are closures in JavaScript?
&lt;/h2&gt;

&lt;p&gt;The entire concept of closures is based on scopes. And scopes decide where can we access a variable or function in our program. As simple as that. Scoping defines which parts of your code can access what. Take a look at the example below to understand scoping better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Variable a has access everywhere because&lt;/span&gt;
&lt;span class="c1"&gt;// it is defined globally.&lt;/span&gt;
&lt;span class="kd"&gt;var&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;function&lt;/span&gt; &lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Variable b has access inside this function because it is defined inside&lt;/span&gt;
  &lt;span class="c1"&gt;// the function. If b is accessed outside function print, it will give an error.&lt;/span&gt;
  &lt;span class="kd"&gt;var&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Variable c has access inside this if block only because it&lt;/span&gt;
    &lt;span class="c1"&gt;// is defined inside the block using let.&lt;/span&gt;
    &lt;span class="c1"&gt;// If c is accessed outside the if block, it will give an error.&lt;/span&gt;
    &lt;span class="kd"&gt;let&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;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;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// This line will give an error as we are trying to&lt;/span&gt;
  &lt;span class="c1"&gt;// access c outside its scope&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;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we are clear on scopes, let us see how JavaScript handles scopes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JavaScript follows lexical scoping.&lt;/strong&gt; This means that, when accessing a variable, if it is not found in the current scope/block, &lt;strong&gt;JavaScript will check the parent scope.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And if still not found, JavaScript will check the parent's parent scope. This way, that variable is searched &lt;strong&gt;all the way up to the scope hierarchy.&lt;/strong&gt; And if not found, JavaScript throws an error.&lt;/p&gt;

&lt;p&gt;In the case of the previous code snippet, when we are accessing variable &lt;code&gt;a&lt;/code&gt; in the &lt;code&gt;if block&lt;/code&gt;, it is not present there. So JavaScript goes all the way up to the global scope and gets the value of &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we have enough ground to learn closures.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is function bundled with its lexical scoping.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To put it in simple words, when a function is accessing variables from outside of its scope, the function and those variables form a closure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do they form a closure?&lt;/strong&gt; — Because, when that function is executed, even if the parent scope has finished executing, it will still need those variables. This typically happens when we are returning functions from functions.&lt;/p&gt;

&lt;p&gt;Let us look at an example to understand this better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;()&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;outerVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I was in outer scope and now I am in closure with innerFunction&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outerVariable&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;innerFunction&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;returnedFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;returnedFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us dissect what is happening here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;outerVariable&lt;/code&gt; is declared in &lt;code&gt;outerFunction&lt;/code&gt; 's scope.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;innerFunction&lt;/code&gt; uses &lt;code&gt;outerVariable&lt;/code&gt; and it is returned without executing.&lt;/li&gt;
&lt;li&gt;When line number 11 is executed, scope for &lt;code&gt;outerFunction&lt;/code&gt; is vanished.&lt;/li&gt;
&lt;li&gt;But when we execute &lt;code&gt;returnedFunction&lt;/code&gt;, it still prints value of &lt;code&gt;outerVariable&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;So what happened is: &lt;strong&gt;&lt;code&gt;innerFunction&lt;/code&gt; was returned with variables from the outer scope bundled together. So in other words, a Closure was returned.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;THAT'S IT!! That is all you need to know about closures. Of course, there are some other complicated examples. It will be easier for us to understand those now.&lt;/p&gt;

&lt;p&gt;One important caveat with closures is that &lt;strong&gt;closures hold references to variables from the parent scope. Not the values&lt;/strong&gt;. So functions with closures can change values and get updated values. This will be useful in other real examples listed below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-world applications of closures
&lt;/h2&gt;

&lt;p&gt;As I mentioned earlier, you might have used closures without realizing it. Following are some use cases I have encountered where closure was a better solution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.amitkhonde.com/blog/write-a-custom-debounce-function-in-javascript"&gt;Writing your own debounce function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.amitkhonde.com/blog/write-a-custom-throttle-function-in-javascript"&gt;Writing your own throttle function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Implementing private variables&lt;/li&gt;
&lt;li&gt;Maintaining states&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;This article was &lt;a href="https://www.amitkhonde.com/blog/what-is-a-closure-in-javascript"&gt;originally published here&lt;/a&gt;. For more such interesting reads, &lt;a href="https://www.amitkhonde.com/blog"&gt;visit my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Explain to me like I am 5: Debounce vs Throttle</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Sat, 05 Jun 2021 15:28:41 +0000</pubDate>
      <link>https://dev.to/amitkhonde/explain-to-me-like-i-am-5-debounce-vs-throttle-n7j</link>
      <guid>https://dev.to/amitkhonde/explain-to-me-like-i-am-5-debounce-vs-throttle-n7j</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mgtx6cvekqjvmqpr3cq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mgtx6cvekqjvmqpr3cq.jpeg" alt="Is debounce and throttle different"&gt;&lt;/a&gt;&lt;br&gt;
Debounce and Throttle are two of the most widely used concepts in web development today. Every popular JavaScript helper library today includes these two functions. Unfortunately, a lot of us are still confused about the actual difference between these two. I myself suffered this confusion until recently when I faced this problem in an interview. So I decided to write about debounce vs throttle in the simplest way where a 5-year-old can understand the difference.&lt;/p&gt;

&lt;p&gt;To understand the following explanations, imagine you are a 5-year-old who loves eating chocolates and have to ask your parents for it. Now let’s dive into how your mom and dad both handle this situation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Throttling
&lt;/h2&gt;

&lt;p&gt;We all know that eating chocolate is very unhealthy for our bodies. That is why your mom has set a rule where she will give you the chocolate only if a day has passed since you had your last chocolate. To put in simple words, whenever you ask your mom for chocolate, there are two possibilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you have had chocolate in the last 24 hours, mom will ignore your request.&lt;/li&gt;
&lt;li&gt;Else, you will get a chocolate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let us see how this fits in the programming world. If you call a throttled function, it will check if the desired amount of time has passed since the last execution. If yes, the function will be executed. Else it will return without executing.&lt;/p&gt;

&lt;p&gt;The following image shows a timeline of calling a throttled function. The scale represents time in seconds. Imagine you are calling the throttle function each second and your throttle time is 5 seconds. So throttled function will be executed in 5 seconds intervals.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4jt7aq05n9ntczb872g.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%2Ft4jt7aq05n9ntczb872g.png" alt="Throttle timeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Debounce
&lt;/h2&gt;

&lt;p&gt;Your dad, on the other hand, wants to teach you the value of patience. He believes that you understand the value of something only if you have the patience to wait for it. To achieve this, your dad has set a rule where whenever you ask for chocolate, he will give you the chocolate after exactly one day. But, if you ask for chocolate within that hour, he will start time from 0 again. To put in simple words, whenever you ask your dad for chocolate, there are two possibilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your dad will set a timer for 24 hours and will give you the chocolate at the end of it.&lt;/li&gt;
&lt;li&gt;If you ask again while this timer is going on, your dad will start the timer from 0 again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Similarly, if you call a debounced function in the programming world, your function will get executed in a certain amount of time. But if you call the function again within that time, the previous call is canceled and the function is executed after the desired time starting now. &lt;/p&gt;

&lt;p&gt;The following diagram shows a timeline of debounced calls. Each number on the scale represents a second and your desired debounce time is 5 seconds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3ugzywi9t7b2t0t9wd0.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%2Fa3ugzywi9t7b2t0t9wd0.png" alt="Debounce timeline"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;I hope you are now clear on the topic of debounce vs throttle. If you want to learn more about how you can write your own debounce and throttle, you can refer to these posts&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.amitkhonde.com/blog/write-a-custom-debounce-function-in-javascript" rel="noopener noreferrer"&gt;Write your own debounce function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.amitkhonde.com/blog/write-a-custom-throttle-function-in-javascript" rel="noopener noreferrer"&gt;Write your own throttle function&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Until then, Happy Coding!!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was &lt;a href="https://www.amitkhonde.com/blog/debounce-vs-throttle-explain-me-like-5" rel="noopener noreferrer"&gt;originally published here&lt;/a&gt;. For more such interesting reads, &lt;a href="https://www.amitkhonde.com/blog" rel="noopener noreferrer"&gt;visit my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Eval is evil - Why we should not use eval in JavaScript</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Fri, 04 Jun 2021 05:14:08 +0000</pubDate>
      <link>https://dev.to/amitkhonde/eval-is-evil-why-we-should-not-use-eval-in-javascript-1lbh</link>
      <guid>https://dev.to/amitkhonde/eval-is-evil-why-we-should-not-use-eval-in-javascript-1lbh</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Eval is evil.&lt;/em&gt; - Some frustrated JavaScript engineer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I recently read this quote on the internet and although it is funny, it got me thinking &lt;em&gt;why would someone say this?&lt;/em&gt; Thus this post. In this post, we will see why we should not use eval in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to eval
&lt;/h2&gt;

&lt;p&gt;To understand why to avoid eval, we first need to understand what eval is. We are not going deep into what eval is and why it exists. We will see its definition and a simple example. If you want to learn more about eval, here is a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval"&gt;great post on MDN&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The eval() function evaluates JavaScript code represented as a string.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Eval is a function in JavaScript that expects a string as a parameter and executes that string as code. If you pass an expression to it, it will execute that expression. If you pass multiple JavaScript statements to it, it will execute those statements. As simple as that. Let us check an example to understand more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2 + 2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// Output: 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why not use eval?
&lt;/h2&gt;

&lt;p&gt;After learning about the eval, you might be wondering why this post is against using eval? After all, it is such a powerful feature.&lt;/p&gt;

&lt;p&gt;But as we all know, &lt;strong&gt;Great power comes at a cost.&lt;/strong&gt; In eval’s case, the costs are performance, security, and difficulties in debugging. Let us look at them one by one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;p&gt;As we can see, the string passed to eval for execution is dynamic. So there is no way our JavaScript code knows about this string. That is why, when JavaScript wants to execute this string, it has to interpret/compile that string at runtime. This tends to be very expensive in terms of performance if the string is complex.&lt;/p&gt;

&lt;h3&gt;
  
  
  Security risks
&lt;/h3&gt;

&lt;p&gt;Usage of eval involves pretty high risks of running malicious code. Let us say you are accepting an expression from the user in an input box. And some mischievous user types in an infinite loop. This is going to be very dangerous especially if eval is used on the server-side.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difficulties in debugging
&lt;/h3&gt;

&lt;p&gt;Let us all admit that we have spent hours debugging the smallest bugs like missing semicolons. Imagine finding bugs that are caused by the dynamic code that JavaScript is trying to execute. If the eval is causing some side effects in your runtime environment, it gives us very little room for finding the issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Are there any other alternatives?
&lt;/h2&gt;

&lt;p&gt;Fortunately, there is an alternative with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function"&gt;window.Function&lt;/a&gt; which will almost do the same thing. But this is a simple alternative. There are things that are only possible with eval. But be sure to be very careful when using eval and only use it if it is an absolute necessity. You will probably find better alternatives on StackOverflow.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was &lt;a href="https://www.amitkhonde.com/blog/why-we-should-not-use-eval-in-javaScript"&gt;originally published here&lt;/a&gt;. For more such interesting reads, &lt;a href="https://www.amitkhonde.com/blog"&gt;visit my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>security</category>
    </item>
    <item>
      <title>JavaScript Interview: Write a custom reduce function</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Thu, 20 May 2021 14:42:32 +0000</pubDate>
      <link>https://dev.to/amitkhonde/javascript-interview-write-a-custom-reduce-function-2cil</link>
      <guid>https://dev.to/amitkhonde/javascript-interview-write-a-custom-reduce-function-2cil</guid>
      <description>&lt;h2&gt;
  
  
  What is this series about?
&lt;/h2&gt;

&lt;p&gt;Hello all! Welcome to the JavaScript interview questions series. In each post of this series, I will talk about the questions (specific to JavaScript) I faced in my recent interviews. This series will be helpful for you if you are preparing for JavaScript interviews or just started to deep dive into JavaScript and want to test your knowledge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Post 3: Write your own custom reduce function
&lt;/h2&gt;

&lt;p&gt;In today’s modern web development, a lot of us work on pretty modern tech stacks like React or Vue, or Angular. But in web development, a lot of projects today still function on legacy code and need active work. This is why, writing custom functions or polyfills for new JavaScript features is a popular interview question. Also, writing our own version of something that we use every day makes the concept easy and quick to grasp.&lt;/p&gt;

&lt;p&gt;In one of my interviews, the interviewer asked me to write a custom function for &lt;code&gt;Array.reduce()&lt;/code&gt;. Before diving further into this post, we must understand what &lt;code&gt;Array.reduce()&lt;/code&gt; does. Here is a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;great post on MDN blog&lt;/a&gt; on what reduce does.&lt;/p&gt;

&lt;p&gt;Now that we are all set, let us dive into writing our own reduce function. 💻 ⚔️&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;Write a custom function like &lt;code&gt;Array.reduce()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Existing Array.reduce() function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducer&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// expected output: 100&lt;/span&gt;


&lt;span class="c1"&gt;// Your code goes here&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;customReduce&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;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Write Code&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;customResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;customReduce&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="nx"&gt;reducer&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;customResult&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;Before diving into the solution, I highly suggest that you try to solve this problem on your own. Here is a hint: Think about what reducer expects and what does it return?&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;As always, we are going to start with the least and do the obvious stuff first. So we can see that our custom function needs to return the final value.&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;customReduce&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;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;value&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;value&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;Further, for each array element, our &lt;code&gt;customReducer&lt;/code&gt; function needs to call the &lt;code&gt;reducer&lt;/code&gt; function (passed in as a parameter). The reducer function expects following parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The return value from previous execution.&lt;/li&gt;
&lt;li&gt;Current array element&lt;/li&gt;
&lt;li&gt;Current index&lt;/li&gt;
&lt;li&gt;The array itself
&lt;/li&gt;
&lt;/ul&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;customReduce&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;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&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="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&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 you might be wondering what will happen to the &lt;code&gt;intialValue&lt;/code&gt;? Well, we want &lt;code&gt;intialValue&lt;/code&gt; to be the value passed to the first execution of the &lt;code&gt;reducer&lt;/code&gt; function. We can do that by assigning that &lt;code&gt;intialValue&lt;/code&gt; to &lt;code&gt;value&lt;/code&gt;. So the final version of the &lt;code&gt;customReduce&lt;/code&gt; function will look like this.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Edit:&lt;br&gt;
As rightfully pointed by &lt;a class="mentioned-user" href="https://dev.to/kennethlum"&gt;@kennethlum&lt;/a&gt;
 , if the &lt;code&gt;initialValue&lt;/code&gt; is not passed, we should consider first element of the array as the &lt;code&gt;initialValue&lt;/code&gt;.&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;function&lt;/span&gt; &lt;span class="nx"&gt;customReduce&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;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;initialValue&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&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;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&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="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Yay!! This looks like a working solution for now. I would love to know what approaches you can come up with for this problem. Do post your suggestions in the comments. And for more interesting questions like this, keep following this series. Until then, Happy Coding!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>career</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Interviews: Implement Memoization in JavaScript</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Fri, 14 May 2021 16:47:25 +0000</pubDate>
      <link>https://dev.to/amitkhonde/javascript-interviews-implement-memoization-in-javascript-eaf</link>
      <guid>https://dev.to/amitkhonde/javascript-interviews-implement-memoization-in-javascript-eaf</guid>
      <description>&lt;h1&gt;
  
  
  What is this series about?
&lt;/h1&gt;

&lt;p&gt;Hello all! Welcome to the JavaScript interview questions series. In each post of this series, I will talk about the questions (specific to JavaScript) I faced in my recent interviews. This series will be helpful for you if you are preparing for JavaScript interviews or just started to deep dive into JavaScript and want to test your knowledge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Post 2: Implement Memoization in JavaScript
&lt;/h2&gt;

&lt;p&gt;Imagine you do not have a memory inside your brain. And someone asks you a question like, &lt;em&gt;"What is 14353 * 34789?"&lt;/em&gt;. You do the calculation and give the answer. Because you do not have any memory, this question and its answer have been wiped out of your mind. Now again if someone asks you the same question, you will again do the calculation and give the answer. Doing this calculation is tedious and it used some of your energy. Isn't this frustrating?&lt;/p&gt;

&lt;p&gt;Now let us come to the real world. You have the memory. Someone asks &lt;em&gt;"What is 14353 * 34789?"&lt;/em&gt;. You do the calculation and give the answer. Now, this question and answer are stored inside your short-term memory. If again in few seconds, you are asked the same question, you will directly access the memory and give the answer without any calculation.&lt;/p&gt;

&lt;p&gt;This technique is known as Memoization. In computer science also, this technique is used to avoid heavy calculations. Now enough of imagination. Let us dive into the real interview question. 👨‍💻👨‍💻&lt;/p&gt;

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

&lt;p&gt;Write a function &lt;code&gt;memoize&lt;/code&gt; which will receive a function and return its memoized version. When the memoized function is called with the same parameters, again and again, it will just log the value with a message "Did not perform calculations. Here is your answer: ". If those parameters have never been passed, it will just print the answer.&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;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Write your code here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&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;num1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;memoizedMultiply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;memoizedMultiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: &lt;/span&gt;
&lt;span class="mi"&gt;90&lt;/span&gt;

&lt;span class="nx"&gt;memoizedMultiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: &lt;/span&gt;
&lt;span class="nx"&gt;Did&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;perform&lt;/span&gt; &lt;span class="nx"&gt;calculations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Here&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;your&lt;/span&gt; &lt;span class="nx"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;

&lt;span class="nx"&gt;memoizedMultiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Expected Output: &lt;/span&gt;
&lt;span class="mi"&gt;80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Before diving into the solution, I highly suggest that you try to solve this problem on your own. Here is a hint: Think Closures.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;As mentioned in the previous post, I always start with the basic stuff being asked in the question. The problem statement tells us that we have to return a function that will call the function that we want to memoize and print the result. Let us write that part first.&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;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great. The next thing problem asks us to memoize the result if we pass the same parameters to the memoized function. This seems like a good opportunity to use Closures. If you are unfamiliar with closures, please read about them &lt;a href="https://www.freecodecamp.org/news/lets-learn-javascript-closures-66feb44f6a44/"&gt;here&lt;/a&gt;. With the help of the closure, our returned function will have to the variables declared in its parent scope. Let us add the Closure now.&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;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&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;argumentsMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The idea we are trying to follow here is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the memoized function is called, we will store the arguments in the &lt;code&gt;argumentsMap&lt;/code&gt; as keys and store the result for that argument as its value.&lt;/li&gt;
&lt;li&gt;If the function is called for the same parameters, we will check if &lt;code&gt;argumentsMap&lt;/code&gt; has the parameters as key. If yes, will directly get the value and not perform any calculations.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The obvious question here is that how will we store the arguments as a key in &lt;code&gt;argumentsMap&lt;/code&gt;? For that, I have chosen an approach where I will apply &lt;code&gt;JSON.stringify&lt;/code&gt; on arguments and then store them as keys. You can come with a different approach for this which you might think is better. I Would love to see what you think about how this can be done. Please share your approaches in the comments.&lt;/p&gt;

&lt;p&gt;With that sorted out, the rest of the code is very simple. We will just add few checks and print the results. The final version of my answer looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&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;argumentsMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;argumentKey&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;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;argumentsMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;argumentKey&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Did not perform calculations. Here is your answer: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;argumentsMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;argumentKey&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;argumentsMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;argumentKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Yay!! This looks like a working solution for now. I would love to know what approaches you can come up with for this problem. Do post your suggestions in the comments. And for more interesting questions like this, keep following this series. Until then, Happy Coding!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>career</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Interviews: Create a deep copy of an object</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Fri, 30 Apr 2021 14:13:01 +0000</pubDate>
      <link>https://dev.to/amitkhonde/javascript-interviews-create-a-deep-copy-of-an-object-4pje</link>
      <guid>https://dev.to/amitkhonde/javascript-interviews-create-a-deep-copy-of-an-object-4pje</guid>
      <description>&lt;h1&gt;
  
  
  What is this series about?
&lt;/h1&gt;

&lt;p&gt;Hello all! Welcome to the JavaScript interview questions series. In each post of this series, I will talk about the questions (specific to JavaScript) I faced in my recent interviews. This series will be helpful for you if you are preparing for JavaScript interviews or just started to deep dive into JavaScript and want to test your knowledge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Post 1: Create a deep copy of an object
&lt;/h2&gt;

&lt;p&gt;Many of us who have worked on any fairly large side projects or have contributed to other side projects must have come across JavaScript helper libraries like Lodash.js, Underscore.js. These libraries provide us with helper functions for things that JavaScript does not provide built-in. One of those functions is copying objects in JavaScript. A lot of us know how to copy objects which only have one level of nesting by Object Destructing. But if your object contains multiple nested levels, there is no in-built way in JavaScript to copy that object.&lt;/p&gt;

&lt;p&gt;A lot of you might be wondering why this question is asked? If we have the helper library, why not just use that? And you are absolutely right. We should use that and we do use them indeed. But writing such a core function is going to test how you grasp and apply things fundamentally. As we will see later in this article, this question tests how you apply the knowledge that you already have. So let us get into some problem-solving mode 👨‍💻⚔️.&lt;/p&gt;

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

&lt;p&gt;Write a function that will take an object as an argument and returns a deep copy of that 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="c1"&gt;// Signature&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;copyObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Usage&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source&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;10&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;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;d&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;copyObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&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;Before diving into the solution, I highly suggest that you try to solve this problem on your own. Here are some hints:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Forget about the nesting part. First, just try to copy each key and value.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Now think about how you can identify if a value is an object itself and what to do with it.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;When I am solving any problem, I always like to write the obvious things first. Those things can be found by reading the problem statement carefully. The very obvious thing that the question asks is to return an object. So let us write that down first.&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;copyObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&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;target&lt;/span&gt; &lt;span class="o"&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;target&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, the problem asks us for a deep copy of the object. But before directly jumping to deep copy, let us first write a simple solution for copying each key value for a single level of nesting. So what do we need for that?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We need all the keys from the source object&lt;/li&gt;
&lt;li&gt;Add all those keys one by one in the target object.
&lt;/li&gt;
&lt;/ul&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;copyObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&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;target&lt;/span&gt; &lt;span class="o"&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;keys&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;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&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;target&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;Great! So we have solved the problem for the simplest use case. Now let us think about nesting. So first of all, how will we know if the value corresponding to the current key is an object itself? By using &lt;code&gt;typeof&lt;/code&gt; operator. And when we know that the current value is an object, how can we get its copy? --&amp;gt; &lt;strong&gt;By using the function that we are writing.&lt;/strong&gt; I know this might sound confusing right now. This technique is known as Recursion (You can learn more about recursion &lt;a href="https://www.geeksforgeeks.org/recursion/"&gt;here&lt;/a&gt;). Let us just write the code and you will understand. So the final solution to the problem will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;copyObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&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;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

    &lt;span class="c1"&gt;// Getting source object keys&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;keys&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;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Checking if current value is an object&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&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;object&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="c1"&gt;// Calling our function recursively for current value&lt;/span&gt;
            &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;copyObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Directly assigning the value&lt;/span&gt;
            &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;target&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;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Yay!! This looks like a working solution now. But there are still minor problems with this solution like handling array and function values in objects. I would encourage you to write the code that will handle these conditions and post it in the comments. And for more interesting questions like this, keep following this series. Until then, Happy Coding!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>career</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Internals - Ignition and TurboFan</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Tue, 13 Apr 2021 15:38:56 +0000</pubDate>
      <link>https://dev.to/amitkhonde/javascript-internals-ignition-and-turbofan-48ef</link>
      <guid>https://dev.to/amitkhonde/javascript-internals-ignition-and-turbofan-48ef</guid>
      <description>&lt;h1&gt;
  
  
  What is this series about
&lt;/h1&gt;

&lt;p&gt;Understanding the things or tools that you use in your daily work is a very crucial part of doing things effectively. As a frontend engineer JavaScript is the tool that we use on an almost daily basis. So it is non-trivial that we understand some internals of JavaScript to do our tasks more effectively.&lt;/p&gt;

&lt;p&gt;With this in mind, I am excited to create JavaScript Internals as a series of posts where I will be writing about the internal workings of the V8 engine and how we can write code that is compiler friendly and in turn performant.&lt;/p&gt;

&lt;h1&gt;
  
  
  Post 4: Ignition and TurboFan
&lt;/h1&gt;

&lt;p&gt;Let me start this post by asking you this question: &lt;em&gt;When was the last time you knowingly type-checked variables in your JavaScript Application?&lt;/em&gt; OR &lt;em&gt;When was the last time you thought about adding or removing a key to/from a JavaScript object at runtime?&lt;/em&gt; Most of us do not remember, right? This is because of the freedom that JavaScript gives us to do almost anything in the language. Some weird examples of this freedom are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Passing any arguments to the functions&lt;/li&gt;
&lt;li&gt;Checking any bizarre combinations of values for equality&lt;/li&gt;
&lt;li&gt;Assigning different types of values to a single variable in the same runtime&lt;/li&gt;
&lt;li&gt;And many more...
The list goes on. I am sure all of us can come up with many other "uses" of this freedom. But let us get one thing straight. Nothing in this world is for free. In our case, it comes with poor performance problems.
Before diving into the details of how JavaScript Engines handle this, let us first understand why this results in poor performance. So as all of us know in some capacity or other, &lt;strong&gt;Every code has to be converted to bytecode for execution.&lt;/strong&gt; So when a piece of code in a statically typed language (languages that have in-built types. For example C++) is getting compiled, the compiler already knows the type of each and every variable being used and hence can make fancy optimizations about the sizes, memory locations, etc. But in the case of JavaScript, the engine does not know which value is going to be inserted in the variable and hence can not make any optimizations based on variables or functions. A demonstration of this can be seen in the following code:
```javascript
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;var obj = {&lt;br&gt;
    x: 20&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;console.log("Adding 1 to x ");&lt;br&gt;
obj.x += 1;&lt;br&gt;
console.log("Incremented value of x", obj.x);&lt;/p&gt;

&lt;p&gt;obj.x = "Hello";&lt;br&gt;
console.log("Appending 'World' to x");&lt;br&gt;
obj.x += "World";&lt;br&gt;
console.log("New value of x", obj.x);&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;As we can see, due to JavaScript's freedom, we can change the type of x at the runtime. Due to this, JavaScript will always have to check what type of value is stored in `obj.x` to perform any kind of operation on it. This results in poor performance. To overcome this problem, V8 has come up with a new **Interpreter named Ignition** and an **Optimizing Compiler named TurboFan.** Let us see what they do and how they help our code run faster.

## What is Ignition
Ignition is an interpreter that JavaScript uses to interpret our code and start running it. Every JavaScript code goes through the following process to actually run it on a browser or machine.
* JavaScript code is fed to the parser.
* The parser parses the code and creates an Abstract Syntax Tree (AST).
* This AST is then fed to the Ignition and bytecode is produced.
* This bytecode is fed to the machine and our code starts running.

Don't worry if some of the terms like AST or Parser are foreign to you right now. We will cover them in some other post. We are right now just interested in the Ignition interpreter. So when the bytecode is running on an actual machine, the Ignition maintains some data about the running code. This data is in various forms and contains different metrics about the code. One of those metrics is Hot Functions. Hot functions are the functions in our code that are used many times. A simple example of a hot function can be some calculation we perform on a value to display it on the page. On every change of this value, the calculation function is executed again and again the page is updated. The Ignition will collect the data about this function like the arguments being passed, what is their type, what is being returned and its type, etc. 

## How TurboFan comes into the picture
After identifying the hot functions in the code, Ignition will send that data to the TurboFan for optimizations. TurboFan will take this code and start running some magical optimizations because it already has the assumptions data from the Ignition. It will then replace the original bytecode with this new optimized bytecode and this process keeps on repeating for the lifetime of our program.
To understand this better, let us take one example and go through the process. Consider the following code:
```javascript


function add(x, y) {
    return x + y;
}

add(1, 2);
add(12, 42);
add(17, 25);
add(451, 342);
add(8, 45);


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

&lt;/div&gt;

&lt;p&gt;When this code is converted to bytecode and ran, Ignition will perform the following lengthy process for the addition process:&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%2F7fcyizadntr04mokaihp.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%2F7fcyizadntr04mokaihp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Hectic right?? Now when we call this function many times with integer arguments, Ignition will classify this as a hot function and send it to the TurboFan with the collected information. TurboFan will optimize this function for integers, produce the bytecode and replace it in the original bytecode. Now when the next time &lt;code&gt;add(21, 45)&lt;/code&gt; function is called, all these lengthy steps will be omitted and the result will be obtained faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fallback mechanism
&lt;/h2&gt;

&lt;p&gt;But wait. What if we call our add function with string arguments? Well, to handle these cases, TurboFan will check the types of arguments being passed. If the types are different from numbers, it will fall back to the original bytecode which was generated by the Ignition and again this lengthy process will be followed. This process is known as Deoptimization. This new information will also be collected and if we call add function with string arguments too many times, Ignition will consider it as a hot function and send it to TurboFan with relevant information collected. The TurboFan will optimize add function for the string parameters also and the next time add function is called, an optimized bytecode will be run thus improving the performance.&lt;/p&gt;

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

&lt;p&gt;Well, this is why it is advised to treat JavaScript variables as statically typed variables so that it makes our code performant. This is not only true in the case of primitive types but also followed in the case of Objects. To understand more in-depth how an object's type is maintained, read the previous post in this series about the &lt;a href="https://dev.to/amitkhonde/javascript-internals-object-shapes-23i3"&gt;Object Shapes&lt;/a&gt;. Now there is a lot more to Ignition and TurboFan apart from the type checking. If this article interests you, do check out the references where the speakers have covered this in-depth. Till then, Happy Coding!!&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Franziska Hinkelmann: JavaScript engines - how do they even?: 
&lt;a href="https://www.youtube.com/watch?v=p-iiEDtpy6I" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=p-iiEDtpy6I&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The JavaScript engine and hot functions: A beginner’s exploration: &lt;a href="https://medium.com/@harumhelmy/the-javascript-engine-and-hot-functions-a-beginners-exploration-part-2-f4e351631229" rel="noopener noreferrer"&gt;https://medium.com/@harumhelmy/the-javascript-engine-and-hot-functions-a-beginners-exploration-part-2-f4e351631229&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Internals - Object Shapes</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Tue, 06 Apr 2021 16:27:31 +0000</pubDate>
      <link>https://dev.to/amitkhonde/javascript-internals-object-shapes-23i3</link>
      <guid>https://dev.to/amitkhonde/javascript-internals-object-shapes-23i3</guid>
      <description>&lt;h1&gt;
  
  
  What is this series about
&lt;/h1&gt;

&lt;p&gt;Understanding the things or tools that you use in your daily work is a very crucial part of doing things effectively. As a frontend engineer JavaScript is the tool that we use on an almost daily basis. So it is non-trivial that we understand some internals of JavaScript to do our tasks more effectively.&lt;/p&gt;

&lt;p&gt;With this in mind, I am excited to create JavaScript Internals as a series of posts where I will be writing about the internal workings of the V8 engine and how we can write code that is compiler friendly and in turn performant.&lt;/p&gt;

&lt;h1&gt;
  
  
  Post 3: Object Shapes
&lt;/h1&gt;

&lt;p&gt;In today's part of the JavaScript Internals series, we going to talk about the very fundamental feature of the language -- Objects. We are going to see how those are handled in the JavaScript engines. And later we will also see some of the best practices we can use to make our web apps performant. So, MDN defines JavaScript object as&lt;br&gt;
"An object is a collection of properties, and a property is an association between a name (or key) and a value."&lt;br&gt;
Wow. Such a simple and easy definition. Well, this simple definition gives us the power to do a lot of crazy things that we can not do in most other programming languages. Things like adding/removing properties from/to objects, manipulate this binding, and not needing a class to initiate the object, etc are some of the things that JavaScript can give us just because of that simple definition (Well, also because ECMAScript standards have defined it like that).&lt;/p&gt;

&lt;p&gt;Now, just for a minute, imagine that you are a JavaScript engine. You get a code to execute, and it contains thousands of objects. Now we know how objects are stored in the variables. The reference of the object is stored. And when we are trying to access any property of the object, (let us say 2nd property) we will go 1 offset further from the object's memory reference. And you (the JavaScript engine) do it every time the code is trying to access the property. From the sound of this, we obviously know that this will result in a terrible performance of the code.&lt;/p&gt;

&lt;p&gt;But, let us imagine that somehow magically, you know that 500 of those objects are going to be of the same structure. Well, that makes it a lot better right? You (still the JavaScript engine) can keep track of the objects that belong to that structure and calculate all the offset sizes of all the properties in advance. So when code tries to access a property of an object that belongs to these 500 objects, you can just directly add the offset size to the memory reference of that address and Eureka! We have the value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is exactly how the JavaScript engine gets us the value of some key. It keeps track of all the structures and when we are accessing any value, it will check the structure of the object and get the value from that offset. And those structures are known as Shapes.&lt;/strong&gt; Some people call it Hidden Classes, some call it classes but I like the name Shape because it defines that structure at its bare minimum level. In the end, it is actually just a Shape for an object.&lt;/p&gt;
&lt;h2&gt;
  
  
  How JavaScript Engines utilizes these shapes and how are they created?
&lt;/h2&gt;

&lt;p&gt;Now comes the interesting part. Now we will go through the journey of an Object. Earlier we were pretending to be the JavaScript Engine, now we will be an Object. So now imagine you are an object and LET US GO!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aNuTlnNE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s32g9suvkmsf2f85dpeb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aNuTlnNE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s32g9suvkmsf2f85dpeb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Ok. So you (the object) are created for the first time. You do not have any properties to you. At this time, an empty shape is maintained for you. You might ask why to maintain an empty shape? But suppose someone is trying to access a property on an empty object. The engine can optimize the performance just by checking the shape and return &lt;code&gt;undefined&lt;/code&gt; from there itself. Now let us proceed and assign some properties to you (the object).&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7PZP_DXi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekyl4vdbnscpz2gv6ukb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7PZP_DXi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekyl4vdbnscpz2gv6ukb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Now we assign a property &lt;code&gt;name&lt;/code&gt; to you (nice name BTW). Now the shape will change and a field will be added to that shape. And that property will be pointing to a property descriptor. Let us not worry about what is property descriptor now. Just understand that it will store the offset of the property from starting address of the object. Now let us add one more property to you (the object) and see what happens.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---vWY_PhR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/es10vdkil9uyp948307b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---vWY_PhR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/es10vdkil9uyp948307b.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
So we have assigned a new property &lt;code&gt;age&lt;/code&gt; and we can clearly see that a new shape is created (BTW, previous shapes are also stored) and if we think about following this pattern, it will be a quite large number of shapes and will quickly be very hard to maintain. We will fall into the same performance pitfall that we were trying to run from. &lt;br&gt;
To avoid this problem, the JavaScript Engine maintains a chain between all of these evolving shapes. With the help of this chain, we do not have to repeat any old shape properties because we can easily go back in the chain and check for properties. So for you (the object), the shape chain will finally look something like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P_7scGAz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rw0zs909yhl50hotgz2y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P_7scGAz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rw0zs909yhl50hotgz2y.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Sigh! That was quite a knowledge journey. But there is just one little concept remaining. Shape Trees. Let us consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&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;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CoolName&lt;/span&gt;&lt;span class="dl"&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CoolerName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&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;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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Heap&lt;/span&gt;&lt;span class="dl"&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;job&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Own Properties&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, obj and obj1 have all properties common and one property different. How do you think shapes are maintained for these two? Well, the diagram will look exactly like the above diagram but when the last two lines are executed, obj, obj1 will have a branch (like a tree branch) from the last shape which will have different properties. This tree structure of shapes is helpful for identifying a common property repeatedly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why was learning all of this necessary?
&lt;/h2&gt;

&lt;p&gt;Well, to answer this question, I will list down some best practices for objects and these practices are derived from the knowledge of shapes. So let us look at those points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use Factory functions when defining objects. This will make sure all of those objects have the same shape.&lt;/li&gt;
&lt;li&gt;Try avoiding the Object constructor.&lt;/li&gt;
&lt;li&gt;Sometimes even the order of keys matters (In earlier or some JavaScript engines).&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;As part of the conclusion, I have a little exploration exercise for you guys. As we have heard, Arrays are also Objects in JavaScript. How do you think shapes are handled for arrays? Well, comment down below with what you think about it or this post, or anything related to JavaScript. Thanks for reading. Till then, Happy Coding!!&lt;/p&gt;

&lt;h2&gt;
  
  
  References:
&lt;/h2&gt;

&lt;p&gt;JavaScript Engines: The Good Parts™ - Mathias Bynens &amp;amp; Benedikt Meurer - JSConf EU 2018 --&lt;a href="https://www.youtube.com/watch?v=5nmpokoRaZI"&gt;https://www.youtube.com/watch?v=5nmpokoRaZI&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>performance</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Internals - Garbage Collection</title>
      <dc:creator>Amit Khonde</dc:creator>
      <pubDate>Wed, 31 Mar 2021 17:19:32 +0000</pubDate>
      <link>https://dev.to/amitkhonde/javascript-internals-garbage-collection-27lh</link>
      <guid>https://dev.to/amitkhonde/javascript-internals-garbage-collection-27lh</guid>
      <description>&lt;h1&gt;
  
  
  What is this series about
&lt;/h1&gt;

&lt;p&gt;Understanding the things or tools that you use in your daily work is a very crucial part of doing things effectively. As a frontend engineer JavaScript is the tool that we use on an almost daily basis. So it is non-trivial that we understand some internals of JavaScript to do our tasks more effectively.&lt;/p&gt;

&lt;p&gt;With this in mind, I am excited to create JavaScript Internals as a series of posts where I will be writing about the internal workings of the V8 engine and how we can write code that is compiler friendly and in turn performant.&lt;/p&gt;

&lt;h1&gt;
  
  
  Post 2: Garbage Collection
&lt;/h1&gt;

&lt;p&gt;Imagine you arrive at a restaurant. There are a hundred tables at the restaurant. Out of 100, 95 tables are still occupied and at the remaining 5 tables, people have just left and their plates are still on the tables. Now for you to sit, restaurant staff will clear out one of those five tables or all of them and you would sit and enjoy your meal.&lt;/p&gt;

&lt;p&gt;Did you see what happened here? If we are to make analogies to the JavaScript environment, you are a new object, 100 tables are the available memory, 95 occupied tables are 95 active objects and 5 empty tables with dirty dishes are the dead objects that your application does not need anymore. Now, the restaurant staff is the JavaScript engine, who will clear those 5 dead objects and make the space for the new objects. THAT’S IT. This process is known as garbage collection. Now there are various algorithms to perform garbage collection optimally, one of which we will learn further in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Learning about Garbage Collection Is Important?
&lt;/h2&gt;

&lt;p&gt;Well, there is this cliche answer again: To improve your application’s performance. I know you all knew this answer coming. So let me give you one small example. Suppose you accidentally assign one global object which is very large in size. Now you are not going to use that object ever again in the application life cycle. But it will hold space in the memory as it is a global object and V8 will assume that you are going to need it. So my point is, after learning the internals, you will be armed with the knowledge of how you can write JavaScript which will leave a lesser memory footprint.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Basics
&lt;/h2&gt;

&lt;p&gt;Cool. So now that we are determined and not bored by the above examples, let us get some basics out of the way. First things first: V8 manages garbage collection. Not the JavaScript. What this implies is that there is no specification to follow on how to implement this piece. So every JavaScript engine implements its own version which developers think is the best approach.&lt;br&gt;
Another thing about V8 is that it uses the generational algorithm for garbage collection. This means it divides data into Younger Generation and Older Generation. We will talk about these generations in detail in further sections. &lt;/p&gt;

&lt;p&gt;You might ask why V8 uses a generational algorithm? Well, the V8 team believes in something known as the generational hypothesis. Which states: All objects die shortly after creation. They have found this hypothesis by observing multiple applications and their memory footprints. This is the reason they use the generational algorithm and run garbage collection cycles mostly on younger generation memory. Now let us talk about the younger and older generation and see how garbage is collected in both of them. &lt;/p&gt;

&lt;h2&gt;
  
  
  Young Generation and Old Generation
&lt;/h2&gt;

&lt;p&gt;Now, we will understand the whole process step by step and with help of some images.&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%2F34y5yazx69f286njwkjf.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%2F34y5yazx69f286njwkjf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
So as we can see, the whole memory in V8 is divided into 2 parts. Young generation and Old generation. The young generation in turn is divided into two parts. Don’t worry about that right now. We will see why it is being done in further steps.&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%2Flquy8fwdeyaflky3xhq8.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%2Flquy8fwdeyaflky3xhq8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Now when we start declaring objects and start using them, V8 will keep filling up the first part of the young generation.&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%2Fzjshiw1gg5gne2d6xm2j.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%2Fzjshiw1gg5gne2d6xm2j.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
When the first part of the young generation is completely filled, V8 will mark the objects dead or alive and the alive objects will be moved to the second part of the young generation. &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%2Fj5xl0givt5clm8qpf1vk.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%2Fj5xl0givt5clm8qpf1vk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Now all the new objects will be placed in the second part of the young generation until that is filled.&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%2Fox3gawp4hh8vfqiord71.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%2Fox3gawp4hh8vfqiord71.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Now comes the interesting part. Once the second part of the young generation is also completely filled, V8 will again mark the objects dead or alive and the alive objects will now be moved to Old Generation because if the object is still surviving, it is like to stay alive for a long time. After this, the second part is cleared and the same process from step 1 to step 5 is followed for new objects. &lt;/p&gt;

&lt;h2&gt;
  
  
  Mark And Sweep in Old Generation
&lt;/h2&gt;

&lt;p&gt;Now while V8 is doing this process, at some point in time, there will be no enough space for a new object in the old generation, a full mark, and sweep garbage collection algorithm will come into the picture. &lt;br&gt;
Mark and sweep is a two-step process. The first part mark is where V8 will try to reach out to every accessible object from the call stack and global scope. All reachable objects are marked as alive and rest are marked as dead. &lt;/p&gt;

&lt;p&gt;Now we have to understand that Old generation is quite large and the object sizes in it are also very large. That is why V8 can just remove dead objects as it does in the young generation because it will be a very heavy computation. Due to this limitation, the sweep process is done in various cycles where V8 will create some parallel threads and keep on sweeping memory in parts. &lt;/p&gt;

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

&lt;p&gt;Sigh!! That was quite a ride. Now there is a lot of other detailing and tricks on how V8 manages garbage collection which we can not cover in a single post. I would suggest you go through the references section of this article and definitely check out the links if those excite you. Till then, Happy Coding!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>performance</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
