<?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: Dhilip kumar</title>
    <description>The latest articles on DEV Community by Dhilip kumar (@dhilipkmr).</description>
    <link>https://dev.to/dhilipkmr</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%2F137232%2F9b12e443-3a63-4a2d-9db5-98cfd290b548.jpeg</url>
      <title>DEV Community: Dhilip kumar</title>
      <link>https://dev.to/dhilipkmr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhilipkmr"/>
    <language>en</language>
    <item>
      <title>The Spooky tracker</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Sat, 07 Aug 2021 13:37:20 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/the-spooky-tracker-2e8m</link>
      <guid>https://dev.to/dhilipkmr/the-spooky-tracker-2e8m</guid>
      <description>&lt;h1&gt;
  
  
  The Spooky tracker
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EYzN4zfR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fl68u1doxiikri7lw52z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EYzN4zfR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fl68u1doxiikri7lw52z.png" alt="Screenshot 2021-08-07 at 6.28.08 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey there!&lt;/p&gt;

&lt;p&gt;Have you ever wondered, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Damn how does this website know what did I shop/search/view?" &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;yeah you are not alone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cookies
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;"An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to the user's web browser. The browser may store it and send it back with later requests to the same server."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The HTTP calls are &lt;strong&gt;stateless&lt;/strong&gt;. Say if you want to inform who the logged in user is to the server, the cookie is the preferred way to go which gets set and sent in every request. And this is just one application for which cookies were created but they have been used for more than just retaining the logged in user's information. &lt;/p&gt;

&lt;p&gt;In the above case, the cookie are set by the same domain in which the user is logged in, and these are called as &lt;code&gt;1st party cookies&lt;/code&gt;.These are &lt;strong&gt;essential&lt;/strong&gt; cookies for the website to function smoothly.&lt;/p&gt;

&lt;h4&gt;
  
  
  More  about cookies:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Cookies are sent along with every request from the domain in the request header once set.&lt;/li&gt;
&lt;li&gt;We can even have a cookie which can't be read by javascript but gets attached to every request &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies"&gt;httponly flag&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Each cookie can have its own expiry. (Expiry on client)&lt;/li&gt;
&lt;li&gt;It is possible to restrict the cookie sent to Cross Origin with the help of &lt;code&gt;same-site&lt;/code&gt; property (more about this later)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Enough, tell me how do they track already!
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Enter 3rd party cookies:
&lt;/h4&gt;

&lt;p&gt;Third party cookies are cookies that are not set by the domain that you are in, but by another domain. And these third party cookies are responsible for providing us with the &lt;strong&gt;"PERSONALISED"&lt;/strong&gt; content aka &lt;strong&gt;"the spooky how does it know what I like cookie"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's go over a scenario:&lt;/p&gt;

&lt;p&gt;Say you are searching for answer for "how to centre a div" in &lt;code&gt;www.stackoverflow.com&lt;/code&gt;,  you'll notice that the other 3rd party domains such as &lt;code&gt;facebook.com&lt;/code&gt;, &lt;code&gt;yahoo.com&lt;/code&gt;, &lt;code&gt;youtube.com&lt;/code&gt;, &lt;code&gt;linkedin.com&lt;/code&gt; also bakes their cookie into the browser while you are still in stackoverflow. This could be as simple as stackoverflow trying to provide the user with the ability to sign the user in with the help of a small banner with embedded login buttons provided by these other domains.&lt;/p&gt;

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

&lt;p&gt;Or it might want to show some ads from these domains etc.&lt;/p&gt;

&lt;p&gt;Now, when you are done looking for your answers and you move on and fix the issue with CSS. Now, these other third party domains will have knowledge that your css skill is not so great.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/1kI8UsCjEwfiXc3TYy/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/1kI8UsCjEwfiXc3TYy/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just kidding, but they know that you are some one who might be interested in CSS.&lt;/p&gt;

&lt;p&gt;Once you are done with your work and want to surf a bit on facebook to check what your friends are upto / memes. Now you will log yourself into facebook and now when you do so, the cookie set by facebook while you were in stackoverflow will also gets attached to the request. So the fb server now says, "okay you were the one who was searching for centering the div, let me do you a favour by pitching you some of the good books/ udemy courses to read more about CSS" and voila there you have the ads, the TARGETED, PERSONAL ads in the feed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/oKzYzmKjlPgEU/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/oKzYzmKjlPgEU/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same scene will apply to multiple other websites that uses google ads, as the cookie is already set in stackoverflow for google.com, when you go to any website which uses google ads, these cookie will automatically gets shared with google.com requests and when the advertisement banner loads it would be totally targeting your interests to gain the click.&lt;/p&gt;

&lt;h4&gt;
  
  
  You don't want this to happen?
&lt;/h4&gt;

&lt;p&gt;Then please don't be in a hurry to blindly click on this "Agree" button on the annoying banner on most websites.&lt;/p&gt;

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

&lt;p&gt;GDPR: &lt;br&gt;
"The General Data Protection Regulation is a regulation in EU law on data protection and privacy in the European Union (EU) and the European Economic Area that aim to enhance individuals' control and rights over their personal data." - Wikipedia&lt;/p&gt;

&lt;p&gt;With the introduction of GDPR, the websites had a mandate to show the users why each cookies are needed to be set and what purpose do they serve and also provides the user with an option to opt out of it if you don't need. So you might be tilted towards thinking, &lt;/p&gt;

&lt;p&gt;"let me &lt;a href="https://www.cookiesandyou.com/disable-cookies/windows/chrome/"&gt;disable third party cookies&lt;/a&gt; in the website altogether for all websites and then I should be out of this tracking hell!"&lt;/p&gt;

&lt;p&gt;Sure you can do that but still... you can be tracked. Find out how in my next blog. &lt;br&gt;
Signing off for now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/hRCtKr97ErR2mgx4wA/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/hRCtKr97ErR2mgx4wA/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cookies</category>
      <category>tracking</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Software Engineer 2 UI Interview at Microsoft</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Sat, 17 Oct 2020 07:55:08 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/software-engineer-2-ui-interview-at-microsoft-1i0b</link>
      <guid>https://dev.to/dhilipkmr/software-engineer-2-ui-interview-at-microsoft-1i0b</guid>
      <description>&lt;p&gt;Hello there,&lt;/p&gt;

&lt;p&gt;For those who don't know me, do check out my &lt;a href="https://www.dhilipkmr.dev/" rel="noopener noreferrer"&gt;website&lt;/a&gt; and my other &lt;a href="http://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I recently accepted Microsoft's offer for Software Engineer II at IDC, Bangalore. I realized a blog about this might help a few :)&lt;/p&gt;

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

&lt;p&gt;In this post, I'll take you through my preparation, strategies, interview rounds, and things to look out for in each round. This will be in a Q&amp;amp;A format.&lt;/p&gt;

&lt;p&gt;Disclaimer:&lt;br&gt;
The following incidents are completely based on my view and what I have observed from my experience and it might vary from one individual to the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  Firstly, why do I have to write this in a blog?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When I got called for the interviews, I was searching all over the Internet to find out how interviews are done for the Frontend domain in Microsoft. I found very less content. And I had to go for the interview without any knowledge about rounds. So I thought of letting others know more about the rounds so that they are prepared.&lt;/li&gt;
&lt;li&gt;When I posted my Job switch news on LinkedIn, surprisingly I got numerous chat requests where people asked me about my preparation and my interview experience.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How did I apply?
&lt;/h3&gt;

&lt;p&gt;I had my profile listed in &lt;a href="https://www.instahyre.com" rel="noopener noreferrer"&gt;Instahyre&lt;/a&gt;, a famous job search portal in India, with the &lt;code&gt;Actively looking for opportunities&lt;/code&gt; option enabled. One evening, I got a call from a person (working in a third-party headhunting firm on behalf of Microsoft) asking if I am interested in the role. And it began...&lt;/p&gt;

&lt;h3&gt;
  
  
  How did I Prepare?
&lt;/h3&gt;

&lt;p&gt;I was attending a few other interviews prior to the above interview call. So when I got the call I was almost in a good position to attend the interview. As it was for the Frontend Engineer role I brushed up on JS basics, Web Performances, my current projects, and Leetcode. I did about 240+ Leetcode Questions by the time I gave the Interview. (It might just be 0 for someone :P). I highly advise anyone attending FE interview at Microsoft or any top MNCs(Amazon, Google,...) and many other top Startups(Rubrik, Flipkart,...) to have enough knowledge on Data Structures and Algorithm. There is no escaping from DS &amp;amp; Algo as it is considered heavily to rate your problem-solving skills. Attaching my &lt;a href="https://leetcode.com/dhilipkmr/" rel="noopener noreferrer"&gt;leetcode profile&lt;/a&gt; here for reference. Feel free to follow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources for Frontend?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Javascript from W3 Schools, &lt;a href="https://www.youtube.com/user/techSithTube" rel="noopener noreferrer"&gt;techsith tutorials&lt;/a&gt;, You Don't Know JS&lt;/li&gt;
&lt;li&gt;React from &lt;a href="https://www.udemy.com/course/react-the-complete-guide-incl-redux/" rel="noopener noreferrer"&gt;udemy&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developers.google.com/web/fundamentals" rel="noopener noreferrer"&gt;Web Fundamentals &amp;amp; performances&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Latest technology updates from Twitter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Do I get to know which team I am being hired before giving the interview?
&lt;/h3&gt;

