<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Gus Pear 🍐</title>
    <description>The latest articles on Forem by Gus Pear 🍐 (@gustavupp).</description>
    <link>https://forem.com/gustavupp</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%2F958655%2Fb6254816-816d-43a6-8aef-a445dab56746.png</url>
      <title>Forem: Gus Pear 🍐</title>
      <link>https://forem.com/gustavupp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gustavupp"/>
    <language>en</language>
    <item>
      <title>Time for a change? 6 questions to decide if changing careers or moving abroad is the right move</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Sat, 06 May 2023 03:49:50 +0000</pubDate>
      <link>https://forem.com/gustavupp/feeling-like-its-time-for-a-change-6-questions-to-decide-if-changing-careers-or-moving-abroad-is-the-right-move-2ji2</link>
      <guid>https://forem.com/gustavupp/feeling-like-its-time-for-a-change-6-questions-to-decide-if-changing-careers-or-moving-abroad-is-the-right-move-2ji2</guid>
      <description>&lt;p&gt;I feel like I face these types of “life-changing” questions every couple of years.&lt;/p&gt;

&lt;p&gt;You as well at some point had to face(or are about to) decisions with the potential to completely shape your path.&lt;/p&gt;

&lt;p&gt;And being stuck in indecision hell is a position is the worst part of it(I’ve been in several times and it sucks big time). &lt;/p&gt;

&lt;p&gt;Once you make a decision it feels like all the weight has been lifted from your shoulders.&lt;/p&gt;

&lt;p&gt;I’ll share with you a fear-setting/decision-making exercise I borrowed from Tim Ferris(who borrowed from Seneca) that I assure will get you a yes or no decision within 10 minutes no matter how big the change you’re facing.&lt;/p&gt;

&lt;p&gt;You only need to answer these 6 questions:&lt;/p&gt;

&lt;h2&gt;
  
  
  #1 What is the worst-case scenario if I pursue this goal?
&lt;/h2&gt;

&lt;p&gt;Fear may be the most powerful motivator for taking action (google: fear for driving action in copywriting)&lt;br&gt;
Here’s an example:&lt;/p&gt;

&lt;p&gt;Say you are considering quitting your job to learn to code in 6 months and pursue a career as a Software developer.&lt;/p&gt;

&lt;p&gt;What is the worst-case scenario that can happen? &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You use up all your savings&lt;/li&gt;
&lt;li&gt;You realize it will take longer than 6 months&lt;/li&gt;
&lt;li&gt;You quit the developer dream and go broke.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the worst-case scenario, you are broke and unemployed with two-thirds of the knowledge you need to get a job as a Developer and had (hopefully) a great 6 months of studying, reading, and drinking lattes.&lt;/p&gt;

&lt;p&gt;Nobody died and you are not in jail.&lt;/p&gt;

&lt;p&gt;Is it as bad as you thought? &lt;/p&gt;
&lt;h2&gt;
  
  
  #2 What is the most likely outcome if I take action toward this goal?
&lt;/h2&gt;

&lt;p&gt;This is not even the best-case scenario, we are talking about the most likely outcome.&lt;/p&gt;

&lt;p&gt;Let’s see how good it can be.&lt;/p&gt;

&lt;p&gt;I’ll take the same want-quit-my-job-to-be-a-dev example.&lt;/p&gt;

&lt;p&gt;The more likely outcome?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You gain 12GB of new knowledge&lt;/li&gt;
&lt;li&gt;You use up your savings&lt;/li&gt;
&lt;li&gt;You receive a job offer at the end of the 6 months&lt;/li&gt;
&lt;li&gt;It’s not 100K/year (like in the youtube videos you watch) but is a great start&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At the end of 6 months, you got the job you wanted. You are poorer than before (which you knew was going to happen and hopefully prepared for) and started a new career.&lt;/p&gt;

&lt;p&gt;Not bad, not bad…&lt;/p&gt;
&lt;h2&gt;
  
  
  #3 What can I do to prevent the worst-case scenario from happening?
&lt;/h2&gt;

&lt;p&gt;This is where you limit your losses.&lt;/p&gt;

&lt;p&gt;What was the worse scenario again? &lt;/p&gt;

&lt;p&gt;You are broke and unemployed with two-thirds of the knowledge you need to get a job as a Developer&lt;br&gt;
Ok.&lt;/p&gt;

&lt;p&gt;What can you do to prevent this scenario from ever happening?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a study plan for your 6 months&lt;/li&gt;
&lt;li&gt;Use the Seinfeld technique to make sure you show up every day&lt;/li&gt;
&lt;li&gt;Make friends that are attempting the same change to keep each other motivated&lt;/li&gt;
&lt;li&gt;Reduce your cost of living to the bare minimum to maximize your savings (or just save more money beforehand)&lt;/li&gt;
&lt;li&gt;Have a backup plan to get back to your old career if it all fails&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  #4 What steps can I take to recover from a worst-case scenario if it does happen?
&lt;/h2&gt;

&lt;p&gt;If it does happen(very unlikely) and you are now broke and unemployed here are a few things you can do to get back on your feet:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Move back with your parents for a short while (if that is an option)&lt;/li&gt;
&lt;li&gt;Get in touch with people within your old industry to try to be referred to a job position&lt;/li&gt;
&lt;li&gt;Keep studying while you don’t find a job(you might get the one-third of knowledge you need for a jr dev role)&lt;/li&gt;
&lt;li&gt;Apply for dev jobs regardless of being ready or not(you’ll never feel ready anyway)&lt;/li&gt;
&lt;li&gt;If it all fails, show up at your closest construction site ready to work and ask for a job or at the nearby restaurant.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  #5 Will this change take me closer or further from my ideal future?
&lt;/h2&gt;

&lt;p&gt;I know it’s hard to have a clear vision of your ideal future (trust me, I struggle with this one)&lt;/p&gt;

&lt;p&gt;On the other hand, if I ask you right now to tell me something you don’t want to do, or someone you don’t want to be like, you will likely give me an answer on the spot.&lt;/p&gt;

&lt;p&gt;That is why I find it easier to create an anti-future-vision.&lt;/p&gt;

&lt;p&gt;As an example, here are a few things in my anti-future vision:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I work on weekends/holidays/night shifts&lt;/li&gt;
&lt;li&gt;I work traveling&lt;/li&gt;
&lt;li&gt;I don’t spend time with my family&lt;/li&gt;
&lt;li&gt;I don’t read anymore&lt;/li&gt;
&lt;li&gt;I am out of shape&lt;/li&gt;
&lt;li&gt;I am an expert procrastinator&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The list goes on…&lt;/p&gt;

&lt;p&gt;Being afraid of not changing will force you to change.&lt;/p&gt;

&lt;p&gt;Again, fear may be the most powerful motivator for taking action.&lt;/p&gt;
&lt;h2&gt;
  
  
  #6 Will you regret not making this decision on your deathbed?
&lt;/h2&gt;

&lt;p&gt;If you are still unsure about taking or not the leap, try the ultimate question:&lt;/p&gt;

&lt;p&gt;Will I regret not having done this on my deathbed?&lt;/p&gt;

&lt;p&gt;That is as far as I can help you.&lt;/p&gt;

&lt;p&gt;And last but not least important consider items 1 and 5 of this tweet:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--4xv_0xcW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://pbs.twimg.com/profile_images/2306694148/kfjncq15nujlw8cda0ws_normal.png" alt="Ryan Holiday profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Ryan Holiday
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @ryanholiday
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kDgU_xDI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev.to/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      5 Stoic lessons from Seneca:&lt;br&gt;&lt;br&gt;1. We suffer more in imagination than in reality&lt;br&gt;2. Associate only with people who improve you&lt;br&gt;3. The greatest remedy for anger is delay&lt;br&gt;4. Value your time more than your possessions&lt;br&gt;5. Death is not in the distant future. We are dying every day
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      16:00 PM - 01 May 2023
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1653066799464976385" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OXOJJiQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev.to/assets/twitter-reply-action-238fe0a37991706a6880ed13941c3efd6b371e4aefe288fe8e0db85250708bc4.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1653066799464976385" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--foTp-unf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev.to/assets/twitter-retweet-action-632c83532a4e7de573c5c08dbb090ee18b348b13e2793175fea914827bc42046.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/like?tweet_id=1653066799464976385" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SFHqU4bF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev.to/assets/twitter-like-action-1ea89f4b87c7d37465b0eb78d51fcb7fe6c03a089805d7ea014ba71365be5171.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;Best of luck in your new beginning.&lt;/p&gt;

&lt;p&gt;Here &lt;a href="https://tim.blog/2017/05/15/fear-setting/"&gt;here&lt;/a&gt; to watch a video of the original fear-setting exercise from Tim Ferris&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>motivation</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How To Read For Maximum Retention</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Mon, 27 Mar 2023 11:03:14 +0000</pubDate>
      <link>https://forem.com/gustavupp/how-to-read-for-maximum-retantion-2g33</link>
      <guid>https://forem.com/gustavupp/how-to-read-for-maximum-retantion-2g33</guid>
      <description>&lt;p&gt;“This reading technique put me in the top 2% of all my classes from undergrad school to my masters at Berkley.” - North Caroline University’s Professor Jeffrey Kaplan&lt;/p&gt;

&lt;p&gt;What is the purpose of reading if you don’t remember any of it later? &lt;/p&gt;

&lt;p&gt;Distraction. &lt;/p&gt;

&lt;p&gt;If you read to learn and not for distraction(nothing wrong with that), pay attention because this has the potential to skyrocket your learning.&lt;/p&gt;

&lt;p&gt;But only if you say yes to this question.&lt;/p&gt;

&lt;p&gt;Would you read 50% slower to retain 10x more?&lt;/p&gt;

&lt;p&gt;It sounds like a no-brainer to me.&lt;/p&gt;

&lt;p&gt;Retaining more means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Actually acquiring knowledge&lt;/li&gt;
&lt;li&gt;Be able to tap into that knowledge at will&lt;/li&gt;
&lt;li&gt;Be able to write efficiently from memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unfortunately…&lt;/p&gt;

&lt;h2&gt;
  
  
  Most people are interested in reading 2x faster
&lt;/h2&gt;

&lt;p&gt;To brag about how many books they read in a month (without retaining anything)&lt;/p&gt;

&lt;p&gt;You don’t want to post that it took you 1 month to read a 200-page book or that your read only 5 books last year, do you?&lt;/p&gt;

&lt;p&gt;I don’t think so.&lt;/p&gt;

&lt;p&gt;But the truth is that the number of books you read or your reading speed is irrelevant if you are reading to gain knowledge and not to pass time.&lt;/p&gt;

&lt;p&gt;If retaining knowledge is what you are after, here is how you can read for maximum retention.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 1: Summarize the first 3 paragraphs into a single sentence each&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is called “synthesis”.&lt;/p&gt;

&lt;p&gt;When you summarize a block of text down to a single sentence, you are forced to understand the meaning of that text.&lt;/p&gt;

&lt;p&gt;You encourage your brain to find relationships and patterns.&lt;/p&gt;

&lt;p&gt;Here’s how I’d do it for the following 3 paragraphs.&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%2F7hck98iycuaz3oyvn7fq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7hck98iycuaz3oyvn7fq.png" alt="3 paragraphs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The easiest way to drive intrinsic value is by writing a list with pen and paper&lt;/li&gt;
&lt;li&gt;Write 25 things you are curious about to the point of spending a weekend studying it&lt;/li&gt;
&lt;li&gt;Be specific, the more detail the better.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 2: Summarize the 3 sentences into a single one.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You’ll have to leave stuff out.&lt;/p&gt;

&lt;p&gt;That is the point.&lt;/p&gt;

&lt;p&gt;Your brain is forced to find and pick what it considers the most important points.&lt;/p&gt;

&lt;p&gt;Here’s how I’d do it for my 3 sentences above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write down in detail 25 things you are very curious about.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 3: Rince and repeat for the rest of the book&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;No, it’s not easy.&lt;/p&gt;

&lt;p&gt;It’ll take you twice the time to read the book.&lt;/p&gt;

&lt;p&gt;But you’ll 10x your retention.&lt;/p&gt;

&lt;p&gt;I suggest skimming through the book to see if the content is worth retaining.&lt;/p&gt;

&lt;p&gt;If so, this is retention in steroids. &lt;/p&gt;

&lt;p&gt;As usual: Easy choices, hard life. Hard choices, easy life.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>webdev</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>14 Research-Backed Techniques To Learn To Code 2x Faster</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Wed, 15 Mar 2023 21:18:04 +0000</pubDate>
      <link>https://forem.com/gustavupp/14-research-backed-techniques-to-learn-to-code-2x-faster-132a</link>
      <guid>https://forem.com/gustavupp/14-research-backed-techniques-to-learn-to-code-2x-faster-132a</guid>
      <description>&lt;p&gt;I’ve been fascinated by “how to learn anything faster” since I was 18. &lt;/p&gt;

&lt;p&gt;I found an online course promising English fluency in 6 months (I’m Brazilian).&lt;/p&gt;

&lt;p&gt;I was sold.&lt;br&gt;
It took me 10 years.&lt;/p&gt;

&lt;p&gt;After studying countless articles and half a dozen books on the topic, In the following 12 years, I learned more stuff than most people will learn in their entire lives.&lt;/p&gt;

&lt;p&gt;Some of the things I've become:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A pencil portrait artist&lt;/li&gt;
&lt;li&gt;Translator&lt;/li&gt;
&lt;li&gt;A qualified carpenter&lt;/li&gt;
&lt;li&gt;A commercial airplane pilot&lt;/li&gt;
&lt;li&gt;A software developer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are the 14 researched-backed techniques to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn to code 2x faster&lt;/li&gt;
&lt;li&gt;Don’t forget what you learn&lt;/li&gt;
&lt;li&gt;And Understand hard concepts&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  #1 Make your practice as close as possible to the goal
&lt;/h2&gt;

