<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kevin Kononenko</title>
    <description>The latest articles on DEV Community by Kevin Kononenko (@kbk0125).</description>
    <link>https://dev.to/kbk0125</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%2F55456%2F42269bac-3e2b-41f6-a1c5-7a84edd26e6b.jpg</url>
      <title>DEV Community: Kevin Kononenko</title>
      <link>https://dev.to/kbk0125</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kbk0125"/>
    <language>en</language>
    <item>
      <title>Merge Sort Explained By Trying To Become A Tennis Champion</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Mon, 10 Feb 2020 04:17:27 +0000</pubDate>
      <link>https://dev.to/kbk0125/recursion-and-the-call-stack-explained-by-reading-a-book-a1</link>
      <guid>https://dev.to/kbk0125/recursion-and-the-call-stack-explained-by-reading-a-book-a1</guid>
      <description>&lt;p&gt;“Merge sort” is popular algorithm for sorting an array from smallest to largest. It is often compared to selection sort, insertion sort, bubble sort and many others.&lt;/p&gt;

&lt;p&gt;However, as I searched the internet for a simple explanation of how merge sort works… I could not find a guide that made it incredibly simple.&lt;/p&gt;

&lt;p&gt;Sure, there is a beautiful visualization over at &lt;a href="https://visualgo.net/bn/sorting"&gt;VisuAlgo&lt;/a&gt;, and FreeCodeCamp has a &lt;a href="https://guide.freecodecamp.org/algorithms/sorting-algorithms/merge-sort/"&gt;comprehensive text explanation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But, I still found myself staring at code blocks for a long time and wondering, “What exactly is happening in this line?”&lt;/p&gt;

&lt;p&gt;So, this guide will give an incredibly simple explanation of how merge sort actually works. It is kind of like a series of tennis tournaments.&lt;/p&gt;

&lt;p&gt;In order to understand this guide, you just need to know the basics of &lt;a href="https://blog.codeanalogies.com/2020/01/14/recursion-and-the-call-stack-explained-by-reading-a-book/"&gt;recursion&lt;/a&gt;. Let’s get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Basics of Merge Sort
&lt;/h3&gt;

&lt;p&gt;One of the fundamental ideas of merge sort, like all other basic JavaScript algorithms, is that you &lt;strong&gt;can only sort an array by comparing two elements at a time and finding the larger element.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, we need a way to run those comparisons as efficiently as possible.&lt;/p&gt;

&lt;p&gt;Let’s imagine that we have an array of 8 numbers that we need to sort from smallest to largest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&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;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="mi"&gt;2&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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Rather than thinking of these as numbers, let’s think of them as skill levels of tennis players, on a scale of 1-10. It’s our job to determine, “Who is the best tennis player of the group?”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nybs2xQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/allplayers.jpg%3Ffit%3D730%252C163%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nybs2xQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/allplayers.jpg%3Ffit%3D730%252C163%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;So, using merge sort, we need to rank this group from lowest skill to highest skill. We can do that by running a series of tennis matches and determining the winner of each one.&lt;/p&gt;

&lt;p&gt;But, in real tennis competitions, players are not forced to travel across the country to compete in one massive tournament. Instead, they must win a series of smaller tournaments before they can compete for the prize of national champion.&lt;/p&gt;

&lt;p&gt;Let’s imagine that we are trying to find the best amateur player in the United States.&lt;/p&gt;

&lt;p&gt;We can group these players into 4 regions: West, Mountain, Central, and East. It would look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t9oIqefy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/MergeSortTennisdiag1.jpg%3Ffit%3D730%252C643%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t9oIqefy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/MergeSortTennisdiag1.jpg%3Ffit%3D730%252C643%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The elements at index 0 and 1 in the array in purple are in the West region… you get the idea.&lt;/p&gt;

&lt;p&gt;We will start with 4 regional tournaments, and then run competitions between regional winners to determine a national champion.&lt;/p&gt;

&lt;p&gt;In other words, we will consistently find the “better” of two tennis players until we reach the national level. At the national level, the “better” player is really the “best” in the United States!&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up The Merge Sort Algorithm
&lt;/h3&gt;

&lt;p&gt;Okay, I admittedly chose 8 players because it is easy to show within a blog post. In order for the algorithm to work properly, it must be able to &lt;strong&gt;handle all arrays with at least 2 elements.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And, it needs to handle cases where there is an odd number of elements in the array ie 9 elements.&lt;/p&gt;

&lt;p&gt;There are really two parts of merge sort:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dividing the array of all tennis players into regional tournaments&lt;/li&gt;
&lt;li&gt;Running the tennis matches at a successively higher level until we are able to determine a national champion.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s why we need recursion: we have no idea how many matches need to be run until we know the size of the array. This algorithm must be able to handle 8 tennis players… or 350.&lt;/p&gt;

&lt;p&gt;We will cover the recursion part later. Now, let’s focus on part 2, the “competition” function that allows us to compare two tennis players and resort them based on their skill level. We will assume the better player wins every time.&lt;/p&gt;

&lt;p&gt;This function can be run an infinite number of times, depending on the size of the player pool.&lt;/p&gt;

&lt;p&gt;This function should take two arrays, and combine them into one properly sorted array, from smallest to largest. It should do this via “competitions”, or 1 on 1 comparisons.&lt;/p&gt;

&lt;p&gt;Here’s what this looks like for two arrays with two elements apiece. This might be the tournament that happens AFTER the regional tournaments have happened.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---ZtJffSg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/simplemergeGIF-1.gif%3Ffit%3D720%252C540%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---ZtJffSg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/simplemergeGIF-1.gif%3Ffit%3D720%252C540%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Here’s a couple key notes about the GIF above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We can only move one player at a time. This is because we only know if one player is better than the one we are facing. We can’t determine the absolute position of multiple players at once.&lt;/li&gt;
&lt;li&gt;One side of the tournament could have all the best players. Therefore, we need to be able to handle the case where only one side of the array has players remaining.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s what the code looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tournament&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&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;var&lt;/span&gt; &lt;span class="nx"&gt;rankings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&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;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&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;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;rankings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&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;rankings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&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;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;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;rankings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&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;rankings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&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;rankings&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;That’s a lot at once. Here’s a summary:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Line 3:&lt;/strong&gt; We start iterating through the players on both sides of the bracket. The number of iterations is determined by the longer array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lines 4-10:&lt;/strong&gt; We “compete” with the first element in each array. When we find a loser, we use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift"&gt;shift() method&lt;/a&gt; to remove the player from the tournament and add it to the next lowest spot in the rankings array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Last Line:&lt;/strong&gt; We return the rankings array with the players ranked from worst to best.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s an animated version of that code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WKnnHGRv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/mergesortdrawingwithcode1.gif%3Ffit%3D720%252C720%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WKnnHGRv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/mergesortdrawingwithcode1.gif%3Ffit%3D720%252C720%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Okay, now let’s move back to the first function to see how we split the players up into tournaments at the regional level and then combine them back into a national tournament.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Recursion Within Merge Sort
&lt;/h3&gt;

&lt;p&gt;Okay, we now have the function that allows us to run “competitions”, but we need a function to split up the array and put it back together.&lt;/p&gt;

&lt;p&gt;Before we can run any competitions, we need to organize the array into “regions” before we can run the first 1v1 competition.&lt;/p&gt;

&lt;p&gt;Here’s how we might go from 8 players of various skill levels to four 1v1 competitions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D6KG8ge9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/arraybreakdowndiagram.jpg%3Ffit%3D690%252C1024%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D6KG8ge9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/arraybreakdowndiagram.jpg%3Ffit%3D690%252C1024%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;There are 7 examples of an array being split into a smaller array or a single element. We cannot hardcode this number because if there were 16 players, there would be 15 examples of an array being split.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; in 1v1 comparisons, we can only tell which player is “better” than another. That’s why we need to break this down into 1v1 comparisons- so that all the smaller arrays are properly sorted before they are compared later.&lt;/p&gt;

&lt;p&gt;And, afterwards, we will reassemble the array after sorting the elements at every layer.&lt;/p&gt;

&lt;p&gt;Here's how the array will be split up into a series of 1v1 competitions:&lt;/p&gt;

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

&lt;p&gt;And here's how we will "reassemble" the array to find the ranking from smallest to largest:&lt;/p&gt;

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

&lt;p&gt;See the parallels between the array splitting and then reassembling? This is a great hint that we will need recursion.&lt;/p&gt;

&lt;p&gt;I will focus in on the “left” side of the array, or the first half. Here’s how we can build a call stack that will allow us to sort the array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XC0gDPnY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/mergesortsimpledrawings.gif%3Ffit%3D720%252C540%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XC0gDPnY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/mergesortsimpledrawings.gif%3Ffit%3D720%252C540%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Every time we split the array in half, we add a call to the call stack that references the previous call. At the end, we can run the tournament() function at each level to sort each smaller array before mergining them.&lt;/p&gt;

&lt;p&gt;Here’s what the code looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;findWinner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;players&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;players&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;lt;=&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;players&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;middle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&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;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;middle&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;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;middle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;players&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;tournament&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;findWinner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;findWinner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;right&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;players&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;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="mi"&gt;2&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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;findWinner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Lines 3-5 allow us to define a midpoint in the array, and split the array down the midpoint. When we do this recursively, we shrink the array until it is a single element.&lt;/p&gt;

&lt;p&gt;The most important code is in lines 2 and 6.&lt;/p&gt;

&lt;p&gt;In line 2, we handle the case where the array has been shrunken to 1 element. This tells us that the recursion should stop, and we can run the lowest-level, regional tournament.&lt;/p&gt;

&lt;p&gt;In line 6, we define that in each call, we will run the tournament() function on the sorted array from the previous call (or a 1v1 matchup, if it is the lowest level)&lt;/p&gt;

&lt;p&gt;Here’s what this looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k0SChUoV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/sortwithtennisplayers.gif%3Ffit%3D720%252C720%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k0SChUoV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/02/sortwithtennisplayers.gif%3Ffit%3D720%252C720%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;In the example above, we have gotten to the level of 1v1 in the “West” and “Mountain” region. So, we can start at the top of the call stack and find the top player by the time we get to the end of the call stack using the tournament() function multiple times.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get The Latest Tutorials
&lt;/h3&gt;

&lt;p&gt;Did you enjoy this guide? Get my latest visual explanations of HTML, CSS and JavaScript topics on the &lt;a href="https://codeanalogies.com"&gt;CodeAnalogies blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Recursion and the Call Stack Explained By Reading A Book</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Wed, 22 Jan 2020 02:14:19 +0000</pubDate>
      <link>https://dev.to/kbk0125/recursion-and-the-call-stack-explained-by-reading-a-book-4khm</link>
      <guid>https://dev.to/kbk0125/recursion-and-the-call-stack-explained-by-reading-a-book-4khm</guid>
      <description>&lt;p&gt;Recursion is one of the most exciting principles of all programming languages.&lt;/p&gt;

&lt;p&gt;A non-recursive function (in other words, the functions that you have used in the past) will run once every time it is called, and output via a return statement.&lt;/p&gt;

&lt;p&gt;However, a &lt;em&gt;recursive&lt;/em&gt; function can be called once and then &lt;strong&gt;call itself an undetermined number of times&lt;/strong&gt; before combining the output of all the function calls in one return statement.&lt;/p&gt;

&lt;p&gt;It kind of looks like this. &lt;/p&gt;

&lt;p&gt;Static version:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jo38tJTU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ppp6jbdbobm3fw9bf1vz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jo38tJTU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ppp6jbdbobm3fw9bf1vz.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Dynamic version:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4rrEyeop--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/u206b4q3jinh5ghhbc0b.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4rrEyeop--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/u206b4q3jinh5ghhbc0b.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The idea that a single function can be called one to infinity times via a single statement is what makes it so exciting!&lt;/p&gt;

&lt;p&gt;At the same time, it is difficult to think of a real-world analogy to this situation. And it gets especially difficult once we discuss the &lt;strong&gt;call stack&lt;/strong&gt; , which we will cover later.&lt;/p&gt;

&lt;p&gt;Some have suggested a series of infinite boxes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fQZmX2K9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/image-1.png%3Ffit%3D730%252C514%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fQZmX2K9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/image-1.png%3Ffit%3D730%252C514%26ssl%3D1" alt=""&gt;&lt;/a&gt;&lt;em&gt;Image Cred: &lt;a href="https://www.manning.com/books/grokking-algorithms"&gt;Grokking Algorithm&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Others have suggested the imagery of “Russian nesting dolls”:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jLs3YKtT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/miro.medium.com/max/669/1%2AHgviugi5d0AZcFgy1-xVqQ.jpeg%3Fw%3D730%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jLs3YKtT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/miro.medium.com/max/669/1%2AHgviugi5d0AZcFgy1-xVqQ.jpeg%3Fw%3D730%26ssl%3D1" alt="Image result for recursion russian dolls"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;However, this is also unhelpful when understanding the call stack.&lt;/p&gt;

&lt;p&gt;So, in this tutorial, we will show two popular examples of recursion, and build a visual language for understanding the function and the &lt;strong&gt;call stack&lt;/strong&gt; , which determines how to make sense of the many function calls in a row.&lt;/p&gt;

&lt;p&gt;Before continuing with this tutorial, you should have a firm understanding of functions in JavaScript. &lt;a href="https://blog.codeanalogies.com/2019/04/14/javascripts-arrow-functions-explained-by-going-down-a-slide/"&gt;Check out this guide to arrow functions&lt;/a&gt; to learn more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1- Factorials
&lt;/h3&gt;

&lt;p&gt;Factorials are the most popular example of recursion.&lt;/p&gt;

&lt;p&gt;You might be familiar with factorials from algebra class.&lt;/p&gt;

&lt;p&gt;They are expressed like: 3!&lt;/p&gt;

&lt;p&gt;And that notation evaluates to 3*2*1, or 6.&lt;/p&gt;

&lt;p&gt;We could express this as a “for” loop where we update a variable outside the loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&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;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;factorial&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;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;i&lt;/span&gt;&lt;span class="o"&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="o"&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But, here we will use recursion instead. Rather than have a loop that updates a variable outside of its scope, we can use recursion with a function and have &lt;em&gt;n-1&lt;/em&gt; calls, where &lt;em&gt;n&lt;/em&gt; is the factorial we want to find.&lt;/p&gt;