&lt;p&gt;If you are part of a massive Interview Drive, you will have to wait until you meet your potential Hiring Manager(sometimes even the one taking your Managerial Round won't be your actual HM). However, you might get to know the product in which you will be working on before the interview(Azure, Office365, etc).&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I know which Level I am being hired for?
&lt;/h3&gt;

&lt;p&gt;From what I have observed and read about, Microsoft does not assign you a Level based on your years of experience. I have seen a lot of posts in blind, leetcode, etc where 8YOE are given L61 and 4YOE are given L62. So, it all boils down to &lt;strong&gt;how well you performed in your interview.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Okay enough, tell me about the rounds already!
&lt;/h3&gt;

&lt;p&gt;Due to COVID-19, all interviews happened virtually through &lt;code&gt;Microsoft Teams&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Round 1 (Machine Coding): (2 hours)
&lt;/h3&gt;

&lt;p&gt;As a Frontend Engineer, I was expected to start with a machine coding round where I was given a problem statement which I have to complete in 2 hours. If you have any doubts regarding the problem you can ask the interviewer. You might be given a zip containing initial boilerplate.&lt;/p&gt;

&lt;p&gt;Example Questions:&lt;/p&gt;

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

 - Design an Email Client like MS Outlook.
 - Create a chat interface like MS teams.
 - Create a Notification interface like MS teams.



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

&lt;/div&gt;

&lt;p&gt;Things to look out for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't jump to writing answers unless you understand the question thoroughly.&lt;/li&gt;
&lt;li&gt;There could be few jargons which could be part of your question, make sure you ask clarifying questions and don't assume anything :)&lt;/li&gt;
&lt;li&gt;Write Semantic HTML with proper tagging (Don't make everything a &lt;code&gt;div&lt;/code&gt;)&lt;strong&gt;!!! Important&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Know the tradeoffs, if you have to dynamically create a complex DOM tree, using JS APIs like &lt;code&gt;document.createElement()&lt;/code&gt; then it would consume a hell lot of time. So think if that suits you or you should go with &lt;code&gt;innerHTML&lt;/code&gt; approach.&lt;/li&gt;
&lt;li&gt;Understand &lt;code&gt;flexbox&lt;/code&gt; or &lt;code&gt;grid&lt;/code&gt; as they will come in handy in creating a responsive layout.&lt;/li&gt;
&lt;li&gt;Try using the latest ES6, ES2020 concepts, it is a platform to show that you are aware of the latest updates.&lt;/li&gt;
&lt;li&gt;Incrementally build your application and make sure to submit the working code :P&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Round 2 (Javascript): (1 hour)
&lt;/h3&gt;

&lt;p&gt;Here I was evaluated on my Javascript knowledge.&lt;/p&gt;

&lt;p&gt;For the first 10 minutes, from the code that I wrote earlier in my machine coding round, I was asked to discuss the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why did I choose a particular approach?&lt;/li&gt;
&lt;li&gt;What other alternatives are there?&lt;/li&gt;
&lt;li&gt;What are the tradeoffs I chose to complete the problem at a specified time?&lt;/li&gt;
&lt;li&gt;If I were to get more time, what would I do better?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Review your code and prepare for these questions before you enter the next round.&lt;/p&gt;

&lt;p&gt;After this, it was full-on javascript questions, where I was asked to write a polyfill for some js APIs introduced in ES6 or ES2020. For me, it was to write a &lt;code&gt;Promise polyfill&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Adding to the above question I was asked to implement the following.&lt;/p&gt;

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

Promise based memoization with a given cache size behaving
as an LRU cache with an expiry time and auto cache burst



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

&lt;/div&gt;

&lt;p&gt;Here I was grilled on my JS knowledge on async, promises, higher-order components, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Round 3 (Design / HLD + LLD /): (1 hour)
&lt;/h3&gt;

&lt;p&gt;In this round I was asked to design a ChessBoard, as I don't know how to play chess😅😅, I told the interviewer and he modified the question to,&lt;/p&gt;

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

Design Snakes and ladders game



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

&lt;/div&gt;

&lt;p&gt;I was asked to write the Classes and methods involved in each one of them. Not expected to run it in the console. It happened over the VS code editor.&lt;/p&gt;

&lt;p&gt;Expectations on this round were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How good am I in identifying the Top-level classes and if I am able to breakdown the tasks into small meaningful chunks.&lt;/li&gt;
&lt;li&gt;What is the overall Data structure that I am using to store the data?&lt;/li&gt;
&lt;li&gt;How readable is the code?&lt;/li&gt;
&lt;li&gt;Am I good at identifying the corner cases?&lt;/li&gt;
&lt;li&gt;How scalable the architecture I used will be?&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Round 4 (PSDS) : (1 hour)
&lt;/h3&gt;

&lt;p&gt;This is a problem-solving round. To evaluate my Problem-solving skills and how quickly am I able to achieve an optimized solution.&lt;/p&gt;

&lt;p&gt;Here I was asked 2 questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find the starting and ending indices of all repeated characters from a string.&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;const&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;hellooooloo&lt;/span&gt;&lt;span class="err"&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;op&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getRepeated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;op&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [(2,3), (4,7), (9,10)]&lt;/span&gt;



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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The next problem is a &lt;code&gt;String Backtracking&lt;/code&gt; approach, an extension of the previous one, where I have to check if I can form a word in the dictionary by removing one or more repeated letters.&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;const&lt;/span&gt; &lt;span class="nx"&gt;dictionary&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;hellolo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;hellooooloo&lt;/span&gt;&lt;span class="err"&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;op&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;canBeFormed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;op&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true,&lt;/span&gt;
&lt;span class="c1"&gt;// because by deleting the repeated characters of `o` we can form `hellolo` which is present in the dictionary&lt;/span&gt;



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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Round 5 (Hiring Manager): (typically 45 mins - 1 hour)
&lt;/h3&gt;

&lt;p&gt;I was asked questions on multiple fronts like Javascript, performances, and Problem-solving. It was like a combination of all the above rounds.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I was given a snippet in JS and asked about its output and how does it work under the hood. (Macro and micro queues related).&lt;/li&gt;
&lt;li&gt;What are the strategies that I follow to increase the speed of any website?&lt;/li&gt;
&lt;li&gt;Web vitals and how they are measured?&lt;/li&gt;
&lt;li&gt;Browser Execution of Document.&lt;/li&gt;
&lt;li&gt;Problem: &lt;code&gt;Space Separator, another string based backtracking question.&lt;/code&gt;
&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;const&lt;/span&gt; &lt;span class="nx"&gt;dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="na"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
 &lt;span class="na"&gt;hello&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;world&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;spaceSeparator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;helloworld&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "hello world"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;spaceSeparator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;helloworldhi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "hello world hi"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;spaceSeparator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;helloworldh&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "" , as h is not present in dict we throw "" as output&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;The code I wrote here for the problem was asked to run on the browser console after completion, to verify its correctness.&lt;/p&gt;

&lt;h3&gt;
  
  
  Round 6 (As Appropriate): (1 hour)
&lt;/h3&gt;

&lt;p&gt;This is the final round and it is &lt;strong&gt;important&lt;/strong&gt; to get a hire in this round too. It is not a gimmick, as I have read posts where people got rejected in this round. &lt;/p&gt;

&lt;p&gt;Here I interacted with an Interviewer who was at a GM/Partner level in Microsoft. This round could be as simple as knowing about your interests and your past project to Complex Data structures. And for me its the latter :P&lt;/p&gt;

&lt;p&gt;It started slowly with an introduction and my background and landed on a data structure question. I was asked to write the following program.&lt;/p&gt;

&lt;p&gt;This was an interesting problem and I encountered this for the first time in this interview.&lt;/p&gt;

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

Consider you are getting millions of tweets per second,
you have to alert whenever a particular word is repeated 
billion times in any 1 hour time frame (moving window)


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;I had to decide the Data structure for each tweet.&lt;/li&gt;
&lt;li&gt;I had to decide how I want to store it in my memory.&lt;/li&gt;
&lt;li&gt;I had to come up with an optimal solution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What Next?
&lt;/h3&gt;

&lt;p&gt;Now you wait!!!&lt;/p&gt;

&lt;p&gt;Microsoft has a lot of applicants for each role, so before they confirm that you are selected they do make sure they have evaluated other candidates who might be a better fit. So, you will have to wait for them to come back.&lt;/p&gt;

&lt;p&gt;It took me 2 weeks to know that I was selected. And releasing the offer took another week. And you will have 5 days to accept the offer.&lt;/p&gt;

&lt;p&gt;Overall I would say the entire journey was smooth and I'll be part of MicrosoftTeams Development. An enterprise chat application used by millions of people.&lt;/p&gt;

&lt;p&gt;So excited!!!! Wish me luck!!!🤩 🥳&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/H1vjPkqdL7liL6O7Bj/giphy-downsized.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/H1vjPkqdL7liL6O7Bj/giphy-downsized.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't forget to follow me!😄&lt;/p&gt;

&lt;p&gt;If you have more questions add it over the comments section I'll try giving the input if I know the answers😛&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://www.dhilipkmr.dev" rel="noopener noreferrer"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt;, and &lt;a href="https://twitter.com/dhilipkmr_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all Folks!!!&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>interview</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A Node Email Service using AWS lambda</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Sat, 03 Oct 2020 06:50:45 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/a-node-email-service-using-aws-lambda-6g9</link>
      <guid>https://dev.to/dhilipkmr/a-node-email-service-using-aws-lambda-6g9</guid>
      <description>&lt;p&gt;Hello there,&lt;/p&gt;

&lt;p&gt;It is always so rewarding when you achieve something after long research. And this is one such instance as I had to at least read 40+ blogs to finally set up a fully running Mailing service. So just thought of collating everything together in a blog to help lambda newbies like me.&lt;/p&gt;

&lt;p&gt;Prerequisites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Javascript&lt;/li&gt;
&lt;li&gt;A Gmail account&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Problem:
&lt;/h2&gt;

&lt;p&gt;Having &lt;code&gt;Contact Me&lt;/code&gt; like the one below is an integral part of most of the personal and small business websites that are built.&lt;/p&gt;

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

&lt;p&gt;I was building one such website, as the entire website is static, I honestly do not want to set up a server just to expose a single endpoint.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution:
&lt;/h2&gt;

&lt;p&gt;I know that &lt;code&gt;cloud functions&lt;/code&gt; are something that solves my problem of having an endpoint without actually setting up the server. I chose AWS Lambda as it was much popular. But, the resources and blogs were not enough to give me a step by step guideline.  &lt;/p&gt;

&lt;h2&gt;
  
  
  What are we building?
&lt;/h2&gt;

&lt;p&gt;We are going to build a node emailer service that accepts a &lt;code&gt;message&lt;/code&gt; in our POST request body and triggers an email to the predefined set of recipients from your Gmail account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Table Of Contents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;1.AWS Account Setup&lt;/li&gt;
&lt;li&gt;2.Setting up Lambda&lt;/li&gt;
&lt;li&gt;3.Uploading the emailer code to your lambda&lt;/li&gt;
&lt;li&gt;4.Google Oauth and GCP Setup&lt;/li&gt;
&lt;li&gt;5.Update the keys in your Code&lt;/li&gt;
&lt;li&gt;6.Creating AWS API Gateway&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1.AWS Account Setup:&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Create an AWS account &lt;a href="https://portal.aws.amazon.com/billing/signup#/start" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Your account setup will be complete with you entering your Credit card details and verifying your email. Charges are usage-based.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.Setting up Lambda:&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Naviagte to &lt;a href="https://console.aws.amazon.com/console/home?region=us-east-1" rel="noopener noreferrer"&gt;AWS Console&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Choose &lt;code&gt;Lambda&lt;/code&gt; under the &lt;code&gt;Find Services&lt;/code&gt; input field.&lt;/li&gt;
&lt;li&gt;You should now be on the lambda functions dashboard which shows the list of your available lambda functions.&lt;/li&gt;
&lt;li&gt;Click on the &lt;code&gt;Create Function&lt;/code&gt; button&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the next screen, fill in your Function name - &lt;code&gt;emailer&lt;/code&gt; and choose Nodejs runtime as we are implementing this using node.&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%2Fi%2Fk929hspojj9k4oi2so05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk929hspojj9k4oi2so05.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On Clicking the &lt;code&gt;Create function&lt;/code&gt; button you should see &lt;code&gt;Successfully created the function emailer&lt;/code&gt; message on the next screen.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On scrolling down the page, you will see a sample nodeJS code with index.js&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a new test with any name of your choice and click on the &lt;code&gt;Test&lt;/code&gt; button, you should be getting the response in the &lt;code&gt;Execution Result&lt;/code&gt; tab.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3.Uploading the emailer code to your lambda: &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The Aws lambda IDE for nodeJS does not allow us to install our npm packages on the go. Due to this, we have to get this set up locally in our machine and then upload the code to lambda by zipping it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download the &lt;a href="https://github.com/dhilipkmr/emailer-aws/raw/download/Archive.zip" rel="noopener noreferrer"&gt;Zip&lt;/a&gt;. It contains the code to be uploaded to your lambda function.&lt;/li&gt;
&lt;li&gt;If you want to create the zip, the content is present inside this &lt;a href="https://github.com/dhilipkmr/emailer-aws" rel="noopener noreferrer"&gt;repo&lt;/a&gt; where there are a &lt;code&gt;nodemailer&lt;/code&gt; dependency and some code to send an email. Make sure to npm install and create a zip from the root directory including your &lt;code&gt;node_modules&lt;/code&gt; folder.&lt;/li&gt;
&lt;li&gt;Once you got the Zip, upload it to the AWS lambda using &lt;code&gt;Actions&lt;/code&gt; -&amp;gt; &lt;code&gt;Upload a .zip file&lt;/code&gt; option.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;If you open &lt;code&gt;index.js&lt;/code&gt; you should be able to see the code where we have given our Email credentials and sending an email.&lt;/li&gt;
&lt;li&gt;Headers are set to handle CORS errors if you try hitting your lambda from another origin.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Google Oauth and GCP Setup:&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You need to set the following auth keys in order to confirm that you are the owner of your email account.
&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;YOUR_CLIENT_ID&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;clientSecret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;YOUR_CLIENT_SECRET&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;refreshToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;YOUR_REFRESH_TOKEN&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;YOUR_ACCESS_TOKEN&amp;gt;&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;ul&gt;
&lt;li&gt;In order to do that this we need to get our Oauth credentials from our GCP project and then use that in Google OAuth playground to generate these keys against your email.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I know it could be a lot of jargon. But trust me it is simple.&lt;/p&gt;

&lt;h4&gt;
  
  
  Setting up GCP:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;So login to &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud&lt;/a&gt; and create a new project.&lt;/li&gt;
&lt;li&gt;Click on &lt;code&gt;Select Project&lt;/code&gt; and then &lt;code&gt;Create new project&lt;/code&gt; button.&lt;/li&gt;
&lt;li&gt;Name it &lt;code&gt;mailer&lt;/code&gt; and click on &lt;code&gt;create&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In your mailer project goto APIs &amp;amp; Services -&amp;gt; Credentials -&amp;gt; Create Credentials -&amp;gt; OAuth Client ID -&amp;gt; Configure Content Screen -&amp;gt; External -&amp;gt; Create&lt;/li&gt;
&lt;li&gt;Again goto Create Credentials -&amp;gt; OAuth Client ID -&amp;gt; Web Application -&amp;gt; Enter Application Name -&amp;gt; Choose &lt;code&gt;https://developers.google.com/oauthplayground&lt;/code&gt; as Authorised redirect URIs and Save it.&lt;/li&gt;
&lt;li&gt;Now you should be getting a popup with your &lt;code&gt;clientID&lt;/code&gt; and &lt;code&gt;clientSecret&lt;/code&gt; copy both.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Setting up OAUTH:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to &lt;a href="https://developers.google.com/oauthplayground/" rel="noopener noreferrer"&gt;Google OAuth Playground&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Click on Setting icon on the top right corner  -&amp;gt; Enable Use your own OAuth credentials &amp;gt; Enter OAuth &lt;code&gt;clientID&lt;/code&gt; &amp;amp; Oatuh &lt;code&gt;clientSecret&lt;/code&gt; that you got from the above steps -&amp;gt; Close.&lt;/li&gt;
&lt;li&gt;In Select &amp;amp; Authorize APIs Field, Type &lt;code&gt;https://mail.google.com&lt;/code&gt; -&amp;gt; Authorize APIs -&amp;gt; Login with the account that you want to send email from.&lt;/li&gt;
&lt;li&gt;Click on Exchange authorization code for tokens -&amp;gt; Copy Refresh Token and Access Token.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Update the keys in your Code:&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now we got all the keys needed.&lt;br&gt;
Now update your &lt;code&gt;clientId&lt;/code&gt;, &lt;code&gt;clientSecret&lt;/code&gt;, &lt;code&gt;refreshToken&lt;/code&gt;, and &lt;code&gt;accessToken&lt;/code&gt; and your Full email ID in the AWS Lambda code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Click &lt;code&gt;Deploy&lt;/code&gt; -&amp;gt; &lt;code&gt;Test&lt;/code&gt; -&amp;gt; Configure your test to include &lt;code&gt;message&lt;/code&gt; parameter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You should get an email with your message on clicking &lt;code&gt;Test&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  6. Creating AWS API Gateway:&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create an API to expose this lambda function as a service.&lt;/li&gt;
&lt;li&gt;Click on Services -&amp;gt; API gateway service from the search bar -&amp;gt; Create API -&amp;gt; REST API -&amp;gt; Build -&amp;gt; API name -&amp;gt; Create.&lt;/li&gt;
&lt;li&gt;You should be on this screen now.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;We need two methods to be created. 1.POST and 2.OPTIONS to handle CORS.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Creating POST:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Actions -&amp;gt; Create Method -&amp;gt; POST -&amp;gt; TICK -&amp;gt; Integration type
-&amp;gt; Lambda -&amp;gt; Lambda Function -&amp;gt; emailer -&amp;gt; Save -&amp;gt; OK.&lt;/li&gt;
&lt;li&gt;We need to allow few Headers so that they could be read by the client.&lt;/li&gt;
&lt;li&gt;Method Response -&amp;gt; Expand the Accordion next to 200.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Add the following headers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
    &lt;span class="nx"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Headers&lt;/span&gt;
    &lt;span class="nx"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Methods&lt;/span&gt;
    &lt;span class="nx"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Origin&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Go to Integration Response -&amp;gt; Expand Accordion -&amp;gt; Header Mappings -&amp;gt; Make the following
&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="nx"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Origin&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;YOUR_DOMAIN&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;


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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If you have multiple headers being passed from your API, in order to consume them you have to enable it here.&lt;/li&gt;
&lt;li&gt;You can now do a test from the TEST option -&amp;gt; pass the following in the body
&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="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&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;HELLO&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;ul&gt;
&lt;li&gt;Click on Test -&amp;gt; you should get an Email with "HELLO" in the Message&lt;/li&gt;
&lt;li&gt;Actions -&amp;gt; Deploy API -&amp;gt; Deployment Stage (New Stage) -&amp;gt; Dev as Stage Name -&amp;gt; Deploy.&lt;/li&gt;
&lt;li&gt;Your POST API is Now deployed.&lt;/li&gt;
&lt;li&gt;Copy the &lt;code&gt;INVOKE URL&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;POST call this &lt;code&gt;INVOKE URL&lt;/code&gt; with message param in body to send the email.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Similarly, create the OPTIONS method, and update the headers. You should get an 'OK' response to testing the Same.&lt;br&gt;
It is mandatory otherwise your Cross-site requests will fail.&lt;/p&gt;

&lt;p&gt;Now do 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="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;INVOKE&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;
&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// {"message":"Email processed succesfully!"}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;You have Done it!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/KEVNWkmWm6dm8/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/KEVNWkmWm6dm8/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't forget to follow me 😄&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://www.dhilipkmr.dev" rel="noopener noreferrer"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt; and &lt;a href="https://twitter.com/dhilipkmr_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats all Folks!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Real life Web Optimizations</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Fri, 27 Mar 2020 13:10:25 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/real-life-webs-optimizations-1kae</link>
      <guid>https://dev.to/dhilipkmr/real-life-webs-optimizations-1kae</guid>
      <description>&lt;h2&gt;
  
  
  Optimising &lt;a href="https://www.goibibo.com/trains/" rel="noopener noreferrer"&gt;Goibibo Trains&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Hello Fellow Devs,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.goibibo.com/" rel="noopener noreferrer"&gt;Goibibo&lt;/a&gt; is one of the top Online Travel Agent companies for booking tickets Online for Flights, Trains, Hotels, Buses and Cars in &lt;strong&gt;India&lt;/strong&gt;. In this I am gonna take you through the problems that we had with existing implementation of goibibo trains and how we resolved it. Basically the journey of re-building &lt;a href="https://www.goibibo.com/trains/" rel="noopener noreferrer"&gt;goibibo trains&lt;/a&gt; vertical from scratch.&lt;/p&gt;

&lt;p&gt;Our Trains Funnel has 3 Main Pages:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Page Name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Home&lt;/td&gt;
&lt;td&gt;To feed in Source, destination and date for travel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Results&lt;/td&gt;
&lt;td&gt;To see the available trains List&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Review&lt;/td&gt;
&lt;td&gt;To avail offers and provide traveller details&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Payment is handled by different independent Module.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problems we had:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1.Feature Parity:
&lt;/h3&gt;

&lt;p&gt;Although our API provided a lot of information, they were not consumed at the Client. Due to this there was no parity in features between Mobile apps and Web Apps which led to much plain UI with no option event to filter items for example.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.Hard to Read/Edit Existing Code:
&lt;/h3&gt;

&lt;p&gt;For someone who came into &lt;code&gt;redux-saga&lt;/code&gt; from &lt;code&gt;redux&lt;/code&gt; for global state management, it was still hard to wrap my head around the concept of how redux-saga's &lt;code&gt;generators&lt;/code&gt; work and how internally they are coupled with &lt;code&gt;watchers&lt;/code&gt;. Every time I touch the code on &lt;code&gt;saga&lt;/code&gt; I had to be extra careful also it uses local storage to maintain store.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.No Shareable URL(Review Page):
&lt;/h3&gt;

&lt;p&gt;Our Review page's URL is static (&lt;code&gt;/trains/booking&lt;/code&gt;) which makes it impossible to share it across to someone else on the web. Because it was built with (Android/IOS) APP first approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.Better User Experience**
&lt;/h3&gt;

&lt;p&gt;Sometimes users were not sure about what the mandatory Info they were missing to proceed further because of lack of appropriate messages.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.Size:
&lt;/h3&gt;

&lt;p&gt;So the Elephant in the room was the &lt;strong&gt;JS bundle Size&lt;/strong&gt;. Our vertical was a &lt;a href="https://en.wikipedia.org/wiki/Single-page_application" rel="noopener noreferrer"&gt;Single Page Application&lt;/a&gt; and as you know we ended up downloading all javascript content up front on page 1 for the rest of the two pages as well. So the Initial sizes were as below:&lt;/p&gt;

&lt;p&gt;JS:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;File Name&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;Compression&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;vendor.js&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;144KB&lt;/td&gt;
&lt;td&gt;Brotli&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;bundle.js&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;46KB&lt;/td&gt;
&lt;td&gt;Brotli&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;review.js&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;14.4KB&lt;/td&gt;
&lt;td&gt;Brotli&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;total&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;204.4KB&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://dev-to-uploads.s3.amazonaws.com/i/rzscpfef6r98aopk9itq.png" rel="noopener noreferrer"&gt;JS browser Info&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CSS:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;File Name&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;Compression&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;combinedStyles.css&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;22.8KB&lt;/td&gt;
&lt;td&gt;gZip&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;other Css Files&lt;/td&gt;
&lt;td&gt;27.3KB&lt;/td&gt;
&lt;td&gt;gZip&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;total&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;50.1KB&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://dev-to-uploads.s3.amazonaws.com/i/wsojh7ls6gionyytzmm6.png" rel="noopener noreferrer"&gt;CSS browser Info&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This resulted in a considerable amount of users dropping off from our funnel without getting the javascript downloaded.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps taken to resolve all the issues listed above:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1.Feature Parity:
&lt;/h3&gt;

&lt;p&gt;Since a lot of Information are already available in the API and just not Integrated, we went ahead with providing Feature Parity to the Existing Dweb of trains with (Offers Carousel, Filters, Best Available Class, Alternate Train Route, Smart Engage, Offers).With this our Average daily booking jumped from &lt;code&gt;200-300&lt;/code&gt; to &lt;code&gt;600-700&lt;/code&gt; on a daily basis.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.Rewrite Train in a Better lightweight framework
&lt;/h3&gt;

&lt;p&gt;We had to get React out of the picture because  just to show a &lt;code&gt;hello World&lt;/code&gt; we'll end up sending around &lt;code&gt;40KB&lt;/code&gt; to the Client.&lt;/p&gt;

&lt;p&gt;(&lt;code&gt;react&lt;/code&gt; - &lt;code&gt;2.6KB&lt;/code&gt; and &lt;code&gt;react-dom&lt;/code&gt; - &lt;code&gt;36KB&lt;/code&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Our Rewrite framework Options:&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  a.No Framework:
&lt;/h3&gt;

&lt;p&gt;Maintaining an e-commerce website in plain JS has its own Pain Points with everyone coding in their own way (functional, object oriented, Es6) which ends up having no uniform way of writing the code. Also updating the DOM won't be as optimised as compared to frameworks like (react).&lt;/p&gt;

&lt;h3&gt;
  
  
  b.Svelte:
&lt;/h3&gt;

&lt;p&gt;We were right on the current trends and noticed about Svelte and its ability to send compiled code as output. So, we ended up doing a POC on &lt;code&gt;Svelte&lt;/code&gt; for our &lt;code&gt;Search&lt;/code&gt; component in &lt;code&gt;Home&lt;/code&gt; page and we found that the total amount of code that is shipped to the client is less than 10KB for the Search Widget which in turn is 45+KB in &lt;code&gt;react&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  c.Preact:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;preact&lt;/code&gt;[3KB] is a lightweight &lt;code&gt;react&lt;/code&gt; as they call it and it also has support for &lt;code&gt;Hooks&lt;/code&gt; with &lt;code&gt;preactX&lt;/code&gt; release. So a POC that ended up with size a little higher(2KB) than Svelte.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Verdict:
&lt;/h3&gt;

&lt;p&gt;Since 'Preact' and 'Svelte' were so close to our requirement of achieving lightest client bundle, we decided to choose between these two frameworks.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Metrics&lt;/th&gt;
&lt;th&gt;&lt;code&gt;preact&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;svelte&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;~4.5KB (with Hooks)&lt;/td&gt;
&lt;td&gt;Although it compiles the code to plain js, it adds a small amount of Svelte framework specific code (~2KB)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Community&lt;/td&gt;
&lt;td&gt;Better than Svelte&lt;/td&gt;
&lt;td&gt;Relatively new and Growing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Package Availability&lt;/td&gt;
&lt;td&gt;All &lt;code&gt;react&lt;/code&gt; and &lt;code&gt;preact&lt;/code&gt; Frameworks are supported in &lt;code&gt;preact&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Not much option in terms of package availability&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Skillset of Developers&lt;/td&gt;
&lt;td&gt;Abundance&lt;/td&gt;
&lt;td&gt;Need time to get into Svelte Ecosystem&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;BoilerPlate&lt;/td&gt;
&lt;td&gt;Comparatively More Boilerplate&lt;/td&gt;
&lt;td&gt;Lesser Boiler Plate with the help of 2 way Binding&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: These were the data at the Time of deciding framework&lt;/p&gt;

&lt;p&gt;With all those comparisons we decided on going with 'preact' as it is not much of size difference when compared with svelte and the community and packages availability are too good.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Enter groot&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/R97jJCEGEmh0I/giphy-downsized.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/R97jJCEGEmh0I/giphy-downsized.gif" alt="groot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;groot&lt;/code&gt; - our new repo.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Motto of &lt;code&gt;groot&lt;/code&gt;:
&lt;/h3&gt;

&lt;h3&gt;
  
  
  1.Lightweight:
&lt;/h3&gt;

&lt;p&gt;Our key metric was to serve the entire Funnel in Under 100KB.(All 3 pages included)&lt;/p&gt;

&lt;h3&gt;
  
  
  2.MPA:
&lt;/h3&gt;

&lt;p&gt;We decided on following the Multi Page Application Approach as the extra byte of code to download on each page transition could go to a maximum of only 20KB.The other resources which are common like &lt;code&gt;vendor.js&lt;/code&gt; are served from service workers for each page except for the first time.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.Server Side Rendered:
&lt;/h3&gt;

&lt;p&gt;Like any other vertical in goibibo, trains will be &lt;code&gt;Server Side Rendered&lt;/code&gt; for providing the User with Meaningful First Paint as well as helping in SEO Indexing.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.IsoMorphic:
&lt;/h3&gt;

&lt;p&gt;The content will be &lt;code&gt;Server Side Rendered&lt;/code&gt; with the same page being used in the funnel as well as for the SEO pages.(This ensures feature parity and avoids multiple team handling multiple versions of the same page).&lt;/p&gt;

&lt;h3&gt;
  
  
  5.Packages:
&lt;/h3&gt;

&lt;p&gt;Do not add packages unless it is absolutely needed. Always prefer native solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  6.State Management:
&lt;/h3&gt;

&lt;p&gt;We decided on leveraging the built-in &lt;code&gt;useReducer&lt;/code&gt; hook combined with &lt;code&gt;useContext&lt;/code&gt; to serve as global State management for our Application.And this helps us reducing the need for yet another third part library like  &lt;code&gt;redux&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  7.No Axios:
&lt;/h3&gt;

&lt;p&gt;We have decided on not using Axios as it contributes to (4.4KB gzip) and to use &lt;code&gt;fetch&lt;/code&gt; instead. However we used &lt;code&gt;node-fetch&lt;/code&gt; for fetching on the server.&lt;/p&gt;

&lt;p&gt;Our client had only the following content in our vendor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dependencies&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;preact&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;10.0.0&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;react-foco&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;^1.3.1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                     
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;siema&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;1.5.1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// Other Server dependencies     &lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Very Little Client Dependencies? Oh yes!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/McgWV3RzDQZZNdFWRA/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/McgWV3RzDQZZNdFWRA/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  App Architecture:
&lt;/h3&gt;

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

&lt;p&gt;Getting our First Page out:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Irctc forgot password&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We began with preact's scaffold as it has optimised webpack build.&lt;/li&gt;
&lt;li&gt;But it always appended the whole preact component to the FirstChild of the body, but we needed &lt;code&gt;goibibo header&lt;/code&gt; which is a plain JS component to be the first child.&lt;/li&gt;
&lt;li&gt;We could not export the &lt;code&gt;webpack&lt;/code&gt;'s config out of the scaffold so we wrote our webpack config from scratch which gave more control in terms of build outputs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The CSS Problem:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We got our designs and implemented the functionality.When it was time to take the release out we noticed that our CSS for that page is about &lt;strong&gt;26KB&lt;/strong&gt; because we used a generic css file known as &lt;code&gt;combinedStyles.css&lt;/code&gt; which has a lot of CSS rules that are shared between all verticals but most of them were not used in our particular page.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://www.npmjs.com/package/purify-css" rel="noopener noreferrer"&gt;&lt;code&gt;purify-css&lt;/code&gt;&lt;/a&gt; to the rescue:
&lt;/h3&gt;

&lt;p&gt;This library creates a new file which contains css rules only for those classes that are used in our code base.&lt;/p&gt;

&lt;p&gt;So I created a new webpack plugin out of purify Css.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OptimizeCssPlugin&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;files&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;./src/*/*.js&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;./src/*/*/*.js&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;./server/*.js&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;./server/*/*.js&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;./server/*/*/*.js&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;./plain_js/*.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
              &lt;span class="na"&gt;cssPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`https://goibibo.ibcdn.com/styleguide/css/newtrains/trainCombined.&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;.css`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="na"&gt;downloadCssPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`./static/trains-static/styles/combinedStyles.css`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="na"&gt;outputCssPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`./static/trains-static/styles/purified.css`&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="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;// other configs&lt;/span&gt;

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

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;files&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;js/css/html files to consider in my repository&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cssPath&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The giant file with all CSS rules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;downloadCssPath&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;If the &lt;code&gt;cssPath&lt;/code&gt; file is in the remote server, then &lt;code&gt;downloadCssPath&lt;/code&gt; is where it will get downloaded.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;outputCssPath&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;It is the location of the final output CSS file which takes the CSS rules from &lt;code&gt;cssPath&lt;/code&gt; file for the classes mentioned in files provided under &lt;code&gt;files&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This worked out ultimately and resulted in reducing the size of the &lt;code&gt;combinedStyles.css&lt;/code&gt; from &lt;code&gt;23KB&lt;/code&gt; to &lt;code&gt;~7KB&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, with the success in that we also involved in serving the content in &lt;code&gt;brotli&lt;/code&gt; compression rather than &lt;code&gt;gzip&lt;/code&gt; which also helped us in reducing the resource size. We did have our Fallback to gzip for the user agents that do not support &lt;code&gt;br&lt;/code&gt; compression.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;File Name&lt;/th&gt;
&lt;th&gt;gzip&lt;/th&gt;
&lt;th&gt;brotli&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;vendor.js&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;17.7KB&lt;/td&gt;
&lt;td&gt;15.7KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;forgotPassword.js&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;14.3KB&lt;/td&gt;
&lt;td&gt;12.3KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;total&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;32KB&lt;/td&gt;
&lt;td&gt;28KB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://www.goibibo.com/trains/irctc-forgot-password/" rel="noopener noreferrer"&gt;Forgot Password&lt;/a&gt; is the first page that we built and is live with a pretty good &lt;code&gt;LightHouse&lt;/code&gt; Score of &lt;code&gt;91&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://i.giphy.com/media/YnBntKOgnUSBkV7bQH/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/YnBntKOgnUSBkV7bQH/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Post that we released multiple URLs which are relatively New.&lt;/p&gt;

&lt;p&gt;As the results were Satisfactory for the pages outside the main funnel, we moved one of the pages inside our funnel to be served from &lt;code&gt;groot&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The REVIEW Page:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Review page is the first in the funnel page we decided to rewrite in &lt;code&gt;groot&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This involved,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Considerable changes in the &lt;code&gt;Backend API&lt;/code&gt; to support &lt;code&gt;page refresh&lt;/code&gt; and other train specific use cases as to show offer, updated fare breakup etc.&lt;/li&gt;
&lt;li&gt;Design changes&lt;/li&gt;
&lt;li&gt;Additional features&lt;/li&gt;
&lt;li&gt;Responsive&lt;/li&gt;
&lt;li&gt;Better UI Error handling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since we wanted to provide the Server rendered Meaningful response on review landing rather than a &lt;code&gt;loading shimmer&lt;/code&gt;,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We hit the &lt;code&gt;Backend API&lt;/code&gt; from our Node Server in &lt;code&gt;groot&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Updated our &lt;code&gt;Store&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Server rendered our Component&lt;/li&gt;
&lt;li&gt;Attached Additional scripts, css required to the final HTML Doc&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Hydrated&lt;/code&gt; on Client on receiving Response.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And we were able to see considerable improvement in terms of performance.&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%2Fi%2Fgqn7xdyjl46ja8wx9jy5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgqn7xdyjl46ja8wx9jy5.png" alt="Review Page Comparison"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Page Drop offs:
&lt;/h2&gt;

&lt;p&gt;People who saw our Server Rendered First Paint but could not get the JS loading.&lt;/p&gt;

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

&lt;p&gt;In our Old Page for Every 100 users almost 8 of them could not get the JS loaded due to which our page appears non-interactive to them.&lt;br&gt;
The same got down by 5 percent to 3 for 100 in our New page. Which again is more than 50% reduction in our Page Dropoffs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Review Page Comparison:
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Metric&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Old Version&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;New Version&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Diff&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;JS Vendor Size&lt;/td&gt;
&lt;td&gt;144KB&lt;/td&gt;
&lt;td&gt;16KB&lt;/td&gt;
&lt;td&gt;~85% Reduction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;JS Main Bundle Size&lt;/td&gt;
&lt;td&gt;60.4KB&lt;/td&gt;
&lt;td&gt;19.5KB&lt;/td&gt;
&lt;td&gt;~67% Reduction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;CSS Size&lt;/td&gt;
&lt;td&gt;50.1KB&lt;/td&gt;
&lt;td&gt;11KB&lt;/td&gt;
&lt;td&gt;~78% Reduction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;LightHouse Score&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;94&lt;/td&gt;
&lt;td&gt;^30 Points&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Page DropOffs&lt;/td&gt;
&lt;td&gt;8.8%&lt;/td&gt;
&lt;td&gt;3.3%&lt;/td&gt;
&lt;td&gt;~62% reduction&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Well!!!&lt;br&gt;
&lt;a href="https://i.giphy.com/media/ghTosDxIm1ab3DkCBV/giphy-downsized.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ghTosDxIm1ab3DkCBV/giphy-downsized.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since Common assets are cached by &lt;code&gt;service worker&lt;/code&gt;, moving from one page to the other will download only the page specific &lt;code&gt;js&lt;/code&gt; file from the server.&lt;/p&gt;

&lt;p&gt;Sincere thanks to &lt;a href="https://twitter.com/RGengage" rel="noopener noreferrer"&gt;@Raghunandan&lt;/a&gt; for helping me with his brilliant ideas and providing me with different perspectives to solve the problem. Thanks to &lt;a href="https://twitter.com/AbhighyaaJain" rel="noopener noreferrer"&gt;@Abhigyaa&lt;/a&gt; for the Axios to fetch Migration&lt;/p&gt;

&lt;p&gt;Happy Coding!!!&lt;/p&gt;

&lt;p&gt;Don't forget to follow me 😄&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://www.dhilipkmr.dev" rel="noopener noreferrer"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt; and &lt;a href="https://twitter.com/dhilipkmr_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats all Folks!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>optimisation</category>
    </item>
    <item>
      <title>Creating JSON.superStringify()</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Sun, 22 Mar 2020 06:42:06 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/creating-json-superstringify-3i9a</link>
      <guid>https://dev.to/dhilipkmr/creating-json-superstringify-3i9a</guid>
      <description>&lt;p&gt;Hello Devs,&lt;/p&gt;

&lt;p&gt;I recently came across an interesting problem, where I was asked to write a &lt;code&gt;JSON.superStringify()&lt;/code&gt; which avoids repetition if &lt;code&gt;(key, value)&lt;/code&gt; are same.&lt;/p&gt;

&lt;p&gt;Let me get you the picture:&lt;/p&gt;

&lt;p&gt;let us assume that there is an object like below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;baz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;bar&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;Then, the output of the same should be a serialised string with no repetition if &lt;code&gt;(key, value)&lt;/code&gt; are same for the object property.&lt;br&gt;
Here 'foo' is the key and value for &lt;code&gt;baz&lt;/code&gt; object. So instead of duplicating it we create it only once.&lt;/p&gt;
&lt;h3&gt;
  
  
  Expected Output:
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;baz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;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;baz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "{"foo":"foo","bar":{}}"&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;superStringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "{"foo","bar":{}}"&lt;/span&gt;

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


&lt;p&gt;&lt;a href="https://i.giphy.com/media/eOewytQL4tOOA/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/eOewytQL4tOOA/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you should be able to understand the problem. It avoids unnecessary payload that is being sent over the network.However, we need our own &lt;code&gt;JSON.superParse()&lt;/code&gt; to make it parse in the right manner.&lt;/p&gt;
&lt;h3&gt;
  
  
  Assumptions:
&lt;/h3&gt;

&lt;p&gt;For keeping the solution Simple,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are not going to worry about cases where the inputs are undefined, null, Symbol, WeakMaps and WeakSets.(However they are just a typecheck away)&lt;/li&gt;
&lt;li&gt;No second or third Parameter for &lt;code&gt;JSON.superStringify()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;p&gt;Let us list down the &lt;code&gt;(key, value)&lt;/code&gt; possibilities.&lt;br&gt;
 The Inputs can be,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A string&lt;/li&gt;
&lt;li&gt;A number&lt;/li&gt;
&lt;li&gt;A boolean&lt;/li&gt;
&lt;li&gt;An array(of type object),&lt;/li&gt;
&lt;li&gt;An object(of type object)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  How do we tackle each of them?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A string's output will have quotes(&lt;code&gt;""&lt;/code&gt;) attached to it in the Stringified output.&lt;/li&gt;
&lt;li&gt;A number's output is just string converted.&lt;/li&gt;
&lt;li&gt;A boolean's output again is just string converted.&lt;/li&gt;
&lt;li&gt;An Array can hold any of the above types and it has to be iterated and then &lt;code&gt;[]&lt;/code&gt; should be added to the output.&lt;/li&gt;
&lt;li&gt;An Object can have all the above as its value, and all its properties has to be stringified and &lt;code&gt;{}&lt;/code&gt; should be added at the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we know how to tackle the problem let's put out verbal steps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: Any keys apart from type &lt;code&gt;Object&lt;/code&gt; should be converted to string.&lt;/li&gt;
&lt;li&gt;Step 2: For all the values which are Objects,

&lt;ul&gt;
&lt;li&gt;Handle Array, it should repeat steps 1 and 2&lt;/li&gt;
&lt;li&gt;Handle objects, it should repeat steps 1 and 2.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Step 3: Return the output string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is clear from our verbal steps that the Array and the Objects have repetitions of above steps which results in having &lt;code&gt;recursion&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/rAa6I6ccp9oR2/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/rAa6I6ccp9oR2/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's get coding.
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Step 1: Let us handle all cases except Object type
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;superStringify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Handling Other types except Object&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleOtherTypes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&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="s1"&gt;string&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="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;val&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&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;handleOtherTypes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&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;ul&gt;
&lt;li&gt;When it is a string type then add quotes &lt;code&gt;""&lt;/code&gt; or just convert it to string and return the output.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OUTPUT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;superStringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "1"&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;superStringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ""hello""&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;superStringify&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="c1"&gt;// "true"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Including Arrays:
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;superStringify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Array&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&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;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&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;val&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;superStringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&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="s2"&gt;`[&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&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;// ... code for handling Other types&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;handleOtherTypes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&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;If it is an Array&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add Square bracket at the End &lt;code&gt;[]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We need to stringify each of the contents inside the Array.&lt;/li&gt;
&lt;li&gt;Since each of them can be of any type (Number, string, boolean or array), for each value of an array we recall our &lt;code&gt;superStringify&lt;/code&gt; function recursively and store the result in an array (&lt;code&gt;output&lt;/code&gt;) through &lt;code&gt;map&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;Finally, we move ahead with joining the output Array with comma.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;OUTPUT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;superStringify&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;444&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// "[1,2,"444","true"]"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Including Objects:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;After Each &lt;code&gt;key&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; pair there should be a &lt;code&gt;comma(,)&lt;/code&gt; which acts as a delimiter in case of objects.&lt;/li&gt;
&lt;li&gt;So let's try utilizing &lt;code&gt;join()&lt;/code&gt; utility of an Array to create delimiter.&lt;/li&gt;
&lt;li&gt;Which means we will put each stringified (&lt;code&gt;key&lt;/code&gt;, &lt;code&gt;value&lt;/code&gt;) pair inside an array and finally join them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since we always update the existing array as and when new &lt;code&gt;(key, value)&lt;/code&gt; properties are stringified, we could go with &lt;a href="https://www.w3schools.com/jsref/jsref_reduce.asp"&gt;&lt;code&gt;Array.reduce()&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;superStringify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ... array handling code&lt;/span&gt;
    &lt;span class="c1"&gt;// Object Handling&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&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="s1"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;keysList&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;input&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;superStringified&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;keysList&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;acc&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;=&amp;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;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&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="c1"&gt;// case 1&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;key&lt;/span&gt; &lt;span class="o"&gt;===&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;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`"&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="s2"&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;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// case 2&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="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="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`"&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="s2"&gt;":&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;superStringify&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="s2"&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;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// case 3&lt;/span&gt;
            &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`"&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="s2"&gt;":&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;handleOtherTypes&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="s2"&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;acc&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;superStringified&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// ... other types handling code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have 3 main cases:&lt;/p&gt;

&lt;p&gt;case 1: If both &lt;code&gt;key&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; are the same we are going to add only key with quotes around it to avoid duplication.&lt;/p&gt;

&lt;p&gt;case 2: If the value is object it could be either array or object which we have already handled in our function so we call &lt;code&gt;superStringify&lt;/code&gt; recursively.&lt;/p&gt;

&lt;p&gt;case 3: If it is not in case 1 or 2 then &lt;code&gt;handleOtherTypes&lt;/code&gt; should produce output for our different types.&lt;/p&gt;

&lt;p&gt;OUTPUT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;input&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;444&lt;/span&gt;&lt;span class="dl"&gt;'&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;superStringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "{"a":[1,2,"444","true"],"b":4,"c"}"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Oh yeah we have created our &lt;code&gt;superStringify&lt;/code&gt; serializer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3nPVcAIb6ft8k/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3nPVcAIb6ft8k/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Full working demo is here at &lt;a href="https://codesandbox.io/s/gallant-fast-7etu4?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark"&gt;codesandbox&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next Steps? Write your own &lt;code&gt;JSON.superParser()&lt;/code&gt; which handles output from &lt;code&gt;superStringify&lt;/code&gt; :P&lt;/p&gt;

&lt;p&gt;Happy Coding!!!&lt;/p&gt;

&lt;p&gt;Don't forget to follow me 😄&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://www.dhilipkmr.dev"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt; and &lt;a href="https://twitter.com/dhilipkmr_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats all Folks!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Understanding `this` with E.D.I.N concepts</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Sun, 17 Nov 2019 08:26:40 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/understanding-this-with-e-d-i-n-concepts-p00</link>
      <guid>https://dev.to/dhilipkmr/understanding-this-with-e-d-i-n-concepts-p00</guid>
      <description>&lt;h2&gt;
  
  
  What is &lt;code&gt;this&lt;/code&gt; in javascript?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; keyword refers to an object, the object that is executing the &lt;strong&gt;current part of our Javascript code&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is it so complicated to understand?
&lt;/h3&gt;

&lt;p&gt;We might feel overwhelmed as there are quite a number of ways to manipulate &lt;code&gt;this&lt;/code&gt; by providing it a different context.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt;  binding has nothing to do with &lt;strong&gt;where a function is declared&lt;/strong&gt; but it has everything to do with &lt;strong&gt;how a function is invoked&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are 4 main Rules which we can make use of, to determine what &lt;code&gt;this&lt;/code&gt; in your code represents.&lt;/p&gt;

&lt;p&gt;E.D.I.N - Stands for Explicit binding, Default binding, Implicit binding and new Binding.&lt;br&gt;
(There is no EDIN concept, its just my way to remember these concepts)&lt;/p&gt;
&lt;h2&gt;
  
  
  Rule 1.Implicit Binding:
&lt;/h2&gt;

&lt;p&gt;Implicit Binding is achieved when the function that is executed is called with a context.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="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;Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;getName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;obj.getName()&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Here we call the &lt;code&gt;getName()&lt;/code&gt; function of the object &lt;code&gt;obj&lt;/code&gt; with &lt;code&gt;obj&lt;/code&gt; as the &lt;em&gt;context&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;obj&lt;/code&gt; has &lt;code&gt;name&lt;/code&gt; property, &lt;code&gt;this.name&lt;/code&gt; would give out &lt;strong&gt;dev&lt;/strong&gt; as the output.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What if your function is nested within objects?
&lt;/h2&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="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;Dev Child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;getName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="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;parentObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;childObj&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="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;Dev Parent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;parentObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Dev Child&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The last level parent before any function call is the context for that function.&lt;/li&gt;
&lt;li&gt;In our case &lt;code&gt;obj&lt;/code&gt; is the &lt;code&gt;this&lt;/code&gt; for &lt;code&gt;getName()&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;Fat Arrow&lt;/code&gt; function catch:
&lt;/h3&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Global Dev&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Local Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;getName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Global Dev 😈 😈 😈&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/3o6nUXo2B7D4tAfQ6k/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3o6nUXo2B7D4tAfQ6k/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The problem here is &lt;code&gt;this&lt;/code&gt; binding has been done for the &lt;code&gt;getName&lt;/code&gt; arrow function and it takes &lt;em&gt;Lexical &lt;code&gt;this&lt;/code&gt;&lt;/em&gt; which is Gloabl in this case.&lt;/li&gt;
&lt;li&gt;So calling the function with &lt;strong&gt;impicit binding&lt;/strong&gt; takes &lt;em&gt;lesser priority&lt;/em&gt; than &lt;strong&gt;Arrow function.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2.Default Binding:
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Default binding&lt;/em&gt; is whenever the function is called &lt;strong&gt;without any context.&lt;/strong&gt;&lt;br&gt;
A common mistake occurs while destructuring a function from an object which has &lt;code&gt;this&lt;/code&gt; context in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Global Dev&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Local Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;getName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getName&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Global Dev  🤔🤔🤔&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we have destructured the &lt;code&gt;getName&lt;/code&gt; function out of the object &lt;code&gt;obj&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then, we have called it &lt;strong&gt;without any context&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;It means the function execution here is happening with &lt;em&gt;Global context.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;So while execution if it encounters any &lt;code&gt;this&lt;/code&gt; in the code that will try to resolve its value by checking window object.&lt;/li&gt;
&lt;li&gt;We have 'Global Dev' value assigned to &lt;code&gt;name&lt;/code&gt; in &lt;code&gt;window&lt;/code&gt; object so &lt;code&gt;this.name&lt;/code&gt; resolved to &lt;code&gt;window.name&lt;/code&gt; which is 'Global Dev'.&lt;/li&gt;
&lt;li&gt;Note: The same would have resulted in returning &lt;code&gt;undefined&lt;/code&gt; in &lt;code&gt;strict&lt;/code&gt; mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How to Overcome this?&lt;br&gt;
  By using &lt;strong&gt;Explicit binding&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  3.Explicit Binding:
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Explicit binding&lt;/em&gt; is a process of specifying what &lt;code&gt;this&lt;/code&gt; object is, while calling the function. It is usually done with the help of the famous Js trio &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;bind&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Global Dev&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Local Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getName&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&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;newContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// Explicit binding takes priority over Default binding &lt;/span&gt;
&lt;span class="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// New Dev &lt;/span&gt;

&lt;span class="c1"&gt;// Explicit binding takes priority over Implicit binding &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;getName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// New Dev &lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;It is clear from the above that, Explicit bindings take priority over Implicit or Default Binding.&lt;/p&gt;

&lt;p&gt;But does it have a Higher priority than Arrow function's &lt;code&gt;this&lt;/code&gt; binding.&lt;br&gt;
No!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fgiphygifs.s3.amazonaws.com%2Fmedia%2FDxA688OXGUMSI%2Fgiphy.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/http%3A%2F%2Fgiphygifs.s3.amazonaws.com%2Fmedia%2FDxA688OXGUMSI%2Fgiphy.gif"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Global Dev&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Local Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;//Arrow function's bind took priority over Explicit binding.&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;getName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Global Dev &lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Priority Ordering:
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Arrow Function&lt;/em&gt; &amp;gt; &lt;em&gt;Explicit Binding&lt;/em&gt; &amp;gt; &lt;em&gt;Implicit Binding&lt;/em&gt; &amp;gt; &lt;em&gt;Default Binding&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ubpB6XcvpYMF2/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ubpB6XcvpYMF2/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4.&lt;code&gt;new&lt;/code&gt; Binding:
&lt;/h2&gt;

&lt;p&gt;If the function is called with &lt;code&gt;new&lt;/code&gt; operator in the prefix then the newly constructed object is the &lt;code&gt;this&lt;/code&gt; reference here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Local Dev&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;MyName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// `new` binding&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name_instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name_instance&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="c1"&gt;// Local Dev&lt;/span&gt;
  &lt;span class="c1"&gt;// Implicit Binding&lt;/span&gt;
  &lt;span class="nx"&gt;name_instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;            &lt;span class="c1"&gt;// Local Dev&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we have defined our instance variable &lt;code&gt;name_instance&lt;/code&gt; which is formed from &lt;code&gt;new&lt;/code&gt; opertor operating on factory function &lt;code&gt;MyName&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;All references to &lt;code&gt;this&lt;/code&gt; in &lt;code&gt;MyName&lt;/code&gt; function refers to the newly created object instance &lt;code&gt;name_instance&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All our Rules from 1 to 3 applied to this instance (&lt;code&gt;name_instance&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;// Default Binding&lt;/span&gt;
    &lt;span class="kd"&gt;var&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;Global Dev&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name_instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;                                      &lt;span class="c1"&gt;// Global Dev&lt;/span&gt;

  &lt;span class="c1"&gt;// Implicit Binding&lt;/span&gt;
    &lt;span class="nx"&gt;name_instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;                        &lt;span class="c1"&gt;// Local Dev&lt;/span&gt;

  &lt;span class="c1"&gt;// Explicit Binding&lt;/span&gt;
    &lt;span class="nx"&gt;name_instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="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;New Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// New Dev&lt;/span&gt;

  &lt;span class="c1"&gt;// Arrow Binding&lt;/span&gt;
    &lt;span class="nx"&gt;MyName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get_Arrow_Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;name_instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_Arrow_Name&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;                  &lt;span class="c1"&gt;// Global Dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Classes:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handle_three&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handle_three&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;handle_one&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;handle_two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;handle_three&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;render&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="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Fragment&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Default binding */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handle_one&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
          &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Arrow Function */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handle_two&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Expicit Binding at constructor*/&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handle_three&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Fragment&lt;/span&gt;&lt;span class="p"&gt;&amp;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;ul&gt;
&lt;li&gt;Handlers on JSX elements will call the function declared.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;handle_one&lt;/code&gt; attachment results in calling the function with no context(Default binding). This results in &lt;code&gt;undefined&lt;/code&gt; because React ensures Default binding results in &lt;code&gt;undefined&lt;/code&gt; rather than a Global.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;handle_two&lt;/code&gt; attachment results in calling the function with the newly created Intsance's (Current App Class's instance's) context.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;handle_three&lt;/code&gt; attachment results in explicit binding to provide value for &lt;code&gt;this&lt;/code&gt; at the constructor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets Celebrate now!&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%2F68v92v82fh2886n5xez7.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%2F68v92v82fh2886n5xez7.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you guys have Enjoyed this Article 😄&lt;/p&gt;

&lt;p&gt;Reference: Kyle Simpson's &lt;a href="https://goodreads.com/book/show/22221108-you-don-t-know-js" rel="noopener noreferrer"&gt;this &amp;amp; Object Prototypes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://www.dhilipkmr.dev" rel="noopener noreferrer"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt; and &lt;a href="https://twitter.com/dhilipkmr_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats all Folks!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>showdev</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Array Flattening in 5 lines</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Fri, 25 Oct 2019 03:19:14 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/flattening-an-array-in-5-lines-57ca</link>
      <guid>https://dev.to/dhilipkmr/flattening-an-array-in-5-lines-57ca</guid>
      <description>&lt;p&gt;Hello there 👋,&lt;/p&gt;

&lt;p&gt;Array flattening is a process of reducing a Multi Dimensional Array into a single dimensional Array or with the dimensions specified.&lt;/p&gt;

&lt;p&gt;Example: &lt;/p&gt;

&lt;p&gt;Input: [1, 2, 3, 4, [5, 6]]&lt;/p&gt;

&lt;p&gt;Output: [1, 2, 3, 4, 5, 6]&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/PglfjMgtwjK6c/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/PglfjMgtwjK6c/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thought Process:
&lt;/h2&gt;

&lt;p&gt;Step 1: We should iterate over the given array.&lt;br&gt;
 Step 2: For Each element if it is not an array push it into the new array.&lt;br&gt;
 Step 3: If it is an array repeat Steps 1 to 3.&lt;/p&gt;
&lt;h2&gt;
  
  
  Observations:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We use the output of one iteration in the next, so the idea here is to use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce"&gt;Array.reduce()&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Since there is a repetition of steps 1 to 3 on getting an Array as input, we will have to do a recursion.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Lets get into coding:
&lt;/h3&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;flattenArr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrToFlatten&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;arrToFlatten&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;acc&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="o"&gt;=&amp;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;value&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flattenArr&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="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Call it as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
&lt;span class="nx"&gt;flattenArr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 3, 4, 5, 6]&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;flattenArr&lt;/code&gt; is a function that accepts &lt;code&gt;arrToFlatten&lt;/code&gt; of type Array as Argument.&lt;/li&gt;
&lt;li&gt;We return the output of &lt;code&gt;arrToFlatten.reduce&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Let the initial value of the result be empty array &lt;code&gt;[]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If the current input &lt;code&gt;value&lt;/code&gt; is not an Array's instance add it to the &lt;code&gt;acc&lt;/code&gt; using &lt;code&gt;concat&lt;/code&gt; utility function.&lt;/li&gt;
&lt;li&gt;If that turns out to be an &lt;code&gt;instanceof&lt;/code&gt; an Array then call &lt;code&gt;flattenArr&lt;/code&gt; again with the Array(&lt;code&gt;value&lt;/code&gt;) as a parameter, then concat that with &lt;code&gt;acc&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So now, with this approach we can reduce any Multi Dimensional Array into  a Single Dimensional Array.&lt;/p&gt;

&lt;p&gt;How about the case where we need it to flatten only upto specified levels deep and anything nested inside that should remain as it is?&lt;/p&gt;

&lt;p&gt;Its simple we get another value from the user in the argument(&lt;code&gt;depth&lt;/code&gt;).&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;flattenArr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrToFlatten&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;depth&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;arrToFlatten&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;acc&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="o"&gt;=&amp;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;value&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;depth&lt;/span&gt; &lt;span class="o"&gt;&amp;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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flattenArr&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;depth&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Call it as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;7&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="nx"&gt;flattenArr&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 3, 4, 5, 6, [7, 8]]&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Add an additional parameter &lt;code&gt;depth&lt;/code&gt; and check if its value is greater than 0 before processing an array inside the Reduce function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the &lt;code&gt;depth&lt;/code&gt; value is greater than 0 it means the Array has to be flattened. So recursively call &lt;code&gt;flattenArr&lt;/code&gt; with the array and &lt;code&gt;depth - 1&lt;/code&gt; as we have gone in one level. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each recursive call means we are going 1 level deep so we reduce the value of &lt;code&gt;depth&lt;/code&gt; by 1&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l4pTosVr0iHCJ11hm/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l4pTosVr0iHCJ11hm/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Oh yeah, I was saying 5 lines. Here you go!!!&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;flattenArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrToFlatten&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;arrToFlatten&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;acc&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="o"&gt;=&amp;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="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;flattenArr&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;depth&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;acc&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="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;
  
  
  Handling edge cases:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What if the first parameter is not an array?&lt;/li&gt;
&lt;li&gt;What if depth is not a number?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets add a check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;typeOfDepth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;depth&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="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrToFlatten&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;typeOfDepth&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&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="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid Argument Supplied&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



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

&lt;p&gt;As George suggested in the comment below, we do have default &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat"&gt;Array.flat()&lt;/a&gt; method however to attain &lt;a href="https://caniuse.com/#search=array.prototype.flat"&gt;browser support&lt;/a&gt; you will have to get help from your js transpilers like babel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l1J3CbFgn5o7DGRuE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l1J3CbFgn5o7DGRuE/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://www.dhilipkmr.dev"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt; and &lt;a href="https://twitter.com/dhilipkmr_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats all Folks!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>showdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Why should you use "noopener"? Beware of security Flaws.</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Sat, 17 Aug 2019 08:28:24 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/why-should-you-use-noopener-beware-of-security-flaws-3i57</link>
      <guid>https://dev.to/dhilipkmr/why-should-you-use-noopener-beware-of-security-flaws-3i57</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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1fkzkrddc5scbqsban29.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1fkzkrddc5scbqsban29.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TLDR:&lt;br&gt;
Checkout the implementation example here:&lt;br&gt;
&lt;a href="https://my-dummy-domain.netlify.com/" rel="noopener noreferrer"&gt;Live Demo&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's Open a url in new tab from within our website
&lt;/h2&gt;
&lt;h3&gt;
  
  
  HTML WAY
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://malicious-domain.netlify.com"&lt;/span&gt; &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"_blank"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Visit Malicious Website!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Okay, here we have a &lt;code&gt;href&lt;/code&gt; attribute to a malicious website and &lt;code&gt;target&lt;/code&gt; as &lt;code&gt;_blank&lt;/code&gt; attribute to make it to open in a new tab.&lt;/p&gt;

&lt;p&gt;Let us say that, the user clicks on "Visit Malicious Website!" from the above code. He gets redirected to the malicious website in a new tab.&lt;/p&gt;

&lt;p&gt;The flow seems so plain and simple what's the possible Security risk that the user has here?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user is redirected to a domain from your page.&lt;/li&gt;
&lt;li&gt;At this time, the browser attaches all your &lt;code&gt;window&lt;/code&gt; variable's content of your current website to &lt;code&gt;window.opener&lt;/code&gt; variable of the malicious website.

&lt;ul&gt;
&lt;li&gt;This is done by Chrome and Firefox browsers which has one of the largest user base.&lt;/li&gt;
&lt;li&gt;So now the malicious website has access to your website's window, which obviously opens up a &lt;strong&gt;security loop hole&lt;/strong&gt; in redirection of this method.&lt;/li&gt;
&lt;li&gt;Now the malicious website once it has access to your website's &lt;code&gt;window&lt;/code&gt; variable through &lt;code&gt;window.opener&lt;/code&gt; it can redirect your previous website to a new &lt;strong&gt;Phishing website&lt;/strong&gt; which could look similar to the actual website you opened and might even ask you to login again. &lt;/li&gt;
&lt;li&gt;The above change can be done in the malicious website by just writing the following code
&lt;/li&gt;
&lt;/ul&gt;
&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opener&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://www.dhilipkmr.dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;So the Innocent users get caught in this trap and would provide the login details which could be exposed to the attacker.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/UuIpr1iwXkRgY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/UuIpr1iwXkRgY/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How do we avoid this?
&lt;/h2&gt;

&lt;p&gt;A simple way is to add a &lt;code&gt;rel&lt;/code&gt; attribute with &lt;code&gt;noopener&lt;/code&gt; to the &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag.&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://malicious-domain.netlify.com"&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"noopener"&lt;/span&gt; &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"_blank"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Visit Malicious Website!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What does it do?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;rel="noopener"&lt;/code&gt; indicates the browser to not to attach the current website's &lt;code&gt;window&lt;/code&gt; variable to the newly opened malicious website.&lt;/li&gt;
&lt;li&gt;This makes the &lt;code&gt;window.opener&lt;/code&gt; of the malicious website to have &lt;code&gt;null&lt;/code&gt; as its value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So be careful when you navigate your users to a new domain that is not maintained by you.&lt;/p&gt;

&lt;p&gt;Not always we open a new tab with &lt;code&gt;a&lt;/code&gt; tag there are cases where you have to open it through executing javascript's &lt;code&gt;window.open()&lt;/code&gt; like below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;openInNewTab&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Some code&lt;/span&gt;
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://malicious-domain.netlify.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"link"&lt;/span&gt; &lt;span class="na"&gt;onclick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"openInNewTab()"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Visit Malicious Website!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here there is no mention of &lt;code&gt;noopener&lt;/code&gt; so this results in passing &lt;code&gt;window&lt;/code&gt; of the current website to the malicious website.&lt;/p&gt;

&lt;h2&gt;
  
  
  The javascript Way!
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How to handle such cases when new tab is opened through js?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;openInNewTabWithoutOpener&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;newTab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="nx"&gt;newTab&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opener&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="nx"&gt;newTab&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://malicious-domain.netlify.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;link&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;onclick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;openInNewTabWithoutOpener()&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Visit&lt;/span&gt; &lt;span class="nx"&gt;Malicious&lt;/span&gt; &lt;span class="nx"&gt;Website&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have opened a dummy tab through &lt;code&gt;window.open()&lt;/code&gt; which opens &lt;code&gt;about:blank&lt;/code&gt;, so it means it has not redirected to the malicious website yet.&lt;/li&gt;
&lt;li&gt;Then we modify the &lt;code&gt;opener&lt;/code&gt; value of the new tab to &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Post that we modify the new tab's url to the malicious website's url.&lt;/li&gt;
&lt;li&gt;This time, again &lt;code&gt;opener&lt;/code&gt; would have been null, due to which it cannot access the &lt;code&gt;window&lt;/code&gt; variable of the first website.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Problem Solved.&lt;/p&gt;

&lt;p&gt;But this method wont be possible in older versions of Safari, so we again have a problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix Safari's issue?
&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;function&lt;/span&gt; &lt;span class="nf"&gt;openInNewTabWithNoopener&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;aTag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;aTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;noopener&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;aTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_blank&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;aTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://malicious-domain.netlify.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;aTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"link"&lt;/span&gt; &lt;span class="na"&gt;onclick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"openInNewTabWithNoopener()"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Visit Malicious Website!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we mimic clicking on an anchor tag.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We create &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag and assign the required attributes then execute &lt;code&gt;click()&lt;/code&gt; over it, which behaves the same way as the link is clicked.&lt;/li&gt;
&lt;li&gt;Do not forget to add &lt;code&gt;rel&lt;/code&gt; attribute to the tag here.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other facts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you click &lt;code&gt;CMD + LINK&lt;/code&gt; on anchor tag, chrome, firefox and Safari considers makes &lt;code&gt;window.opener&lt;/code&gt; of the malicious website as &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;However, on &lt;code&gt;CMD + LINK&lt;/code&gt; on an element where new tab opening is handled through javascript, the browser attaches &lt;code&gt;window&lt;/code&gt; variable and sends it to the new tab.&lt;/li&gt;
&lt;li&gt;By default, the new version of Safari removes &lt;code&gt;window.opener&lt;/code&gt; when used with anchor tag for all cases, to pass the &lt;code&gt;window&lt;/code&gt; info to the new tab you have to explicitly specify &lt;code&gt;rel='opener'&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Checkout the live implementation example here:&lt;br&gt;
&lt;a href="https://my-dummy-domain.netlify.com/" rel="noopener noreferrer"&gt;Live Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;None shall bypass your Security.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/5SAPlGAS1YnLN9jHua/giphy-downsized-large.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/5SAPlGAS1YnLN9jHua/giphy-downsized-large.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me may be :P&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://www.dhilipkmr.dev" rel="noopener noreferrer"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt; and &lt;a href="https://twitter.com/dhilipkmr_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats all Folks!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>showdev</category>
      <category>security</category>
    </item>
    <item>
      <title>Optional Chaining in Javascript</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Thu, 15 Aug 2019 01:32:18 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/optional-chaining-in-javascript-1175</link>
      <guid>https://dev.to/dhilipkmr/optional-chaining-in-javascript-1175</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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F23q1qkg3utg8f2l36rhf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F23q1qkg3utg8f2l36rhf.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What if I tell you that the following snippet is Possible in javascript?&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;hasWorld&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;world&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;hasWorld&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;world&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;This way of checking the property of an object known as Optional Chaining.&lt;/p&gt;

&lt;p&gt;This is currently in Stage 3 &lt;code&gt;tc39&lt;/code&gt;, however with &lt;code&gt;babel&lt;/code&gt; we will be able to use it in our code right now by adding the following plugin &lt;code&gt;@babel/plugin-proposal-optional-chaining&lt;/code&gt; to your config file for babel v7+.&lt;/p&gt;

&lt;p&gt;Below is the Transpiled Babel Code Example with Optional Chaining.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://bit.ly/optional-chaining" rel="noopener noreferrer"&gt;Transpiled code Example&lt;/a&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Creating Infinite Scroll with 15 Elements</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Mon, 05 Aug 2019 21:17:40 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/creating-infinite-scroll-with-15-elements-4dg4</link>
      <guid>https://dev.to/dhilipkmr/creating-infinite-scroll-with-15-elements-4dg4</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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwyovrb8mub735ozgohgi.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwyovrb8mub735ozgohgi.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Infinite scrolling&lt;/em&gt; is where we load a part of the result up front, and on reaching the end of the loaded list we load the next set of results and so on.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what is the problem?
&lt;/h2&gt;

&lt;p&gt;Let us say that there is a list with 10,000 elements, say each element is  displayed inside an &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; tag. So when we reach the end of the list then there will be 10,000 &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; nodes attached to the parent.&lt;/p&gt;

&lt;p&gt;In case of complex &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; with more number of children inside it , this will cause a huge hit in the website's performance and also affects scrolling performance of the webpage.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to overcome this overloaded DOM size?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It can be overcome by &lt;em&gt;Unloading&lt;/em&gt; or &lt;em&gt;discarding&lt;/em&gt; the top &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; which are not part of the user viewport.&lt;/li&gt;
&lt;li&gt;(i.e) As and when the user scrolls down we start adding the new rows and should delete the existing rows which are above the fold.&lt;/li&gt;
&lt;li&gt;We will be achieving this with fixed children size, say 15 rows max and we will update the same for new set of inputs thus maintaining a lower DOM size.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Things to consider:
&lt;/h2&gt;

&lt;p&gt;*Adding new node to the bottom of the list should be done when the last element of the list enters the viewport.&lt;br&gt;
*After adding the new elements to the DOM the existing elements should be deleted such that the deleted elements lies above the fold.(i.e) out of user's viewport.&lt;br&gt;
*When the user scrolls up and reaches the top element then we should load the deleted top elements and should delete the bottom ones, which are below the viewport.&lt;/p&gt;
&lt;h2&gt;
  
  
  What are we gonna Implement?
&lt;/h2&gt;

&lt;p&gt;A Component that can render a list of n number of items with fixed window size of 15 elements.(i.e) At any time only 15 DOM nodes will be present even on infinite scrolling through &lt;code&gt;n&lt;/code&gt; elements.&lt;/p&gt;
&lt;h3&gt;
  
  
  Output:
&lt;/h3&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fflsqzrldjuu2eac1xrmo.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fflsqzrldjuu2eac1xrmo.gif"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Approach:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We are going to get some help from CSS in achieving this.(We will make use of CSS's Absolute positioning).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our Parent div which wraps all our element is going be of &lt;code&gt;relative&lt;/code&gt; in position.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All our children will be positioned &lt;code&gt;absolute&lt;/code&gt;ly with respect to the parent div.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We calculate the &lt;code&gt;top&lt;/code&gt; for each of the child and apply it in style parameter of the &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;At any given time we will maintain 15 &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; elements in the DOM maximum.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: For Simplicity, we are going to consider only fixed size &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; cases.&lt;/p&gt;
&lt;h1&gt;
  
  
  Implementation:
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Initialization:
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&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;THRESHOLD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SlidingWindowScroll&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;start&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="na"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;THRESHOLD&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$topElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createRef&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$bottomElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createRef&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;ul&gt;
&lt;li&gt;We have created our &lt;code&gt;SlidingWindowScroll&lt;/code&gt; component which has a state with &lt;code&gt;start&lt;/code&gt; value and &lt;code&gt;end&lt;/code&gt; value in it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;start&lt;/code&gt; is the starting index of the list array which has to be loaded lazily on scroll.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;end&lt;/code&gt; is the last index of the list array.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;THRESHOLD&lt;/code&gt; gives the maximum number of &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; elements that should be present in the DOM.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We create two refs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;this.$topElement&lt;/code&gt;, will point the first element(0th index) in the list item.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this.$bottomElement&lt;/code&gt;, will point the last element(14th index) in the list item.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever the new elements are added or deleted the refs has to be updated accordingly to point to the top and bottom of the presently rendered list.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Render:
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt; &lt;span class="nx"&gt;getReference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isLastIndex&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$topElement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Attach this ref for first element&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;isLastIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$bottomElement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Attach this ref for last element&lt;/span&gt;
    &lt;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;render&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&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;updatedList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&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;lastIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;updatedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;relative&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;updatedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;start&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="s1"&gt;px&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;refVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getReference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;lastIndex&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;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;index&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;top&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;lastIndex&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bottom&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="p"&gt;);&lt;/span&gt;
          &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"li-card"&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;refVal&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;);&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;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;ul&gt;
