<?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: Briggs Elsperger</title>
    <description>The latest articles on DEV Community by Briggs Elsperger (@i3uckwheat).</description>
    <link>https://dev.to/i3uckwheat</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%2F750896%2F2a9408ab-5caf-4c08-9d49-c017ef2e1d9a.png</url>
      <title>DEV Community: Briggs Elsperger</title>
      <link>https://dev.to/i3uckwheat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/i3uckwheat"/>
    <language>en</language>
    <item>
      <title>Understanding Callbacks</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Tue, 15 Mar 2022 21:27:29 +0000</pubDate>
      <link>https://dev.to/i3uckwheat/understanding-callbacks-2o9e</link>
      <guid>https://dev.to/i3uckwheat/understanding-callbacks-2o9e</guid>
      <description>&lt;h1&gt;
  
  
  Understanding Callbacks
&lt;/h1&gt;

&lt;p&gt;Callbacks seem to be a sticking point for people new to programming. Put simply, callbacks are functions that are passed into another function as an argument. With the many ways one can define a function in JavaScript, it's no wonder why callbacks get confusing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Anatomy of a Function
&lt;/h2&gt;

&lt;p&gt;JavaScript has many ways of defining a function, but they all follow a similar pattern and have the same pieces, they just look a bit different. There is more technical terminology surrounding functions, but we are going to gloss over them for now. (If you are interested, feel free to look up "Function Declarations" and "Function Expressions").&lt;/p&gt;

&lt;h3&gt;
  
  
  Normal Functions (Named Functions)
&lt;/h3&gt;

&lt;p&gt;Normal functions, probably the first way you learned about creating functions. Understanding the anatomy of these will help you understand the other types of functions as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funkyFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isWhiteBoy&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;isWhiteBoy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Play: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is actually called a &lt;code&gt;function declaration&lt;/code&gt; and is broken into a few parts.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;function&lt;/code&gt; keyword

&lt;ul&gt;
&lt;li&gt;This tells the JavaScript compiler you are making a named function&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The name

&lt;ul&gt;
&lt;li&gt;This is the name of the function, and what you will use when you call it. It is also used in stack traces.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The parameters

&lt;ul&gt;
&lt;li&gt;everything between &lt;code&gt;(&lt;/code&gt; and &lt;code&gt;)&lt;/code&gt; is a parameter, these must be separated by commas if there is more than one. There may also be nothing between the &lt;code&gt;()&lt;/code&gt; if the function does not take any parameters. The parenthesis are required.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The function body

&lt;ul&gt;
&lt;li&gt;This is where the function actually does something. This code gets run with whatever values are passed into the parameters.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Calling a function looks similar to declaring it. When you call the function, you type the name of the function and add &lt;code&gt;()&lt;/code&gt; after. (without the &lt;code&gt;function&lt;/code&gt; keyword and the body). Inside the &lt;code&gt;()&lt;/code&gt; you may pass it the values you want the parameters you defined to represent. These &lt;code&gt;arguments&lt;/code&gt; are used like variables inside the body of the 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="c1"&gt;// Calling a function&lt;/span&gt;
&lt;span class="nx"&gt;funkyFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;that funky music&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// This prints "Play: that funky music" in the terminal.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Anonymous Functions
&lt;/h3&gt;

&lt;p&gt;These are very similar to normal functions, with just a few differences. Anonymous functions are not 'named', and have a few different syntaxes. Even though they cannot have a name, they can be assigned to a variable. Even though when assigned to a variable they show up in stack traces, they are still considered an anonymous function. They may show up as 'anonymous function' in stack traces when passed into other functions as callbacks, however.&lt;/p&gt;

&lt;p&gt;Anonymous functions are mostly used by passing them into other functions as a &lt;code&gt;callback&lt;/code&gt;. This will become more clear later.&lt;/p&gt;

&lt;p&gt;Each of the functions below are identical to the funkyFunction above in their 'funk-tionality'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This example is still an anonymous function even though we used the `function` keyword, as it doesn't have a name.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;funkyFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isWhiteBoy&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;isWhiteBoy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Play: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// This is called an arrow function, we'll get into these soon.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;funkyFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isWhiteBoy&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;isWhiteBoy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Play: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An anonymous function is just a function that does not have a name, this doesn't mean that it cannot be called. Each of the above functions can be called exactly the same way:&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;funkyFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;that funky music&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is because functions are 'first class citizens' in JavaScript and can be assigned to variables. Or passed as an argument to another function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Arrow Functions
&lt;/h4&gt;

&lt;p&gt;These are just a shorter way to write a function. They do have some special rules however, and understanding the rules imposed by arrow functions will help you understand callbacks. We're going to ignore the &lt;code&gt;this&lt;/code&gt; binding rules for these functions for now.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there is only one argument, the parenthesis &lt;code&gt;()&lt;/code&gt; can be omitted&lt;/li&gt;
&lt;li&gt;if arrow functions are one line, the brackets &lt;code&gt;{}&lt;/code&gt; can be omitted.

&lt;ul&gt;
&lt;li&gt;When omitting the brackets, the arrow function returns the evaluated expression without requiring the &lt;code&gt;return&lt;/code&gt; keyword.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;The functions below are variations of the rules 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;playThe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;funky&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;funky&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; music&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;playThe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;funky&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;funky&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; music&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;playThe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;funky&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;funky&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; music&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// You can call all of these functions like: `playThe('blues')`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below are some examples of an arrow function without an argument. These functions are all identical as well. Notice the &lt;code&gt;()&lt;/code&gt; in place of any named arguments. It is required because there aren't any parameters.&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;playThat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;funky music&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;playThat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;funky music&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;playThat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;funky music&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Point
&lt;/h3&gt;

&lt;p&gt;Take some time and study the function examples above and note how they are similar and how the same parts exist in both, with the exception of the &lt;code&gt;function&lt;/code&gt; keyword.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Callbacks Look Like
&lt;/h2&gt;