&lt;p&gt;I will show the code first, and then we can evaluate how it works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;getFactorial&lt;/span&gt; &lt;span class="o"&gt;=&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="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;num&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="mi"&gt;1&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;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;getFactorial&lt;/span&gt;&lt;span class="p"&gt;(&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="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;getFactorial&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;// 24&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Woah! This accomplishes the same thing as the code block above.&lt;/p&gt;

&lt;p&gt;But, if you look at line 5, you can see that the &lt;em&gt;return&lt;/em&gt; statement includes a reference to the function itself.&lt;/p&gt;

&lt;p&gt;So… when does this function return a final value, exactly? How do we link the 4 calls of the function together to return the value of 24?&lt;/p&gt;

&lt;p&gt;This is where the &lt;strong&gt;call stack&lt;/strong&gt; becomes useful. It determines the rules for the order that these function calls will return.&lt;/p&gt;

&lt;p&gt;But, now we are stacking two concepts on top of each other: recursion and call stack. That’s a lot at once.&lt;/p&gt;

&lt;p&gt;In order to visualize the call stack, let’s think of a stack that builds &lt;strong&gt;from left to right&lt;/strong&gt;. Every time a block gets added, it is added to the left side of the stack and pushes the other blocks to the right.&lt;/p&gt;

&lt;p&gt;So, as we go through this recursive function, we generate a stack like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lGzVWMvu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/recursionGIFsNum1.gif%3Ffit%3D720%252C540%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lGzVWMvu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/recursionGIFsNum1.gif%3Ffit%3D720%252C540%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The call stack is being generated at the bottom of the screen throughout the GIF above. At the end, we are left with 1*2*3*4 which results in 24.&lt;/p&gt;

&lt;p&gt;The call stack is composed of 4 function calls, and &lt;strong&gt;none of them run until the function returns 1&lt;/strong&gt;. They sit on the stack until the last value is added, in this case 1.&lt;/p&gt;

&lt;p&gt;This is because each of the first 3 calls includes a reference to the next call on the stack!&lt;/p&gt;

&lt;p&gt;So, when num=4, the function returns 4*getFactorial(3). Of course, that cannot actually return a value until we know the value of getFactorial(3). That’s why we need a call stack!&lt;/p&gt;

&lt;p&gt;So, recursion allows a function to be called an indefinite number of times in a row AND it updates a call stack, which returns a value after the final call has been run.&lt;/p&gt;

&lt;p&gt;The call stack updates from left to right, and then you can read all the calls in the order they are resolved. Most recent is resolved first, first call resolved last.&lt;/p&gt;

&lt;p&gt;However, our GIF above did not do a good job of showing this relationship between each call. So, here’s an updated version that shows how all the calls are connected via the &lt;em&gt;return&lt;/em&gt; statement:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yEOAytJY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/recursionGIFsfactorialnum2.gif%3Ffit%3D720%252C540%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yEOAytJY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/recursionGIFsfactorialnum2.gif%3Ffit%3D720%252C540%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2- Splitting a String
&lt;/h3&gt;

&lt;p&gt;In the example above, we used a mathematical example that resembled a question from algebra class.&lt;/p&gt;

&lt;p&gt;This works, but there are also plenty of examples of recursion that go beyond math. In this example, we will show how you can use recursion to manipulate a string.&lt;/p&gt;

&lt;p&gt;Here’s the challenge: Reverse a string.&lt;/p&gt;

&lt;p&gt;In other words, return a string with the letters of an input string in the opposite order.&lt;/p&gt;

&lt;p&gt;You can also do this with a “for” loop, but we will use recursion in this example.&lt;/p&gt;

&lt;p&gt;Let’s think about how we should reverse the string “cat”.&lt;/p&gt;

&lt;p&gt;Every time we run a function call, we need to isolate the first or last letter of the string, and then chop off a letter from the string. When we run the function again, we should again grab the first or last letter.&lt;/p&gt;

&lt;p&gt;At the end, the call stack will allow us to return the letters in the correct order.&lt;/p&gt;

&lt;p&gt;Here’s the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;testStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&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;revStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;revStr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substr&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="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="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;revStr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;testStr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// 'tac'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Okay. Let’s dig in, just like we did above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RNCsV7A9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/recursionex2v1.gif%3Ffit%3D720%252C540%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RNCsV7A9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/recursionex2v1.gif%3Ffit%3D720%252C540%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Again, although the GIF above makes it look easy, we need to dig deeper into the final &lt;em&gt;return&lt;/em&gt; statement if we want to truly understand these function calls.&lt;/p&gt;

&lt;p&gt;There is one more important difference in this example compared to the one above- we are doing string concatenation rather than multiplication.&lt;/p&gt;

&lt;p&gt;Therefore, the order of the strings in that &lt;em&gt;return&lt;/em&gt; statement matters quite a bit, because it determines which order we will use for concatenation.&lt;/p&gt;

&lt;p&gt;Since this is not a series of multiplication problems, the call stack is a little easier to understand. Here is a visual:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B-Ug2Flt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/recursionex2.gif%3Ffit%3D720%252C540%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B-Ug2Flt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/recursionex2.gif%3Ffit%3D720%252C540%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This is why the order of the strings matters so much- as we build the call stack in the GIF above, there is a specific order of the recursive function call and the string fragment (&lt;em&gt;str[0]&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;As we run all the calls in the stack, this order allows us to rebuild the string in the reverse order.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get The Latest Tutorials
&lt;/h3&gt;

&lt;p&gt;Did you enjoy this tutorial? Check out the &lt;a href="https://codeanalogies.com"&gt;CodeAnalogies blog&lt;/a&gt; to get the latest visual web development tutorials.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Bubble Sort Algorithm Explained By Picking Teams At Recess</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Wed, 15 Jan 2020 02:43:11 +0000</pubDate>
      <link>https://dev.to/kbk0125/bubble-sort-algorithm-explained-by-picking-teams-at-recess-3f19</link>
      <guid>https://dev.to/kbk0125/bubble-sort-algorithm-explained-by-picking-teams-at-recess-3f19</guid>
      <description>&lt;p&gt;There is a common problem in all coding languages around learning algorithms.&lt;/p&gt;

&lt;p&gt;The number of lines of code &lt;strong&gt;does not match up with the complexity of the code.&lt;/strong&gt; It also does not reflect the &lt;strong&gt;amount of mental simulation you need to do&lt;/strong&gt; in order to understand the code!&lt;/p&gt;

&lt;p&gt;Here’s what I mean- let’s say that you have a simple 5-10 line algorithm (like bubble sort). This algorithm can be wrapped in a function and then used with pretty much any array.&lt;/p&gt;

&lt;p&gt;But, since it is so short, you might assume that it is relatively simple to understand. After all, experienced programmers can read it once and then quickly recognize a simple sorting algorithm.&lt;/p&gt;

&lt;p&gt;However, every newbie will read the code, and by line 3, they will realize that it is MUCH more complicated than they anticipated.&lt;/p&gt;

&lt;p&gt;In this guide, we will talk about how to understand the bubble sort algorithm, and get around the cognitive traps that make it so difficult to understand.&lt;/p&gt;

&lt;p&gt;First of all, let’s get a quick definition out of the way: an &lt;strong&gt;algorithm&lt;/strong&gt; is comprised of multiple lines of code that achieve a specific task.&lt;/p&gt;

&lt;p&gt;It’s a pretty wide-ranging definition.&lt;/p&gt;

&lt;p&gt;In this example, the &lt;strong&gt;bubble sort algorithm&lt;/strong&gt; is meant to sort a series of elements in an array from smallest to largest.&lt;/p&gt;

&lt;p&gt;We are going to do a line-by-line breakdown of bubble sort that uses &lt;strong&gt;space&lt;/strong&gt; and &lt;strong&gt;motion&lt;/strong&gt; to explain what is happening in each line. Your brain uses space and motion to understand the relationship between multiple objects.&lt;/p&gt;

&lt;p&gt;Here’s a real-world scenario that you might be familiar with- you and your friends want to play soccer at recess. So, you have to pick teams before you get started.&lt;/p&gt;

&lt;p&gt;Bubble sort helps you answer the question:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Who should be picked first…second…third… etc. out of all the players?”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lgO0-OpR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/initschooldiagram.png%3Ffit%3D730%252C403%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lgO0-OpR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/initschooldiagram.png%3Ffit%3D730%252C403%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;So, let’s re-rank these players using bubble sort! Players are ranked on a 1-10 scale.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Basic Setup of Bubble Sort
&lt;/h3&gt;

&lt;p&gt;Here’s the array that we will use in this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;players&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;6&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;9&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;2&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;10&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We need to reorder this array from smallest to largest. So, if we want to get the player ranked a “10” into the last position in the array, here is what that would look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x-ToBVsT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/image.png%3Ffit%3D572%252C614%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x-ToBVsT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/image.png%3Ffit%3D572%252C614%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The player at index 7 in the array needs to move 2 spots to get to index 9, the last element.&lt;/p&gt;

&lt;p&gt;So, there are 2 key questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How do we move elements through the array?&lt;/li&gt;
&lt;li&gt;How do we reorder the elements correctly as they are moving?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As for “moving” elements through the array, we must iterate through the entire array to move elements. We can do this with:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A “for” loop&lt;/li&gt;
&lt;li&gt;A “do/while” loop&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this example, I will use a “for” loop. Then, as we are moving elements, we will need to decide how far to move each element within the array.&lt;/p&gt;

&lt;p&gt;We can create comparisons via “if” statements. So, every time we move an element in the array, we must use an “if” statement to see if it should be moved (and how far it should be moved)&lt;/p&gt;

&lt;p&gt;So, here’s an animated version of how the player with a rating of 10 goes from index 7 to index 9:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q9H_nwbq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/BubbleSortDrawing1GIF.gif%3Ffit%3D720%252C540%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q9H_nwbq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/BubbleSortDrawing1GIF.gif%3Ffit%3D720%252C540%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the index is 6, we compare the player at index 6 to index 7. Since 1 &amp;lt; 10, we do not change the order.&lt;/li&gt;
&lt;li&gt;When the index is 7, we compare the player at index 7 to index 8. Since 10 &amp;gt; 4, we move the element in index 7 back to index 8.&lt;/li&gt;
&lt;li&gt;When the index is 8, we compare the player at index 8 to index 9. Since 10 &amp;gt; 5, we move the element in index 8 back to index 9.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That’s an example of one element that we need to move. Now let’s see how to take this action on every element with bubble sort.&lt;/p&gt;

&lt;h3&gt;
  
  
  Turning it into an Algorithm
&lt;/h3&gt;

&lt;p&gt;Now we need to think about how to convert this to a scalable algorithm that will work on any array of numbers.&lt;/p&gt;

&lt;p&gt;As shown above, we will need to iterate through the array, so let’s start off with a “for” loop. We will create a function called &lt;em&gt;bubbleSort&lt;/em&gt; that takes an array as an argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bubbleSort&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;length&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="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;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="c1"&gt;// rest of function goes here&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;bubbleSort&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;players&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 one important fact that will define the rest of this algorithm: &lt;strong&gt;we can only move elements through the array by comparing the element to a single other element in the array.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If it was possible to have some sort of “memory”, we would be able to reorder these elements in one complete iteration through the array. But we do not have any “memory”, so we will need to compare elements one by one.&lt;/p&gt;

&lt;p&gt;Here’s what one iteration through the array would look like. We still haven’t written the logic to make it work, that comes next:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c-UQpYI5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/BubbleSortDrawingsGif2.gif%3Ffit%3D720%252C720%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c-UQpYI5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/BubbleSortDrawingsGif2.gif%3Ffit%3D720%252C720%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;There is a big issue: when we get to the end of the array, it is not properly sorted! Only the best player has achieved the highest rank. In the rest of the array, values are still out of order.&lt;/p&gt;

&lt;p&gt;In fact, since we can only move elements one by one, we will need to iterate through the array up to the length of the array. In this case, that means we may need to iterate through 10 times! That means we will need nested “for” loops.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finishing Bubble Sort
&lt;/h3&gt;

&lt;p&gt;Okay, so now we need to add a second “for” loop within the first “for” loop. This is where we need to write the “swapping” logic.&lt;/p&gt;

&lt;p&gt;Let’s figure out the swapping logic. We need to compare two elements: the element with the current index. and the next element. So, the element with the current index + 1. If the current element is bigger, we will flip the positions.&lt;/p&gt;

&lt;p&gt;The “if” statement is pretty simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But, beyond that, we need to store the current index in a separate variable if the swap is going to happen.&lt;/p&gt;

&lt;p&gt;Remember this example? When the player with a “10” rating goes below the other elements, it is being stored in a variable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q9H_nwbq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/BubbleSortDrawing1GIF.gif%3Ffit%3D720%252C540%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q9H_nwbq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/BubbleSortDrawing1GIF.gif%3Ffit%3D720%252C540%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Here’s what that code looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]){&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;holder&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;index&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;index&lt;/span&gt;&lt;span class="p"&gt;]&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;index&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;holder&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 GIF above, the holder variable is updated twice since the top-rated moves by two positions.&lt;/p&gt;

&lt;p&gt;One last thing before we combine the two code blocks: After every iteration, we are ensuring that the end of the array becomes a queue of the top-rated players. That means we do not need to continue to iterate through those elements, so the number of iterations should shrink each time.&lt;/p&gt;

&lt;p&gt;Here’s the final code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bubbleSort&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;length&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="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;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;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;j&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;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;i&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;j&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="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]){&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;holder&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;index&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;index&lt;/span&gt;&lt;span class="p"&gt;]&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;index&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;holder&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;span class="nx"&gt;bubbleSort&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;players&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 second “for” loop, we ensure that we use “length-1” for the initial number of iterations, because we know that the last element in the first iteration of that loop will be the highest value.&lt;/p&gt;

&lt;p&gt;Here’s an animation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k7AyyDzo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/BubbleSortDrawingsFinalGIF.gif%3Ffit%3D720%252C720%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k7AyyDzo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2020/01/BubbleSortDrawingsFinalGIF.gif%3Ffit%3D720%252C720%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Get More Tutorials
&lt;/h3&gt;

&lt;p&gt;Did you enjoy this tutorial? Check out the &lt;a href="//codeanalogies.com"&gt;CodeAnalogies blog&lt;/a&gt; to get my latest tutorials on web development topics.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Docker Containers Explained by Renting Office Space</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Tue, 12 Nov 2019 04:53:17 +0000</pubDate>
      <link>https://dev.to/kbk0125/docker-containers-explained-by-renting-office-space-p0o</link>
      <guid>https://dev.to/kbk0125/docker-containers-explained-by-renting-office-space-p0o</guid>
      <description>&lt;p&gt;In the last 15 years, it has become exponentially easier to deploy and manage web applications.&lt;/p&gt;