&lt;li&gt;We get the &lt;code&gt;list&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; from &lt;code&gt;props&lt;/code&gt; and &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; of the list from &lt;code&gt;state&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;updatedList&lt;/code&gt; gives the new set of elements to be rendered.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; is made &lt;code&gt;relative&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each item in the list, we calculate it's &lt;code&gt;top&lt;/code&gt; position from its relative parent.&lt;/li&gt;
&lt;li&gt;It is calculated by the position of the current item in the &lt;code&gt;list&lt;/code&gt; (index + start) multiplied by &lt;code&gt;height&lt;/code&gt; of each element.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;refVal&lt;/code&gt; gives the &lt;code&gt;ref&lt;/code&gt; that has to be attached. It will have reference to &lt;code&gt;this.$topElement&lt;/code&gt; in case of index 0 and reference to &lt;code&gt;this.$bottomElement&lt;/code&gt; in case of last index.&lt;/li&gt;
&lt;li&gt;We attach &lt;code&gt;id&lt;/code&gt; with value &lt;code&gt;top&lt;/code&gt; for first element and &lt;code&gt;bottom&lt;/code&gt; as id for last element.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Setting up the Observer for refs:
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
  &lt;span class="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;intiateScrollObserver&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;componentDidUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prevState&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;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;intiateScrollObserver&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;ul&gt;