&lt;p&gt;If you are getting prepared for a white-board style interview, you should try and simulate the real interview conditions as closely as possible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use a white-board(duh) or pen and paper to solve the problems&lt;/li&gt;
&lt;li&gt;Set a 45min timer (or whatever time they usually give at interviews)&lt;/li&gt;
&lt;li&gt;Describe your reasoning as you code (as if talking to the interviewer)
The closer your practice resembles the real thing, the more prepared you’ll be. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  #2 Passive learning doesn’t work
&lt;/h2&gt;

&lt;p&gt;In coding (as in most other things) passive learning has little effect. &lt;/p&gt;

&lt;p&gt;You have to code things out to learn.&lt;br&gt;
Just read about the merge sort algorithm? Now get your PC and write it out.&lt;/p&gt;

&lt;p&gt;Watched a tutorial on how to build a menu navbar? Now it’s time to try it out for yourself.&lt;/p&gt;

&lt;p&gt;1 hour of active learning = 10 hours of passive learning (maybe more)&lt;/p&gt;

&lt;h2&gt;
  
  
  #3 Practice with purpose
&lt;/h2&gt;

&lt;p&gt;You should set specific goals for your coding practice and focus on areas that need improvement. &lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
Goal&lt;br&gt;
I want to build a chrome plugin using chatGPT and notion APIs that does x.&lt;br&gt;
Steps&lt;br&gt;
Divide the goal into tasks and choose to complete a single task every morning.&lt;/p&gt;

&lt;h2&gt;
  
  
  #4 Connect new concepts to things you already know
&lt;/h2&gt;

&lt;p&gt;For example, if you're learning about object-oriented programming, think about how it relates to other programming paradigms you've learned or to things in the real world (e.g. the class is like a factory and the object instance is like one product of that factory).&lt;/p&gt;

&lt;h2&gt;
  
  
  #5 Create mental models
&lt;/h2&gt;

&lt;p&gt;Create mental models of coding concepts to better understand them. &lt;/p&gt;

&lt;p&gt;For example, if have are learning about recursion, try to sketch out a diagram to help you understand the flow of the code&lt;br&gt;
Like in &lt;a href="https://dev.to/gustavupp/the-recursion-explanation-i-wish-i-had-read-as-a-self-taught-developer-3g4p"&gt;this post&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  #6 Always try to recall from memory
&lt;/h2&gt;

&lt;p&gt;Say you have just learned a concept or a piece of syntax. Now, try to recall it from memory without looking it up. &lt;/p&gt;

&lt;p&gt;For example, after reading about a for loop, try to write out an example of one from memory.&lt;/p&gt;

&lt;p&gt;You don't have to get it right to reap the benefits, you just have to try.&lt;/p&gt;

&lt;h2&gt;
  
  
  #7 Space it out
&lt;/h2&gt;

&lt;p&gt;Practice a little regularly rather than a lot once. &lt;/p&gt;

&lt;p&gt;For example, code for 1 hour every day instead of for 7 hours once a week.&lt;/p&gt;

&lt;h2&gt;
  
  
  #8 Mix it up
&lt;/h2&gt;

&lt;p&gt;For example, if you are solving coding problems, don’t solve all array problems, then all graph problems, then all linked list problems. Instead, solve them randomly. &lt;/p&gt;

&lt;p&gt;You’ll have to figure out what type of algorithm and data structure is needed to solve that problem(like in a real code interview, no one is going to tell what the problem is about).&lt;/p&gt;

&lt;h2&gt;
  
  
  #9 Reflect on what you have learned
&lt;/h2&gt;

&lt;p&gt;After a coding session, take a few minutes to reflect on what you learned and what you found challenging. Write down any questions or areas you need to work on.&lt;/p&gt;

&lt;h2&gt;
  
  
  #10 Tune it up
&lt;/h2&gt;

&lt;p&gt;Regularly check your skills and understanding by testing yourself with quizzes or coding challenges or asking for feedback from others.&lt;/p&gt;

&lt;h2&gt;
  
  
  #11 Break it up
&lt;/h2&gt;

&lt;p&gt;Break larger coding tasks into smaller, more manageable pieces. For example, if you're building a website, break it down into specific pages or features.&lt;/p&gt;

&lt;h2&gt;
  
  
  #12 Ask for feedback
&lt;/h2&gt;

&lt;p&gt;These days you can also tell chatGPT to play the role of a grumpy senior dev to check your code.&lt;/p&gt;

&lt;p&gt;Try to get feedback on your coding from more experienced developers. Use feedback to improve.&lt;/p&gt;

&lt;h2&gt;
  
  
  #13 Rehearse mentally
&lt;/h2&gt;

&lt;p&gt;I used this one a lot during my pilot training(it’s free to fly in your head)&lt;/p&gt;

&lt;p&gt;When you are commuting to work for example and don’t have a computer with you, you can for example, mentally go over the steps needed to create a button that toggles a sidebar.&lt;/p&gt;

&lt;h2&gt;
  
  
  #14 Rest and recharge
&lt;/h2&gt;

&lt;p&gt;This is as important as the other ones. &lt;br&gt;
In the beginning, you don’t give a s***, but when you feel the burnout creeping in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take breaks and engage in activities that allow for mental recovery and relaxation. &lt;/li&gt;
&lt;li&gt;Go for a walk or watch a movie.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Wish You Had More Time? They Did Too…</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Wed, 08 Mar 2023 20:52:00 +0000</pubDate>
      <link>https://forem.com/gustavupp/the-people-changing-their-lives-in-their-spare-time-4eik</link>
      <guid>https://forem.com/gustavupp/the-people-changing-their-lives-in-their-spare-time-4eik</guid>
      <description>&lt;p&gt;Did you ever stop to think that most of the world’s great men achieved their life work, not in their day job, but in their &lt;strong&gt;spare time&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;I had not.&lt;/p&gt;

&lt;p&gt;I thought they were privileged and could work on their passion all day, every day.&lt;/p&gt;

&lt;p&gt;I was wrong. &lt;/p&gt;

&lt;p&gt;Just like you and I, they had a day job that paid the bills and put food on the table. They made the most of the little spare time they had and changed their lives (and ours)&lt;/p&gt;

&lt;h2&gt;
  
  
  Abraham, Edison, and Alexander
&lt;/h2&gt;

&lt;p&gt;After a long day of hard work, while his fellow workers slept or goofed off, a tired rail-splitter spent his evenings studying his worn-out books by the light of a candle or fire. &lt;strong&gt;Abraham Lincoln&lt;/strong&gt; cut out his path to immortality in his &lt;strong&gt;spare time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Despite being overworked and underpaid, a telegraph operator with unwavering faith in his fantastic dreams gave up precious hours of sleep or leisure time at night to bring them to fruition. The world is now reaping the benefits of what &lt;strong&gt;Edison&lt;/strong&gt; accomplished during his &lt;strong&gt;spare time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;An instructor in a little-known college, burdened by the tedious work he despised, found comfort in tinkering with a strange device during his evenings and holidays, much to the amusement of his colleagues. However, &lt;strong&gt;Alexander Graham Bell&lt;/strong&gt; ultimately went on to invent the telephone during his &lt;strong&gt;spare time&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  There's not enough time. Period.
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;There is never enough time to do everything. But there is always enough time to do the most important things. – Brian Tracy&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you are waiting for when you have enough time to do stuff you want to do, you are going to wait your entire life.&lt;/p&gt;

&lt;p&gt;Time is a constraint we have to learn to live with.&lt;/p&gt;

&lt;p&gt;Time is valuable and scarce.&lt;/p&gt;

&lt;p&gt;We don’t make spoons out of diamonds (unless you are filthy rich and bored), do we? That is because diamonds are valuable and scarce, so we use them wisely, we don’t waste them.&lt;/p&gt;

&lt;p&gt;Our time is way more important than diamonds. But we are still eating breakfast with a diamond spoon.&lt;/p&gt;

&lt;p&gt;It’s all a matter of priorities and systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 2 things you can do about it
&lt;/h2&gt;

&lt;p&gt;I started writing online a couple of months ago. &lt;/p&gt;

&lt;p&gt;I met &lt;a href="https://twitter.com/PathToIkigai"&gt;Leo&lt;/a&gt; on Twitter, he is 43, has a full-time job, a wife, 2 boys, and still finds the time to crush it online by talking about his passion(growth strategies)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GD71Dlno--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsy1ygxwdf7cnkkxg1q9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GD71Dlno--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsy1ygxwdf7cnkkxg1q9.png" alt="leo's tweet" width="710" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;He probably has less free time than you or me (unless you have 2 jobs and 3 kids)&lt;/p&gt;

&lt;p&gt;How did he achieve such exponential growth on Twitter in such a short time?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prioritized&lt;/strong&gt;: He wrote every single day since November (9.757 tweets so far)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Systematized&lt;/strong&gt;: He created a system to help him write faster and better even when he doesn’t feel like so
I have a tone to learn from Leo.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That’s the only 2 things you can do about your time (not including wasting it).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prioritize&lt;/strong&gt; the things that’ll help you achieve your goals.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create systems&lt;/strong&gt; to make it easier to keep consistent.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It’s one of those things that are much easier said than done (like most important things). I struggle with prioritizing every day. I have bad days, lazy days, sick days, and normal days when I don’t feel like writing (or have no ideas to write about). That is how life is. But we can do better, our future selves will thank us. &lt;/p&gt;

&lt;p&gt;The person you’ll be in 2 years is the sum of what you’ll do every day for the next 730 days. &lt;/p&gt;

&lt;p&gt;What are you going to be better at?&lt;/p&gt;

</description>
      <category>writing</category>
      <category>codenewbie</category>
      <category>motivation</category>
      <category>productivity</category>
    </item>
    <item>
      <title>C# FileStream For Lazy Developers In A Hurry. The Bare Minimum</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Tue, 28 Feb 2023 21:19:07 +0000</pubDate>
      <link>https://forem.com/gustavupp/c-filestream-for-lazy-developers-in-a-hurry-the-bare-minimum-3ic9</link>
      <guid>https://forem.com/gustavupp/c-filestream-for-lazy-developers-in-a-hurry-the-bare-minimum-3ic9</guid>
      <description>&lt;p&gt;If you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have no idea what a stream is&lt;/li&gt;
&lt;li&gt;Have a blurry idea but need a better analogy&lt;/li&gt;
&lt;li&gt;Don't know how to use(understand) streams in actual code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post is for you.&lt;/p&gt;

&lt;p&gt;This is my brain trying to understand streams last week.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Streams?
&lt;/h2&gt;

&lt;p&gt;A Stream is the flow of data(bytes) that moves from one point to another, just like a stream of water flowing down a river.&lt;/p&gt;

&lt;p&gt;Streams are used to read and write data to and from: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Files&lt;/li&gt;
&lt;li&gt;Networks&lt;/li&gt;
&lt;li&gt;Memory&lt;/li&gt;
&lt;li&gt;Other streams&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  An Analogy To Help Us Navigate The Examples
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fwktmogvnp49yre1j0qeb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fwktmogvnp49yre1j0qeb.png" alt="water tank analogy" width="800" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine a huge tank of muddy water(you don't know how much water is in there) and you need to clean this water and fill another tank with clean water.&lt;/p&gt;

&lt;p&gt;This tank has a faucet on the outside that you can open and let the muddy water flow out.&lt;/p&gt;

&lt;p&gt;You find a hose that has a water filter in it.&lt;/p&gt;

&lt;p&gt;You connect the hose to the muddy water tank and put the other end in the clean water tank.&lt;/p&gt;