&lt;p&gt;First, services like &lt;a href="https://aws.amazon.com/"&gt;Amazon Web Services&lt;/a&gt; allowed you to easily rent a portion of a server via virtual machines. Rather than, you know, buying the whole server and running it from your closet.&lt;/p&gt;

&lt;p&gt;And now, you can use services like &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt;, which use containers to make it even cheaper and easier to manage dynamic web apps.&lt;/p&gt;

&lt;p&gt;But, new web developers often get the two underlying technologies (virtual machines and containers) mixed up. Although they both make it much easier to deploy web applications, they have some significant differences.&lt;/p&gt;

&lt;p&gt;In fact, it’s kind of like the difference between renting your own office space and renting coworking space (using a company like WeWork). So, this tutorial will use the office example to show how they are different.&lt;/p&gt;

&lt;p&gt;In order to understand this tutorial, you must first understand the concept of AWS and virtual machines. &lt;a href="https://blog.codeanalogies.com/2018/07/31/amazon-web-services-aws-explained-by-operating-a-brewery/"&gt;Check out my separate guide to AWS&lt;/a&gt; if you need to review that first.&lt;/p&gt;

&lt;p&gt;Now, imagine that you are the owner of a small software company (5 employees) and you are looking for office space.&lt;/p&gt;

&lt;h3&gt;
  
  
  The History of Office Space and Servers
&lt;/h3&gt;

&lt;p&gt;Let’s rewind 20 years to the year 2000. If you were searching for office space as a small software startup, your options were likely very limited and cost prohibitive. You could:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Buy a small office in a strip mall (or something like that)&lt;/li&gt;
&lt;li&gt;Sign a multiyear lease with a significant upfront payment&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You would face similar difficulty with getting your product online. You would need to buy an entire server, store it somewhere, and make sure it stayed online 24/7. Very inefficient.&lt;/p&gt;

&lt;p&gt;But, over the last 20 years, your choices for renting office space have changed! As tech startups became more popular, you could rent a portion of an office on a one year lease, or even a month-to-month lease.&lt;/p&gt;

&lt;p&gt;And, as coworking spaces became trendy in the 2010s, you could just rent a desk or a closed room within a space shared by many companies at once!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7UNlXCXR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2019/11/OfficeSpaceDrawing.png%3Ffit%3D730%252C163%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7UNlXCXR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2019/11/OfficeSpaceDrawing.png%3Ffit%3D730%252C163%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;At the same time, this was also creating a different relationship between the office building itself, and the team of people required to run the building.&lt;/p&gt;

&lt;p&gt;When you purchased the entire office at once, you needed to also manage all the services needed to run the space- snacks, cleaning, furniture etc. If the physical space is like &lt;strong&gt;hardware&lt;/strong&gt; , the office services are like &lt;strong&gt;software&lt;/strong&gt;. And the office manager is the &lt;strong&gt;operating system (OS)&lt;/strong&gt;, since they determine how the office works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rpu5NrH5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2019/11/hardware-v-software.png%3Ffit%3D730%252C714%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rpu5NrH5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2019/11/hardware-v-software.png%3Ffit%3D730%252C714%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;But, as we have made office space more accessible, we have also greatly increased the complexity of office services. In other words, there are a greater number of &lt;strong&gt;operating systems&lt;/strong&gt; that must work together.&lt;/p&gt;

&lt;p&gt;For example, let’s say your company begins to rent a floor of a larger office building. Now, there must be a general office coordinator to manage all the floors, and you must have your own office coordinator to manage the services for your floor!&lt;/p&gt;

&lt;p&gt;Okay, let’s be honest, it’s a small company so that means the CEO is probably the office coordinator for their floor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KqD7Hpvc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2019/11/officeversionofVMs.png%3Ffit%3D730%252C713%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KqD7Hpvc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2019/11/officeversionofVMs.png%3Ffit%3D730%252C713%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now, each CEO needs to figure out the operating system for their office- which snacks, when it will get cleaned, and any furniture that is needed.&lt;/p&gt;

&lt;p&gt;It’s a heck of a lot better than needing to buy real estate, at least.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Difference Between Virtual Machines and Containers
&lt;/h3&gt;

&lt;p&gt;Let’s tie this back to virtual machines and containers.&lt;/p&gt;

&lt;p&gt;Much like our second scenario, virtual machines add their own operating system on top of the existing operating system on the server.&lt;/p&gt;

&lt;p&gt;And, they must use a layer of middleware called a hypervisor to allow each virtual machine to share the hardware capacity.&lt;/p&gt;

&lt;p&gt;It kind of looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RtmdqVFX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/miro.medium.com/max/2971/1%2AVtMtEmgKUBXMMLLCk-vt7w.png%3Fw%3D730%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RtmdqVFX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/miro.medium.com/max/2971/1%2AVtMtEmgKUBXMMLLCk-vt7w.png%3Fw%3D730%26ssl%3D1" alt=""&gt;&lt;/a&gt;&lt;em&gt;&lt;a href="https://medium.com/flow-ci/introduction-to-containers-concept-pros-and-cons-orchestration-docker-and-other-alternatives-9a2f1b61132c"&gt;Image Credit&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, there are three levels of software that must work together alongside the files in your application:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Host OS&lt;/li&gt;
&lt;li&gt;Hypervisor&lt;/li&gt;
&lt;li&gt;Guest OS&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are certainly some advantages here- for example, each virtual machine can run its own operating system, which adds more flexibility. But, it also adds a series of resource-intensive software layers.&lt;/p&gt;

&lt;p&gt;Let’s return to the office space example to learn about using containers. Imagine the year is now 2015, and coworking spaces like WeWork have become popular.&lt;/p&gt;

&lt;p&gt;At these coworking spaces, you simply need to start paying a month-to-month lease per desk. The property managers take care of snacks, cleaning, furniture and everything else.&lt;/p&gt;

&lt;p&gt;In other words, you are able to benefit from the space’s &lt;strong&gt;existing operating system&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Here’s what that looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9iHd8sOk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2019/11/officeascontainers.png%3Ffit%3D730%252C651%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9iHd8sOk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2019/11/officeascontainers.png%3Ffit%3D730%252C651%26ssl%3D1" alt=""&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Great! Now each CEO can focus on just running their company.&lt;/p&gt;

&lt;p&gt;In fact, this is the key distinction between containers and virtual machines- as you can see in the diagram above, containers share the host operating system. That means they do not need to run their own OS, or work with a hypervisor to distribute hardware resources across multiple operating system.&lt;/p&gt;

&lt;p&gt;This means that containers tend to be much more scalable than virtual machines- you can easily deploy new containers in a standardized environment with fewer points of failure. It’s also more cost-effective since you do not need to pay to run all the extra software.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages/Disadvantages of Containers
&lt;/h3&gt;

&lt;p&gt;Keep in mind, there are hundreds of servers on AWS that are running the exact same OS all around the world. That means that you can easily manage containers with your web app all around the world, with very little overhead or custom setup.&lt;/p&gt;

&lt;p&gt;There is one major disadvantage to containers- security vulnerabilities. Each container has root access to the server, so problems that start with one container on a server can then affect others as well.&lt;/p&gt;

&lt;p&gt;Let’s return to the original point of this article- Docker allows developers to create and manage containers.&lt;/p&gt;

&lt;p&gt;So, in our analogy, the Docker service isn’t the building itself- that’s hardware (servers) and Amazon Web Services. And it isn’t an individual company that rents space- those are like containers.&lt;/p&gt;

&lt;p&gt;In this case, Docker is like the coworking space management company- they make it possible for you to rent office space (or server space) in a new way!&lt;/p&gt;

&lt;p&gt;Interested in using other similar tutorials? Check out the &lt;a href="https://blog.codeanalogies.com"&gt;CodeAnalogies blog&lt;/a&gt; for explanations of other common webdev topics.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>State (in JavaScript) Explained by Cooking A Simple Meal</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Tue, 30 Apr 2019 00:19:50 +0000</pubDate>
      <link>https://dev.to/kbk0125/state-in-javascript-explained-by-cooking-a-simple-meal-4ego</link>
      <guid>https://dev.to/kbk0125/state-in-javascript-explained-by-cooking-a-simple-meal-4ego</guid>
      <description>&lt;p&gt;When you start writing simple JavaScript programs, you don’t need to worry about the number of variables you are using, or how different functions and objects work together.&lt;/p&gt;

&lt;p&gt;For example, most people start out by using a lot of &lt;strong&gt;global&lt;/strong&gt;  &lt;strong&gt;variables&lt;/strong&gt; , or variables that are scoped at the top level of the file. They are not part of any individual class, object or function.&lt;/p&gt;

&lt;p&gt;For example, this is a global variable called &lt;em&gt;state&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;global&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But once your program starts to involve many different functions and/or objects, you will need to create a more rigorous set of rules for your code.&lt;/p&gt;

&lt;p&gt;This is where the concept of &lt;a href="https://en.wikipedia.org/wiki/State_(computer_science)" rel="noopener noreferrer"&gt;state&lt;/a&gt; comes into play. State describes the status of the entire program or an individual object. It could be text, a number, a boolean, or another data type.&lt;/p&gt;

&lt;p&gt;It’s a common tool for coordinating code. For example, once you update state, a bunch of different functions can instantly react to that change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.codeanalogies.com/2016/10/04/react-props-state-explained-through-darth-vaders-hunt-for-the-rebels/" rel="noopener noreferrer"&gt;This article describes state in the context of React&lt;/a&gt;, a popular JavaScript library.&lt;/p&gt;

&lt;p&gt;But guess what? Even state can give you headaches once your code gets complicated! Changing state can cause unintended consequences.&lt;/p&gt;

&lt;p&gt;Let’s stop right there. State is a popular tool in &lt;strong&gt;object-oriented programming&lt;/strong&gt; , or OOP. But, many programmers prefer &lt;strong&gt;functional programming&lt;/strong&gt; , which discourages state changes. JavaScript supports both paradigms.&lt;/p&gt;

&lt;p&gt;Okay, that’s a lot of terminology at once. I wanted to find a way to show how OOP and functional programming can accomplish the same goals, even if functional programming does not use &lt;strong&gt;state&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This tutorial will show how you might cook a meal of spaghetti and sauce from an OOP and functional perspective.&lt;/p&gt;

&lt;p&gt;Here’s a quick preview of the two different approaches:&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%2Fi0.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2FOverviewdiagramv4.jpg%3Ffit%3D730%252C643%26ssl%3D1" 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%2Fi0.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2FOverviewdiagramv4.jpg%3Ffit%3D730%252C643%26ssl%3D1"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Let’s jump into it. In order to understand this tutorial, you just need to understand functions and objects in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Object-Oriented Method (Using State)
&lt;/h3&gt;