&lt;p&gt;You most likely have seen, or even used, callbacks and not realized it. They are used frequently in JavaScript. Understanding JavaScript is impossible without understanding callbacks. Below is an example of something you may have run into before.&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;notes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;do&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;re&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;me&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the &lt;code&gt;forEach&lt;/code&gt; array method. This method simply takes a &lt;code&gt;callback&lt;/code&gt; function as its argument. (Don't forget that &lt;code&gt;forEach&lt;/code&gt; is a function itself).&lt;/p&gt;

&lt;p&gt;There are many other ways to do the same thing (as is tradition in JavaScript), below are a few more ways to write this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;notes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;do&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ray&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;me&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// This one is tricky, but will make more sense later&lt;/span&gt;
&lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How Callbacks Work
&lt;/h2&gt;

&lt;p&gt;To state it once more: Callbacks are just functions passed into other functions as arguments (as a parameter).&lt;/p&gt;

&lt;h3&gt;
  
  
  Iterator Functions
&lt;/h3&gt;

&lt;p&gt;Below is what &lt;code&gt;forEach&lt;/code&gt; might look like under the hood, notice it calls the &lt;code&gt;callback&lt;/code&gt; function each time it loops over an item.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&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;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;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// This is when the callback function gets called, or executed&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// You would call it like this:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myArry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;myForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myArry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;WHOA, hold up. Where did &lt;code&gt;item&lt;/code&gt; come from?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This came from the function &lt;code&gt;myForEach&lt;/code&gt; calling the callback with an argument. The line with &lt;code&gt;callback(array[i])&lt;/code&gt; is calling the callback function with an argument, which we defined inline as an anonymous function. Below are more examples of how this could be called.&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;myArry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// We do not need the `()` in this case, as we only have one argument and we are using an arrow function&lt;/span&gt;
&lt;span class="nx"&gt;myForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myArry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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="c1"&gt;// We can pass arguments to this kind of anonymous function as well&lt;/span&gt;
&lt;span class="nx"&gt;myForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myArry&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;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// This time we are declaring the function we want to use as a callback&lt;/span&gt;
&lt;span class="c1"&gt;// Notice we define `item` as a parameter to be passed in when it's called by the `myForEach` function.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printItemPlusTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// `item` is passed into the function, we do not need to declare it here because we declared it elsewhere. &lt;/span&gt;
&lt;span class="c1"&gt;// It is the same as the 'console.log' example above except we declared our own function.&lt;/span&gt;
&lt;span class="nx"&gt;myForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myArry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;printItemPlusTwo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another good example of how callbacks work might be the &lt;code&gt;.map&lt;/code&gt; method (read more on MDN), below is one way it might be implemented.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myNewArray&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;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;callbackResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&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;myNewArray&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;callbackResult&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;myNewArray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="c1"&gt;// This could be called like this:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myMap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayNum&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;arrayNum&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="p"&gt;});&lt;/span&gt;


&lt;span class="c1"&gt;// OR&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myMap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayNum&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;arrayNum&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Event Listeners (DOM)
&lt;/h3&gt;

&lt;p&gt;Event listeners in JavaScript seem to be confusing to people, but after understanding callbacks, these should be a lot easier to understand. &lt;/p&gt;

&lt;p&gt;Let's review what they look like, see if you can pick out the different things going on.&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#myId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&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;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// `event` is passed into the callback from the `.addEventListener` function when it receives a 'click' event.&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you notice, the second argument (value you pass into a function) to &lt;code&gt;addEventListener&lt;/code&gt; is a function. In this case it's an anonymous arrow function. This piece of code could have also have been written like this and it would behave identically.&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#myId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Part of what confuses people is the &lt;code&gt;event&lt;/code&gt; object. Where does it come from? How does it get there? &lt;/p&gt;

&lt;p&gt;This event object is passed into the callback function by the &lt;code&gt;.addEventListener&lt;/code&gt; function. A function is calling another function.&lt;/p&gt;

&lt;p&gt;This is because.... Callbacks are just functions passed into another function as arguments.&lt;/p&gt;

&lt;p&gt;That means we can declare a function outside of the argument list and just add it by its name as well. Like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myEventHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// do something, probably with 'event'&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#myId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;myEventHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how we didn't 'call' the function called &lt;code&gt;myEventHandler&lt;/code&gt;? If we were to call it inside the parameter list, the function we called &lt;code&gt;myEventHandler&lt;/code&gt; would run immediately and give the &lt;code&gt;addEventListener&lt;/code&gt; the result of calling that function. (in this case, it would be undefined)&lt;/p&gt;

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

&lt;p&gt;Callbacks are an important part of JavaScript, they are vital to understand, even with the onset of promises and async/await. Callbacks get called by another function, so you don't have to call them in the arguments, ( Calling a function is using a function's name and adding &lt;code&gt;()&lt;/code&gt; to the end of it, like &lt;code&gt;console.log()&lt;/code&gt; )&lt;/p&gt;

&lt;p&gt;These are something that you will learn if you give yourself time, understanding how they work will make your JavaScript career a lot easier!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Early Return Pattern</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Tue, 15 Mar 2022 21:25:06 +0000</pubDate>
      <link>https://dev.to/i3uckwheat/early-return-pattern-558i</link>
      <guid>https://dev.to/i3uckwheat/early-return-pattern-558i</guid>
      <description>&lt;h1&gt;
  
  
  Early Return Pattern
&lt;/h1&gt;

&lt;p&gt;People new to programming sometimes struggle to understand returning early inside a function, especially early beginners when coming from a language like Ruby, which has implicit returns. Today we're going to explore what an early return is, and what it can be used for in the context of JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding &lt;code&gt;return&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;To understand what an early return is, you must first understand what &lt;code&gt;return&lt;/code&gt; does in a function. Put simply, &lt;code&gt;return&lt;/code&gt; returns a value from the function; it could be anything. In JavaScript you can return anything- a string, an object, or even another function. When a &lt;code&gt;return&lt;/code&gt; statement has been found, the function ends immediately and returns the value to whatever called the 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;function&lt;/span&gt; &lt;span class="nx"&gt;timesTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&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="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timesTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example you would expect &lt;code&gt;42&lt;/code&gt; to be logged to the console, and you'd be correct. The &lt;code&gt;timesTwo&lt;/code&gt; function returned the result of &lt;code&gt;x * 2&lt;/code&gt; to &lt;code&gt;console.log&lt;/code&gt;, and &lt;code&gt;console.log&lt;/code&gt; printed the value to the console.&lt;/p&gt;