&lt;li&gt;On &lt;code&gt;Mounting&lt;/code&gt; and on whenever the value for &lt;code&gt;start&lt;/code&gt; or &lt;code&gt;end&lt;/code&gt; changes the &lt;code&gt;ref&lt;/code&gt; which points to the top and bottom of the rendered &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; is changed.&lt;/li&gt;
&lt;li&gt;Since the &lt;code&gt;ref&lt;/code&gt; starts pointing to different element we will have to listen to those two refs to know when they come into viewport.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We use &lt;code&gt;IntersectionObserver&lt;/code&gt; to identify if the &lt;code&gt;root&lt;/code&gt; or &lt;code&gt;bottom&lt;/code&gt; element is in the viewport.&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;intiateScrollObserver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;// To listen to window scroll&lt;/span&gt;
      &lt;span class="na"&gt;rootMargin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// if there is any margin associated with it&lt;/span&gt;
      &lt;span class="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.01&lt;/span&gt;      &lt;span class="c1"&gt;// if 1% of the element is in view&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;IntersectionObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$topElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$topElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$bottomElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$bottomElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&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;ul&gt;
&lt;li&gt;We create our &lt;code&gt;IntersectionObserver&lt;/code&gt; with a &lt;code&gt;callback&lt;/code&gt; that should get &lt;em&gt;fired when the elements enters and leaves&lt;/em&gt; the viewport and &lt;code&gt;options&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;options&lt;/code&gt; we specify that we are listening to the scroll event in window and the element should be marked as visible even when 1% of the element comes into view(by means of &lt;code&gt;threshold&lt;/code&gt; key).&lt;/li&gt;
&lt;li&gt;Then, we observe both the refs (&lt;code&gt;this.$topElement&lt;/code&gt; and &lt;code&gt;this.$bottomElement&lt;/code&gt;) to know when it enters/ leaves viewport.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Handling viewport entry of &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;listLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="c1"&gt;// Scroll Down&lt;/span&gt;
      &lt;span class="c1"&gt;// We make increments and decrements in 10s&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;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isIntersecting&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;entry&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;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bottom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;maxStartIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;listLength&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;THRESHOLD&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// Maximum index value `start` can take&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;maxEndIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;listLength&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="c1"&gt;// Maximum index value `end` can take&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;end&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="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;maxEndIndex&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;maxEndIndex&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;newStart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;maxStartIndex&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;maxStartIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newStart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newEnd&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="c1"&gt;// Scroll up&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;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isIntersecting&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;entry&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;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;THRESHOLD&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;THRESHOLD&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;THRESHOLD&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;THRESHOLD&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;newStart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;start&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="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="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;&amp;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;start&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newStart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newEnd&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="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Whenever &lt;code&gt;this.$topElement&lt;/code&gt; or &lt;code&gt;this.$bottomElement&lt;/code&gt; comes into viewport or leaves the viewport &lt;code&gt;callback&lt;/code&gt; will be called.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;entries&lt;/code&gt; is an array with all observers in the order of creation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;entries&lt;/code&gt; in our case will have &lt;code&gt;this.$topElement&lt;/code&gt; and &lt;code&gt;this.$bottomElement&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isIntersecting&lt;/code&gt; property gives if the element is in viewport and &lt;code&gt;id&lt;/code&gt; helps us in deciding if it is the bottom element that came into view or the top one.&lt;/li&gt;