&lt;p&gt;In the graphic above, we showed two different approaches to making this pasta dinner:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A method that is focused on the &lt;strong&gt;state of the different tools&lt;/strong&gt; , like the stove, the pot and the pasta.&lt;/li&gt;
&lt;li&gt;A method that is focused on the &lt;strong&gt;progression of the food itself&lt;/strong&gt; , with no mention of state of the individual tools (pots, stoves etc.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The object-oriented approach focuses on &lt;strong&gt;updating state&lt;/strong&gt; , so our code will have state at two different levels:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Global, or the state of this entire meal.&lt;/li&gt;
&lt;li&gt;Local for each object.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We are going to use ES6 syntax in this tutorial to create objects. Here’s an example of global state and the “Pot” prototype.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;stoveTemp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&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;Pot&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;boilStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;startBoiling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;stoveTemp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;boilStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;boiling&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pastaPot&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;Pot&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;pastaPot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startBoiling&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;pastaPot&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Pot { boilStatus = 'boiling'; }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: I simplified the console.log statement to focus on the state update.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here’s a visual representation of that logic:&lt;/p&gt;

&lt;p&gt;Before&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%2Fcdn.filestackcontent.com%2FLJTWRZcmRtuawK6QrL3Y" 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%2Fcdn.filestackcontent.com%2FLJTWRZcmRtuawK6QrL3Y" alt="InitialBoilStatus.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After&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%2Fcdn.filestackcontent.com%2FAvfl28MERnWjIDtAHwlS" 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%2Fcdn.filestackcontent.com%2FAvfl28MERnWjIDtAHwlS" alt="afterpastaboiling.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are two states, and when the pastaPot is created via the Pot prototype, it initially has an empty boilStatus. But then, there is a &lt;strong&gt;state change&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We run pastaPot.startBoiling(), and now the boilStatus &lt;strong&gt;(local state)&lt;/strong&gt; is “boiling”, since the &lt;strong&gt;global state&lt;/strong&gt; of stoveTemp is over 400.&lt;/p&gt;

&lt;p&gt;Now let’s go one step further. We will allow the pasta to become boiled due to the state of pastaPot.&lt;/p&gt;

&lt;p&gt;Here’s the code we will add to the snippet above:&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;Pasta&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookedStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addToPot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;boilStatus&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;boilStatus&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;boiling&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookedStatus&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="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;myMeal&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;Pasta&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;myMeal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addToPot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pastaPot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;boilStatus&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;myMeal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookedStatus&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;Woah! That’s a lot at once. Here’s what happened.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We created a new prototype of “Pasta”, where every object will have a &lt;strong&gt;local state&lt;/strong&gt; called &lt;em&gt;cookedStatus&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;We created a new instance of Pasta called myMeal&lt;/li&gt;
&lt;li&gt;We used the state from the pastaPot object that we created in the last snippet to determine if we should update the state called cookedStatus in myMeal to cooked. &lt;/li&gt;
&lt;li&gt;Since the state of boilStatus in pastaPot was “boiling”, our pasta is now cooked!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s that process visually:&lt;/p&gt;

&lt;p&gt;Before&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%2Fcdn.filestackcontent.com%2FxUfyBOM3RVniQTblneNW" 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%2Fcdn.filestackcontent.com%2FxUfyBOM3RVniQTblneNW" alt="pastapotstate1.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After&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%2Fcdn.filestackcontent.com%2Fd6NPOXJhQ7ywWiBAHQR8" 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%2Fcdn.filestackcontent.com%2Fd6NPOXJhQ7ywWiBAHQR8" alt="pastapotstate2.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, we now have the local state of one object, that depends on the local state of another object. And that local state depended on some global state! You can see how this can be challenging. But, it is at least easy to follow for now, since states are updated explicitly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional Method (without state)
&lt;/h3&gt;

&lt;p&gt;In order to fully understand state, you should be able to find a way to accomplish the same outcome as the code above without actually modifying state. This is where &lt;strong&gt;functional programming&lt;/strong&gt; helps!&lt;/p&gt;

&lt;p&gt;Functional programming has two core values that separate it from OOP: immutability and pure functions.&lt;/p&gt;

&lt;p&gt;I am not going to go into too much depth on those topics, but if you want to learn more, I encourage you to &lt;a href="https://opensource.com/article/17/6/functional-javascript" rel="noopener noreferrer"&gt;check out this guide to functional programming&lt;/a&gt; in JavaScript.&lt;/p&gt;

&lt;p&gt;Both of these principles discourage the use of state modification in your code. That means that we can’t use local or global state.&lt;/p&gt;

&lt;p&gt;Functional programming instead encourages us to pass in parameters to individual functions. We can use outside variables, but we can’t use them as state.&lt;/p&gt;

&lt;p&gt;Here’s an example of a function that will boil the pasta.&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;stoveTemp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&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;cookPasta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;temp&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;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cooked&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;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;cookPasta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stoveTemp&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// 'cooked'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will successfully return a string of ‘cooked’. But notice- there is no object that we are updating. The function simply returns the value that will be used in the next step.&lt;/p&gt;

&lt;p&gt;Instead, we are focused on the inputs and outputs of one function: cookPasta.&lt;/p&gt;

&lt;p&gt;This perspective looks at the transformation of the food itself, rather than the tools that are used to cook it. It’s a little harder to visualize, but we don’t need to have the function depend on external state.&lt;/p&gt;

&lt;p&gt;Here’s what it looks like.&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%2Fi1.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2FcookPastaStart.jpg%3Ffit%3D730%252C257%26ssl%3D1" 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%2Fi1.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2FcookPastaStart.jpg%3Ffit%3D730%252C257%26ssl%3D1"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Think of it as a “timeline view” for the progress of the meal- this particular function just covers the first part, the transition from dry pasta to cooked pasta.&lt;/p&gt;

&lt;p&gt;Now let’s cover the second part as the food is served. Here’s the code that will serve the meal. It will come after the code block above.&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;serveMeal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pasta&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;pasta&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cooked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dinner is ready.&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="nf"&gt;serveMeal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cookPasta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stoveTemp&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// 'Dinner is ready.'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we are delivering the results of the cookPasta function directly into the serveMeal function. Again, we are able to do this without changing state, or changing data structures.&lt;/p&gt;

&lt;p&gt;Here’s a diagram that uses the “timeline view” to show how these two functions work together.&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%2Fi0.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2FcookPastaPathPt2.jpg%3Ffit%3D730%252C146%26ssl%3D1" 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%2Fi0.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2FcookPastaPathPt2.jpg%3Ffit%3D730%252C146%26ssl%3D1"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Interested In More Visual Tutorials?
&lt;/h3&gt;

&lt;p&gt;If you would like to read more visual tutorials about HTML, CSS and JavaScript, check out the &lt;a href="http://codeanalogies.com" rel="noopener noreferrer"&gt;main CodeAnalogies site&lt;/a&gt; for 50+ tutorials.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript’s Arrow Functions Explained By Going Down A Slide</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Sat, 20 Apr 2019 17:41:00 +0000</pubDate>
      <link>https://dev.to/kbk0125/javascript-s-arrow-functions-explained-by-going-down-a-slide-1ebm</link>
      <guid>https://dev.to/kbk0125/javascript-s-arrow-functions-explained-by-going-down-a-slide-1ebm</guid>
      <description>&lt;p&gt;If you have been using JavaScript for a few years, you are probably familiar with this syntax:&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;addTen&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="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;10&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="nf"&gt;addTen&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;//20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function syntax was popular in ES5, or ECMAScript 5.&lt;/p&gt;

&lt;p&gt;There is one major advantage to this syntax: It includes the word function, so it is obvious that you are writing a function!&lt;/p&gt;

&lt;p&gt;A function clearly takes in anywhere from 0 to many arguments and runs a specific set of statements every time that it is called.&lt;/p&gt;

&lt;p&gt;But then the world of JavaScript took a leap forward with ES6 in 2015.&lt;/p&gt;

&lt;p&gt;Now, the same function as above would be written 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;let&lt;/span&gt; &lt;span class="nx"&gt;addTen&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&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;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;addTen&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;//20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, there is no &lt;em&gt;function&lt;/em&gt; keyword, and no return statement! Functions in ES6 are much more &lt;strong&gt;terse&lt;/strong&gt; , or concise.&lt;/p&gt;

&lt;p&gt;So, since those obvious clues have been removed, you might be having a little bit of a hard time with understanding the different parts of arrow functions.&lt;/p&gt;

&lt;p&gt;Fortunately, as you will soon see with a few animations, arrow functions are pretty easy to understand once you learn to visualize the arrow “=&amp;gt;” in a new way.&lt;/p&gt;

&lt;p&gt;So here is how arrow functions are just like a water slide. In order to fully understand this tutorial, it might help to know about &lt;a href="https://blog.codeanalogies.com/2018/02/20/javascript-map-method-explained-by-going-on-a-hike/" rel="noopener noreferrer"&gt;map functions&lt;/a&gt; and &lt;a href="https://blog.codeanalogies.com/2017/11/22/how-javascript-variable-scoping-is-just-like-multiple-levels-of-government/" rel="noopener noreferrer"&gt;scoping&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrow Functions Visualized
&lt;/h3&gt;

&lt;p&gt;Let’s explore the addTen function a little more deeply.&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;addTen&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&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;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;addTen&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;//20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function will transform one parameter and output that parameter with 10 added.&lt;/p&gt;

&lt;p&gt;The transformation happens with that subtle “=&amp;gt;” arrow.&lt;/p&gt;

&lt;p&gt;It can also be written with a return statement:&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;addTen&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;addTen&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;//20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I like to transform that arrow into a tube slide in my mind to show what is actually happening. Here is what I mean:&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%2Fi1.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2FarrowGif1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2FarrowGif1.gif"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The equals sign is like the tube slide and the arrow is like the landing pad.&lt;/p&gt;

&lt;p&gt;Arrow functions follow this pattern:&lt;/p&gt;

&lt;p&gt;(parameters) =&amp;gt; {statements}&lt;/p&gt;

&lt;p&gt;So let’s add those to the diagram with our addTen function example.&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%2Fi2.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2Farrowgif2.gif%3Ffit%3D730%252C411%26ssl%3D1" 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%2Fi2.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2Farrowgif2.gif%3Ffit%3D730%252C411%26ssl%3D1"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The last thing we need to show is how the parameter, which is 10 in this case, goes down the slide and becomes available in the statements of the function. Here’s what that looks like.&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%2Fi2.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2Farrowgif3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi2.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2Farrowgif3.gif"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;That’s all it is! Pretty straightforward.&lt;/p&gt;

&lt;p&gt;Now, let’s look at an example where there are two different parameters. Here is our new function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="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;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;10&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;//20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case we are just multiplying the two parameters together. Both will go down the slide together. Like this:&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%2Fi1.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2Farrowgif4.gif%3Ffit%3D730%252C411%26ssl%3D1" 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%2Fi1.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2Farrowgif4.gif%3Ffit%3D730%252C411%26ssl%3D1"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;There’s one more example you should know about- combining the map() method with arrow functions.&lt;/p&gt;

&lt;p&gt;The map() method will send every element in an array into the arrow function, in order.&lt;/p&gt;

&lt;p&gt;Let’s go through an example- imagine that you have an array of numbers, and you want to get the square root of each one.&lt;/p&gt;

&lt;p&gt;Here’s the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;nums&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;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&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;num&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&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="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;squares&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// [1, 2, 3]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need to know a little about the map method to understand this one. But, you will probably notice the terse syntax yet again- the map() method is much shorter than writing a for() loop.&lt;/p&gt;

&lt;p&gt;Here’s what is happening in this code:&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%2Fi2.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2Farrowgif5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi2.wp.com%2Fblog.codeanalogies.com%2Fwp-content%2Fuploads%2F2019%2F04%2Farrowgif5.gif"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There are three elements in the &lt;em&gt;nums&lt;/em&gt; array, so the &lt;em&gt;num&lt;/em&gt; parameter goes down the slide 3 times. &lt;/li&gt;
&lt;li&gt;The Math.sqrt() method takes the square root of the number each time.&lt;/li&gt;
&lt;li&gt;The result is stored in the &lt;em&gt;squares&lt;/em&gt; array each time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Difference Between Arrow Functions And Traditional Functions
&lt;/h3&gt;

&lt;p&gt;You might be wondering… is this simply a difference in syntax?&lt;/p&gt;

&lt;p&gt;Actually, there is one important way that the traditional ES5 functions and ES6 functions work differently.&lt;/p&gt;

&lt;p&gt;The big change is that arrow functions do not have their own scope. Therefore, if you try to use the &lt;em&gt;this&lt;/em&gt; keyword, you will be surprised when it does not refer to the scope of the arrow function.&lt;/p&gt;

&lt;p&gt;To go back to our slide analogy, this means that &lt;em&gt;this&lt;/em&gt; is the same at the top and bottom of the slide. If we were using ES5 functions, then &lt;em&gt;this&lt;/em&gt; would be different at the top and bottom of the slide.&lt;/p&gt;

&lt;p&gt;To quickly recognize this in code, just look for the &lt;em&gt;function&lt;/em&gt; keyword. If you see it, that means that a new scope is being created. If not, assume that you are using the scope of the enclosing function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interested In More Visual Tutorials?
&lt;/h3&gt;

&lt;p&gt;If you would like to read more visual tutorials about HTML, CSS and JavaScript, check out the &lt;a href="http://codeanalogies.com" rel="noopener noreferrer"&gt;main CodeAnalogies site&lt;/a&gt; for 50+ tutorials.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Node Package Manager (NPM) Explained by Directing A Movie</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Wed, 24 Oct 2018 03:03:08 +0000</pubDate>
      <link>https://dev.to/kbk0125/node-package-manager-npm-explained-by-directing-a-movie-359</link>
      <guid>https://dev.to/kbk0125/node-package-manager-npm-explained-by-directing-a-movie-359</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you understand the general way that Hollywood movies are made, then you can understand Node Package Manager (NPM).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Did you know that the initial version of Node.js was written by just one programmer, Ryan Dahl, in 2009?&lt;/p&gt;

&lt;p&gt;Today, in 2018, millions of developers have used Node.js to create the back-end for their web applications. But, Node has relied on an active open-source community to build out many specialized packages within the Node Package Manager, or NPM. That was created by Isaac Schlueter in 2010.&lt;/p&gt;

&lt;p&gt;There are two good reasons why Isaac did not develop all of these specialized functions himself:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Developers would not want to use a massive, clunky framework that was written to cover hundreds of use cases&lt;/li&gt;
&lt;li&gt;It would take too damn long to create all the specialized packages yourself!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Instead, Ryan knew that if Node grew in popularity, developers would be willing to contribute. So, he launched NPM in 2010 to organize all these packages created by the community.&lt;/p&gt;

&lt;p&gt;However, as a beginner web developer, it can be challenging to understand this whole ecosystem, and how to access it on your local computer.&lt;/p&gt;

&lt;p&gt;After thinking on it for awhile, I realized that using NPM is kind of like being the director of a Hollywood movie. It’s your job to juggle a bunch of  people (or packages) with specialized functions without making everyone crazy (or making it impossible to build your app).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JxmTkHV4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/OverviewNPM.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JxmTkHV4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/OverviewNPM.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here’s the full guide to NPM. In order to understand this tutorial, you just need to know the &lt;a href="https://blog.codeanalogies.com/2018/04/07/front-end-v-back-end-explained-by-waiting-tables-at-a-restaurant/"&gt;difference between front-end v. backend&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is NPM?
&lt;/h2&gt;

&lt;p&gt;Imagine that you are the director a new Hollywood movie. After accepting the role, you immediately need to begin hiring actors and other executives to create the movie alongside you.&lt;/p&gt;

&lt;p&gt;Of course, that brings the immediate question: which actors/actresses will you hire? How will you make sure that they can all work together? How will you fit it within budget?&lt;/p&gt;

&lt;p&gt;Or, will you go a completely different direction, and try to build the perfect team from scratch with relatively unknown actors?&lt;/p&gt;

&lt;p&gt;If you want to hire out a team, you will need to look at some sort of directory to find the right people. I don’t know if Hollywood has some sort of internal directory, but &lt;a href="http://imdb.com/"&gt;IMDB&lt;/a&gt; is one that comes to mind. Or, in the days, before the Internet, there was probably even a physical directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---4fSEZJm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/NPMDirectoryBasics.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---4fSEZJm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/NPMDirectoryBasics.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just like IMDB has information on thousands of actors that you can use to make a hiring decision, NPM has hundreds of thousands of “packages” that offer specialized functions. They are all written in JavaScript, so TECHNICALLY you could rewrite them… but that is not the point. They are meant to make your life easier by making new functionality instantly available.&lt;/p&gt;

&lt;p&gt;It’s just like hiring a known actor/actress- that person is a specialist in certain types of roles due to past experience.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qhL-EpOt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/limaneeson.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qhL-EpOt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/limaneeson.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;We all know Liam Neeson’s specialty&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, as a developer, you access the NPM registry to add specific packages to your web app, which should make your life easier. And just like all actors/actresses have built their own careers, all NPM packages have been built by individual developers or teams and contributed to the registry.&lt;/p&gt;

&lt;p&gt;Within your code, all of these packages are tracked in the package.json file. So, that file is kind of like the list of people that are involved in the movie. Here is an example of that list:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hsTv20nh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/packageexample.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hsTv20nh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/packageexample.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will cover the versions later in this tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Packages Explained
&lt;/h2&gt;

&lt;p&gt;Let’s imagine that your movie has 100 roles that need to be filled. If you had an unlimited budget, would you want to fill all those roles with famous actors/actresses that had played similar roles in the past?&lt;/p&gt;

&lt;p&gt;Probably not. Everyone would want to be the star of the show and bend the rules to fit their character. It would be a nightmare. But, if you only hire unknown actors… it is going to be pretty tough to make an excellent movie! It takes years of experience to become a great actor, after all.&lt;/p&gt;

&lt;p&gt;Similarly, when you are building a web app, you are constantly faced with a choice: can I build this functionality myself, or should I use a package to accomplish the task?&lt;/p&gt;

&lt;p&gt;Do you know the movie “&lt;a href="https://en.wikipedia.org/wiki/Super_Troopers"&gt;Super Troopers&lt;/a&gt;“? It’s a comedy classic, and the budget was just $3 million dollars. However, most excellent movies cost $10-100 million dollars. You can build an incredible web app from scratch… but you probably want to use packages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NEuRHXBe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/Supertrooper.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NEuRHXBe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/Supertrooper.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s say that you are hiring Mark Wahlberg, a famous American actor, for your movie. When Mark joins your movie, it is not just him that joins! He has a team of people that support him and make him successful. A chef… a trainer… an agent…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QRJwqs03--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/dependenciesex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QRJwqs03--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/dependenciesex.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, each individual package does not operate independently. The package authors used other NPM packages to make their life easier too. In fact, their package will share some dependencies with other packages, just like Mark Wahlberg might share a private chef with other actors and actresses.&lt;/p&gt;

&lt;p&gt;Here’s the list of dependencies for &lt;a href="https://www.npmjs.com/package/request"&gt;request&lt;/a&gt;, a popular package:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2i16Apkf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/realdepex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2i16Apkf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/realdepex.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let’s return to our package.json exale from above:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hsTv20nh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/packageexample.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hsTv20nh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/packageexample.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you add the “&lt;em&gt;axios&lt;/em&gt;” package to your app, you are not just adding the axios file. You are also adding any dependencies that the &lt;em&gt;axios&lt;/em&gt; file will need, if you had not previously added them with another package. Those are not explicitly stated, but you can always find it within the &lt;em&gt;node_modules&lt;/em&gt; folder.&lt;/p&gt;

&lt;p&gt;This is one of the benefits of npm. When you add a new package, you don’t even need to check if you are already using all the necessary packages that support the package that you are using. npm will automatically add the enw ones to your directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Versions Explained
&lt;/h2&gt;

&lt;p&gt;Do you see the three sets of numbers next to each package above? That’s the &lt;strong&gt;version number&lt;/strong&gt;. Since developers are constantly updating their packages, you can choose to use a specific version of a package, or automatically use the latest version.&lt;/p&gt;

&lt;p&gt;So, when you are using 40 different packages in your project, and they are all constantly shifting… you may find that compatibility issues arise. For example, when React releases its newest version, your app may no longer function as you would expect. That’s where testing comes into play, but that’s the subject of another tutorial.&lt;/p&gt;

&lt;p&gt;Think of it as actors/actresses at different stages of their career. Some may play similar roles throughout their career, while others may change drastically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B5_l0GmU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/a_clint.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B5_l0GmU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/a_clint.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Young vs. old Clint Eastwood&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the Command Line
&lt;/h2&gt;

&lt;p&gt;When you download Node and NPM to your local machine, you can instantly use a variety of commands to work with the NPM directory. One common one is:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npm install&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you want to install the &lt;em&gt;express&lt;/em&gt; package, you would type in the command line:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npm install express&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Then, NPM would download the express code and its dependencies to your local computer. That’s kind of like the act of hiring a new actor. Or:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npm uninstall express&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That’s like “firing” the express package from your app.&lt;/p&gt;

&lt;p&gt;You can add new commands in the “scripts” section of your package.json file. One common one is “start”, which means “start running the node server”. It looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jMFn6pCo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/nodeServerrun.png%3Ffit%3D232%252C45%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jMFn6pCo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/nodeServerrun.png%3Ffit%3D232%252C45%26ssl%3D1" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kind of like a director yelling “Action!”.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependencies Explained
&lt;/h2&gt;

&lt;p&gt;So far, we have only briefly touched on the concept of dependencies. Your app will likely have a couple packages that only run on the local version, like testing and transpiling tools. In other words, you should have a couple tools that you only use in your local environment, and not on the production version.&lt;/p&gt;

&lt;p&gt;If you need a refresher on the difference between localhost and production, &lt;a href="https://blog.codeanalogies.com/2018/02/02/localhost-explained-by-trying-to-start-a-microbrewery/"&gt;check out this guid&lt;/a&gt;e.&lt;/p&gt;

&lt;p&gt;Think about all the time and energy that goes into making a movie. Only a small fraction of it occurs on the official set with the cameras rolling. Behind the scenes, there are hours of work on memorizing lines, lifting weights in the gym and learning new accents. To make the team more effective, the director might hire specialized coaches for each one of these functions.&lt;/p&gt;

&lt;p&gt;So, when we look at all the packages being used by a Node app, we can actually divide it up into two categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Packages used in both production and local&lt;/li&gt;
&lt;li&gt;Packages just used locally&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--voLpqRly--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/LocalvProdDependencies.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--voLpqRly--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/LocalvProdDependencies.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gruntjs.com/"&gt;Grunt&lt;/a&gt; is a task runner that automates repetitive commands on the command line. &lt;a href="https://github.com/remy/nodemon"&gt;Nodemon&lt;/a&gt; automatically restarts your server upon any changes in your server code.&lt;/p&gt;

&lt;p&gt;In your package.json file, these are separated into two sections: dependencies and devDependencies. Here is what that looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dQAYAb2n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/dependciessection.png%3Ffit%3D254%252C63%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dQAYAb2n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/dependciessection.png%3Ffit%3D254%252C63%26ssl%3D1" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Hjw-qA8r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/devdependenciessection.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hjw-qA8r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/blog.codeanalogies.com/wp-content/uploads/2018/09/devdependenciessection.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Get The Latest Tutorials
&lt;/h2&gt;

&lt;p&gt;Did you enjoy this tutorial? Check out the &lt;a href="https://codeanalogies.com"&gt;Codeanalogies blog&lt;/a&gt; to get the latest explanations of HTML, CSS and JavaScript concepts.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Promises Explained By Gambling At A Casino</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Tue, 11 Sep 2018 03:49:29 +0000</pubDate>
      <link>https://dev.to/kbk0125/javascript-promises-explained-by-gambling-at-a-casino-4jdo</link>
      <guid>https://dev.to/kbk0125/javascript-promises-explained-by-gambling-at-a-casino-4jdo</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you have ever gambled or watched a movie about gambling, then you can understand promises in JavaScript.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We all love the asynchronous capabilities of JavaScript. In fact, we love them so much that sometimes, we overindulge. And then we get code that looks like this “&lt;a href="https://en.wikipedia.org/wiki/Pyramid_of_doom_%28programming%29" rel="noopener noreferrer"&gt;pyramid of doom&lt;/a&gt;”.&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%2Fcomr6pekagmjln0ugfae.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%2Fcomr6pekagmjln0ugfae.png" width="626" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.quora.com/Whats-the-difference-between-a-promise-and-a-callback-in-Javascript" rel="noopener noreferrer"&gt;Image Credit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is commonly known as “callback hell” because you probably don’t want to re-read that code and try to understand how everything works, and in what sequence it works. In fact, nobody on your team does either.&lt;/p&gt;

&lt;p&gt;A few things are difficult about the above example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unclear error handling. What happens if something goes wrong?&lt;/li&gt;
&lt;li&gt;Each function depends on the previous function. You do not need the asynchronous style. You want to make the order clear to others reading the code. When you chain this many functions together, a synchronous style of code will be more readable.&lt;/li&gt;
&lt;li&gt;You need to continually track the variables for input into a function, and then output. And also track the logic that happens to each output. This becomes exhausting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You could make this entire process more understandable using &lt;strong&gt;promises&lt;/strong&gt;. If you are like me, you may have heard of promises once or twice, but then ignored them because they seemed confusing. The basic uses of promises are actually pretty easy if you understand callbacks.&lt;/p&gt;

&lt;p&gt;Promises encourage straightforward, single-purpose functions that will allow you to write clear code and understand every step without headaches. After thinking about it for a while, I realized that promises are just like a trip to the casino. While a casino “modifies” the amount of money in your bank account (ahem, removes), a chain of promises modifies data in a specific sequence.&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%2Fjk0zmastrkard3ovgrgk.jpg" 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%2Fjk0zmastrkard3ovgrgk.jpg" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, let’s jump into it. If you do not have experience with callbacks, check out &lt;a href="https://blog.codeanalogies.com/2016/04/11/javascript-callbacks-explained-using-minions/" rel="noopener noreferrer"&gt;my explanation on the principles of callbacks&lt;/a&gt;. If you are looking for a more technical explanation of promises, check out &lt;a href="http://www.telerik.com/blogs/what-is-the-point-of-promises" rel="noopener noreferrer"&gt;this guide&lt;/a&gt; or &lt;a href="https://www.promisejs.org/" rel="noopener noreferrer"&gt;this guide&lt;/a&gt; or &lt;a href="https://www.youtube.com/watch?v=obaSQBBWZLk" rel="noopener noreferrer"&gt;this video&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a promise?
&lt;/h3&gt;

&lt;p&gt;Let’s say that you are taking a weekend vacation to a casino. You have two weeks of salary in your pocket, and you are going to enjoy every moment as you bet it away, down to the last dime. Or perhaps you will get lucky, and end up winning money?&lt;/p&gt;

&lt;p&gt;You get to your hotel room, then head down to the casino. Each type of game accepts cash, so you will need to go to the ATM to withdraw $1000 and get started.&lt;/p&gt;

&lt;p&gt;Let’s take a step back and think about this scenario. Although cash can be used for anything outside of the casino, it means one thing inside- the number of games you have left before you run out of money. That cash amount will likely shrink further and further over the course of the weekend. It could also grow, but you have already promised yourself that you will not lose more than $1000 this weekend.&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%2F055fiy1zrpj8soion85f.jpg" 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%2F055fiy1zrpj8soion85f.jpg" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how your remaining money amount gets passed from game to game in the diagram above?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A promise holds the place of a value that does not yet exist, but will certainly exist in the future.&lt;/strong&gt; This allows you to clearly follow a function and understand its beginning and end. As shown above, promises are a great way of giving clarity to consecutive asynchronous functions and clarifying inputs and outputs.&lt;/p&gt;

&lt;p&gt;Promises pass the products of one asynchronous function directly into the next function. That function will start as soon as the previous function has returned a value. Or, if it returns an error, you will run a different function. We can cover that contingency later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating Your First Promise
&lt;/h3&gt;

&lt;p&gt;There are actually two types of promises: &lt;strong&gt;producer&lt;/strong&gt; and &lt;strong&gt;consumer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The producer is the first promise in the chain, while the consumers wait on a result from a previous promise in the chain. In this case, going to the ATM is the producer, since you need money to play games (obviously).&lt;/p&gt;

&lt;p&gt;Also, a promise can have one of three states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pending- has not completed yet&lt;/li&gt;
&lt;li&gt;Fulfilled- Promise has completed and returned a value&lt;/li&gt;
&lt;li&gt;Rejected- Promise has completed with an error or failed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, if you visit an ATM, and you fail to complete the operation that you intended… well, you may not have the $1000 in your bank account and should exit the casino immediately. If you successfully withdraw $1000, then you have returned a value.&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%2Fggcaf293dt2wyg2yvt8j.jpg" 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%2Fggcaf293dt2wyg2yvt8j.jpg" width="800" height="494"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let’s turn this into code. Here is the promise syntax.&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;withdraw&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;Promise&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;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;reject&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;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;visitATM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;resolve&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here is a line by line explanation of that code.&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%2Fcdn.filestackcontent.com%2FjqQFGj6IS3uCvAvVbeB9" 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%2Fcdn.filestackcontent.com%2FjqQFGj6IS3uCvAvVbeB9" alt="promisecodeblock2.jpg" width="1900" height="628"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Line 1- Declare the promise with two possible results: fulfill or reject&lt;/p&gt;

&lt;p&gt;Line 2- A function to withdraw money from the ATM&lt;/p&gt;

&lt;p&gt;Line 3- Return a settled state with the value from the visitATM function&lt;/p&gt;

&lt;p&gt;Just like any other asynchronous code, this approach allows your code to wait on the status of the visitATM function. There’s no point in continuing if that isn’t completed!&lt;/p&gt;

&lt;h3&gt;
  
  
  Chaining Multiple Promises
&lt;/h3&gt;

&lt;p&gt;Let’s assume that you want to play slots, poker and roulette while you are at the casino. Each one requires you to buy-in via cash. Of course, if you bet too much money on poker and run out, then you will not be able to play any of the following games.&lt;/p&gt;

&lt;p&gt;Let’s say that you want to play slots first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;withdraw&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;Promise&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;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;reject&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;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;visitATM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;resolve&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="p"&gt;});&lt;/span&gt; 