&lt;h2&gt;
  
  
  Early Returns
&lt;/h2&gt;

&lt;p&gt;An early return is a &lt;code&gt;return&lt;/code&gt; statement that is placed before some other code has been run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;notFunkyFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;... And just when, it hit me, somebody turned around and shouted:&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Play that funky music white boy&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;notFunkyFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function will only log &lt;code&gt;'... And just when, it hit me, somebody turned around and shouted:'&lt;/code&gt; and will never log &lt;code&gt;'Play that funky music white boy'&lt;/code&gt; due to the &lt;code&gt;return&lt;/code&gt; statement. Using this, we can do some neat things to make our code look cleaner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Early Return pattern
&lt;/h2&gt;

&lt;p&gt;Since we can use a &lt;code&gt;return&lt;/code&gt; statement to end a function immediately, we can take advantage of it. Let's look at a traditional &lt;code&gt;if else&lt;/code&gt; statement and how one might convert it to the early return pattern.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;healthBarColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;maxHealth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hasShield&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;percentLeft&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;maxHealth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&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;hasShield&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;percentLeft&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;percentLeft&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;healthBarColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;124&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, we will find the value &lt;code&gt;'yellow'&lt;/code&gt; in the console. This looks OK, but it can be cleaner with the use of an early return. Let's refactor &lt;code&gt;healthBarColor&lt;/code&gt; to use this pattern now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;healthBarColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;maxHealth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hasShield&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;percentLeft&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;maxHealth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&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;hasShield&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;percentLeft&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;percentLeft&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;healthBarColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;124&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is functionally the same as the code above, but it is arguably more readable and is shorter. This works because when we &lt;code&gt;return&lt;/code&gt; from a function, the function ends then and there and gives us the value we desire. We can clean this up even more though, with a quirk of JavaScript. In JavaScript, if you have an if statement that is one line, you can omit the curly braces ( &lt;code&gt;{&lt;/code&gt; &lt;code&gt;}&lt;/code&gt; ). That would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;healthBarColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;maxHealth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hasShield&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;percentLeft&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;maxHealth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&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;hasShield&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;blue&lt;/span&gt;&lt;span class="dl"&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;percentLeft&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;15&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;red&lt;/span&gt;&lt;span class="dl"&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;percentLeft&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;60&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;yellow&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;green&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;healthBarColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;124&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&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 have some clean, and easy to understand code that is still performant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gotcha!'s
&lt;/h2&gt;

&lt;p&gt;Although the early return pattern can be very clean, there are times where it is contraindicated, mainly in non-functional style functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;attackedHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getArmorStrength&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;damageValue&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;damage&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;damageValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;calculateDamage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&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;damage&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;damageValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;damage&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damage&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;damageValue&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;applyDamage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damageValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;setArmorStrength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damageValue&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 looks clean, but is a bad idea because each &lt;code&gt;if&lt;/code&gt; statement has to be evaluated, even if the first one is &lt;code&gt;true&lt;/code&gt;. This technically isn't an early return pattern, but it does mimic it, even though it is bad practice. To fix this, you would have to use the standard &lt;code&gt;if else&lt;/code&gt; 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;function&lt;/span&gt; &lt;span class="nx"&gt;attackedHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getArmorStrength&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;damageValue&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;damage&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;damageValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;calculateDamage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&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;damage&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;damageValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;damage&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="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;damage&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;damageValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;applyDamage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damageValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;setArmorStrength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damageValue&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;Another time the early return pattern will not work is when you have to run code after the &lt;code&gt;if else&lt;/code&gt; statement. The above code is a good example of this. If you were to return from the function within the &lt;code&gt;if&lt;/code&gt; statements, the rest of the function would not run. This is called 'unreachable code' and would introduce bugs and logic errors in your program.&lt;/p&gt;

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

&lt;p&gt;The early return pattern (have I said 'early return pattern' enough yet?), is a popular pattern and something you might see in other's code. Understanding why it's used and when to use it is important when it comes down to using it yourself. Now get out there and use this newfound knowledge to write better code!&lt;/p&gt;

&lt;h3&gt;
  
  
  P.S.
&lt;/h3&gt;

&lt;p&gt;This pattern can be used with multi-line conditionals as well. The example below is perfectly fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;attackedHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getArmorStrength&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;damage&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&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;damageValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;calculateDamage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&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;takeDamage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damageValue&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;damage&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;armorStrength&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;takeArmorDamage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;damage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Conclusion</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Sat, 12 Mar 2022 21:17:56 +0000</pubDate>
      <link>https://dev.to/theodinproject/conclusion-367n</link>
      <guid>https://dev.to/theodinproject/conclusion-367n</guid>
      <description>&lt;p&gt;The Odin Project is around to help you succeed. The people who wrote the project are professionals and have very successful careers. Among the team are ex-teachers, Red Hat employees, large bank employees, and startup employees. The skills and expertise the team brings together for everyone to learn from is immense. There’s good reason to trust The Odin Project and the team behind it. Now, with that being said, get out there and become a success story.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Strategically building your portfolio</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Sat, 12 Mar 2022 21:17:34 +0000</pubDate>
      <link>https://dev.to/theodinproject/strategically-building-your-portfolio-1km4</link>
      <guid>https://dev.to/theodinproject/strategically-building-your-portfolio-1km4</guid>
      <description>&lt;p&gt;There is no degree or certification for finishing The Odin Project, but it does leave you with the potential for a killer portfolio. There are times when you should spend extra time on a project, and times when you shouldn’t. Not every project should be a portfolio piece.&lt;/p&gt;