&lt;p&gt;As you turn on the tap, the water flows through the hose, is filtered and clean water comes out. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The stream is the flow of water(not the water itself).&lt;/li&gt;
&lt;li&gt;The muddy tank is the source you are trying to read(file in our case).&lt;/li&gt;
&lt;li&gt;The filter is a helper class that helps us clean the water(convert bytes to text).&lt;/li&gt;
&lt;li&gt;The clean water tank is like the output on the other end(in our case we'll be logging the file content to the console as it comes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmj2hoyeaptg035fifsbc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmj2hoyeaptg035fifsbc.png" alt="reading from file analogy" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  C# classes to work with streams
&lt;/h2&gt;

&lt;p&gt;These are the classes to use when reading and writing to and from different sources like files, memory, network, other streams, etc(I don't know what else).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fklzglildihwp5p1rh4ph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fklzglildihwp5p1rh4ph.png" alt="stream hierarchy" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stream&lt;/strong&gt; is the abstract class that provides methods to transfer bytes to and from the source(file, memory, network, etc). It is the &lt;strong&gt;core&lt;/strong&gt; class to transfer bytes. &lt;/p&gt;

&lt;p&gt;If a class wants to read or write from a source(file, memory, or network) it needs to implement the Stream class.&lt;/p&gt;

&lt;p&gt;We'll only focus on the &lt;strong&gt;FileStream&lt;/strong&gt; and the helper classes &lt;strong&gt;StreamWriter&lt;/strong&gt; and &lt;strong&gt;StreamReader&lt;/strong&gt; in this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  How To Read a File Using the &lt;strong&gt;FileStream&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let's create a simple method that takes a string path and reads the content of the file and outputs it to the console&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ReadFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//E.g. @"d:\text.txt"&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fileStream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FileStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileAccess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fileStream&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
       &lt;span class="n"&gt;fileStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

       &lt;span class="n"&gt;fileStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Flush&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ensure that any data still in the buffer(bytesArray) is written to the file&lt;/span&gt;
       &lt;span class="n"&gt;fileStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//release any system resources associated with the object&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;The file path is the muddy water in the tank(but instead of mud we have bytes)&lt;/li&gt;
&lt;li&gt;The FileStream object is the muddy water tank's faucet(you open and bytes flow out instead of muddy water)&lt;/li&gt;
&lt;li&gt;The Encoding.UTF8.GetString() method is the filter cleaning the muddy water. In this case, it converts bytes into text.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simplified way to write this method is with the &lt;strong&gt;using&lt;/strong&gt; block. When the variable you declared inside of the &lt;strong&gt;using&lt;/strong&gt; falls out of scope, it calls the .Dispose() method which internally calls .Flush() and .Close() automatically for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ReadFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//E.g. @"d:\text.txt"&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fileStream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FileStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileAccess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
       &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;bytesArray&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fileStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
           &lt;span class="n"&gt;fileStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytesArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
           &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytesArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
           &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if I don't want to deal with bytes directly is there a way to make this more high-level?&lt;/p&gt;

&lt;p&gt;Here is where the helper class &lt;strong&gt;StreamReader&lt;/strong&gt; comes in.&lt;br&gt;
Have a look at the same code as above but using StreamReader instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ReadFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//E.g. @"d:\text.txt"&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;streamReader&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StreamReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;streamReader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadToEnd&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: This is only one way of doing it. There are others, e.g. &lt;code&gt;File.ReadAllText(path)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the above example, we don't even need to use the &lt;code&gt;FileStream&lt;/code&gt;. The &lt;code&gt;StreamReader&lt;/code&gt; deals with the &lt;code&gt;FileStream&lt;/code&gt; under the hood to make our lives simpler.&lt;/p&gt;

&lt;p&gt;Let's have a look at another example.&lt;/p&gt;

&lt;p&gt;What if we are reading a file that is bigger than our available RAM? &lt;/p&gt;

&lt;p&gt;One thing is for sure, we cannot read the whole file in memory at once! We have to use the Hannibal Lecter approach and do it piece by piece(forgive my analogy)&lt;/p&gt;

&lt;p&gt;Here is one way you can do it(Lower level version):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ReadLargeFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bufferSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// read the file in 4KB chunks&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StringBuilder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fileStream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FileStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileAccess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;streamReader&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StreamReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileStream&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bufferSize&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bytesRead&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="n"&gt;bytesRead&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;streamReader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bufferSize&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytesRead&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="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&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;Note that in the above example, we use one of the &lt;code&gt;StreamReader&lt;/code&gt; constructors that take a &lt;code&gt;FileStream&lt;/code&gt; as a parameter. We do that because we need more control over the stream in this case.&lt;/p&gt;

&lt;p&gt;Can we do away with the ugly lower-level code?&lt;br&gt;
Yes, here we go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ReadLargeFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StringBuilder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FileStream&lt;/span&gt; &lt;span class="n"&gt;fileStream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FileStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileAccess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileShare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StreamReader&lt;/span&gt; &lt;span class="n"&gt;streamReader&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StreamReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileStream&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;line&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="n"&gt;line&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;streamReader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AppendLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&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="n"&gt;sb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&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;In the above case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are not dictating the size of the buffer to be read at a time(just do it line by line, however much that is)&lt;/li&gt;
&lt;li&gt;we don't have to keep track of the bytes read.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is much simpler to use.&lt;/p&gt;

&lt;p&gt;What about writing a file? Let's have a look next.&lt;/p&gt;

&lt;h2&gt;
  
  
  How To Write A File Using The &lt;strong&gt;FileStream&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This simple method takes a string path, creates a file a writes some content to it.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;FileStream&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;WriteText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//E.g. @"d:\text.txt"&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hey my dude!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;bytesArray&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fileStream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FileStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;fileStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytesArray&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;Using the &lt;code&gt;StreamWriter&lt;/code&gt; helper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;WriteText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//E.g. @"d:\text.txt"&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StreamWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&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;Note: The &lt;code&gt;File&lt;/code&gt; class provides some handy methods as well. The above example could've been written as: &lt;code&gt;File.WriteAllText(path, text);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's raise the same size problem again.&lt;/p&gt;

&lt;p&gt;What if you want to write a big ass file that is too large to fit in your RAM at once?&lt;/p&gt;

&lt;p&gt;You can use the same break it up as you write the same way we did when reading:&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;FileReader&lt;/code&gt; only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;WriteFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Create a new file at the specified path&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FileStream&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FileStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileAccess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Write the content to the file in chunks&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;chunkSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;chunkSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;remainingBytes&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bytesToWrite&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;remainingBytes&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;chunkSize&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;remainingBytes&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;chunkSize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytesToWrite&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;Using &lt;code&gt;FileReader&lt;/code&gt; and &lt;code&gt;StreamWriter&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;WriteLargeFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bufferSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bufferSize&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bytesRead&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FileStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileAccess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileShare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bufferSize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SequentialScan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StreamWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bytesToWrite&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bufferSize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytesToWrite&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Flush&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
                &lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;bytesToWrite&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, you can see that you still have to deal with the file chunks more directly.&lt;/p&gt;

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

&lt;p&gt;I hope streams make more sense to you now as it does for me.&lt;/p&gt;

&lt;p&gt;Thanks again for reading!&lt;/p&gt;

&lt;p&gt;If you like this article:&lt;/p&gt;

&lt;p&gt;Leave a comment (You can just say hi!)&lt;br&gt;
Let's connect on Twitter &lt;a href="https://twitter.com/theguspear" rel="noopener noreferrer"&gt;@theguspear&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Catch you next week,&lt;/p&gt;

&lt;p&gt;Gus.&lt;/p&gt;

</description>
      <category>welcome</category>
    </item>
    <item>
      <title>The Recursion Explanation I Wish I Had Read As A Self-Taught Developer</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Thu, 16 Feb 2023 02:53:23 +0000</pubDate>
      <link>https://forem.com/gustavupp/the-recursion-explanation-i-wish-i-had-read-as-a-self-taught-developer-3g4p</link>
      <guid>https://forem.com/gustavupp/the-recursion-explanation-i-wish-i-had-read-as-a-self-taught-developer-3g4p</guid>
      <description>&lt;p&gt;It took me several attempts to understand recursive functions.&lt;/p&gt;

&lt;p&gt;Once I understood how it works, I figured I didn't completely understand 2 things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Function’s Return Value&lt;/li&gt;
&lt;li&gt;The Callstack&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once I understood these 2, recursion made sense.&lt;/p&gt;

&lt;p&gt;This post is how I’d teach my beginner-self recursion if I could go back.&lt;/p&gt;

&lt;h1&gt;
  
  
  #1 Function’s Return Value
&lt;/h1&gt;

&lt;p&gt;When a function &lt;strong&gt;returns&lt;/strong&gt; a value, the function itself is &lt;strong&gt;replaced&lt;/strong&gt; by the return value.&lt;/p&gt;

&lt;p&gt;Let me try to exemplify:&lt;/p&gt;

&lt;p&gt;This is a function that returns the square of a number&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;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num&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="nf"&gt;square&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is the same 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;num&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="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;square(3)&lt;/code&gt; is "replaced" by 9, since 9 is the value that the function returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fw0qvbng3denkge0vtalv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw0qvbng3denkge0vtalv.png" alt="function return value" width="735" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cool? Great.&lt;/p&gt;

&lt;p&gt;Do you remember that in Maths sometimes the order or the operations mattered? This is the case when working with functions. The code interpreter can’t add 1 to &lt;code&gt;square(3)&lt;/code&gt; before it knows what the heck is the &lt;strong&gt;value&lt;/strong&gt; of that &lt;code&gt;square(3)&lt;/code&gt; returns. So functions have to be executed before the other calculations on the same line.&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;function&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//line 1&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num&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="nf"&gt;square&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="c1"&gt;//line 2&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;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//10        //line 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interpreter will run the above code in the following order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Line 1 (read the function declaration)&lt;/li&gt;
&lt;li&gt;Line 2 (Oh there is a function here, let's call this function passing the value 3 into it)&lt;/li&gt;
&lt;li&gt;Line 1 (function is called with 3 and returns with the value 9)&lt;/li&gt;
&lt;li&gt;Line 2 (it replaces the function with 9 and now can calculate 9 + 1 and assign it to num)&lt;/li&gt;
&lt;li&gt;Line 3 Finally we log 10 to the console.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The takeaway is that every time there is a function call, the interpreter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stops on that line and calls the function&lt;/li&gt;
&lt;li&gt;It runs the function code with the value passed in(if any)&lt;/li&gt;
&lt;li&gt;It goes back to the line that called it&lt;/li&gt;
&lt;li&gt;Replaces the function call with the &lt;strong&gt;returned&lt;/strong&gt; value&lt;/li&gt;
&lt;li&gt;Resume from there
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is essential to understand this to understand recursion(this was one of the things I had problems with that made it harder to understand recursion).&lt;/p&gt;

&lt;h1&gt;
  
  
  #2 The Callstack
&lt;/h1&gt;

&lt;p&gt;The call stack is how the interpreter(like the javascript interpreter in this browser) keeps track of the functions called by the script.&lt;/p&gt;

&lt;p&gt;That is how the interpreter knows how to come back to the line that has called the function.&lt;/p&gt;

&lt;p&gt;I’d like to think of it as a box to store the books you read and in which page and line you stop at each.&lt;/p&gt;

&lt;p&gt;Say you are in your local library. &lt;/p&gt;

&lt;p&gt;You start reading the book “Sapiens”. On page 72 line 87 it mentions the diprotodon(the biggest marsupial to walk on earth) which itches your curiosity. So you put a marker on the page and write down the line you are at and put the book in the box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhprb3jyn4veyvkcuil74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhprb3jyn4veyvkcuil74.png" alt="box with a book" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then you find a book about the diprotodons. But on page 94 line 138 it mentions the giant sloths and again curiosity kicks in. You put a marker on page 94 and write the line you are at on it and put it in the box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fovdt7gz9ekjexwta613k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fovdt7gz9ekjexwta613k.png" alt="2 boxes with books" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you read more about the giant sloths you realize it’s time to go home. You put another marker on page 121 line 147 and put the book in the box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbcybcme6i8mmnejoph8o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbcybcme6i8mmnejoph8o.png" alt="3 boxes with books" width="800" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next morning you rush to the library to finish your readings. &lt;/p&gt;

&lt;p&gt;How do you know in what book, page, and line you left off? Since you were organized with your stack of books and markers, the answer is easy. Just pick the top book from the box, the marker inside the book will tell the page and line.&lt;/p&gt;

&lt;p&gt;Know you can make your way back to the initial book("Sapiens") and keep reading until the end(or until curiosity strikes again)&lt;/p&gt;

&lt;p&gt;The analogy is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The box is the call stack&lt;/li&gt;
&lt;li&gt;Each book is a function call&lt;/li&gt;
&lt;li&gt;Each marker is in which line the function was called&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If I'd translate this example into code it would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;readSapiens&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="c1"&gt;//read until page 72 line 87&lt;/span&gt;
   &lt;span class="nf"&gt;readDiprotodonsBook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="c1"&gt;//finish reading sapiens(when we are back from reading about diprotodons)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;readDiprotodonsBook&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="c1"&gt;//read until page 94 line 138&lt;/span&gt;
   &lt;span class="nf"&gt;readGiatSlothsBook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="c1"&gt;//finish reading diprotodon's book(when we are back from reading about giant sloths)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;readGiatSlothsBook&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="c1"&gt;//read until page 121 line 147&lt;/span&gt;
   &lt;span class="k"&gt;debugger&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;readSapiens&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you hit f12 now, copy this code, paste it on the console, hit enter, and go to the sources tab you can see the call stack.&lt;/p&gt;

&lt;p&gt;It will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fonmgbsbxrehsqe19sw4o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fonmgbsbxrehsqe19sw4o.png" alt="the callstack" width="800" height="225"&gt;&lt;/a&gt;&lt;br&gt;
The code is paused inside the third function call.&lt;/p&gt;

&lt;p&gt;See that there is no magic, everything is built on top of data structures.&lt;/p&gt;

&lt;p&gt;Now we are prepared to go over an actual recursive function example.&lt;/p&gt;
&lt;h2&gt;
  
  
  Basic Example
&lt;/h2&gt;

&lt;p&gt;The simplest example I can think of is a function that adds all numbers until the number you passed in.&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;addNums&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;addNums&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: I am not taking into account 0 or negative numbers to simplify the 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="nf"&gt;addNums&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="c1"&gt;//6&lt;/span&gt;
&lt;span class="nf"&gt;addNums&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="c1"&gt;//15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's go through it line by line:&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;addNums&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;//line 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;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&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;//line 2&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;addNums&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//line 3&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;                             &lt;span class="c1"&gt;//line 4&lt;/span&gt;
                              &lt;span class="c1"&gt;//line 5&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sumThree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;addNums&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="c1"&gt;//line 6&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;sumThree&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//6    //line 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;[Lines 1 to 5]: Smooth execution, it just declares the function(now we can call it when need to)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;[Line 6]: The interpreter declares the variable &lt;code&gt;sumThree&lt;/code&gt; but before it assigns the value &lt;code&gt;addNums(3)&lt;/code&gt; he realizes that it is a function, so it has to call the function first(passing 3 into it) to then assign the return value to the variable &lt;code&gt;sumThree&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2.1. [Waiting on Line 6]: It calls the function on line 1, passing 3 into it. 3 is not equal to 1 so we move past line 2. On line 3 we should return &lt;code&gt;3 + addNums(3 - 1)&lt;/code&gt; but wait, this is another function call. So it has to call it first passing 2 into it.&lt;/p&gt;

&lt;p&gt;2.2. [Waiting on Line 6 [waiting on Line 3]]: It calls the function on line 1, passing 2 into it. 2 is not equal to 1 so we move past line 2. On line 3 we should return &lt;code&gt;2 + addNums(2 - 1)&lt;/code&gt; but wait, this is another function call. So it has to call it first passing 1 into it.&lt;/p&gt;

&lt;p&gt;2.3. [Waiting on Line 6 [waiting on Line 3 [waiting on Line 3]]]: It calls the function on line 1, passing 1 into it. This time we hit the base case. 1 === 1 is true, so for the &lt;strong&gt;first time&lt;/strong&gt; we return a value from a function call.&lt;/p&gt;

&lt;p&gt;2.4. [Waiting on Line 6 [waiting on Line 3]]: It goes back to line 3 of the previous function call. It now can calculate &lt;code&gt;2 + addNums(2 - 1)&lt;/code&gt; because it now knows that &lt;code&gt;addNums(2 - 1)&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;. So this function now can return &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;2.5. [Waiting on Line 6]: Lastly it goes back to the initial call on &lt;strong&gt;line 6&lt;/strong&gt;. It can now calculate &lt;code&gt;3 + addNums(3 - 1)&lt;/code&gt; because it now knows that &lt;code&gt;addNums(3 - 1)&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;. So this function now can finally return &lt;code&gt;6&lt;/code&gt;. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;[Line 7]: It logs &lt;code&gt;6&lt;/code&gt; to the console.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I also tried to explain it visually(to the best of my abilities), have a look:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbh3dqfqbdegva6705zj0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbh3dqfqbdegva6705zj0.png" alt="Image description" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;If you read this far, first I want to thank you, it means a lot to me. Second, if you got lost at any point in this post, can you do me a favor? &lt;/p&gt;

&lt;p&gt;Leave a quick comment saying where I lost you. &lt;/p&gt;

&lt;p&gt;I'll try to improve this post so it can help you that like me once had a hard time understanding recursion.&lt;/p&gt;

&lt;p&gt;Thanks again for reading!&lt;/p&gt;

&lt;p&gt;If you like this article:&lt;/p&gt;

&lt;p&gt;Leave a comment (You can just say hi!)&lt;br&gt;
Let's connect on Twitter &lt;a href="https://twitter.com/theguspear" rel="noopener noreferrer"&gt;@theguspear&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Catch you next week,&lt;/p&gt;

&lt;p&gt;Gus.&lt;/p&gt;

</description>
      <category>career</category>
      <category>productivity</category>
      <category>learning</category>
    </item>
    <item>
      <title>6 Reasons Mastering DSA Can Double Your Paycheck, Make You A Better Dev And Boost Your Confidence</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Sat, 04 Feb 2023 13:53:20 +0000</pubDate>
      <link>https://forem.com/gustavupp/6-reasons-mastering-data-structures-and-algorithms-is-the-fastest-way-to-double-your-paycheck-and-boost-your-confidence-127o</link>
      <guid>https://forem.com/gustavupp/6-reasons-mastering-data-structures-and-algorithms-is-the-fastest-way-to-double-your-paycheck-and-boost-your-confidence-127o</guid>
      <description>&lt;p&gt;I am sitting in a room with another 6 candidates.&lt;/p&gt;

&lt;p&gt;My hands are sweating and my heart pounding fast. It's hands down the most anxious I've felt my entire life. The door opens. The guy calls my name. It's my turn. I want to run away, instead, I stand up and mechanically follow him out of the room.&lt;/p&gt;

&lt;p&gt;This is not a software engineer interview.&lt;/p&gt;

&lt;p&gt;This is a simulator test to be an airline pilot.&lt;/p&gt;

&lt;p&gt;Developers are not the only ones doing scary interviews, aspiring pilots go through a sh** load to get a job. If you are wondering, I failed that interview 🤷‍♀️. But fast forward to today, I am glad I did.&lt;/p&gt;

&lt;p&gt;What both interviews have in common is that they require huge preparation.&lt;/p&gt;

&lt;p&gt;And the single skill that will get you in is DSA &lt;strong&gt;Data Structure and Algorithms&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Why? Here's:  &lt;/p&gt;

&lt;h2&gt;
  
  
  Reason #1 You Need them To Pocket The Fattest Paychecks In The Industry
&lt;/h2&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%2F5xp9bvs6277ny5vgj8f2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xp9bvs6277ny5vgj8f2.png" alt="quote"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not sure about you but I work for the money.&lt;/p&gt;

&lt;p&gt;I'm not a greedy dude, I couldn't care less about material stuff(I still use clothes from 10 years ago). But money buys something nothing else can. Freedom. And I want that very much.&lt;/p&gt;

&lt;p&gt;Mastering DSA is the fastest way to make +100k/year(not the easiest)&lt;/p&gt;

&lt;p&gt;Whether you like them or not, if you don't learn them you are missing out on the potential to be working for the coolest and highest-paying companies out there. Unfortunately(or fortunately for those who mastered them) that is how most big companies to interview.&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%2Fhlgkutafsddpxgrx37kn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhlgkutafsddpxgrx37kn.png" alt="quote"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The fastest you accept that this is how things work, and start putting in the work. The fastest you'll land your dream job dream paycheck.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reason #2 They Are The Cornerstone Of Computer Science
&lt;/h2&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%2Fatv50n6j60ztqfq5e1iq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fatv50n6j60ztqfq5e1iq.png" alt="quote"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I remember watching a mock whiteboard code interview on TY(Clement's channel) just after starting to learn to code.&lt;/p&gt;

&lt;p&gt;The problem seemed so complex, so out of my league I literally had no clue what was going on. Somehow the idea that I could have the knowledge to solve that kind of problem exited me. But I had a sea of concepts to navigate to get there.&lt;/p&gt;

&lt;p&gt;I've barely come a few miles off the shore.&lt;/p&gt;

&lt;p&gt;All software is built on top of DSA.&lt;/p&gt;

&lt;p&gt;The algorithm controls the logic and flow and data structures organize the data efficiently. This stuff won't be out of date next year(like javaScript frameworks) it hasn't changed since it was created. It's what I call perpetual knowledge. It makes everything else you learn after make more sense.&lt;/p&gt;

&lt;p&gt;You and I should spend our time using the ratio:&lt;/p&gt;

&lt;p&gt;80% foundations&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Algorithms&lt;/li&gt;
&lt;li&gt;Data structures&lt;/li&gt;
&lt;li&gt;Design pattern&lt;/li&gt;
&lt;li&gt;Mastering 1 Language&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;20% on the rest.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Frameworks&lt;/li&gt;
&lt;li&gt;Tools&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Reason #3 It Sharpens You Problem Solving Skills(The Most Important Skill In Life)
&lt;/h2&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%2Fcmkuks1viq1o1ervovx3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcmkuks1viq1o1ervovx3.png" alt="quote"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first few Leetcode puzzles I tried to solve were both eye-opening and discouraging.&lt;/p&gt;

&lt;p&gt;I didn't even know how to get started. I didn't know what the question was asking for. Problem-solving is a skill just like any other, it requires you to learn and practice to improve.&lt;/p&gt;

&lt;p&gt;My axe was dull.&lt;/p&gt;

&lt;p&gt;You are probably not going to be implementing merge sort or Dijkstra's algorithm on your daily job. Not even in Google.&lt;/p&gt;

&lt;p&gt;These problems are actually much harder than those you'll face daily. It's more like: if he can solve these problems he won't have a problem with his daily tasks. Companies want to know what is your thinking process for solving a problem(do you ask for clarification? for help? can you clearly explain what and why you are doing?)&lt;/p&gt;

&lt;p&gt;Problem-solving is the most sought-after skill in the industry.&lt;/p&gt;

&lt;p&gt;It's a sure way to fast-track your career.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reason #4 It Improves Your Ability To Understand Complex Topics
&lt;/h2&gt;

&lt;p&gt;Have you tried reading a DSA book from cover to cover?&lt;/p&gt;

&lt;p&gt;If you didn't fall asleep before the end of the first chapter you are my hero.&lt;/p&gt;

&lt;p&gt;The most effective way you and I learn anything is by using active learning methods(learn it, code it, run it, and test it). Especially complex topics like DSA. You won't learn it by passively reading a book or watching YT videos(no matter how many). You don't have to reinvent the wheel, smart people have developed systems that we can borrow to learn complex stuff.&lt;/p&gt;

&lt;p&gt;I like using &lt;a href="https://www.colorado.edu/artssciences-advising/resource-library/life-skills/the-feynman-technique-in-academic-coaching" rel="noopener noreferrer"&gt;Feynman's Technique&lt;/a&gt;  for most things.&lt;/p&gt;

&lt;p&gt;Mastering DSA will show that you, above all, know &lt;strong&gt;how to learn&lt;/strong&gt;, have a system to understand complex stuff, and can break down complex topics into understandable bits. And that is a skill that not a lot of people have.&lt;/p&gt;

&lt;p&gt;Tell me, if you have mastered DSA(you can solve and explain most problems) do you think you'll have any trouble understanding any other CS topic?&lt;/p&gt;

&lt;p&gt;And to top it off it shows you are committed and persistent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reason #5 It Gives You All The Tools You'll Need To Do Your Job Better And Faster
&lt;/h2&gt;

&lt;p&gt;Think of a carpenter that only has a hammer and a handsaw.&lt;/p&gt;

&lt;p&gt;He might be able to get away with those two but in most cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;screws would be a better choice for fastening&lt;/li&gt;
&lt;li&gt;a jigsaw for irregular cuts&lt;/li&gt;
&lt;li&gt;a circular saw for long-straight cuts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;He would do his job 5x faster and better just by using the right tools&lt;/p&gt;

&lt;p&gt;It's like the old saying: If you only have a hammer every problem will seem like a nail.&lt;/p&gt;

&lt;p&gt;Then, this same tradesman finds a bag of money and buys all the tools, and signs up for workshops to learn to operate all of them given by master craftsmen. This guy would easily double his earnings after that, wouldn't he?&lt;/p&gt;

&lt;p&gt;That is how I see learning DSA for devs.&lt;/p&gt;

&lt;p&gt;The difference is that we don't need to find a bag of money, you can learn it all for FREE online.&lt;/p&gt;

&lt;p&gt;This is the greatest advantage of a tech career.&lt;/p&gt;

&lt;p&gt;You can train as much as you want. It's only up to you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reason #6 All Of The Above Will Make You A Much More Confident Dev And Person.
&lt;/h2&gt;

&lt;p&gt;Do you know anyone with sloppy skills but that somehow gets great jobs?&lt;/p&gt;

&lt;p&gt;I bet this guy is confident.&lt;/p&gt;

&lt;p&gt;At the end of the day knowledge alone will only get you so far(80% of the way). You have to convince people that you know what your CV says you know. And they have to believe it to hire you.&lt;/p&gt;

&lt;p&gt;Scott Adams the creator of the comic strip Dilbert in his book “How to Fail At Almost Everything And Still Win Big” tells that he bullshitted his way to the top of the corporate world simply because he was good at interviews. He was a good communicator and therefore good at making people believe he was capable.&lt;/p&gt;

&lt;p&gt;If you are confident you show that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you value yourself&lt;/li&gt;
&lt;li&gt;you can take risks&lt;/li&gt;
&lt;li&gt;you can meet new challenges&lt;/li&gt;
&lt;li&gt;you can deliver&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And last but not least it helps you get a job.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Plan To Master DSA
&lt;/h2&gt;

&lt;p&gt;I don't plan on memorizing the implementation of every Data Structures or Algorithms.&lt;/p&gt;

&lt;p&gt;This would be like cramming for an exam. You might pass the exam but 1 week later you won't remember a thing. My intention is quite the opposite. I want to chuck DSA into my long-term memory.&lt;/p&gt;

&lt;p&gt;I want to persist this knowledge in my hard drive, not my RAM.&lt;/p&gt;

&lt;p&gt;Here is my plan:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="https://roadmap.sh/computer-science" rel="noopener noreferrer"&gt;roadmap&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The most common &lt;a href="https://www.educative.io/courses/grokking-coding-interview-patterns-javascript" rel="noopener noreferrer"&gt;patterns&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Practice the &lt;a href="https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions" rel="noopener noreferrer"&gt;blind 75 questions&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;I'll use the &lt;a href="https://www.colorado.edu/artssciences-advising/resource-library/life-skills/the-feynman-technique-in-academic-coaching" rel="noopener noreferrer"&gt;Feynman Techique&lt;/a&gt; and &lt;a href="https://www.tandfonline.com/doi/full/10.1080/20004508.2022.2092977" rel="noopener noreferrer"&gt;storytelling&lt;/a&gt; to make algorithms more memorable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I plan on writing posts as I go.&lt;/p&gt;

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

&lt;p&gt;It's 11:53pm on a Saturday night.&lt;/p&gt;

&lt;p&gt;I am the only one awake at home. I am working on my consistency by writing one article per week for at least 6 months. So here is the wrapper.&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%2Fy92tydx84zdtmi6yb32i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy92tydx84zdtmi6yb32i.png" alt="quote"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you don't plan on interviewing for a FANG(like me, at least not soon) I think you should still do it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It will make you a significantly better programmer
It will give you a Free confidence boost&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Grind now, and rip the benefits later.&lt;/p&gt;

&lt;p&gt;Time to hit the sack.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;If you like this article:&lt;/p&gt;

&lt;p&gt;Leave a comment (You can just say hi!)&lt;br&gt;
Let's connect on Twitter &lt;a href="https://twitter.com/theguspear" rel="noopener noreferrer"&gt;@theguspear&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Catch you next week,&lt;/p&gt;

&lt;p&gt;Gus.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>motivation</category>
      <category>productivity</category>
    </item>
    <item>
      <title>5 Life-Changing Lessons I Learned From Being Laid Off That Will Put You Ahead Of 97% Of People</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Sat, 28 Jan 2023 02:21:09 +0000</pubDate>
      <link>https://forem.com/gustavupp/5-life-changing-lessons-i-learned-from-being-laid-off-that-will-put-you-ahead-of-97-of-people-1fb7</link>
      <guid>https://forem.com/gustavupp/5-life-changing-lessons-i-learned-from-being-laid-off-that-will-put-you-ahead-of-97-of-people-1fb7</guid>
      <description>&lt;p&gt;I am sitting in this auditorium with 50 other coworkers.&lt;/p&gt;

&lt;p&gt;Our manager and the HR lady are there. They hand us paper and a pen, and that's when I realize we are all being fired. We had only been working there for over a year. &lt;/p&gt;

&lt;p&gt;In that year, my wife and I bought an apartment that cost us $1800 per month. That alone was more than what I made in my previous job.&lt;/p&gt;

&lt;p&gt;From one day to the next, I was &lt;strong&gt;unemployed&lt;/strong&gt; and +200k in &lt;strong&gt;debt&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here are the lessons I learned.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson #1 When People Are Fearful Is Time To Get Prepared
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9jktqun7lt6eoilysshl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9jktqun7lt6eoilysshl.png" alt="quote" width="773" height="528"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;job market&lt;/strong&gt; is like the &lt;strong&gt;stock market&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;When the media says that a company is doing amazingly well(stock &lt;strong&gt;price rises&lt;/strong&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Most people rush to buy it &lt;/li&gt;
&lt;li&gt;Smart investors hold or sell&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the media says that a company is going through tough times(stock &lt;strong&gt;price falls&lt;/strong&gt;) &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Most people sell desperately&lt;/li&gt;
&lt;li&gt;Smart investors buy more&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The job market behaves the same way&lt;/p&gt;

&lt;p&gt;When the media says that tech jobs are in &lt;strong&gt;high demand&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Most people start to get prepared to get a tech job &lt;/li&gt;
&lt;li&gt;Smart people are already prepared&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the media says that the tech sector will &lt;strong&gt;lay off&lt;/strong&gt; 40k jobs&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Most people give up on finding a tech job and go do something else&lt;/li&gt;
&lt;li&gt;Smart people get prepared&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Get prepared when most people are discouraged and you'll come out ahead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson #2 When People Are Fearful There Is Less Competition
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fu4ph2qgfvmfin8x4cumx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fu4ph2qgfvmfin8x4cumx.png" alt="quote" width="695" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bad news clears away the weak-minded competition.&lt;/p&gt;

&lt;p&gt;When most people are reluctant about the future, there are only a few consistent and diligent cats putting in the effort to get what they want(hopefully you are one of them)&lt;/p&gt;

&lt;p&gt;This plummets the candidates per job ratio.&lt;/p&gt;

&lt;p&gt;When hiring resumes, guess who is getting the jobs?&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson #3 Get Practical. Focus On What Is Within Your Control
&lt;/h2&gt;

&lt;p&gt;I sure didn't want to be fired, we had just bought an apartment!&lt;/p&gt;

&lt;p&gt;My bank account was as dry as the desert.&lt;/p&gt;

&lt;p&gt;Did I whine and moan about how unfair life was? &lt;/p&gt;

&lt;p&gt;Shit no, I saw that time as an opportunity and I went and got my commercial pilot's license during that time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fs6tmxzdycqrbt5469bmw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fs6tmxzdycqrbt5469bmw.png" alt="quote" width="757" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We spend far too much energy focusing on what is out of our control.&lt;/p&gt;

&lt;p&gt;It's &lt;strong&gt;wasted&lt;/strong&gt; energy since there is &lt;strong&gt;nothing&lt;/strong&gt; you can do about it.&lt;/p&gt;

&lt;p&gt;I learned to reframe the problem into a question that focuses on what &lt;strong&gt;I can do&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example: &lt;br&gt;
Big techs are laying people off and there are fewer jobs.&lt;/p&gt;

&lt;p&gt;Reframe it to:&lt;br&gt;
What &lt;strong&gt;can I do&lt;/strong&gt; to improve my chances of getting hired now that big techs are laying people off and there are fewer jobs?&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson #4 Rethink Your Lifestyle And Priorities
&lt;/h2&gt;

&lt;p&gt;As a flight attendant, I made 6x what I used to in IT Support.&lt;/p&gt;

&lt;p&gt;The same was true for most of my colleagues. But then, most of them upscaled their lifestyle to match their bigger salary. 1 year later, they were used to driving a new car, wearing fancy clothes, and eating at expensive restaurants.&lt;/p&gt;

&lt;p&gt;After getting fired, they had an awful time downscaling their newly acquired tastes and handing over their new cars to the bank.&lt;/p&gt;

&lt;p&gt;I was the same old dude, driving my old car, wearing the same clothes, and still eating cheaply.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhfje1h9roh6ir5c15mlg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhfje1h9roh6ir5c15mlg.png" alt="quote" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The most important stuff in life costs little or nothing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A good book&lt;/li&gt;
&lt;li&gt;Quality time with your family&lt;/li&gt;
&lt;li&gt;Coffee with a friend&lt;/li&gt;
&lt;li&gt;A walk in nature&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Less stuff, more knowledge, and experiences.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson #5 Consistency Pays Off
&lt;/h2&gt;

&lt;p&gt;I didn't learn this one when I was laid off(just couldn't leave it out of the list) &lt;/p&gt;

&lt;p&gt;This one took me a long time and many shoves to understand.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjihoed8xi5w43xmsooxi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjihoed8xi5w43xmsooxi.png" alt="Image description" width="751" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know how hard it is to keep consistent when all the odds seem to be against you.&lt;/p&gt;

&lt;p&gt;The reality is that &lt;strong&gt;nothing&lt;/strong&gt; that has the potential to change your life will happen in a month. Meaningful things like great work take time to be created. If you want to escape mediocrity, you have no other choice but to learn to be consistent.&lt;/p&gt;

&lt;p&gt;If you are like I was, you'll probably need to read: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can't Hurt Me (David Goggins) &lt;/li&gt;
&lt;li&gt;So Good They Can't Ignore You (Cal Newport)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Life is not a straight line.&lt;/p&gt;

&lt;p&gt;At times you are on top, at others you are at the bottom. What you'll always have is the choice to focus your energy where you are now or where you want to go. Learn to choose the apple over ice cream, the book over Netflix.&lt;/p&gt;

&lt;p&gt;If you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get prepared when people are fearful and competition is the lowest&lt;/li&gt;
&lt;li&gt;Get practical and focus on what is within your control&lt;/li&gt;
&lt;li&gt;Rethink your lifestyle and priorities&lt;/li&gt;
&lt;li&gt;And be consistent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I guarantee you'll be ahead of 97% of those around you within a year.&lt;/p&gt;

&lt;p&gt;Note: This post above all is a reminder to myself. The closest ears to my mouth are my own.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;If you like this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Leave a comment (You can just say hi!)&lt;/li&gt;
&lt;li&gt;Let's connect on Twitter &lt;a href="https://twitter.com/theguspear" rel="noopener noreferrer"&gt;@theguspear&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Catch you later,&lt;/p&gt;

&lt;p&gt;Gus.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>documentation</category>
    </item>
    <item>
      <title>5 Reasons To Choose JavaScript Maps Over Objects For Storing Key-Value Pairs(With Examples)</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Sat, 21 Jan 2023 09:51:29 +0000</pubDate>
      <link>https://forem.com/gustavupp/5-reasons-to-choose-javascript-maps-over-objects-for-storing-key-value-pairswith-examples-39dd</link>
      <guid>https://forem.com/gustavupp/5-reasons-to-choose-javascript-maps-over-objects-for-storing-key-value-pairswith-examples-39dd</guid>
      <description>&lt;p&gt;At 18 I started my "career" as an IT support guy.&lt;br&gt;
At 20 I was flying 80 hours per month as a flight attendant.&lt;br&gt;
At 22 I got my commercial airplane pilot license.&lt;br&gt;
At 29 I was a carpenter and today at 32 I work as a software developer.&lt;/p&gt;

&lt;p&gt;If there's one thing I know how to do well, it is how to keep up with changes. Because if you don't, you are left behind. I know firsthand how comfortable it is to use stuff you know by heart instead of giving the 'new' a try.&lt;/p&gt;

&lt;p&gt;But discomfort is what makes you grow, as a human and as a dev.&lt;/p&gt;

&lt;p&gt;Let's dive head first and see what the "newish" &lt;code&gt;Map&lt;/code&gt; can teach us today.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is the Map object?
&lt;/h2&gt;

&lt;p&gt;Here's the holy source's (MDN) definition:&lt;/p&gt;

&lt;p&gt;The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.&lt;/p&gt;

&lt;p&gt;To put it simply, Map is JavaScript's native hash or dictionary data structure.&lt;/p&gt;
&lt;h2&gt;
  
  
  Syntax: Map vs Object
&lt;/h2&gt;

&lt;p&gt;Here's a handy cheat sheet that does a great job at showing their syntax differences, posted by &lt;a href="https://twitter.com/reactive_dude" rel="noopener noreferrer"&gt;Andrej&lt;/a&gt; on Twitter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fqwrkz5px2ahdxa6i9d7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqwrkz5px2ahdxa6i9d7b.png" alt="Image description" width="800" height="683"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;He also recorded &lt;a href="https://www.youtube.com/watch?v=MH22Tuf65DI&amp;amp;t=2s" rel="noopener noreferrer"&gt;performance tests&lt;/a&gt; if you are interested.&lt;/p&gt;
&lt;h2&gt;
  
  
  Reason 1: You Won't Overwrite Default Keys By Accident
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Map&lt;/strong&gt;&lt;br&gt;
By default, a Map does not contain any keys. It is a clean slate. It only has what you put into it. &lt;/p&gt;

&lt;p&gt;Neither more nor less.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9qo5hzu3hvfohw7koppd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9qo5hzu3hvfohw7koppd.png" alt="Image description" width="270" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Object&lt;/strong&gt;&lt;br&gt;
Objects by default have their prototype, so it contains default keys that could potentially clash with your own keys.&lt;/p&gt;

&lt;p&gt;This can be bypassed by passing &lt;code&gt;null&lt;/code&gt; as the prototype when creating a new Object: &lt;code&gt;Object.create(null)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Check out the difference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New Object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F43wav4nctn3w3qeglpya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F43wav4nctn3w3qeglpya.png" alt="Image description" width="578" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New Object with &lt;code&gt;null&lt;/code&gt; prototype&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fj876nfoletesbk63mp3v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fj876nfoletesbk63mp3v.png" alt="Image description" width="287" height="108"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Reason 2: It Accepts Any Type As Key
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Map&lt;/strong&gt;&lt;br&gt;
Maps accept &lt;strong&gt;any&lt;/strong&gt; type of key. That includes functions, objects, and all primitives(string, number, boolean, symbol, undefined, null, and bigint).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&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;a&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;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;func&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//you can also initialize multiple values at once using array syntax&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;map&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;Map&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123&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="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;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&lt;/span&gt;&lt;span class="dl"&gt;'&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="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// 123, true, Object, () =&amp;gt; 'hey'&lt;/span&gt;
&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&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="c1"&gt;// 'object'&lt;/span&gt;
&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 'function'&lt;/span&gt;
&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&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="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="c1"&gt;// undefined &lt;/span&gt;
&lt;span class="c1"&gt;//Object and Functions are stored by reference, so { 'a':'a' } and obj are different objects)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Object&lt;/strong&gt;&lt;br&gt;
Object's key must be either a String or a Symbol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="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="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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;func&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;123&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;true&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;obj1&lt;/span&gt;&lt;span class="p"&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="na"&gt;func&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&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="c1"&gt;// ['123', 'true', 'obj1', 'func'] converts all keys to strings&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;func&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;//undefined&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;func&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// 'function'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reason 3: Maps Are Iterable
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Map&lt;/strong&gt;&lt;br&gt;
Maps are natively iterable. That means you can loop over them with &lt;code&gt;for of&lt;/code&gt; or &lt;code&gt;.forEach()&lt;/code&gt; loops.&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;map&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;Map&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="nf"&gt;set&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;zero&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;set&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//you can chain .set()&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;map&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="s2"&gt;`key: &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;, value: &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="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// key: 0, value: zero&lt;/span&gt;
&lt;span class="c1"&gt;// key: 1, value: one&lt;/span&gt;

&lt;span class="c1"&gt;//if you just want the 'values' or just the 'keys'&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// or map.values()&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;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="c1"&gt;// 1&lt;/span&gt;

&lt;span class="nx"&gt;map&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;value&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="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="s2"&gt;`key: &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;, value: &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="c1"&gt;// key: 0, value: zero&lt;/span&gt;
&lt;span class="c1"&gt;// key: 1, value: one&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Object&lt;/strong&gt;&lt;br&gt;
Objects are not iterable, even though you can iterate over them using &lt;code&gt;for in&lt;/code&gt; and &lt;code&gt;Object.Entries()&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;let&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="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;zero&lt;/span&gt;&lt;span class="dl"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&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;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="s2"&gt;`key: &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;, value: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;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="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// key: 0, value: zero&lt;/span&gt;
&lt;span class="c1"&gt;// key: 1, value: one&lt;/span&gt;

&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&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;forEach&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="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="s2"&gt;`key: &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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s2"&gt;, value: &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="mi"&gt;1&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="c1"&gt;// key: 0, value: zero&lt;/span&gt;
&lt;span class="c1"&gt;// key: 1, value: one&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reason 4: Maps Can Be Merged With Arrays, And Converted To Arrays
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Map&lt;/strong&gt;&lt;br&gt;
Maps are Arrays are 100% compatible, making it intuitive move from one to another.&lt;/p&gt;

&lt;p&gt;Here's how you can convert a &lt;code&gt;Map&lt;/code&gt; into an &lt;code&gt;Array&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;let&lt;/span&gt; &lt;span class="nx"&gt;map&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;Map&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&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="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;two&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//[ [1, 'one'], [2, 'two'] ] exactly the same array you initially passed in&lt;/span&gt;

&lt;span class="c1"&gt;//or you can use the spread operator&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;code&gt;Array&lt;/code&gt; into a &lt;code&gt;Map&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;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&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="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;two&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;new&lt;/span&gt; &lt;span class="nc"&gt;Map&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 =&amp;gt; 'one', 2 =&amp;gt; 'two' }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's how you can &lt;strong&gt;merge&lt;/strong&gt; a &lt;code&gt;Map&lt;/code&gt; and an &lt;code&gt;Array&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;let&lt;/span&gt; &lt;span class="nx"&gt;map&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;Map&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&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="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;two&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;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;three&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;combinedMap&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;Map&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;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// { 1 =&amp;gt; 'one', 2 =&amp;gt; 'two', 3 =&amp;gt; 'three' }&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;combinedArr&lt;/span&gt; &lt;span class="o"&gt;=&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;arr&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;// [ [1, 'one'], [2, 'two'], [3, 'three'] ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Object&lt;/strong&gt;&lt;br&gt;
Concerting an &lt;code&gt;Object&lt;/code&gt; into an &lt;code&gt;Array&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;let&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;two&lt;/span&gt;&lt;span class="dl"&gt;'&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="k"&gt;from&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="c1"&gt;// [] doesn't work&lt;/span&gt;

&lt;span class="c1"&gt;//you'd have to do&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&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="c1"&gt;//[ ['1', 'one'],['2', 'two'] ]&lt;/span&gt;

&lt;span class="c1"&gt;//or&lt;/span&gt;
&lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&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="c1"&gt;//[ ['1', 'one'],['2', 'two'] ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reason 5: You Can Easily Check The Size
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Map&lt;/strong&gt;&lt;br&gt;
Map has a built-in &lt;code&gt;size&lt;/code&gt; property that returns its size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;map&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;Map&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&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="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="s1"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&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;size&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Object&lt;/strong&gt;&lt;br&gt;
To check the size of an object you have to combine &lt;code&gt;Object.keys()&lt;/code&gt; with &lt;code&gt;.length&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;let&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;true&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The downside? No Native Method For Serialization And Parsing
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Map&lt;/strong&gt;&lt;br&gt;
Maps have no native support for serialization or parsing to and from JSON.&lt;/p&gt;

&lt;p&gt;The documentation suggests implementing your own by using the &lt;code&gt;replacer&lt;/code&gt; argument that can be passed to &lt;code&gt;JSON.stringify(obj, replacer)&lt;/code&gt; and the &lt;code&gt;reviver&lt;/code&gt; argument passed to &lt;code&gt;JSON.parse(string, reviver)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can find the suggested implementation &lt;a href="https://stackoverflow.com/questions/29085197/how-do-you-json-stringify-an-es6-map" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Object&lt;/strong&gt;&lt;br&gt;
You can natively serialize and parse an Object to and from JSON using &lt;code&gt;JSON.stringify()&lt;/code&gt; and &lt;code&gt;JSON.parse()&lt;/code&gt; respectfully. &lt;/p&gt;
&lt;h2&gt;
  
  
  2 Examples of how you can replace &lt;code&gt;Object&lt;/code&gt; with &lt;code&gt;Map&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;#1&lt;/strong&gt; Calculating the total price and items of an eCommerce shopping cart&lt;/p&gt;

&lt;p&gt;This is one of the examples from the &lt;a href="https://dev.to/gustavupp/a-complete-guide-to-javascripts-reduce-function-with-real-world-examples-5h6k"&gt;Complete Guide To JavaScript .reduce() Function&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Given the following array&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;shoppintCart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;amount&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;amount&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;amount&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="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to return an &lt;code&gt;Object&lt;/code&gt; like { totalItems: 6, totalPrice: 45 }&lt;/p&gt;

&lt;p&gt;This is the original code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;shoppintCart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentItem&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="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;totalItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;totalItems&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;totalPrice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;totalPrice&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&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="na"&gt;totalItems&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;totalPrice&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="c1"&gt;//initial value object&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// { totalItems: 6, totalPrice: 45 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a version using &lt;code&gt;Map&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="nx"&gt;shoppintCart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentItem&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;accumulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;totalItems&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;totalItems&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="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;totalPrice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;totalPrice&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="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="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;Map&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;totalItems&lt;/span&gt;&lt;span class="dl"&gt;'&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;totalPrice&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// { 'totalItems' =&amp;gt; 6, 'totalPrice' =&amp;gt; 45 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;#2&lt;/strong&gt; A Different Way To Remove Duplicate Objects From An Array&lt;/p&gt;

&lt;p&gt;This is a piece of code I actually used in the &lt;a href="https://gustavupp.github.io/book-lookUp/dist/index.html" rel="noopener noreferrer"&gt;bookshelf app&lt;/a&gt; I made when I started learning JavaScript.&lt;/p&gt;

&lt;p&gt;Googling about how to remove duplicate objects from an array led me to the example I share below.&lt;/p&gt;

&lt;p&gt;This is an array with one duplicate object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;How To Win Friends And Influence People&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dale Carnegie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;How To Win Friends And Influence People&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dale Carnegie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&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="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The Alchemist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Paulo Coelho&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;Here's a peculiar way to remove the duplicate:&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;uniqueObjsArr&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;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;books&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;book&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;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;])).&lt;/span&gt;&lt;span class="nf"&gt;values&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;There is a bit too much happening on the above one-liner.&lt;br&gt;
Let's break it down into chunks so it is easy to digest.&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;// 1. map the array into an array of arrays with `id` and `book`&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrayOfArrays&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&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;book&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;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;// arrayOfArrays:&lt;/span&gt;
&lt;span class="cm"&gt;/*
[ 
   [ 1, {id: 1, title: 'How To Win Friends And Influence People', author: 'Dale Carnegie' } ],
   [ 1, {id: 1, title: 'How To Win Friends And Influence People', author: 'Dale Carnegie' } ],
   [ 2, { title: 'Alchemist', author: 'Paulo Coelho' } ]
]
*/&lt;/span&gt;

&lt;span class="c1"&gt;// 2. The duplicate is automatically removed as keys have to be unique&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapOfUniqueObjects&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;Map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayOfArrays&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// mapOfUniqueObjects: &lt;/span&gt;
&lt;span class="cm"&gt;/*
{
   1 =&amp;gt; {id: 1, title: 'How To Win Friends And Influence People', author: 'Dale Carnegie'},
   2 =&amp;gt; { title: 'Alchemist', author: 'Paulo Coelho' }
}
*/&lt;/span&gt;

&lt;span class="c1"&gt;// 3. Convert the values back into an array.&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;finalResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;mapOfUniqueObjects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;()];&lt;/span&gt;
&lt;span class="c1"&gt;// finalResult:&lt;/span&gt;
&lt;span class="cm"&gt;/*
[
   {id: 1, title: 'How To Win Friends And Influence People', author: 'Dale Carnegie'},
   {id: 2, title: 'The Alchemist', author: 'Paulo Coelho'}
]
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;If you need to store key-value pairs(hash or dictionary) use &lt;code&gt;Map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you're only using string-based keys and need maximum read performance, then objects might be a better choice.&lt;/p&gt;

&lt;p&gt;Other than that, use whatever you want because, at the end of the day, this is just a blog post of a random dude on the internet Lol.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here's what we covered:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is the Map object?&lt;/li&gt;
&lt;li&gt;Syntax: Map vs Object&lt;/li&gt;
&lt;li&gt;Reason 1: You Won't Overwrite Default Keys By Accident&lt;/li&gt;
&lt;li&gt;Reason 2: It Accepts Any Type As Key&lt;/li&gt;
&lt;li&gt;Reason 3: Maps Are Iterable&lt;/li&gt;
&lt;li&gt;Reason 4: Maps Can Be Merged With Arrays, And Converted To Arrays&lt;/li&gt;
&lt;li&gt;Reason 5: You Can Easily Check The Size&lt;/li&gt;
&lt;li&gt;The downside? No Native Method For Serialization And Parsing&lt;/li&gt;
&lt;li&gt;2 Examples of how you can replace &lt;code&gt;Object&lt;/code&gt; with &lt;code&gt;Map&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;If you like this article:&lt;/p&gt;

&lt;p&gt;*Leave a comment below(You can just say hi!)&lt;br&gt;
*Follow me on Twitter&lt;a href="https://twitter.com/theguspear" rel="noopener noreferrer"&gt;@theguspear&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Catch you later,&lt;/p&gt;

&lt;p&gt;Gus.&lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>llm</category>
    </item>
    <item>
      <title>9 Common JavaScript Interview Questions You Might Not Know The Answer For.</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Sat, 14 Jan 2023 03:32:41 +0000</pubDate>
      <link>https://forem.com/gustavupp/9-common-javascript-interview-questions-you-might-not-know-the-answer-for-3a8g</link>
      <guid>https://forem.com/gustavupp/9-common-javascript-interview-questions-you-might-not-know-the-answer-for-3a8g</guid>
      <description>&lt;p&gt;Whether you like them or not, tricky questions are still asked by interviewers in the wild.&lt;/p&gt;

&lt;p&gt;The reason is, these questions can tell a lot about your core understanding of the language, and therefore if you are a fit for the job. &lt;/p&gt;

&lt;p&gt;Common concepts addressed in these questions include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hoisting&lt;/li&gt;
&lt;li&gt;Closure&lt;/li&gt;
&lt;li&gt;Scope&lt;/li&gt;
&lt;li&gt;Value vs Reference type&lt;/li&gt;
&lt;li&gt;Prototype inheritance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Today we will kill two birds with one stone.&lt;/p&gt;

&lt;p&gt;Prepare to ace your next interview and brush up on the core concepts, at once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why typeof &lt;code&gt;f&lt;/code&gt; is undefined?
&lt;/h2&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;y&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;f&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;y&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The condition statement &lt;code&gt;if(function f() {})&lt;/code&gt; returns &lt;code&gt;function f() {}&lt;/code&gt; which is truthy, so the code executes inside the if statement.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;typeof f&lt;/code&gt; returns &lt;code&gt;undefined&lt;/code&gt; because the function &lt;code&gt;f(){}&lt;/code&gt; was never declared outside the &lt;code&gt;if&lt;/code&gt; statement, so it doesn't "exist" outside the &lt;code&gt;if&lt;/code&gt; brackets &lt;code&gt;if (f(){}) { It doesn't exist here }&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is how we would "fix" this code snippet:&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;y&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;function&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt; &lt;span class="c1"&gt;//declare function outside if brackets&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;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//f exists so we enter the if block&lt;/span&gt;
  &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//here typeof f is function&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;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Write an example of closure
&lt;/h2&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;createFunction&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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;msg&lt;/span&gt; &lt;span class="o"&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;let&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hey &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;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dude&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Hey Dude&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first function (createFunction) returns an anonymous function&lt;/li&gt;
&lt;li&gt;The anonymous function returns the parameter &lt;code&gt;msg&lt;/code&gt; from the outer function(createFunction()) + &lt;code&gt;name&lt;/code&gt; from itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we declare the variable &lt;code&gt;javascript let myFunc = createFunction("Hey ")&lt;/code&gt; the variable &lt;code&gt;myFunc&lt;/code&gt; holds a reference to the anonymous function returned by the outer &lt;/p&gt;

&lt;p&gt;What is interesting is that when you call &lt;code&gt;javascript myFunc("Dude")&lt;/code&gt; (passing just the 'name' parameter) it still "remembers" the value of &lt;code&gt;msg&lt;/code&gt; that was passed in when &lt;code&gt;createFunction()&lt;/code&gt; was executed.&lt;/p&gt;

&lt;p&gt;The ability to access variables from the outer 'scope' where the function was created is one of the definitions of closure.&lt;/p&gt;

&lt;p&gt;Learn more about closures &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Write a multiply() function that can be called like multiply(2)(5)(10) and returns 100
&lt;/h2&gt;

&lt;p&gt;This is similar to the example above, the difference being that we are retuning an additional function and invoking all of them straight away(no using variables for holding references)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiply&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;z&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;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;z&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="nx"&gt;multiply&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;5&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By invoking multiply like&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;javascript multiply(2)(5)(10)&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 We are invoking the returned functions one after the other.&lt;/p&gt;

&lt;p&gt;We could use intermediate variables to make it clearer. Let's have a look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;func1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// x is 2&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// function(y) { return function(z) { return x*y*z } }&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;func2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;func1&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="c1"&gt;// y is 5&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// function(z) { return x*y*z }&lt;/span&gt;

&lt;span class="nx"&gt;func2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// z is 10&lt;/span&gt;
&lt;span class="c1"&gt;// finally has all 3 values and returns their product.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Learn more aboute Nested Functions&lt;a href="https://www.geeksforgeeks.org/javascript-nested-functions/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;delete&lt;/code&gt; operator on a local variable
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What would be the output of the following code?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;x&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;x&lt;/span&gt;&lt;span class="p"&gt;;&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*The delete operator is meant to be used to delete properties of objects, and not on value types(number in this case).&lt;/p&gt;

&lt;p&gt;This would work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;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;Gus&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;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;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'Gus' }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;delete&lt;/code&gt; operator on Object
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="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;Gus&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&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;person1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&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;person1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;person1&lt;/code&gt; is created with its prototype set to the &lt;code&gt;Person&lt;/code&gt; object. &lt;/li&gt;
&lt;li&gt;When the instance &lt;code&gt;person1&lt;/code&gt; has it's &lt;code&gt;age&lt;/code&gt; property deleted, we can still access the &lt;code&gt;age&lt;/code&gt; property of the prototype object(Person)&lt;/li&gt;
&lt;li&gt;That is why it seems like it didn't work.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a big topic, you can learn more &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;delete&lt;/code&gt; operator on Array
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What would be the result of logging arr.length ?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&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;b&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;c&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;d&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When used on arrays, the delete operator sets the delete element to 'empty' but does not remove it from the array, not changing the length of the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What will be the value of both console.log's?
&lt;/h2&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;favouriteAnime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dragon Ball&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;favouriteAnime&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;favouriteAnime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Naruto&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;favouriteAnime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;

&lt;span class="c1"&gt;// undefined &lt;/span&gt;
&lt;span class="c1"&gt;// Naruto&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is how the compiler interpreted this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;favouriteAnime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declared and initialized with undefined&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;favouriteAnime&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;favouriteAnime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Naruto&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;favouriteAnime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;

&lt;span class="nx"&gt;favouriteAnime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dragon Ball&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// undefined &lt;/span&gt;
&lt;span class="c1"&gt;// Naruto&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to keep in mind when declaring JavaScript functions and variables.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variable assignment(myVar = 5) takes precedence over function declaration(function func(){})&lt;/li&gt;
&lt;li&gt;Function declarations(function func(){}) take precedence over variable declarations(var myVar;) *let or const&lt;/li&gt;
&lt;li&gt;Function declarations(function func(){}) are hoisted over variable declarations(var myVar;) but not over variable assignments(myVar = 5;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a best practice, you should always declare your functions before invoking them.&lt;/p&gt;

&lt;p&gt;Learn more about hoisting &lt;a href="https://www.digitalocean.com/community/tutorials/understanding-hoisting-in-javascript"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How would you check if a number is an integer?
&lt;/h2&gt;

&lt;p&gt;The easiest way to check if a numner is a decimal ir integer is by using the built-in &lt;code&gt;Number.isInteger()&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isInteger&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="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;12.2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another way is to see if there is a remainder left when you divide by 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&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;num&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="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;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;isInt&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="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;12.2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: There is a limit to JavaScript precision when handling floating points. If you check &lt;code&gt;Number.isInteger(1.0000000000000001)&lt;/code&gt; will return &lt;code&gt;true&lt;/code&gt;. You can learn more &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger"&gt;here&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Remember to hit f12 and try out the examples for yourself.&lt;/p&gt;

&lt;p&gt;This tiny action will help you remember what you learned for much longer.&lt;/p&gt;

&lt;p&gt;This is a recap of what we have seen today:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why typeof &lt;code&gt;f&lt;/code&gt; is undefined?&lt;/li&gt;
&lt;li&gt;Write an example of closure&lt;/li&gt;
&lt;li&gt;Write a multiply() function that can be called like multiply(2)(5)(10) and returns 100&lt;/li&gt;
&lt;li&gt;Delete operator on a local variable&lt;/li&gt;
&lt;li&gt;Delete operator on Object&lt;/li&gt;
&lt;li&gt;Delete operator on Array&lt;/li&gt;
&lt;li&gt;What will be the value of both console.log's?&lt;/li&gt;
&lt;li&gt;How would you check if a number is an integer?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;If you like this article:&lt;/p&gt;

&lt;p&gt;*Leave a comment below(You can just say hi!)&lt;br&gt;
*Follow me on Twitter &lt;a href="https://twitter.com/theguspear"&gt;@theguspear&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Catch you later my dude,&lt;/p&gt;

&lt;p&gt;Gus.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>9 Essential Array Puzzles To Level Up Your JavaScript [JS Foundation]</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Sat, 07 Jan 2023 04:13:16 +0000</pubDate>
      <link>https://forem.com/gustavupp/9-essential-array-puzzles-to-level-up-your-javascript-js-foundation-2oim</link>
      <guid>https://forem.com/gustavupp/9-essential-array-puzzles-to-level-up-your-javascript-js-foundation-2oim</guid>
      <description>&lt;p&gt;There are no shortcuts to becoming a great problem solver(which is what a dev is).&lt;/p&gt;

&lt;p&gt;Mastery comes at the cost of practice. And since algorithms and data structures form the backbone of every software, practicing them should be on top of your priority queue. As the most used data structure for storing a sequence of values, arrays are as essential as they can be.&lt;/p&gt;

&lt;p&gt;Today we'll hone our skills by learning how to:&lt;/p&gt;

&lt;p&gt;*Flatten an array&lt;br&gt;
*Remove duplicates from an array&lt;br&gt;
*Remove duplicates from an array of objects&lt;br&gt;
*Sort an array of objects&lt;br&gt;
*Merge 2 arrays&lt;br&gt;
*Return the difference between two arrays&lt;br&gt;
*Check if an array contains duplicate elements&lt;br&gt;
*Return the Intersection of two arrays&lt;br&gt;
*Find the Longest String in an Array&lt;/p&gt;

&lt;p&gt;As the old saying goes "practice makes perfect". Let's crack it.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to flatten an array:
&lt;/h2&gt;

&lt;p&gt;The easiest way is to use the built-in .flat() method. &lt;br&gt;
The flat() method takes an optional maximum depth parameter.&lt;/p&gt;

&lt;p&gt;For example, given the following array:&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can flatten it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;flattened&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//max depth&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;flattened&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;p&gt;The flat() method returns a new array with all sub-arrays concatenated into it recursively up to the specified depth.&lt;/p&gt;

&lt;p&gt;You can also create your own recursive function that iterates over the elements of the array and adds them to a new array, one at a time. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If an element is an array, the function should call itself with the element as an argument, and then concatenate the resulting array to the new array. &lt;/li&gt;
&lt;li&gt;If an element is not an array, the function should simply add it to the new array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of how you could write a recursive function to flatten an array:&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;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="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="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;
    &lt;span class="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;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="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;result&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;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="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="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="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="mi"&gt;5&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="nf"&gt;flatten&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]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can make this function more concise by using the .reduce() method and the ternary operator.&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;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
    &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentItem&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;If you need a refresher on the .reduce() check this guide -&amp;gt; &lt;a href="https://dev.to/gustavupp/a-complete-guide-to-javascripts-reduce-function-with-real-world-examples-5h6k"&gt;https://dev.to/gustavupp/a-complete-guide-to-javascripts-reduce-function-with-real-world-examples-5h6k&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This function has a linear time complexity(which means that it is efficient for large arrays). &lt;br&gt;
It also has a constant space complexity(which means that it does not use up more memory as the size of the array increases).&lt;/p&gt;
&lt;h2&gt;
  
  
  How to remove duplicates from an array:
&lt;/h2&gt;

&lt;p&gt;By far the easiest and more concise way of removing duplicates of an array is by using the Set object. &lt;/p&gt;

&lt;p&gt;The Set object stores only unique values, so you can create a new set from the array and then use the Array.from() method to convert the set back into an array.&lt;/p&gt;

&lt;p&gt;This is one way you can do it using the Set object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;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;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="mi"&gt;5&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;unique&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;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;unique&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use our loved .reduce() (what can't you do with the reduce?? lol)&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;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="mi"&gt;5&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="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentItem&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;accumulator&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="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentItem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;
&lt;span class="p"&gt;},[])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to sort an array of objects
&lt;/h2&gt;

&lt;p&gt;To sort an array of objects you have to choose a property from the object to sort the array by.&lt;/p&gt;

&lt;p&gt;You can then call .sort() and pass it a comparator function. &lt;/p&gt;

&lt;p&gt;This is how the comparator function works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It takes two arguments&lt;/li&gt;
&lt;li&gt;If the first should come before the second it returns a negative value. &lt;/li&gt;
&lt;li&gt;If the first should come after the second it returns a positive value&lt;/li&gt;
&lt;li&gt;It returns 0 if the values are equal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how you could sort an array of objects by the value of the name property:&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="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;Bob&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;  
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;  
  &lt;span class="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;Charlie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//you can sort by any property&lt;/span&gt;
    &lt;span class="k"&gt;return&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;else&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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&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;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="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;span class="cm"&gt;/*
[
  {name: 'Alice', age: 25}, 
  {name: 'Bob', age: 30}, 
  {name: 'Charlie', age: 35}
] */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use the ternary operator to make a one-liner version:&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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;-&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;a&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;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Sometimes nested ternaries can be hard to read.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If sorting by a &lt;code&gt;string&lt;/code&gt; it's better to use the .localeCompare() method:
&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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;localeCompare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep in mind that the sort() method modifies the original array, so if you want to preserve the original array, you should make a copy of it first.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to merge 2 arrays
&lt;/h2&gt;

&lt;p&gt;To merge two arrays you can use the concat() method. &lt;br&gt;
The concat() method returns a new array that includes the elements from both arrays.&lt;/p&gt;

&lt;p&gt;Here is an example of how you could use concat() to merge two arrays:&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;arr1&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;merged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="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;merged&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;p&gt;A fancier or rather an ES6 version uses the spread operator (...). &lt;br&gt;
The spread operator expands the elements of an array into separate arguments.&lt;/p&gt;

&lt;p&gt;This is how it would look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;merged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="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;merged&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;p&gt;Both of these approaches: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new array that includes the elements from both arrays&lt;/li&gt;
&lt;li&gt;Do not modify the original array. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If modifying the array is not an issue you can simply use .push()  to add the elements of the other array to the end of the first array.&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;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="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;arr1&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;h2&gt;
  
  
  Return the difference between 2 arrays
&lt;/h2&gt;

&lt;p&gt;There are kinds of differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Non-symmetric&lt;/li&gt;
&lt;li&gt;Symmetric&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Non-symmetric&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Elements that are in the first array but not in the second&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Symmetric&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Elements that are in the first array but not in the second, and elements that are in the second but not in the first&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is how you can use the filter() method to find the non-symmetric difference between two arrays:&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;arr1&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="mi"&gt;5&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;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="mi"&gt;7&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;difference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;arr2&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="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is to get the symmetric difference:&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;arr1&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="mi"&gt;5&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;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="mi"&gt;7&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;difference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;arr2&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="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 6, 7]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach returns a new array and does not modify the original arrays.&lt;/p&gt;

&lt;p&gt;Alternatively, you can use the Set object and its .has() instead of .includes()&lt;/p&gt;

&lt;p&gt;Non-symmetric difference:&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="kd"&gt;set&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;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;difference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Symmetric difference:&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="kd"&gt;set&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;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;set1&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;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;difference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;set1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 6, 7]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  how to Check if an array contains duplicate elements
&lt;/h2&gt;