&lt;span class="nx"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;slotResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;playSlots&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="mi"&gt;100&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;slotResults&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&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;slotResults&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;Promises use the &lt;strong&gt;.then&lt;/strong&gt; syntax to show what should happen after the previous promise is &lt;strong&gt;settled&lt;/strong&gt; , or completed. In this case, the final result of the withdraw promise is contained within &lt;em&gt;amount&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, when we set up the next promise using .then(), we also name the argument &lt;strong&gt;amount&lt;/strong&gt; to correspond to that previous result.&lt;/p&gt;

&lt;p&gt;One other important note- playSlots is a made-up function. We are imagining that it takes two arguments – the total amount of money you have, and the amount you are willing to gamble.&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%2F1quxn2cmkmsi8lxt194o.jpg" 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%2F1quxn2cmkmsi8lxt194o.jpg" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s add another step to this promise chain- a game of poker. It will work similarly to the slot machine promise. We will gamble as much as we want in this one.&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;withdraw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;slotResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;playSlots&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="mi"&gt;100&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;slotResults&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&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;slotResults&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="nf"&gt;then&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;slotResults&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;pokerResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;playPoker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slotResults&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;pokerResults&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&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;pokerResults&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;So, we feed whatever cash remains after playing slot machines into the poker game. Pretty aggressive, if you ask me.&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%2Fgqilrwubriq1nyrlw9i4.jpg" 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%2Fgqilrwubriq1nyrlw9i4.jpg" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s a code diagram of this part.&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%2F1z6csvgm39t4wvbzbvxz.jpg" 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%2F1z6csvgm39t4wvbzbvxz.jpg" width="800" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s imagine that we have now gambled away all our money. Although we originally intended to play more games, we have no money left. There may be more promises added in this chain, but we will not be able to settle them.&lt;/p&gt;