&lt;h1&gt;
  
  
  Foundations isn’t a strong portfolio generator
&lt;/h1&gt;

&lt;p&gt;People tend to spend a lot of time on the first few projects expecting them to be portfolio pieces; the problem with this approach is that you will be building more impressive projects very soon after Foundations. Save your time and energy for those projects. The capstone project for Foundations, however, &lt;em&gt;is&lt;/em&gt; worth spending extra time on to ensure you are demonstrating the best of your abilities at that time.&lt;/p&gt;

&lt;h1&gt;
  
  
  Pick projects you like and use them as portfolio fodder
&lt;/h1&gt;

&lt;p&gt;You will like some projects more than others. Building a portfolio can be a lot more effective if you spend the time on projects you are enjoying. Pick a few projects past Foundations that you really enjoy, go the extra mile, and make them your own.&lt;/p&gt;

&lt;h1&gt;
  
  
  Always spend extra time on capstone projects
&lt;/h1&gt;

&lt;p&gt;Spend the most time on the final projects for each course. This will ensure you have strong portfolio pieces and will help you understand if you need to go back into the course and refresh.&lt;/p&gt;

&lt;h1&gt;
  
  
  Use git properly
&lt;/h1&gt;

&lt;p&gt;Git is &lt;strong&gt;very&lt;/strong&gt; underrated among budding developers. Don’t be afraid to experiment on your code; if you’re properly using git, you can easily go back to a working state (which helps immensely with refactoring). Use branches for experiments; if they’re successful, merge them with your main branch, otherwise, toss them out! Writing good commit messages means you will know what you were doing when you come back to the project in 2 weeks. This is a great resource on commit messages: &lt;a href="https://cbea.ms/git-commit/"&gt;https://cbea.ms/git-commit/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Applying to jobs</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Mon, 07 Mar 2022 17:50:34 +0000</pubDate>
      <link>https://dev.to/theodinproject/applying-to-jobs-1cac</link>
      <guid>https://dev.to/theodinproject/applying-to-jobs-1cac</guid>
      <description>&lt;p&gt;Getting a job is the goal of most people working through The Odin Project, so it would feel foolish to not talk about applying for jobs. Applying for jobs tends to take a lot of time; it’s best to do it strategically.&lt;/p&gt;

&lt;h1&gt;
  
  
  Avoid freelancing
&lt;/h1&gt;

&lt;p&gt;Freelancing is often described as an easy way to break into the programming field. The problem with this mindset is based on a surface level understanding of web development. Wordpress, for example, makes it easy to get a basic brochure site live and visible. This is what makes people generalize about freelancing being easy. Don't mistake that simplicity for what is brought to you by TOP. Freelancing using the tools The Odin Project helps you learn requires much more knowledge and experience. The tools introduced bring more potential for difficult situations that can make freelancing a negative experience for all parties involved. Without the mentorship of experts, you may not be able to ship software that is scalable and safe for your clients. Certain practices can really only be learned at a company or through failure, and failure during freelancing could mean a lost revenue stream, or worse, a lawsuit. Freelancing also means you have the burden of becoming the marketer, the CEO, the financial department, and all of the other business development representatives at once. This can cut deeply into your time, and that time could be better spent becoming a stronger developer hired into a full time role.&lt;/p&gt;

&lt;h1&gt;
  
  
  Don’t apply too early
&lt;/h1&gt;

&lt;p&gt;Applying too early wastes valuable time that could be spent moving the needle away from “luck” and towards “likely”, by gaining more skills. Generally, you should apply around the midpoint of Rails, or near the end of Node. Don’t rush yourself. If your strategy is to be “lucky”, consider that employers that are willing to hire someone with underdeveloped skills may be predatory and may not have devices available to help you grow as a developer. Foundations, and even the React sections, are not enough for you to be a strong programmer in most cases. More general programming concepts are introduced later in the curriculum that you will need on the job.&lt;/p&gt;

&lt;h1&gt;
  
  
  Check out the getting-hired section when you’re ready
&lt;/h1&gt;

&lt;p&gt;After you’ve hit a place where you should be applying, make sure you go over the getting-hired section of the curriculum. Also check out the pins in the getting-hired section of the Discord.&lt;/p&gt;

&lt;h1&gt;
  
  
  Look at already reviewed resumes
&lt;/h1&gt;

&lt;p&gt;Get in the Discord, in the getting-hired channel, and look for a pin that teaches you how to find resumes that have been reviewed. (In case it disappears: use the search and search for images in the getting-hired channel.). Base your resume on those: read all the feedback they’ve gotten and apply it to your own. Pay close attention to verb tenses, summaries, and what resume builders are being used.&lt;/p&gt;

&lt;h1&gt;
  
  
  Ask for a resume review
&lt;/h1&gt;

&lt;p&gt;After looking at, and following the advice found in the getting-hired course and Discord channel, post your resume for review (make sure to use a screenshot of your resume; do not upload a PDF; do block out your address and phone number.). Expect some strong and terse feedback, but do not take it personally; everyone is there to help you succeed. Carefully select those you will listen to. Generally the Maintainers have strong advice. Once you’ve gotten feedback, post the updated resume and gather more feedback. Building and refining a resume is an iterative process.&lt;/p&gt;

&lt;p&gt;Next up, Strategically building your portfolio. Then, the conclusion!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Learning Code</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Sat, 26 Feb 2022 18:24:30 +0000</pubDate>
      <link>https://dev.to/theodinproject/learning-code-f56</link>
      <guid>https://dev.to/theodinproject/learning-code-f56</guid>
      <description>&lt;p&gt;Ultimately we all need to write software in a way that is professional and extensible. This skill is something that will be developed over time; don’t expect to write perfect code on your first attempt, or even your 400th attempt. Constant improvement is the goal. Even professionals do not write perfect code. Constantly improving in a self paced course with minimal feedback can be difficult, but is not impossible.&lt;/p&gt;