&lt;li&gt;We make calculation to maintain 15 elements between &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; of the &lt;code&gt;state&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We add and remove items in number of 10 and we make sure atleast 15 elements are present.&lt;/li&gt;
&lt;li&gt;Finally, we update &lt;code&gt;state&lt;/code&gt; with new values for &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Updating State:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;resetObservation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unobserve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$bottomElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unobserve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$topElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$bottomElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createRef&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$topElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createRef&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;updateState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newStart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newEnd&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&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;start&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;newStart&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;newEnd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resetObservation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newStart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newEnd&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;ul&gt;
&lt;li&gt;We set the &lt;code&gt;state&lt;/code&gt; with new values and also reset all the observer.&lt;/li&gt;
&lt;li&gt;While resetting, all the observers should be made &lt;code&gt;unobserve&lt;/code&gt; to not to observe it's change in the future. And we create a new &lt;code&gt;ref&lt;/code&gt; for &lt;code&gt;this.$bottomElement&lt;/code&gt; and &lt;code&gt;this.$topElement&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now on scroll we have only 15 elements at a time but giving the user the sense of having it all in the DOM.&lt;/p&gt;

&lt;p&gt;Have a unique key to avoid re-rendering of same &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt;s.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output:
&lt;/h3&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fflsqzrldjuu2eac1xrmo.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fflsqzrldjuu2eac1xrmo.gif"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Follow me for interesting contents.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/zealous-firefly-5r7ie?fontsize=14" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" alt="Edit zealous-firefly-5r7ie"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dhilipkmr/intersection_observer" rel="noopener noreferrer"&gt;Repo Link&lt;/a&gt;&lt;br&gt;
My &lt;a href="https://www.dhilipkmr.dev" rel="noopener noreferrer"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt; and &lt;a href="https://twitter.com/dhilipkmr_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's All Folks!&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Find Max. Paths to last Index in a 2D Matrix in Javascript</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Thu, 06 Jun 2019 16:17:39 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/find-max-paths-to-last-index-in-a-2d-matrix-in-javascript-4li6</link>
      <guid>https://dev.to/dhilipkmr/find-max-paths-to-last-index-in-a-2d-matrix-in-javascript-4li6</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqc7jsshh86bh56wa7jv5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqc7jsshh86bh56wa7jv5.png" width="748" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read all my blogs here at My &lt;a href="https://www.dhilipkmr.dev/blogs" rel="noopener noreferrer"&gt;Website&lt;/a&gt; and follow me here on &lt;a href="https://twitter.com/dhilipkmr_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It has been a while since my last blog.&lt;br&gt;