&lt;p&gt;Instead, since we have $0 left after poker, this promise will throw an error. It is still &lt;strong&gt;settled&lt;/strong&gt; , but in a &lt;strong&gt;rejected&lt;/strong&gt; state.&lt;/p&gt;

&lt;p&gt;This is where the &lt;strong&gt;.catch() method&lt;/strong&gt; comes in handy. Catch allows us to handle any errors that may occur in our promise chain. We don’t need to write error handlers for each callback.&lt;/p&gt;

&lt;p&gt;Let’s imagine that you head straight to the bar when you have gambled all your money. Here’s what that looks like in 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;withdraw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;slotResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;playSlots&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="mi"&gt;100&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;slotResults&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&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;slotResults&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="nf"&gt;then&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;slotResults&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;pokerResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;playPoker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slotResults&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;pokerResults&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&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;pokerResults&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;catch&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;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nf"&gt;goToBar&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;This one catch statement will work regardless of which promise is rejected.&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%2Ff9faoyg36tc3v7pvbzqt.jpg" 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%2Ff9faoyg36tc3v7pvbzqt.jpg" width="800" height="702"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Objects Within Promises
&lt;/h3&gt;

&lt;p&gt;So far, our promises have only returned a number. But, they can also pass any other type of data along the chain.&lt;/p&gt;

&lt;p&gt;Let’s imagine that you played a slot machine, and won some money. The slot machine does not give out straight cash- it gives you a ticket that you can redeem later. That’s called the &lt;a href="https://en.wikipedia.org/wiki/Ticket-in,_ticket-out" rel="noopener noreferrer"&gt;ticket-in, ticket-out system&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, you need to track two values throughout the chain- the amount of cash on hand, and the value of your tickets. An object would work best in this situation.&lt;/p&gt;

&lt;p&gt;Let’s modify the second promise in the chain, where you played slots.&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;withdraw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;ticketValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;playSlots&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="mi"&gt;100&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;ticketValue&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&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;tickets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ticketValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;cash&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are now returning an object with two properties. Here is what that looks like:&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%2Fqvnkixxgjvc3n5wh9qim.jpg" 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%2Fqvnkixxgjvc3n5wh9qim.jpg" width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The poker table will only accept cash for chips, so you need to use that property in the next promise.&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;withdraw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;ticketValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;playSlots&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="mi"&gt;100&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;ticketValue&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&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;tickets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ticketValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;cash&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="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slotResults&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;pokerResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;playPoker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slotResults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cash&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;pokerResults&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&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;tickets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;slotResults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tickets&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;cash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pokerResults&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;catch&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;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nf"&gt;goToBar&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;Notice a couple things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I only used the cash value in the poker game. But, at the end, I still need to add the ticket value into the final object in order to pass it along the chain. Otherwise, I would have lost my winnings.&lt;/li&gt;
&lt;li&gt;slotResults contains the object from the previous promise, even though that object did not have a name.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Get The Latest Tutorials
&lt;/h3&gt;

&lt;p&gt;Did you enjoy this explanation? Check out the &lt;a href="https://codeanalogies.com" rel="noopener noreferrer"&gt;CodeAnalogies blog&lt;/a&gt; to get the latest visualized tutorials of HTML, CSS and JavaScript.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Amazon Web Services (AWS) Explained by Operating a Brewery</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Thu, 30 Aug 2018 04:55:55 +0000</pubDate>
      <link>https://dev.to/kbk0125/amazon-web-services-aws-explained-by-operating-abrewery-2j0</link>
      <guid>https://dev.to/kbk0125/amazon-web-services-aws-explained-by-operating-abrewery-2j0</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you understand how a brewery works, then you can understand Amazon Web Services (AWS).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you are working on building your first web app, you ALWAYS hear about the ease of launching a new product compared to past years.&lt;/p&gt;

&lt;p&gt;People will say things like, &lt;em&gt;“Back in my day, you had to buy your own server and configure it yourself!”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Or, &lt;em&gt;“We would stay up all night debugging the latest issue with our server infrastructure.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Fortunately, those days are long gone. You can deploy your new web app for $10 a month within an hour, if you know what you are doing.&lt;/p&gt;

&lt;p&gt;But, there is one small problem. Standard tools like Amazon Web Services (AWS) can be pretty complicated, at least to a newbie. Although they offer the incredible ability to scale from your first users to hundreds of millions of users… they also require some configuration.&lt;/p&gt;

&lt;p&gt;I wanted to learn about all the major options that AWS offers. After thinking about it for a few hours, I realized that the AWS ecosystem is similar to all the different parts of a large-scale brewery.&lt;/p&gt;

&lt;p&gt;Here’s a quick preview:&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2AvdBGfOwhkhaQ2lTd" 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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2AvdBGfOwhkhaQ2lTd"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, here is how 5 popular AWS tools work behind the scenes of a web app. I’ll also explain &lt;a href="http://heroku.com/" rel="noopener noreferrer"&gt;Heroku&lt;/a&gt;, a popular tool for deploying web apps that offers less flexibility but is easier to get started with.&lt;/p&gt;

&lt;p&gt;The official video from AWS gives some context about the tools we will be discussing:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/mZ5H8sn_2ZI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In order to understand this tutorial, you just need to understand the concept of the &lt;a href="https://blog.codeanalogies.com/2018/02/02/localhost-explained-by-trying-to-start-a-microbrewery/" rel="noopener noreferrer"&gt;client-server model&lt;/a&gt;, which you can &lt;a href="https://blog.codeanalogies.com/2018/02/02/localhost-explained-by-trying-to-start-a-microbrewery/" rel="noopener noreferrer"&gt;learn more about here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Context of Amazon Web Services
&lt;/h4&gt;

&lt;p&gt;Let’s imagine that you are passionate about brewing beer. You start in your kitchen by brewing for yourself and friends. Soon, word escapes about your delicious work. In order to meet the growing demand, you decide to rent some equipment and space in a warehouse to see if you can create a full-blown company. You will take orders from distributors, restaurants, and independent businesses.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2A2zJ-RgXq4mX3eN-t" 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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2A2zJ-RgXq4mX3eN-t"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, an order comes in from a &lt;strong&gt;client &lt;/strong&gt; — &lt;strong&gt; &lt;/strong&gt; one of the restaurants or distributors. That’s called a &lt;strong&gt;request&lt;/strong&gt;. Your brewery will provide the order and collect payment via an invoice. That’s called the &lt;strong&gt;response&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, web browsers send requests to servers based on actions taken by users. The server returns the required information via a response.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2AMsF4PEVc5qVit5Xc" 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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2AMsF4PEVc5qVit5Xc"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is just a high-level view, of course. We are going to dig into all the different processes that happen on the &lt;strong&gt;server side&lt;/strong&gt; in our exploration of AWS. In our brewery analogy, we are assuming that the orders are already coming in left and right. It is now our job to organize a whole brewery so we can deliver those orders reliably.&lt;/p&gt;

&lt;h3&gt;
  
  
  Heroku Explained — A Much Simpler Alternative to AWS
&lt;/h3&gt;

&lt;p&gt;Before we get into the 5 parts of AWS, you probably should know about the simpler alternative. Heroku manages many of these systems for you. In fact, it is built on top of AWS infrastructure.&lt;/p&gt;

&lt;p&gt;Heroku allows you to deploy new versions of your app straight from your command line using &lt;em&gt;git push heroku master&lt;/em&gt;. It also has a &lt;a href="https://elements.heroku.com/addons" rel="noopener noreferrer"&gt;rich library of add-ons&lt;/a&gt; that allow you to add new functionality to your &lt;em&gt;dynos&lt;/em&gt;, or virtual servers.&lt;/p&gt;

&lt;p&gt;Heroku is kind of like hiring a 3rd party contractor to manage your brewery. All you need to do is provide the recipes, and this contractor will use their expertise in brewery operation to produce the beer. They provide the team, the equipment, and relationships with suppliers.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AHo0-aZixv74gV3-v0RbTyA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AHo0-aZixv74gV3-v0RbTyA.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AdosvXrcjnrJ9UetnJVZTyw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AdosvXrcjnrJ9UetnJVZTyw.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This may feel miraculous. Now you can skip all that painful time and energy that you would need to spend on learning how to run a brewery! But there are two reasons to be cautious.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This approach will be more expensive. You are paying the firm for their expertise alongside the cost of salaries, raw materials, etc.&lt;/li&gt;
&lt;li&gt;They may not scale up like you would like them to. Let’s imagine that you start receiving millions of dollars in orders, and you need to grow your operation. They may not be prepared to scale as quickly as you are.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Heroku has the same pros/cons. It is a little more expensive but allows you to get started immediately. If you scale up, you may need to migrate your services to AWS, which will mean that you will need to learn the AWS system anyways.&lt;/p&gt;

&lt;p&gt;With that, let’s get into the different tools within AWS.&lt;/p&gt;

&lt;h3&gt;
  
  
  AWS Storage Tools
&lt;/h3&gt;

&lt;p&gt;This is the first of three categories that are going to seem similar, so buckle up! Within our brewery, there are plenty of &lt;strong&gt;static assets&lt;/strong&gt; that do not change but are necessary for any type of brewing. Think about the machinery, the assembly line or the power tools that the workers use. You cannot take these items apart and recombine them. But, they can be used over and over again and still be useful.&lt;/p&gt;

&lt;p&gt;This is kind of like &lt;a href="https://en.wikipedia.org/wiki/Amazon_S3" rel="noopener noreferrer"&gt;Amazon S3&lt;/a&gt;. S3 is the cloud service that allows you to store static assets like images. It stands for Simple Storage Service.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Ae9abmVhUIIGeLSkYlbHb6Q.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Ae9abmVhUIIGeLSkYlbHb6Q.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2An053ea_uJrzL4Wb0kBmp8A.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2An053ea_uJrzL4Wb0kBmp8A.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The concept of “storage of static assets”, in this case, is a little different than what you might think about in real life. In real life, storing static items might mean putting something in an attic and forgetting about it. But when it comes to cloud services, it means preparing an object or image to be used within milliseconds.&lt;/p&gt;

&lt;p&gt;This is different than database storage, because data in a database can be &lt;strong&gt;queried&lt;/strong&gt;. Static assets can only be &lt;strong&gt;requested&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  AWS Database Services
&lt;/h3&gt;

&lt;p&gt;Amazon Relational Database Service (RDS) allows you to set up and operate your relational database within AWS. Some common examples include MySQL, PostGreSQL and Microsoft SQL Server.&lt;/p&gt;

&lt;p&gt;In our brewery example, this is kind of like the parts of the brewery that store bottles, labels, hops, malt and any other ingredients you will need to make the beer. And I guess it would also include your company bank account, since that is a form of dynamic storage.&lt;/p&gt;

&lt;p&gt;You might be wondering why we are discussing storage AGAIN after the previous section. It’s because all of these elements are much more dynamic- they are being constantly combined or modified to handle requests from the users/customers.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Aci_kNJOcrUCpn0mcW6VHqg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Aci_kNJOcrUCpn0mcW6VHqg.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AwWmbObMp__nfxL14zzW3cQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AwWmbObMp__nfxL14zzW3cQ.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the type of data that you will &lt;strong&gt;query&lt;/strong&gt; , rather than request, like in the previous example. If you were managing the databases for Facebook, for example, this would be user data and posts that would later be included in the News Feed.&lt;/p&gt;

&lt;p&gt;One more final note: this data (or hops/malt/bottles) is only useful for actually delivering a response to the user. If you wanted to measure your brewery’s performance, you would need to set up a new process for your operations team.&lt;/p&gt;

&lt;p&gt;Imagine if your operations team was frantically running around your brewery, trying to keep track of your performance while the normal workers were trying to produce and bottle as much beer as possible. These two teams have separate interests.&lt;/p&gt;

&lt;p&gt;That is where the data warehouse comes into play.&lt;/p&gt;

&lt;h3&gt;
  
  
  AWS Data Warehouse Tools
&lt;/h3&gt;

&lt;p&gt;Let’s make one thing clear here: “data warehouse” is a terrible name for cloud service. Okay sure, it might make sense to a developer with years of experience, but to a newbie… how many things in web development are similar to a warehouse? MANY.&lt;/p&gt;

&lt;p&gt;One common example of a &lt;a href="https://en.wikipedia.org/wiki/Data_warehouse" rel="noopener noreferrer"&gt;data warehouse product&lt;/a&gt; is &lt;a href="https://en.wikipedia.org/wiki/Amazon_Redshift" rel="noopener noreferrer"&gt;Redshift&lt;/a&gt;. These types of tools make it easy for developers to analyze their data. They include data from relational databases, as well as ERP, CRM and marketing automation data.&lt;/p&gt;

&lt;p&gt;Let’s return to our brewery analogy. So far, in our brewery, all of our “data” is not in a very accessible form. It’s in the format of bottles, hop bags and whatever other raw materials are lying around the facility. An analyst would need to manually count all of these physical items if they wanted to analyze the factory’s efficiency.&lt;/p&gt;

&lt;p&gt;You need a way to convert that physical data into machine-readable data, which your ops team can use to make the brewery more efficient. Think of it like a sensor system around the facility. The sensors convert the physical movement of raw materials into machine-readable data, which can later be analyzed.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AEySamZAufAFciP0g2y3U8g.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AEySamZAufAFciP0g2y3U8g.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AAtoYuA5nKYjrwlOEBNL0gQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AAtoYuA5nKYjrwlOEBNL0gQ.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So now, as our factory operates, data is automatically collected and shared with the factory ops team so they can analyze efficiency.&lt;/p&gt;

&lt;p&gt;See how this reduces stress on both the factory and production team? The production team can focus on making beer on the floor while the operations team can analyze from their office. This is a good reason to use a data warehouse tool as well. It reduces the number of queries on your database, which can slow performance.&lt;/p&gt;

&lt;p&gt;Hopefully, you see why “data warehouse” tool is slightly misleading. Yes, this does create a new dataset strictly for analysis. But, it is hard to tell why it is more “warehouse-like” than any other part of the system.&lt;/p&gt;

&lt;h3&gt;
  
  
  AWS Computing Tools
&lt;/h3&gt;

&lt;p&gt;In all of the diagrams so far, have you noticed that “brewery” is in the middle, regardless of whether the diagram shows an actual brewery or a cloud computing environment?&lt;/p&gt;

&lt;p&gt;That is because we haven’t yet covered the service that ties this all together: &lt;a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html" rel="noopener noreferrer"&gt;EC2, or Elastic Compute Cloud&lt;/a&gt;. EC2 allows you to launch virtual instances, which are kind of like the brewery in this analogy.&lt;/p&gt;