&lt;h1&gt;
  
  
  Focus on the point of the assignment
&lt;/h1&gt;

&lt;p&gt;If the assignment is focusing on Javascript, don’t spend 10 hours on styling. Focus on the task at hand and keep moving through the curriculum. This will keep things fresh and ensure you are constantly learning things rather than tweaking the font size for 2 hours. That doesn’t mean you should totally ignore other tasks &lt;em&gt;around&lt;/em&gt; the main task, however. Spend some time styling your projects to some extent; just don’t waste your time on it. You’ll still want to practice other skills as you move through the curriculum so you don’t get rusty. Remember that TOP is meant to constantly build on the previous lessons.&lt;/p&gt;

&lt;p&gt;Your approach should be different if you’re using a project as a portfolio piece, however, In that case, spend a good amount of time polishing and making sure it’s your best work. There is more about portfolio pieces in the “Strategically building your portfolio” section.&lt;/p&gt;

&lt;h1&gt;
  
  
  Run every piece of code you come across
&lt;/h1&gt;

&lt;p&gt;Examples in TOP are meant to be run and played with. Try to pick each example apart yourself and see how it works. Modify it, break it, fix it, run it, bop it. Doing so will help you understand the concepts that are being shared during your readings.&lt;/p&gt;

&lt;p&gt;One strategy to doing this effectively is to make assumptions about the code, and then check those assumptions. You should NOT make assumptions that you do not check. If something behaves in a way you do not expect, ensure you spend a bit of time trying to understand why. Avoid rabbit holes: sometimes things are explained later, often even in the next section.&lt;/p&gt;

&lt;h1&gt;
  
  
  Avoid rabbit holes
&lt;/h1&gt;

&lt;p&gt;It can be tempting to go deep into a subject and feel like you need to become an expert &lt;em&gt;right now&lt;/em&gt;. Avoid these feelings. There are a lot of chicken/egg problems in programming where you won’t be able to fully understand something until you understand something else first, but you can’t fully understand that something else without understanding the first thing. Do a bit of research, but if you’re finding yourself following a bunch of links, or on the 3rd page of Google, it may be time to move on from the subject. You can always ask in the chat.&lt;/p&gt;

&lt;h1&gt;
  
  
  Plan your projects on the macro level, use pseudocode on the micro level
&lt;/h1&gt;

&lt;p&gt;Plan out, avoiding too much detail, how your project will look and function. Try to identify a few major pieces of the application and their interactions and create a wireframe to help with the visual aspect of the app. You should do this with &lt;strong&gt;every project&lt;/strong&gt;; it’ll keep you on track and out of the weeds. It’s a mistake to go into too much detail in your application before writing it, but outlining some of the major systems that you’ll need to build can help in keeping your code organized. Resist the urge to over plan. You cannot predict everything you will run into in your application, and trying to stick to an over-planned project is going to slow you down.&lt;/p&gt;

&lt;p&gt;Once you are at the implementation phase, pseudocode your functions before you write them in code. Pseudocode helps you think about a problem separately from thinking about the code. Separating the “problem” from the “code” is an effective strategy for preventing stalls. It also has you get thoughts down if you have to step away unexpectedly. Check this article out for more information: &lt;a href="https://users.csc.calpoly.edu/%7Ejdalbey/SWE/pdl_std.html" rel="noopener noreferrer"&gt;https://users.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Don’t look at solutions until you’ve completed the project
&lt;/h1&gt;

&lt;p&gt;Wanting to look at a solution to get yourself started is a normal inclination, but it’s one that should be resisted. Giving yourself the opportunity to struggle through something will help to build the pathways in your brain that will make problem solving easier in the future. Not allowing those pathways to be built means that more difficult problems (that likely will not have solutions for you to peek at) could become insurmountable. Instead, if you get completely stuck, ask in the Discord chat. People there can help guide you to an answer, rather than give you a solution outright and circumvent a learning moment.&lt;/p&gt;

&lt;h1&gt;
  
  
  Read Others’ Code
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;After&lt;/em&gt; you’ve completed a project, you should spend a significant amount of time reading others’ code and trying to understand it. Read good code, bad code, and everything in between. Ensure that you are looking at solutions that are NOT the top rated as well. At first it may be difficult to see the difference between good and bad code, but over time you will start to recognize the patterns that are strong, and those that are weak. Compare pieces of code to yours and identify the differences that make something easier or harder to read. Try to understand what was going through someone’s mind while writing the code, and what kinds of trade-offs they made. Make sure you run the code and tweak it and play with it as well, as this is where most of your learning happens.&lt;/p&gt;

&lt;p&gt;It is also beneficial to read and review code to the best of your ability in the showcase on the Discord server. You can also spend time discussing &lt;em&gt;why&lt;/em&gt; someone decided to do something a certain way.&lt;/p&gt;

&lt;h1&gt;
  
  
  Refactor your own code
&lt;/h1&gt;

&lt;p&gt;Now that you’ve finished your project and have looked through others’ solutions for a while, spend a bit of time refactoring your own code using the lessons learned. This isn’t a process that has to be exhaustive, but spending time implementing some patterns that you found effective can really help you learn &lt;em&gt;why&lt;/em&gt; those patterns were used, or &lt;em&gt;how&lt;/em&gt; someone discovered them. Be sure you are not spending too much time on this instead of moving on. Additionally, don’t use anything from someone else’s code if you don’t understand it; either seek to understand it or move on for the time being. You never know when you’ll be asked about it.&lt;/p&gt;

&lt;p&gt;One mistake people make is throwing their code out when they find they’re running into difficulty. Instead, try to refactor the code you have into the shape you ideally want it to be. There are times where throwing code out is useful, but that is usually in the beginning sections of TOP, on the very early projects. In general, avoid throwing code out as much as possible.&lt;/p&gt;

&lt;h1&gt;
  
  
  Be OK with OK
&lt;/h1&gt;