I will be writing and explaining some algos as well as some  interesting Javascript/React concepts that I come across.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvicvrov8vgjt63jzo27a.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvicvrov8vgjt63jzo27a.gif" width="222" height="245"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  PROBLEM STATEMENT:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Given the length of Row and Column of a 2D matrix.&lt;/li&gt;
&lt;li&gt;Starting from origin (0,0), find the maximum number of Paths one can take to reach the last index.&lt;/li&gt;
&lt;li&gt;Allowed ways to move =&amp;gt; Right and Bottom.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let, row's length and column length be (2, 2)&lt;/li&gt;
&lt;li&gt;Starting from (0,0) one should reach (1,1)&lt;/li&gt;
&lt;li&gt;So the number of paths for that are: 2&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Path 1: (0,0) -&amp;gt; (0,1) -&amp;gt; (1,1)&lt;br&gt;
Path 2: (0,0) -&amp;gt; (1,0) -&amp;gt; (1,1)&lt;/p&gt;
&lt;h2&gt;
  
  
  Thought Process:
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Checking for a Pattern:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We are allowed to traverse in two ways Down and Right for every Index. So this is a pattern.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Edge Cases:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;While we iterate we need to be careful about the ending index's count and so we should handle those cases.&lt;/li&gt;
&lt;li&gt;Path is 0 when the input is less than 1 for either row or column.(i.e.) Input cant be less than 1 for row length and column length&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We found a pattern to go through for every index we can choose to solve with iteration / recursion.&lt;/p&gt;