&lt;p&gt;Instances are &lt;strong&gt;virtual servers&lt;/strong&gt;. Unlike previous technologies, where your code was tied to one physical server, virtual servers allow you to launch your own environment in the cloud, which is composed of many connected servers. This is the part that ties all the other AWS services together.&lt;/p&gt;

&lt;p&gt;It’s kind of like being able to start up or shut down a new brewery at any time in any part of the world by copying your existing brewery. That may not be possible in real life, but it is possible in the AWS ecosystem. EC2 provides similar functionality to both the physical space and the workers in the brewery.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2ATbPj10B8QH10Dx_tiIC-Iw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2ATbPj10B8QH10Dx_tiIC-Iw.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Al0y3g0gR4MUX7O8t5HhVvA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Al0y3g0gR4MUX7O8t5HhVvA.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The diagram may be slightly misleading, because a virtual server does not take up a whole server. It exists within a network of servers. But that was too complicated to show in a small diagram. So I kept it.&lt;/p&gt;

&lt;h3&gt;
  
  
  AWS Management Tools
&lt;/h3&gt;

&lt;p&gt;The final category of tools is management tools, like Elastic Beanstalk or CloudWatch. These tools can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Monitor other tools listed above&lt;/li&gt;
&lt;li&gt;Set up processes to help tools from multiple categories work together&lt;/li&gt;
&lt;/ol&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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AV5AvyPtgGhHLXZF2" 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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AV5AvyPtgGhHLXZF2"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;em&gt;Image Cred: FreeCodeCamp&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the graphic above, you can see how many tools can help with deploying and maintaining your app. That’s why these tools are similar to the company’s management team. They are not personally producing beer for your company, but instead, they help all the divisions work together.&lt;/p&gt;

&lt;p&gt;There is a wide variety of tools in the management category, so I am not going to cover any individual one in depth. They sit at a level above the other tools discussed.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2A8hrWANJasTDpQaERaaF-aQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2A8hrWANJasTDpQaERaaF-aQ.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Ad_Kx52WUCIS0tYPVGHm18Q.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Ad_Kx52WUCIS0tYPVGHm18Q.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Get The Latest Visual Tutorials
&lt;/h3&gt;

&lt;p&gt;Did you enjoy this tutorial? Check out the &lt;a href="https://codeanalogies.com" rel="noopener noreferrer"&gt;CodeAnalogies blog&lt;/a&gt; to get other visual tutorials on HTML, CSS and JavaScript.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript’s Reduce Method Explained By Going On a Diet</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Wed, 01 Aug 2018 03:31:47 +0000</pubDate>
      <link>https://dev.to/kbk0125/javascripts-reduce-method-explained-by-going-on-adiet-3078</link>
      <guid>https://dev.to/kbk0125/javascripts-reduce-method-explained-by-going-on-adiet-3078</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you have ever read a nutrition label, then you can understand the reduce() method in JavaScript.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The reduce method in JavaScript gives you a simple way to take an array of values and combine them into one value, or sum the array based on multiple category.&lt;/p&gt;

&lt;p&gt;Wow, that is a lot in one sentence, so let’s take a step back here.&lt;/p&gt;

&lt;p&gt;Of course, you can always use a &lt;em&gt;for&lt;/em&gt; loop to iterate through an array and take a specific action on each value. But, if you do not use methods like filter(), map() and reduce(), then your code will become harder to read. Other developers will need to read each loop thoroughly to understand the purpose. And, it will create more chances for bugs, since you will need to create more variables to track individual values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.codeanalogies.com/2018/05/14/javascripts-filter-function-explained-by-applying-to-college/"&gt;Filter methods&lt;/a&gt; take all the elements in an initial array, and only allow certain elements into a final array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.codeanalogies.com/2018/02/20/javascript-map-method-explained-by-going-on-a-hike/"&gt;Map methods&lt;/a&gt; run a function on each element in an initial array before storing it in a final array.&lt;/p&gt;

&lt;p&gt;And reduce methods combine elements from an initial array into a final value or array of values.&lt;/p&gt;

&lt;p&gt;I realized that this was kind of like dieting. From very simple methods like calorie counting, to more complicated diets like Atkins or WeightWatchers, the goal is to distill all the food that you might eat over the course of the day into one value (or values) to see if you are on track to lose weight.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rKzhOWmk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/0%2A6uTOdFevTD49equ3" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rKzhOWmk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/0%2A6uTOdFevTD49equ3" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let’s jump into it! In order to understand this tutorial, you just need to have experience with &lt;em&gt;for&lt;/em&gt; loops.&lt;/p&gt;

&lt;h3&gt;
  
  
  Simulating Reduce With a For Loop
&lt;/h3&gt;

&lt;p&gt;Here’s a quick way to show the functionality of reduce(), using a for loop. Let’s say that you have an array with the calorie counts of 5 separate foods that you have eaten over the course of the day. You want to figure out how many total calories you have consumed. Here’s the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;calories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;700&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;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;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;foods&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="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;calories&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="c1"&gt;//value of total will be 2100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s pretty simple. You create a variable to hold the final amount, then add to it as you run through the array. But, you still needed to introduce a new variable, and the loop gives no clues about the purpose of the loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Simple Example of Reduce
&lt;/h3&gt;

&lt;p&gt;Let’s learn how to accomplish the same goal with the reduce() method.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reduce is an array method so we will start with the array of calorie counts.&lt;/li&gt;
&lt;li&gt;It uses a callback function that runs on each element in the array.&lt;/li&gt;
&lt;li&gt;It uses a return statement to show how the value should accumulate after each iteration of the array.
&lt;/li&gt;
&lt;/ol&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;foods&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;700&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;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So, &lt;em&gt;reduce&lt;/em&gt; has some concept of memory. As you go through each item in the array, the values are being tracked in the &lt;em&gt;sum&lt;/em&gt; argument. In our previous example, we had to declare a new variable outside the scope of the loop to “remember” the values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UdRUBuKU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A9p2n2qurUUU8fbPVcj6l7A.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UdRUBuKU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A9p2n2qurUUU8fbPVcj6l7A.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ql_JV69a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A-_qAZrNzGc3mHwcmZKbBCg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ql_JV69a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A-_qAZrNzGc3mHwcmZKbBCg.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There may not seem to be too much of a difference in readability between this and for(). But, when you (or another developer) need to scan hundreds of lines of code, reduce will give you a quick hint about the purpose of the code block.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2- Using Objects
&lt;/h3&gt;

&lt;p&gt;We have just been looking at one-dimensional arrays thus far. But, if you can iterate through an array full of numbers, you can also iterate through an array full of objects. Let’s add a name to each element so we can figure out what we are actually eating over the course of the day.&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;foods&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;steak&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;800&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;fruit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&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;salad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&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;chips&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&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;ice cream&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;700&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;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;calories&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;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//value of total will be 2100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You ate a steak for breakfast… with some fruit… then some salad and chips for lunch… then finally ice cream for dinner. That’s a heck of a day.&lt;/p&gt;

&lt;p&gt;Hopefully, you can see the flow as we go through the array. As we go through each element, the sum grows to represent the total calories consumed over the day. The entire point is to put these values into one big bucket- the number of calories over the day.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3- Using Mulitple Categories
&lt;/h3&gt;

&lt;p&gt;So if it is all about calories, why the heck are there so many different diets? I am not going to wade into the&lt;a href="http://time.com/magazine/us/4793878/june-5th-2017-vol-189-no-21-u-s/"&gt;nutrition science&lt;/a&gt;, but here’s the general summary- there is plenty of disagreement over the “best” way to lose weight. Some encourage you to just count calories, while others will look at protein, carbs, fat and any other number of factors.&lt;/p&gt;

&lt;p&gt;Let’s imagine for a moment that you want to change the code so that you can evaluate your diet based on any common dieting system. You will need to track carbs and fat for each type of food. Then you will need to summarize it all at the end so you can figure out how many grams you consumed in each category. Here is our foods object with fake nutritional values.&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;foods&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;steak&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;carbs&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;fat&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;fruit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;carbs&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;fat&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="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;salad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;carbs&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;fat&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="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;chips&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;carbs&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;fat&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="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;ice cream&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;700&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;carbs&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;fat&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="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we need to run the reduce() method. But, it can’t all be tracked in one value. We want to keep our categories. So, our accumulator needs to be an object with the same categories as the array.&lt;/p&gt;