&lt;p&gt;You’re likely new to writing code, so resist the urge to spend too much time trying to write things perfectly. If you have completed the requirements using the topics introduced in the sections prior to a project, you can consider the project complete. Being a perfectionist is a direct route to burnout and potentially to not finishing the course. If you are unsure if your project is complete enough, make sure that you post it in the showcase.&lt;/p&gt;

&lt;h1&gt;
  
  
  Write your unit tests
&lt;/h1&gt;

&lt;p&gt;When you hit the sections of the curriculum that teach you how to write unit tests, &lt;strong&gt;write them&lt;/strong&gt;. They are important. If you have not reached the point in the curriculum where tests are talked about in detail, remember they are something to spend extra time with. In addition to helping you write cleaner code, having unit tests in your code makes you appear more professional if a potential employer takes a look at your GitHub.&lt;/p&gt;

&lt;h1&gt;
  
  
  Use Git
&lt;/h1&gt;

&lt;p&gt;Use it, it’s used everywhere, it’s one of the easiest ways to make yourself look like a professional, and makes it very easy to share code when you need help. Spend time writing good commit messages and using it as it’s intended. Read this &lt;a href="https://cbea.ms/git-commit/" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://cbea.ms/git-commit" rel="noopener noreferrer"&gt;https://cbea.ms/git-commit&lt;/a&gt; and come back to it often. Make sure your projects are pushed to GitHub enabling you to easily share code with helpers in the Discord server, or even to employers. Learning to effectively use git will also make life a lot easier when it comes to experimenting with code (which you should do a lot of).&lt;/p&gt;

&lt;h1&gt;
  
  
  Strategically building your portfolio
&lt;/h1&gt;

&lt;p&gt;There is no degree or certification for finishing The Odin Project, but it does leave you with the potential for a killer portfolio. There are times when you should spend extra time on a project, and times when you shouldn’t. Not every project should be a portfolio piece.&lt;/p&gt;

&lt;h1&gt;
  
  
  Foundations isn’t a strong portfolio generator
&lt;/h1&gt;

&lt;p&gt;People tend to spend a lot of time on the first few projects expecting them to be portfolio pieces; the problem with this approach is that you will be building more impressive projects very soon after Foundations. Save your time and energy for those projects. The capstone project for Foundations, however, &lt;em&gt;is&lt;/em&gt; worth spending extra time on to ensure you are demonstrating the best of your abilities at that time.&lt;/p&gt;

&lt;h1&gt;
  
  
  Pick projects you like and use them as portfolio fodder
&lt;/h1&gt;

&lt;p&gt;You will like some projects more than others. Building a portfolio can be a lot more effective if you spend the time on projects you are enjoying. Pick a few projects past Foundations that you really enjoy, go the extra mile, and make them your own.&lt;/p&gt;

&lt;h1&gt;
  
  
  Always spend extra time on capstone projects
&lt;/h1&gt;

&lt;p&gt;Spend the most time on the final projects for each course. This will ensure you have strong portfolio pieces and will help you understand if you need to go back into the course and refresh.&lt;/p&gt;

&lt;h1&gt;
  
  
  Use git properly
&lt;/h1&gt;

&lt;p&gt;Git is &lt;strong&gt;very&lt;/strong&gt; underrated among budding developers. Don’t be afraid to experiment on your code; if you’re properly using git, you can easily go back to a working state (which helps immensely with refactoring). Use branches for experiments; if they’re successful, merge them with your main branch, otherwise, toss them out! Writing good commit messages means you will know what you were doing when you come back to the project in 2 weeks. This is a great resource on commit messages: &lt;a href="https://cbea.ms/git-commit/" rel="noopener noreferrer"&gt;https://cbea.ms/git-commit/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Next up, Applying to jobs!&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>learning</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Reading Comprehension</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Sat, 12 Feb 2022 15:48:39 +0000</pubDate>
      <link>https://dev.to/theodinproject/reading-comprehension-22e0</link>
      <guid>https://dev.to/theodinproject/reading-comprehension-22e0</guid>
      <description>&lt;p&gt;Reading comprehension is a developer’s closest friend. Most of the information you will use comes from text documentation. Strengthening reading skills requires practice, so slow down and focus on what you are reading. When you notice your mind drifting, stop, step away for a moment, trace back, then reread what was missed. Getting bored is okay, but if you work toward strengthening your capacity to read, reading boring texts will get easier.&lt;/p&gt;

&lt;h1&gt;
  
  
  Read everything, in order
&lt;/h1&gt;

&lt;p&gt;Be sure to read EVERYTHING in any TOP lesson. Each lesson has very important information that will likely be referenced later. The curriculum is careful to not include too many redundancies, so it can be useful to read over a lesson more than once. When learners have issues with installations, it’s almost always due to missing some nuance or skipping a step. Slowing down and focusing can really help prevent mistakes like this.&lt;/p&gt;

&lt;h1&gt;
  
  
  Generally, don’t take notes
&lt;/h1&gt;

&lt;p&gt;Taking notes is often touted as a study strategy, but with programming you already have every piece of documentation at your fingertips via the Web. It is therefore not ideal to maintain your own version of the docs using your own notes. In addition, when you look something up there is a chance you might gather some tangential information related to the topic you are trying to understand; that information could then help you later when you need to do a related task. This doesn’t mean you shouldn’t write things down when watching a video or reading an article, but it does mean that you should &lt;em&gt;not&lt;/em&gt; rely on your own material as a source of truth. Everything in TOP is an “open book test,” so rely on your Google skills and official documentation. If you do write notes, they should be conceptual and never aim to replace documentation. Sometimes people will write notes and immediately throw them away, using them as a focus strategy rather than a reference list. If this technique is useful for you, continue to apply it. The important thing is to not create notes that get you stuck or trapped.&lt;/p&gt;

&lt;h1&gt;
  
  
  Before acting on directions
&lt;/h1&gt;