&lt;p&gt;The simplest way of doing this is by using the Set object and the size property. &lt;/p&gt;

&lt;p&gt;The Set object stores only unique values, so you can create a set from the array and compare the size of the set to the size of the original array. &lt;/p&gt;

&lt;p&gt;If the size of the set is smaller than the size of the array, it means that the array contains duplicate elements.&lt;/p&gt;

&lt;p&gt;Here's how you'd do 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;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="mi"&gt;5&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hasDuplicates&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;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hasDuplicates&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;p&gt;You can achieve the same with an object(also called a hash or dictionary) &lt;/p&gt;

&lt;p&gt;As you loop through the array you set the current value as the &lt;code&gt;key&lt;/code&gt; and &lt;code&gt;true&lt;/code&gt; as the keys' value. &lt;/p&gt;

&lt;p&gt;If you try to access a &lt;code&gt;key&lt;/code&gt; and there is already a value there, it means you found a duplicate.&lt;/p&gt;

&lt;p&gt;This is a way of doing 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;const&lt;/span&gt; &lt;span class="nx"&gt;hasDuplicates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
   &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
       &lt;span class="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;hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]){&lt;/span&gt;
           &lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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="nf"&gt;hasDuplicates&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;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both of these approaches create a new object (a set or a hash object) and do not modify the original array. &lt;br&gt;