&lt;p&gt;Here we will solve it through RECURSION!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1jgv7tmwiwaoxe7wo40r.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1jgv7tmwiwaoxe7wo40r.gif" width="490" height="490"&gt;&lt;/a&gt;&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;num_of_paths&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findMaxPathSrcToDes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Number of Paths&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num_of_paths&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We call &lt;code&gt;findMaxPathSrcToDes&lt;/code&gt; and pass row length and column length and log it.&lt;/p&gt;

&lt;h3&gt;
  
  
  The intermediate Function:
&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;function&lt;/span&gt; &lt;span class="nf"&gt;findMaxPathSrcToDes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cols&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Initial rows and columns to begin with.0,0 is the first row and col index we are choosing&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;findMaxPath&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cols&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;findMaxPathSrcToDes&lt;/code&gt; function accepts the row length and column length from the user.&lt;/li&gt;
&lt;li&gt;It then returns the output from &lt;code&gt;findMaxPath&lt;/code&gt; function to which we pass the origin which is (0,0) and destination index(rows -1, cols - 1).&lt;/li&gt;
&lt;li&gt;We can modify this origin and destination index to user defined positions by accepting those, so that we can identify the number of paths any index from any index.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Finding the Paths:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;findMaxPath&lt;/code&gt; function takes in 4 params and outputs the number of path.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;currentRow&lt;/code&gt; - Which indicates the current index's Row that is being processed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;currentColumn&lt;/code&gt; - Which indicates the current index's Column  that is being processed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;destRow&lt;/code&gt; - Destination row index&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;destCol&lt;/code&gt; - Destination column index&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In any Recursive solution, start with writing the &lt;strong&gt;base conditions or Exit conditions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So, what is a Base condition or Exit condition?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It is basically the case on satisfying which our algorithm should terminate. So, let's formulate one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When &lt;code&gt;currentRow &amp;gt; destRow&lt;/code&gt; (In this case it indicates that the &lt;code&gt;currentRow&lt;/code&gt; count has gone out of bound).&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;currentColumn &amp;gt; destCol&lt;/code&gt; (In this case it indicates that the &lt;code&gt;currentColumn&lt;/code&gt; count has gone out of bound).
So we return '0' in both the cases.
&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;function&lt;/span&gt; &lt;span class="nf"&gt;findMaxPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentRow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentColumn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;destRow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;destCol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Base condition&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;currentRow&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;destRow&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;currentColumn&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;destCol&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="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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Success Case:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;if &lt;code&gt;currentRow === destRow&lt;/code&gt; or &lt;code&gt;currentColumn === destCol&lt;/code&gt; this indicates that we have reached the destination index so we return &lt;code&gt;1&lt;/code&gt; to indicate a successful path.
&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentRow&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;destRow&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;currentColumn&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;destCol&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Recursion Case:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;For each index there are two ways 1.Right and 2.Down&lt;/li&gt;
&lt;li&gt;So, we have to recurse in both ways and add the path formed from each ways.&lt;/li&gt;
&lt;li&gt;We call &lt;code&gt;findMaxPath&lt;/code&gt; by incrementing &lt;code&gt;currentRow&lt;/code&gt; by 1.&lt;/li&gt;
&lt;li&gt;Then again by incrementing  &lt;code&gt;currentColumn&lt;/code&gt; by 1 and adding the outputs of these two and return them.
&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;pathsInRows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findMaxPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentRow&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentColumn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;destRow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;destCol&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;pathsInColums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findMaxPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentRow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentColumn&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;destRow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;destCol&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="nx"&gt;pathsInRows&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;pathsInColums&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CodePen link &lt;a href="https://codepen.io/dhilipkmr/pen/dEExbQ?editors=0011" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next Step:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You could try printing all possible paths along with count.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrw7sbjoojry2rl268ls.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrw7sbjoojry2rl268ls.gif" width="384" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me for interesting contents.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dhilipkmr/datastructures/blob/master/maxPath2DMatrix.js" rel="noopener noreferrer"&gt;Repo Link&lt;/a&gt;&lt;br&gt;
My &lt;a href="https://www.dhilipkmr.dev" rel="noopener noreferrer"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt; and &lt;a href="https://twitter.com/dhilipkmr_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's All Folks!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>datastructures</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Implementing our own `split()` in Javascript</title>
      <dc:creator>Dhilip kumar</dc:creator>
      <pubDate>Wed, 08 May 2019 15:09:59 +0000</pubDate>
      <link>https://dev.to/dhilipkmr/implementing-our-own-split-in-javascript-ii7</link>
      <guid>https://dev.to/dhilipkmr/implementing-our-own-split-in-javascript-ii7</guid>
      <description>&lt;h2&gt;
  
  
  What are we going to achieve?