&lt;p&gt;Due to the complex nature of some of TOP’s topics, directions can be extremely specific, especially for installations. A strong strategy to avoid missing steps, or getting stuck without realizing the solution is in the very next step, is to read every step from start to finish before executing any commands or actions. If you do so, you will be more prepared to follow the directions, and you will recognize if a step has unexpected results, and avoid going too deeply down a difficult path. Of course, there may still be times you get stuck, and that’s okay. If you do, go ask for help on the Discord server. More on that below.&lt;/p&gt;

&lt;h1&gt;
  
  
  Learning Styles - “I’m a visual learner, so I learn best with videos”
&lt;/h1&gt;

&lt;p&gt;It’s not uncommon for people to believe they have a “learning style”, and say they learn best by watching videos rather than reading. Usually what people &lt;em&gt;actually&lt;/em&gt; mean by saying this is that reading articles is hard and not as immediately stimulating as a video. Learning from videos isn’t easier than reading, but may &lt;em&gt;feel&lt;/em&gt; easier because it’s easy to turn your brain off, let the video play, then copy the code and run it at the end. Engaging your brain at every step of learning is critical. That isn’t to say that videos are useless; there are plenty of useful lectures that focus on conceptual information. But code-along videos are largely something to be avoided. They are mostly useful for the &lt;em&gt;very&lt;/em&gt; beginning stages of learning to program.&lt;/p&gt;

&lt;p&gt;Having a fixed mindset about “learning styles” can really set up roadblocks in your learning. Here is a helpful description of a study about learning styles: &lt;a href="https://poorvucenter.yale.edu/LearningStylesMyth"&gt;https://poorvucenter.yale.edu/LearningStylesMyth&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Instead of getting wrapped up in the idea of your “learning style”, allow yourself to learn from all of TOP’s suggested resources. You may find that learning from new types of resources is surprisingly (and far more!) effective.&lt;/p&gt;

&lt;p&gt;Coming soon, Learning Code!&lt;/p&gt;

</description>
      <category>theodinproject</category>
      <category>reading</category>
    </item>
    <item>
      <title>The Discord Server</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Sat, 05 Feb 2022 03:58:40 +0000</pubDate>
      <link>https://dev.to/theodinproject/the-discord-server-4m73</link>
      <guid>https://dev.to/theodinproject/the-discord-server-4m73</guid>
      <description>&lt;p&gt;The TOP community is one of the most important resources at your disposal. It’s a place to get some help with, and advice on, your code and your TOP journey. In fact, most of the advice in this guide is gathered from advice that has been given on the server. Due to the nuances of text communication and the amount of conversations happening, you should prioritize understanding the goals, policies, and procedures of the community; then you will be ready when you really need help with something.&lt;/p&gt;

&lt;h1&gt;
  
  
  Rules and FAQ
&lt;/h1&gt;

&lt;p&gt;There are rules on the server, and it is in your best interest to read all of them. Anything that is not answered in the rules is likely answered in the FAQ. Ensuring you are up to date on both the rules and FAQ shows that you are acting in good faith in the server, and not being lazy. The rules are important and are taken seriously by the moderation team, who enforce them in order to keep the community safe and professional. The community is massive in size and global reach*.* The team at The Odin Project has iterated over the rules, with the intent of making them as fair as possible while still keeping avenues for professional discourse open and on topic. Ultimately, the rules are in place to help our learners get help as soon as possible while avoiding misunderstandings (both trivial and significant), and to keep the chat a friendly, safe, and welcoming place for everyone.&lt;/p&gt;

&lt;h1&gt;
  
  
  Culture
&lt;/h1&gt;

&lt;p&gt;It’s not a bad idea for you to lurk in the chat for a while to understand what kinds of interactions happen there and how they are resolved. Observing and picking up on &lt;em&gt;how&lt;/em&gt; people interact, before posting something that may not fit well with the culture of the community, can help you avoid an uncomfortable interaction with another member of the community or a moderator. Take your time when observing; culture cannot just be explained, so it’ll be your responsibility to make judgment calls on your posts. The members of the Discord server are forgiving and kind, so don’t expect to be bullied or have issues with not perfectly “fitting in”. Everyone on the server is there to help.&lt;/p&gt;

&lt;p&gt;Despite the fact that it’s impossible to fully explain the culture, there &lt;em&gt;are&lt;/em&gt; a few general tricks someone new to the server can follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assume positive intent. Nearly all tone is lost when sending messages in a text setting. It’s usually best to assume that someone has the best intentions and is not attacking you or your work. Jumping to the conclusion that someone is trying to be rude is a surefire way to feel attacked when you most likely are not. Keep in mind that people from all backgrounds, from varying parts of the world, find their way to Odin, and that English may be someone’s second language.&lt;/li&gt;
&lt;li&gt;If you feel frustrated or upset, make sure to take time to cool down before asking questions. Asking questions when you’re frustrated can make it harder to assume positive intent in others’ messages. Let yourself cool off, then ask your question as kindly and thoroughly as possible. It’s likely nobody wants to deal with you when you’re frustrated as well. Relax, and everyone can have a more productive time in the chat.&lt;/li&gt;
&lt;li&gt;Don’t take “terse” messages as an attack. Often people send short messages for efficiency, not as an indicator they’re annoyed with you, or upset.&lt;/li&gt;
&lt;li&gt;Using bot commands is a reasonable way to communicate; do not assume that they are impersonal or an attack. When someone replies with /question after you’ve asked a question, for example, the intention is to remind you to refresh yourself on the article and be able to ask something that can be more easily answered. This isn’t an attack, but an efficiency to save everyone’s time, including yours.&lt;/li&gt;
&lt;li&gt;Behave the way that you would at a job; be professional and respectful.

&lt;ul&gt;
&lt;li&gt;Being professional means:

&lt;ul&gt;
&lt;li&gt;Not posting random memes in any channel&lt;/li&gt;
&lt;li&gt;Using reacts positively, instead of to tease people&lt;/li&gt;
&lt;li&gt;Replying to questions in a timely manner&lt;/li&gt;
&lt;li&gt;Not jumping into conversations that do not concern you&lt;/li&gt;
&lt;li&gt;Not saying anything that could make someone uncomfortable&lt;/li&gt;
&lt;li&gt;Understanding that others in the server are colleagues first, friends second&lt;/li&gt;
&lt;li&gt;Using proper grammar and punctuation&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h1&gt;
  
  
  Questions
&lt;/h1&gt;

&lt;p&gt;It’s likely that you’ll have some questions, and the TOP Discord server exists for exactly that reason. This doesn’t mean someone should ask with recklessness though; there are strategies to asking a question in a way that makes it more likely to get answered. Here are some of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don’t ask to ask; just ask. This article summarizes it nicely: &lt;a href="https://dontasktoask.com/"&gt;https://dontasktoask.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Ask well thought out questions. Ensure you’ve done your research and can articulate a few things you’ve tried. Follow this guide religiously: &lt;a href="https://medium.com/@gordon_zhu/how-to-be-great-at-asking-questions-e37be04d0603"&gt;https://medium.com/@gordon_zhu/how-to-be-great-at-asking-questions-e37be04d0603&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Be available for discourse; don’t ask a question then leave to make dinner. Make dinner first, then ask your questions so you are around to answer follow-up questions and report back on suggestions.&lt;/li&gt;
&lt;li&gt;Wait around 30 minutes before asking your question again. If your question hasn’t been answered within that time frame, consider that you may not have asked an effective question. There’s also a good chance that there simply was not anybody around that could help you with that question. Don’t forget that the people helping in the server are doing so on a volunteer basis (which means entirely without pay); be sure you are not wasting their time with a poor question.&lt;/li&gt;
&lt;li&gt;Ask in the proper channel or expect to be redirected to the proper channel.

&lt;ul&gt;
&lt;li&gt;Be careful to not speak over others getting help. There’s often another room for the topic. Try elsewhere so you don’t get buried, as long as there is a different relevant channel.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Post ALL of your code when asking a question; this can be in the form of the GitHub repository you’ve been pushing to, a &lt;a href="http://repl.it/"&gt;repl.it&lt;/a&gt;, codepen, or codesandbox. Often the problem isn’t where you think it is, but in the context around it. Make sure you have the code handy when asking a question.&lt;/li&gt;
&lt;li&gt;If you’re working on a large code base, try to recreate the error in a smaller environment. Sometimes this can help you discover where the error is, and if it doesn’t, it will help you frame your question when you ask it.&lt;/li&gt;
&lt;li&gt;Thank the people helping you. A simple @username ++ will give a user a point; use this as a way to say “thank you”. This gesture goes a long way and people will remember it when you next ask for help.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Helping others
&lt;/h1&gt;

&lt;p&gt;Go into the chat and help others learn. You likely have some debugging skills and can work with others to help them solve their problems. Helping out will bolster your own strengths and help prevent some specific weaknesses related to communicating about code.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Mindset</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Sat, 05 Feb 2022 03:25:42 +0000</pubDate>
      <link>https://dev.to/theodinproject/mindset-2bbn</link>
      <guid>https://dev.to/theodinproject/mindset-2bbn</guid>
      <description>&lt;p&gt;Managing your mindset while working through TOP is extremely important, more so than following any other single strategy. The first few sections of the curriculum (the ones about mindset) do a fine job covering how to manage the difficulty, and it can be beneficial to reread those sections when running into difficult times.&lt;/p&gt;

&lt;h1&gt;
  
  
  Get Gritty
&lt;/h1&gt;

&lt;p&gt;There are many technical exercises involved when learning to program; they can be overwhelming and often make a learner feel as if they’re hitting roadblocks over and over. This feeling is something everybody doing the course should expect. The countermeasure for giving up when things get rough is grit, something every prospective programmer MUST develop. Read about grit here: &lt;a href="https://en.wikipedia.org/wiki/Grit_(personality_trait)"&gt;https://en.wikipedia.org/wiki/Grit_(personality_trait)&lt;/a&gt;.&lt;br&gt;
Things will get hard, so spend some time with the articles at the beginning of TOP and get comfortable with encountering difficult things.&lt;/p&gt;

&lt;h1&gt;
  
  
  Take the time you need
&lt;/h1&gt;

&lt;p&gt;Avoid rushing through the curriculum; rushing is a surefire way to burn out. Using the Pomodoro technique can help you stay on task while ensuring that you take the breaks you need. Hard deadlines should be avoided as well, as they will make skipping and rushing very attractive. Moving through the curriculum without having the prerequisite understanding of the previous topics will make each subsequent lesson more and more difficult to learn properly as you dive deeper into the course.&lt;/p&gt;

</description>
      <category>theodinproject</category>
    </item>
    <item>
      <title>A TOP Strategy Guide</title>
      <dc:creator>Briggs Elsperger</dc:creator>
      <pubDate>Sat, 05 Feb 2022 03:09:38 +0000</pubDate>
      <link>https://dev.to/theodinproject/becoming-a-top-success-story-mindset-3dp2</link>
      <guid>https://dev.to/theodinproject/becoming-a-top-success-story-mindset-3dp2</guid>
      <description>&lt;h1&gt;
  
  
  Becoming a TOP Success Story
&lt;/h1&gt;

&lt;p&gt;The Odin Project is known to be very intense, but also has a track record of helping learners find consistent success. Its intensity is the “secret sauce” that helps people become strong developers. TOP is not easy, and for good reason. It strives to help learners get exposure to all the necessary skills to become a successful developer. The specific technical skills (i.e. hard skills) that you need to pick up are obvious, but it can be easy to overlook or neglect the equally important "soft skills" like mindset and communication. This guide will explain some strategies that have been employed to great effect by successful TOP learners. These strategies will help you develop both hard and soft skills.&lt;/p&gt;

&lt;p&gt;With this first section, we'll be covering mindset.&lt;/p&gt;

</description>
      <category>theodinproject</category>
      <category>programming</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