They also have a linear time complexity(they are efficient for large arrays).&lt;/p&gt;
&lt;h2&gt;
  
  
  How to return the intersection between 2 arrays
&lt;/h2&gt;

&lt;p&gt;The most common way would be by using the filter() and includes().&lt;/p&gt;

&lt;p&gt;It would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="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="mi"&gt;5&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;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="mi"&gt;7&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;intersection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;arr2&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="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;intersection&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach returns a new array and does not modify the original arrays.&lt;/p&gt;

&lt;p&gt;As an alternative, you can use the Set object(again) and the .has() method&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="kd"&gt;set&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;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;intersection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;intersection&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you see a pattern? Whenever work something out with a plain object you can create a version using the Set Object and vice versa.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to find the longest string in an array
&lt;/h2&gt;

&lt;p&gt;To do so, you can use our popular .reduce() method and a function to compare the values. &lt;/p&gt;

&lt;p&gt;The comparator function will compare the length of the current string to the length of the previous longest string, and return the current string if it is longer, or the previous longest string if it is not.&lt;/p&gt;

&lt;p&gt;This is how you can do 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;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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;short&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;medium&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;long&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;longest&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;longest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&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;&amp;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;length&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;acc&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="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;longest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'longest'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach creates a new variable (the acc or accumulator) and does not modify the original array. &lt;/p&gt;