&lt;/h2&gt;

&lt;p&gt;Implement our custom split function &lt;code&gt;myCustomSplit&lt;/code&gt; which behaves the same way as &lt;code&gt;split()&lt;/code&gt; in javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;val1&amp;amp;$val2&amp;amp;$val3&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;op&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myCustomSplit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;amp;$&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;op&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// output&lt;/span&gt;
&lt;span class="c1"&gt;// ["val1", "val2", "val3"]&lt;/span&gt;

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



&lt;p&gt;Do checkout my &lt;a href="https://www.dhilipkmr.dev/blogs"&gt;Website&lt;/a&gt; for all my blogs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's Split:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3og0IHyZMxZNkNOWT6/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3og0IHyZMxZNkNOWT6/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myCustomSplit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;splitVal&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;outputArr&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;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;nextVal&lt;/span&gt; &lt;span class="o"&gt;=&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;splitlength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;splitVal&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="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="c1"&gt;// ...Some code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;myCustomSplit&lt;/code&gt; - Name of the custom Split function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;splitVal&lt;/code&gt; - Value based on which we have to do the split.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;string&lt;/code&gt; - The string on which we execute split.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;outputArr&lt;/code&gt; - The array that will be returned as output.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nextVal&lt;/code&gt; - Gives the intermediate string that is formed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;splitlength&lt;/code&gt; - Length of the &lt;code&gt;splitVal&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;while&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;string&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="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// When the current character is same as splitVal's first character&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;string&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="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;splitVal&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;let&lt;/span&gt; &lt;span class="nx"&gt;split_length_remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;splitlength&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;split_length_passed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;similarSplit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&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="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;split_length_remaining&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;string&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="nx"&gt;split_length_passed&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;splitVal&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;split_length_passed&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;similarSplit&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;string&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="nx"&gt;split_length_passed&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nx"&gt;split_length_passed&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;split_length_remaining&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;split_length_remaining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;outputArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextVal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;nextVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;nextVal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;similarSplit&lt;/span&gt;&lt;span class="p"&gt;;&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;split_length_passed&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;// When the current character is different from `splitVal` 's first character&lt;/span&gt;
    &lt;span class="nx"&gt;nextVal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;string&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="o"&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;h2&gt;
  
  
  Explanation:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Loop for the entire string Length.&lt;/li&gt;
&lt;li&gt;(else case)Check the current character with the &lt;code&gt;splitval&lt;/code&gt;'s first character, if they are &lt;strong&gt;different&lt;/strong&gt; concatenate with &lt;code&gt;nextVal&lt;/code&gt; and increment &lt;code&gt;i&lt;/code&gt; by 1.&lt;/li&gt;
&lt;li&gt;(if case)If the &lt;strong&gt;current character&lt;/strong&gt; is same as the &lt;code&gt;splitval&lt;/code&gt;'s first character, then we go into &lt;strong&gt;inner while loop&lt;/strong&gt; which helps when the split value is more than single character.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Inside &lt;code&gt;if&lt;/code&gt; case:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;split_length_remaining&lt;/code&gt; gives the remaining length of the &lt;code&gt;splitVal&lt;/code&gt; that we have to iterate for.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;split_length_passed&lt;/code&gt; is set to 1 because we already compared the first character of the &lt;code&gt;splitVal&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;similarSplit&lt;/code&gt; helps in adding all the values after the first value is matched in inner while loop, so that if the last character of &lt;code&gt;splitVal&lt;/code&gt; fails then we can add the content of &lt;code&gt;similarSplit&lt;/code&gt; to the &lt;code&gt;nextVal&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;split_length_remaining&lt;/code&gt; is not &lt;code&gt;0&lt;/code&gt;, we did not completely compare all the values. So, we go on for other values to check if they match the input string.If they are equal we increment &lt;code&gt;i&lt;/code&gt; value and  decrement  &lt;code&gt;split_length_remaining&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;if &lt;code&gt;split_length_remaining&lt;/code&gt; has reached &lt;code&gt;0&lt;/code&gt; then we would have already matched all the values in &lt;code&gt;splitVal&lt;/code&gt; with the string, so push the content into &lt;code&gt;nextVal&lt;/code&gt; and reset it to &lt;strong&gt;empty string&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nx"&gt;outputArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextVal&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;outputArr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, push all the contents in &lt;code&gt;nextVal&lt;/code&gt; to &lt;code&gt;outputArr&lt;/code&gt; and return it.&lt;/p&gt;

&lt;h3&gt;
  
  
  OUTPUT:
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;val1&amp;amp;$val2&amp;amp;$val3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myCustomSplit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;amp;$&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// output: ["val1", "val2", "val3"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Checkout the code at &lt;a href="https://codepen.io/dhilipkmr/pen/KLKvdQ"&gt;codepen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ackDumdRl8fxm/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ackDumdRl8fxm/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me for more interesting contents.&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://www.dhilipkmr.dev"&gt;Website&lt;/a&gt;, &lt;a href="https://dev.to/dhilipkmr"&gt;blogs&lt;/a&gt; and &lt;a href="https://twitter.com/dhilipkmr_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  That's All Folks!
&lt;/h3&gt;

</description>
      <category>showdev</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