&lt;p&gt;Here’s a quick GIF of the process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L16XQisV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AE2u29KZCOjKUHTxACzKFew.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L16XQisV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AE2u29KZCOjKUHTxACzKFew.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you go through each item, you will change the value of a specific property within an object. If that object does not yet have a property with the correct name, you will create it. Here is the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;calories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;calories&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;carbs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;carbs&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;fat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;calories&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;calories&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;carbs&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;carbs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fat&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;fat&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;buckets&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="cm"&gt;/*total object is
{name: 'steak', calories: 2100, carbs: 60, fat:65}
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using &lt;em&gt;buckets&lt;/em&gt; as an object to categorize the values based on name of their property. We use the &lt;a href="https://www.w3schools.com/js/js_operators.asp"&gt;+= operator&lt;/a&gt; to add to the appropriate bucket for each value within the object originally from the &lt;em&gt;foods&lt;/em&gt; array. Notice how we don’t store the name of each food? That is because it is insignificant- we just want the numbers so we can analyze the success of your diet for the day.&lt;/p&gt;

&lt;p&gt;As you can see, there was one issue in our output. We have a name field that is set to 'steak'. We don't want to store the name! So, we need to specify another argument- the initial value.&lt;/p&gt;

&lt;p&gt;This argument comes after the callback, and we want to initialize the calories, carbs and fat field to 0, so that our reduce method knows that those are the only three key/value pairs we will be using for the buckets argument. You can see an example here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;calories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;calories&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;carbs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;carbs&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;fat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;calories&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;calories&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;carbs&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;carbs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fat&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;fat&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;buckets&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;calories&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;carbs&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;fat&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="cm"&gt;/*total object is
{calories: 2100, carbs: 60, fat:65}
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Get The Latest Tutorials
&lt;/h3&gt;

&lt;p&gt;Did you enjoy this tutorial? Check out the &lt;a href="https://codeanalogies.com"&gt;CodeAnalogies blog&lt;/a&gt; to get the latest visual guides to HTML, CSS and JavaScript topics.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>javascipt</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Web Caching Explained by Buying Milk at the Supermarket</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Tue, 26 Jun 2018 03:20:25 +0000</pubDate>
      <link>https://dev.to/kbk0125/web-caching-explained-by-buying-milk-at-the-supermarket-9k4</link>
      <guid>https://dev.to/kbk0125/web-caching-explained-by-buying-milk-at-the-supermarket-9k4</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you have ever bought milk at the supermarket, then you can understand server-side and browser-side caching.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you are an avid internet user (you probably are), you have benefitted from caching over and over again. But, you might not know when or how it is working its magic behind the scenes.&lt;/p&gt;

&lt;p&gt;From a developer’s perspective, caching makes it much easier to build high-performing web apps and web servers. Instead of needing to constantly optimize servers that are overwhelmed by thousands of requests, developers can implement caching protocols to make life much easier.&lt;/p&gt;

&lt;p&gt;Since caching might make a difference between 1 second to load a page and 2 seconds, the impact can feel a little bit…underwhelming. But, it’s necessary if you want to handle a high volume of users.&lt;/p&gt;

&lt;p&gt;After using caching in a past web app, I realized that there had to be a better way to explain it than just walking through the terminology. I noticed that it aligned very well with the pathway of milk from a farm to your refrigerator, so I figured that would be a better way to explain it.&lt;/p&gt;

&lt;p&gt;In order to understand this guide, you just need to know about the &lt;a href="https://blog.codeanalogies.com/2018/04/26/web-servers-explained-by-running-a-microbrewery/" rel="noopener noreferrer"&gt;basics of web servers&lt;/a&gt;. Let’s get into it!&lt;/p&gt;

&lt;h3&gt;
  
  
  What would the internet look like without caching?
&lt;/h3&gt;

&lt;p&gt;Before we get into caching, let’s think about what the internet would look like without caching. Imagine, for a moment, that you are living in the 1700s or 1800s in a rural area. You own a farm, and there is no refrigeration available. You have a few cows on your farm, but their milk is not nearly as valuable since it will spoil quickly.&lt;/p&gt;

&lt;p&gt;Quick interruption: Some cultures still do not have access to refrigeration. They will either drink raw milk directly from a cow’s udder, or &lt;a href="https://seymourjacklin.co.uk/2010/11/01/milk-monday-dairy-in-the-days-before-pasteurisation-and-refrigeration/" rel="noopener noreferrer"&gt;mix milk with grains and let it ferment&lt;/a&gt;. Interesting.&lt;/p&gt;

&lt;p&gt;ANYWAYS, you want to sell your milk to others in your village. But, they will have a very limited time to drink the milk. Let’s say that one of your cows can produce a gallon of milk per day. But, if too many people show up to your farm looking for milk, you will need to send some home and ask them to wait til the next day.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2AUqjXaIdDFuVLfE5k" 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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2AUqjXaIdDFuVLfE5k" width="1446" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, you can’t even think about adding more cows and scaling up your operation since you have such limited distribution. Only the other members of your village can buy your milk. You have some clear limits.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2ALaOWTms2NkZtDFj5" 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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2ALaOWTms2NkZtDFj5" width="1024" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without caching, you are limited by the computing power of your servers. Caching is used to load static assets, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Images&lt;/li&gt;
&lt;li&gt;CSS&lt;/li&gt;
&lt;li&gt;Static HTML files&lt;/li&gt;
&lt;li&gt;JavaScript files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A server, by default, must submit a new response for every incoming request. But, a request to load a page could actually mean 4 separate requests — one from each of the categories above. When you take into account larger image files, your servers can become overwhelmed by users all over the world. Then, users will experience slow load times as they wait for their page to load.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2A4ajis8HoFZeo7SRf" 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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2A4ajis8HoFZeo7SRf" width="1024" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ideally, you would want to ease the demand on your servers by storing responses to common requests. Your server would not need to handle each new individual request, but instead, your cache could deliver an immediate response. You could always pay for more servers, but that can lead to uncontrollable expenses.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is server-side caching?
&lt;/h3&gt;

&lt;p&gt;Back to our farm scenario. Know what would make it a LOT easier to run a successful dairy farm?&lt;/p&gt;

&lt;p&gt;A supermarket with refrigeration!&lt;/p&gt;

&lt;p&gt;That way, people will not need to show up to your farm and consume the milk immediately. You will be able to keep it safely stored for a couple weeks at a time.&lt;/p&gt;

&lt;p&gt;The supermarket removes a lot of the stress on your farm, because your cows will not be expected to produce in real-time. The supermarket will handle the demand. You just need to keep the cows productive on a daily basis. Even better, residents of all the surrounding villages can now buy milk from your farm, because it will always be available in the refrigerator.&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%2Fih1olv0vrjqvqvjjfd0v.jpeg" 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%2Fih1olv0vrjqvqvjjfd0v.jpeg" width="800" height="487"&gt;&lt;/a&gt;&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%2Fvk8wdizfv9hqsu9e4oar.jpeg" 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%2Fvk8wdizfv9hqsu9e4oar.jpeg" width="800" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just like a supermarket, a &lt;a href="https://www.digitalocean.com/community/tutorials/web-caching-basics-terminology-http-headers-and-caching-strategies" rel="noopener noreferrer"&gt;server-side cache&lt;/a&gt; will handle popular requests and deliver content much more quickly and reliably.&lt;/p&gt;

&lt;p&gt;In the image above, I use the term &lt;strong&gt;caching proxy&lt;/strong&gt;. A caching proxy is a server that stores the static files that are used to respond to common requests. A caching proxy will intercept common requests and quickly deliver a response. It prevents those requests from stressing your main web servers.&lt;/p&gt;

&lt;p&gt;You probably have a bunch of questions like,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What determines a “popular” request?&lt;/li&gt;
&lt;li&gt;How long will the proxy cache the responses?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That would require a longer tutorial on setting up caching, but for now, you should know about one important concept called &lt;strong&gt;freshness&lt;/strong&gt;. The caching proxy will have different files that have been cached at different times, and it needs to decide whether it should still serve these files. This will depend on your &lt;strong&gt;caching policy&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This also works just like milk in a supermarket. A supermarket manager needs to decide how long they will hold your milk before throwing it out. Caching proxies measure their success through a &lt;strong&gt;cache hit ratio —&lt;/strong&gt;  the percentage of content that can be served through the caching server.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a CDN?
&lt;/h3&gt;

&lt;p&gt;So far, there is one grocery store selling your milk. Although that is a big improvement, you still have no way to get your milk to people outside the range of this local store. You are going to need to add more stores if you want to scale up your operation.&lt;/p&gt;

&lt;p&gt;So, let’s say you start distributing your milk to more supermarkets. Now, you can satisfy customers across a much larger geographic range. This is similar to a content delivery network, or a CDN. A CDN is a series of proxy servers (like we discussed above) located all over the world.&lt;/p&gt;

&lt;p&gt;As an end-user, you probably feel that high-speed internet allows most sites to load very quickly. However, this is only because they use CDNs to deliver static files at rapid speed!&lt;/p&gt;

&lt;p&gt;If you are located in England and you are trying to load a file cached in a server in Virginia, you will experience some delay since the original signal can only travel so quickly along thousands of miles of cable. A local caching proxy in the United Kingdom would allow the site to load faster.&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%2F7hwu5pt9mtuh4cbrjp9m.jpeg" 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%2F7hwu5pt9mtuh4cbrjp9m.jpeg" width="800" height="487"&gt;&lt;/a&gt;&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%2Fy883rlwgabv3j5epjonh.jpeg" 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%2Fy883rlwgabv3j5epjonh.jpeg" width="800" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, your servers can send a copy of static assets to each of these proxy servers within your CDN network, and they can handle local requests until the assets are no longer “fresh”. Some common CDN providers include Rackspace, Akamai, and Amazon Web Services.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about browser caching?
&lt;/h3&gt;

&lt;p&gt;Now, people across the country (or the world) can bring home cold milk from your farm. There’s just one issue — they have no way to store it in their own homes. Your customers still need to drink the milk pretty quickly after they buy it, and then return to the grocery store for more. So, this system still doesn’t serve customers particularly well.&lt;/p&gt;

&lt;p&gt;The solution? A refrigerator!&lt;/p&gt;

&lt;p&gt;With a fridge, you can store the milk locally and avoid a return trip to the supermarket. In caching terms, we’re talking about a completely separate location for storing static assets since it is on the client-side, or on the same computer as the browser. Our proxy server was located in a remote location.&lt;/p&gt;

&lt;p&gt;This is great for sites like Facebook or Amazon that you might frequently visit. It’s great for their server costs too, since they can reduce the number of requests they need to handle.&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%2F0yqq0l9vau983lz6jpfd.jpeg" 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%2F0yqq0l9vau983lz6jpfd.jpeg" width="800" height="487"&gt;&lt;/a&gt;&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%2Facse0b6f7kraz6itf5ed.jpeg" 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%2Facse0b6f7kraz6itf5ed.jpeg" width="800" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One key thing to note — we are NOT saying that milk magically arrives in your refrigerator! You still need to make that initial request that reaches either the server or the proxy server. After that, you can cache some of the files locally.&lt;/p&gt;

&lt;p&gt;How does your browser know when to request new files from the server? Otherwise, you would never experience updated versions of these local files.&lt;/p&gt;

&lt;p&gt;Well, just like milk producers put a date on their milk packaging, servers will add some sort of identifier within the HTTP response header. There are actually &lt;a href="https://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/" rel="noopener noreferrer"&gt;4 separate systems for HTTP caching&lt;/a&gt;. The scenario shown above closely resembles the “expiration date” method. Some of the other methods still require your browser to check with the server before sending the cached file.&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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2A7yAXQNZN-0XzIQHB" 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%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2A7yAXQNZN-0XzIQHB" width="1024" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When To Start Using Caching
&lt;/h3&gt;

&lt;p&gt;Let’s say that you are building your first web app. Until you have thousands of users, you probably won’t need to worry about caching protocols, since server costs will still be low. However, as you scale up, you will need to implement caching if you want your app to load quickly.&lt;/p&gt;

&lt;p&gt;Heroku, for example, is a great tool for deploying your first web app. But, it requires you to use a &lt;a href="https://devcenter.heroku.com/articles/http-caching" rel="noopener noreferrer"&gt;separate service to implement caching&lt;/a&gt;, like Amazon’s CloudFront or CloudFlare. That will take more time to learn.&lt;/p&gt;

&lt;p&gt;On the browser-side, you have probably experienced caching when you are trying to reload a page with new static assets, but the page simply won’t change. No matter how many times you refresh the page, nothing changes.&lt;/p&gt;

&lt;p&gt;This is usually because of some caching protocol on the browser-side. To bypass your browser’s cache and request new assets from the server, you can use &lt;strong&gt;Cmd+Shift+R&lt;/strong&gt; on a Mac or &lt;strong&gt;Ctrl+Shift+R&lt;/strong&gt; on PC.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get More Visual Tutorials
&lt;/h3&gt;

&lt;p&gt;Did you enjoy this tutorial? Check out the rest of my visual explanations of web development topics on the &lt;a href="//codeanalogies.com"&gt;CodeAnalogies blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript’s Filter Function Explained By Applying To College</title>
      <dc:creator>Kevin Kononenko</dc:creator>
      <pubDate>Sun, 03 Jun 2018 17:01:18 +0000</pubDate>
      <link>https://dev.to/kbk0125/javascripts-filter-function-explained-by-applying-to-college-58j0</link>
      <guid>https://dev.to/kbk0125/javascripts-filter-function-explained-by-applying-to-college-58j0</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you are familiar with the college application process, then you can understand JavaScript’s filter functions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Compared to the map() and reduce() methods in JavaScript, the filter( )method has probably the most straightforward name.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You input an array, and you filter out the elements that fulfill a specific condition into a new array.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This seems simple, but I always seemed to find myself reverting to for() loops. So, I decided to find a better way to understand how filter() functions worked.&lt;/p&gt;

&lt;p&gt;I realized that filter functions are kind of like a college admissions officer. They use a set of parameters to decide which students should be admitted to their particular college. Yes, we all wish that colleges were a little more flexible and judged our accomplishments holistically, but in reality, most still have hard numbers around SAT, ACT and GPA scores that determine who will be considered.&lt;/p&gt;

&lt;p&gt;Let’s get into 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%2Fd6e97b1xau45qwry81i5.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%2Fd6e97b1xau45qwry81i5.png" width="800" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using A For Loop Instead of Filter Function
&lt;/h3&gt;

&lt;p&gt;Okay, let’s say that we have an array of 4 students with names and GPAs. This particular college only wants to admit students with a 3.2 GPA or higher. Here is how you might do that.&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;students&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="s2"&gt;david&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sheila&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alonzo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.65&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="s2"&gt;Mary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.8&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;admitted&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;students&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="nx"&gt;students&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;gpa&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;3.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="nx"&gt;admitted&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;students&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="cm"&gt;/*admitted = [
  { 
    name: "david", 
    GPA: 3.3 
  }, 
  { 
    name: "Alonzo", 
    GPA: 3.65 
  }, 
  { 
    name: "Mary", 
    GPA: 3.8 
  }
];*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wow! That was way more complicated than it needed to be. If someone was reading over your code, they would need to track multiple arrays just to learn that you were simply filtering one array into another. And, you need to carefully track &lt;em&gt;i&lt;/em&gt; as you go in order to avoid any bugs. Let’s learn how to use the filter method to accomplish the same thing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using the Filter() Method
&lt;/h3&gt;

&lt;p&gt;Let’s learn how to accomplish the same goal with the filter() method.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Filter is an array method, so we will start with the array of students.&lt;/li&gt;
&lt;li&gt;It uses a callback function that runs on each element in the array.&lt;/li&gt;
&lt;li&gt;It uses a return statement to show which elements will actually end up in the final array, in this case, the admitted students.
&lt;/li&gt;
&lt;/ol&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;students&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="s2"&gt;david&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sheila&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alonzo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.65&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="s2"&gt;Mary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.8&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;admitted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;students&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&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;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gpa&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;3.2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="cm"&gt;/*admitted = [
  { 
    name: "david", 
    GPA: 3.3 
  }, 
  { 
    name: "Alonzo", 
    GPA: 3.65 
  }, 
  { 
    name: "Mary", 
    GPA: 3.8 
  }
];*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The inputs and outputs are the same, so here’s what we did differently:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We didn’t need to declare the admitted array and then fill it later. We declared it and then filled it with elements in the same code block&lt;/li&gt;
&lt;li&gt;We actually used a condition within the return statement! That means that we only return elements that pass a certain condition.&lt;/li&gt;
&lt;li&gt;We can now use &lt;em&gt;student&lt;/em&gt; for each element in the array, rather than students[i] like we did in the &lt;em&gt;for&lt;/em&gt; loop.&lt;/li&gt;
&lt;/ol&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%2Fcdn.filestackcontent.com%2FIDqqk2Y7RvC5mkYGwom6" 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%2Fcdn.filestackcontent.com%2FIDqqk2Y7RvC5mkYGwom6" alt="filterDiagramV1.jpg" width="711" height="980"&gt;&lt;/a&gt;&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%2Fcdn.filestackcontent.com%2FBb3Z8tQiRpal46iYJJqT" 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%2Fcdn.filestackcontent.com%2FBb3Z8tQiRpal46iYJJqT" alt="filterDiagramV2.jpg" width="711" height="980"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may notice that one thing is counterintuitive- getting admitted to college is the last step, but in our code, the variable &lt;em&gt;admitted&lt;/em&gt; is the first part of the statement! You might usually expect to find the final array as the last statement within the function. Instead, we use return to indicate which elements will end up in &lt;em&gt;admitted&lt;/em&gt;.&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%2Fcdn.filestackcontent.com%2FCJFC1S8VShCCAjcNCLqK" 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%2Fcdn.filestackcontent.com%2FCJFC1S8VShCCAjcNCLqK" alt="filterCodeBlock1.jpg" width="1670" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2- Using Two Conditions Within Filter
&lt;/h3&gt;

&lt;p&gt;So far, we have just used one condition in our filter methods. But that does not represent the college admission process at all! Usually, admissions officers are looking at 10+ factors.&lt;/p&gt;

&lt;p&gt;Let’s look at two factors- GPA and SAT scores. Students must have a GPA over 3.2 and an SAT score over 1900. Here is what the same function 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;let&lt;/span&gt; &lt;span class="nx"&gt;students&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="s2"&gt;david&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;SAT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2000&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="s2"&gt;sheila&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;SAT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1600&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="s2"&gt;Alonzo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.65&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;SAT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1700&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="s2"&gt;Mary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;GPA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;SAT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2100&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;admitted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;students&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&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;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gpa&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;3.2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SAT&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1900&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="cm"&gt;/*admitted = [
  {
    name: "david",
    GPA: 3.3,
    SAT: 2000
  },
  {
    name: "Mary",
    GPA: 3.8,
    SAT: 2100
  }
];*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks pretty similar, right? Now we just have two conditions within the &lt;em&gt;return&lt;/em&gt; statement. But let’s break that code down a bit further.&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;admitted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;students&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&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;goodStudent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gpa&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;3.2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SAT&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1900&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;goodStudent&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;Aha! So here is another important difference when compared to &lt;em&gt;for&lt;/em&gt; loops. If you check out the goodStudent variable, you can see that it will only evaluate to &lt;em&gt;true&lt;/em&gt; or &lt;em&gt;false&lt;/em&gt;. Then, that boolean is fed into the return statement.&lt;/p&gt;

&lt;p&gt;So, that &lt;em&gt;true&lt;/em&gt; or false really just decides if each member of the original array will be included or not in the resulting array, &lt;em&gt;admitted&lt;/em&gt;.&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%2Fcdn.filestackcontent.com%2Fly7otC19QNasdN29wBQF" 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%2Fcdn.filestackcontent.com%2Fly7otC19QNasdN29wBQF" alt="twopartfilter1.jpg" width="712" height="980"&gt;&lt;/a&gt;&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%2Fcdn.filestackcontent.com%2FzYB4yi8ZSmKSu1loFM92" 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%2Fcdn.filestackcontent.com%2FzYB4yi8ZSmKSu1loFM92" alt="twopartfilter2.jpg" width="712" height="980"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Get More Visual Tutorials
&lt;/h3&gt;

&lt;p&gt;Did you enjoy this tutorial? You will probably also enjoy my other visual explanations of web development topics on the &lt;a href="https://codeanalogies.com" rel="noopener noreferrer"&gt;CodeAnalogies blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