&lt;p&gt;The time complexity is linear (meaning that it is efficient for large arrays).&lt;/p&gt;

&lt;p&gt;The same can be achieved by using the sort() method and the spread operator (...) to find the longest string in an array more concisely:&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;longest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'longest'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In this article we have learned how to:&lt;/p&gt;

&lt;p&gt;*Flatten an array&lt;br&gt;
*Remove duplicates from an array&lt;br&gt;
*Remove duplicates from an array of objects&lt;br&gt;
*Sort an array of objects&lt;br&gt;
*Merge 2 arrays&lt;br&gt;
*Return the difference between two arrays&lt;br&gt;
*Check if an array contains duplicate elements&lt;br&gt;
*Return the Intersection of two arrays&lt;br&gt;
*Find the Longest String in an Array&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;If you like this article:&lt;/p&gt;

&lt;p&gt;*Leave a comment below(You can just say hi!)&lt;br&gt;
*Follow me on Twitter &lt;a href="https://twitter.com/theguspear" rel="noopener noreferrer"&gt;@theguspear&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Catch you later,&lt;/p&gt;

&lt;p&gt;Gus.&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>The Complete Guide To JavaScript's .reduce() Function (With Real-world Examples)</title>
      <dc:creator>Gus Pear 🍐</dc:creator>
      <pubDate>Sat, 31 Dec 2022 03:55:20 +0000</pubDate>
      <link>https://forem.com/gustavupp/a-complete-guide-to-javascripts-reduce-function-with-real-world-examples-5h6k</link>
      <guid>https://forem.com/gustavupp/a-complete-guide-to-javascripts-reduce-function-with-real-world-examples-5h6k</guid>
      <description>&lt;p&gt;In this guide we are going to cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is the .reduce() function?&lt;/li&gt;
&lt;li&gt;Creating our version of the .reduce() function&lt;/li&gt;
&lt;li&gt;Calculating the total items and price for a e-commerce shopping cart &lt;/li&gt;
&lt;li&gt;Creating an array of unique categories for a restaurant menu&lt;/li&gt;
&lt;li&gt;Removing duplicate objects from the array&lt;/li&gt;
&lt;li&gt;Replacing .filter().map() with .reduce()&lt;/li&gt;
&lt;li&gt;Grouping objects by key (Similar to SQL GROUP BY or C#'s .GroupBy())&lt;/li&gt;
&lt;li&gt;How to flatten an array of arrays&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://github.com/gustavupp/Short-Projects/blob/main/AlgorithmsAndDataStructures/reduceExamples.js" rel="noopener noreferrer"&gt;Link for all code snippets used in this guide&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  It seems confusing at first
&lt;/h2&gt;

&lt;p&gt;If you have ever struggled to understand the .reduce() function, you are not alone. It takes messing about with it a couple of times to really understand how it works and use it with confidence.&lt;/p&gt;

&lt;p&gt;Most of the examples out there use a simple array of numbers or strings, but the reality is that we normally work with complex objects which is rarely covered.&lt;/p&gt;

&lt;p&gt;But before we jump in.&lt;br&gt;
Here's how you can get more out of this content.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open your code edtitor and try the examples yourself&lt;/li&gt;
&lt;li&gt;tinker with it (alter values, add or remove elements to/from the array, etc)&lt;/li&gt;
&lt;li&gt;If you find yourself lost in one of the examples, track down the value of the variables either with a pen and paper or on your code editor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the .reduce() function?
&lt;/h2&gt;

&lt;p&gt;It is a method that executes a callback function on each element of an array. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The return value from the calculation is passed on to the next element. &lt;/li&gt;
&lt;li&gt;In the first iteration, there is no "return value of the previous calculation", if passed in, a initial value can be used in it's place. &lt;/li&gt;
&lt;li&gt;Otherwise, the first element of the array is used as the initial value and iteration starts from the next element.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To put it simply, it is just a variable and a loop.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The variable holds the new value&lt;/li&gt;
&lt;li&gt;The loop iterates over the array so we can grab what we want from each element&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This:&lt;br&gt;
&lt;a href="https://media2.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%2Frlb58wi7p4fx47u6jual.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Frlb58wi7p4fx47u6jual.png" alt="Image description" width="455" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Achieves the same as this:&lt;br&gt;
&lt;a href="https://media2.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%2Fcs3my28wqisj23jj8br9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fcs3my28wqisj23jj8br9.png" alt="Image description" width="666" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty much every time you need to use a variable and a loop, you can replace it with the reduce function.&lt;/p&gt;

&lt;p&gt;As there is no better way of learning than actually trying things out, let's build our version of the .reduce() method so we can really understand it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building our own .reduce()
&lt;/h2&gt;

&lt;p&gt;Let's do it by solving a simple problem: &lt;br&gt;
We have an array of numbers and we want the sum of all of them.&lt;/p&gt;

&lt;p&gt;It is just like the example above of a variable and a loop.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Frlb58wi7p4fx47u6jual.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Frlb58wi7p4fx47u6jual.png" alt="Image description" width="455" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We need 3 things:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an array &lt;/li&gt;
&lt;li&gt;a variable that holds the total value &lt;code&gt;total&lt;/code&gt;(usually called the accumulator)&lt;/li&gt;
&lt;li&gt;a loop to iterate over the array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's say you want to turn it into a function so you can use it easily, how would you do it?&lt;/p&gt;

&lt;p&gt;Maybe something along these lines:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fnf7qolc5n0lbi19lc4ol.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnf7qolc5n0lbi19lc4ol.png" alt="Image description" width="577" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We pass the array as a parameter into our function&lt;/li&gt;
&lt;li&gt;We return &lt;code&gt;total&lt;/code&gt; at the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is looking good, but what if we want the option to set the initial value of the variable &lt;code&gt;total&lt;/code&gt;? &lt;br&gt;
We can add the option as another parameter and set &lt;code&gt;total&lt;/code&gt; to its value.&lt;/p&gt;

&lt;p&gt;Like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Frbwke6oscwrp7n80q4qx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Frbwke6oscwrp7n80q4qx.png" alt="Image description" width="584" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But there are still 2 problems with this function. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There is no way we can add different logic to be executed inside the loop (It only adds the current element to the &lt;code&gt;sum&lt;/code&gt; variable)&lt;/li&gt;
&lt;li&gt;It works only on simple arrays (where each item is a value type, not an object)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I'll show you what I mean.&lt;/p&gt;

&lt;p&gt;Let's create an array of objects this time&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fwj081fcwz3y35pdks86v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fwj081fcwz3y35pdks86v.png" alt="Image description" width="749" height="75"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's invoke our myReducer function passing in the array of objects&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzlaydwql31yrsgsjpmii.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzlaydwql31yrsgsjpmii.png" alt="Image description" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It doesn't work because the value we want to add is inside the object and not the object itself. &lt;br&gt;
We have to access it as &lt;code&gt;total += arr[i].number&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But this function didn't account for that. &lt;/p&gt;

&lt;p&gt;However, if we add a callback function as a second parameter, we can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Execute the callback function on each element of the array&lt;/li&gt;
&lt;li&gt;Create any custom logic we need&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's modify our &lt;code&gt;myReducer&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8o50kemtzdf3t5cpo6zp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8o50kemtzdf3t5cpo6zp.png" alt="Image description" width="585" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's test it again with the objects array.&lt;/p&gt;

&lt;p&gt;First, declare the callback function and call &lt;code&gt;myReduce&lt;/code&gt; passing it in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fijuvd12pth51gdcxkm04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fijuvd12pth51gdcxkm04.png" alt="Image description" width="547" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that we added &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;else&lt;/code&gt; conditions based on the value of &lt;code&gt;total&lt;/code&gt; which is our initialValue option.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;If &lt;code&gt;initialValue&lt;/code&gt; is not given&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it doesn't execute the callback on 1st iteration&lt;/li&gt;
&lt;li&gt;There is nothing to add, initialValue is undefined.&lt;/li&gt;
&lt;li&gt;just set &lt;code&gt;total&lt;/code&gt; to the value of the current element&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If &lt;code&gt;initialValue&lt;/code&gt; is given&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It executes the callback on all iterations&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is pretty much what the original &lt;code&gt;Array.prototype.reduce()&lt;/code&gt; does.&lt;/p&gt;

&lt;p&gt;The main difference is that there's no need to pass the array as an argument as the original &lt;code&gt;.reduce()&lt;/code&gt; is bound to &lt;code&gt;Array.prototype&lt;/code&gt; so the array is always the array upon which &lt;code&gt;.reduce()&lt;/code&gt; was called. &lt;/p&gt;

&lt;h2&gt;
  
  
  Calculate total items and price for an ecommerce shopping cart
&lt;/h2&gt;

&lt;p&gt;Say you have an array like this one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9ht9f3f1d9cqkwoek907.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9ht9f3f1d9cqkwoek907.png" alt="Image description" width="346" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you need to reduce this array into an object like this &lt;code&gt;{ totalItems: 0, totalPrice: 0 }&lt;/code&gt; so we can display the correct information on the checkout page.&lt;/p&gt;

&lt;p&gt;This is how we can do it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F42hsycrt1xmwys6vd0k5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F42hsycrt1xmwys6vd0k5.png" alt="Image description" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create an array of unique categories for a restaurant menu
&lt;/h2&gt;

&lt;p&gt;Given this array:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fya18qnzn8f03pqivp1zh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fya18qnzn8f03pqivp1zh.png" alt="Image description" width="704" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need to reduce it into an array of unique categories like &lt;code&gt;['Appetizer','Entree','Main']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is how we can do it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7d253qu7r0191usgy2cx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7d253qu7r0191usgy2cx.png" alt="Image description" width="648" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing duplicate objects from the array
&lt;/h2&gt;

&lt;p&gt;Similar to the example above, but this time we need to return the same array of objects filtered with only unique objects.&lt;/p&gt;

&lt;p&gt;This is the array with a duplicate object:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fl0c5syyo5j02ymv9czce.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fl0c5syyo5j02ymv9czce.png" alt="Image description" width="396" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: You have to have a property similar to &lt;code&gt;id&lt;/code&gt; that is unique for each object&lt;/p&gt;

&lt;p&gt;This is how we can create another array with only unique elements:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8rmzbe0cintt6vnjjn5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8rmzbe0cintt6vnjjn5f.png" alt="Image description" width="706" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Replace .filter().map() with .reduce()
&lt;/h2&gt;

&lt;p&gt;It's very common having to filter an array and then modify the elements of the filtered array.&lt;/p&gt;

&lt;p&gt;We will use the same array of the menu categories above&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F80emyfw0mgn8wxrjp0mp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F80emyfw0mgn8wxrjp0mp.png" alt="Image description" width="743" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But this time we want to get an array with the &lt;code&gt;itemName&lt;/code&gt; only of the items whose category is 'Entree'&lt;/p&gt;

&lt;p&gt;We can use .filter() the array and then .map() to accomplish it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F4l32avcddc8q93mca9v4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4l32avcddc8q93mca9v4.png" alt="Image description" width="614" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or we can do it with .reduce()&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9ras2x6nf6twptyk837x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9ras2x6nf6twptyk837x.png" alt="Image description" width="667" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The advantage of using .reduce() is that you iterate over the array only once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Group objects by key (Similar to SQL GROUP BY or C#'s .GroupBy())
&lt;/h2&gt;

&lt;p&gt;This is the one I like the most. This is a very handy function that will be included in JavaScript's array methods in probably on next update (currently on stage 3 for approval)&lt;/p&gt;

&lt;p&gt;This groupBy function can also be used as a middle step to solving more complex problems.&lt;/p&gt;

&lt;p&gt;We again will use the menu categories array:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fa0e2d9ij3y0g2kc60zsf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fa0e2d9ij3y0g2kc60zsf.png" alt="Image description" width="703" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is our groupBy function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F411foq0g29k850hb7kja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F411foq0g29k850hb7kja.png" alt="Image description" width="800" height="709"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It can get even handier if we add the option to pass the &lt;code&gt;key&lt;/code&gt; as a function.&lt;/p&gt;

&lt;p&gt;Just replace this line:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fuvt6dwpsae5r1jepmhk1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fuvt6dwpsae5r1jepmhk1.png" alt="Image description" width="535" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fursvb7gg8clww8wtpii5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fursvb7gg8clww8wtpii5.png" alt="Image description" width="760" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way we can call it and pass a function that returns the key, as opposed to writing the string ourselves, avoiding typos.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjcwknn60or4w3ogkrcye.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjcwknn60or4w3ogkrcye.png" alt="Image description" width="799" height="63"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to flatten an array of arrays
&lt;/h2&gt;

&lt;p&gt;This is a one-liner to flatten an array of arrays:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F4bvb0o7hs4h14wgu7msk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4bvb0o7hs4h14wgu7msk.png" alt="Image description" width="800" height="83"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: It only works with one level of nesting. For multiple levels you have to write a recursive version.&lt;/p&gt;

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

&lt;p&gt;In this article we have learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is the .reduce() function&lt;/li&gt;
&lt;li&gt;How to create our version of the .reduce() function&lt;/li&gt;
&lt;li&gt;To calculate the total items and price for an e-commerce shopping cart &lt;/li&gt;
&lt;li&gt;To create an array of unique categories for a restaurant menu&lt;/li&gt;
&lt;li&gt;To remove duplicate objects from the array&lt;/li&gt;
&lt;li&gt;To replace .filter().map() with .reduce()&lt;/li&gt;
&lt;li&gt;How to group objects by key (Similar to SQL GROUP BY or C#'s .GroupBy())&lt;/li&gt;
&lt;li&gt;How to flatten an array of arrays&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading! &lt;/p&gt;

&lt;p&gt;If you like this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Leave a comment below&lt;/li&gt;
&lt;li&gt;Follow me on Twitter &lt;a href="https://twitter.com/theguspear" rel="noopener noreferrer"&gt;@theguspear&lt;/a&gt; for more content like this one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Catch you later, &lt;/p&gt;

&lt;p&gt;Gus.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
  </channel>
</rss>
