<?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: Zakk</title>
    <description>The latest articles on DEV Community by Zakk (@zfleischmann).</description>
    <link>https://dev.to/zfleischmann</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%2F55058%2F208c3ee3-5c82-46be-824b-64ba65b86d40.jpg</url>
      <title>DEV Community: Zakk</title>
      <link>https://dev.to/zfleischmann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zfleischmann"/>
    <language>en</language>
    <item>
      <title>forEach: Learn JavaScript's Array Methods by Building Them</title>
      <dc:creator>Zakk</dc:creator>
      <pubDate>Tue, 17 Nov 2020 18:38:39 +0000</pubDate>
      <link>https://dev.to/zfleischmann/foreach-learn-javascript-s-array-methods-by-building-them-1ck6</link>
      <guid>https://dev.to/zfleischmann/foreach-learn-javascript-s-array-methods-by-building-them-1ck6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;JavaScript's array methods are really powerful tools for writing code that is expressive and easy to read. But for beginners, they can be difficult to understand. Once you're comfortable with loops, &lt;code&gt;forEach&lt;/code&gt; is a great first array method to learn because it is just a small abstraction over a single &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;This article is part of a series I'll be doing where I explain JavaScript's built-in array methods. I'll describe how and when to use them as well as how to implement them yourself. The goal of implementing the array methods yourself is to help you understand what's going on under the hood. So when you want to use &lt;code&gt;map&lt;/code&gt; or see a code block that is using &lt;code&gt;reduce&lt;/code&gt;, you'll be able to understand what's going on.&lt;/p&gt;

&lt;p&gt;Developing a deeper understanding of JavaScript's array methods is a great way to improve the readability of your code. Every array method is an abstraction over a &lt;code&gt;for&lt;/code&gt; loop, meaning anything you can do with an array method can be done with a &lt;code&gt;for&lt;/code&gt; loop, and vice versa. The difference is that array methods make your code a lot more readable by making it more expressive.&lt;/p&gt;

&lt;p&gt;I think of learning new programming language constructs as similar to learning new categories of vocabulary of a foreign language. If you learned Spanish or French in school, then you probably had lessons dedicated to asking for directions, ordering food at a restaurant, or interviewing for a job. In these lessons, you learned words and phrases that were more likely to come up in those specific settings so that you could better express yourself.&lt;/p&gt;

&lt;p&gt;Sure, you can sit down at a restaurant and ask for "the plate with green stuff on it," ("el plato con cosas verdes") but the waiter will have an easier time understanding what you mean if you just say "a salad, please" ("una ensalada, por favor").&lt;/p&gt;

&lt;p&gt;Learning this vocabulary helps you express yourself more clearly, just as learning programming language constructs helps you write more expressive code for particular kinds of problems. JavaScript's array methods will help you write more expressive code when working with arrays.&lt;/p&gt;

&lt;p&gt;I'm going to go through every built-in array method and explain what it does and how to use it, as well as show you how it works by explaining how you could implement it yourself. If that sounds like something you're interesting in, follow along by signing up for my newsletter and following me on Twitter.&lt;/p&gt;

&lt;p&gt;Let's get into it&lt;/p&gt;

&lt;h2&gt;
  
  
  How it Works
&lt;/h2&gt;

&lt;p&gt;We're starting with &lt;code&gt;forEach&lt;/code&gt; because it is the lightest abstraction over a standard &lt;code&gt;for&lt;/code&gt; loop. A common task when working with arrays is to loop through and perform some action on each item in the array. For example, you might do something like this:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above snippet of code, we create an array of numbers (called &lt;code&gt;numbers&lt;/code&gt;), then create a &lt;code&gt;for&lt;/code&gt; loop to go through each item in the array. Within the &lt;code&gt;for&lt;/code&gt; loop, we simply grab the current item and &lt;code&gt;console.log&lt;/code&gt; it.&lt;/p&gt;

&lt;p&gt;If we wanted to make the above snippet of code slightly more abstract, we could modify it to 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;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&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;numbers&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;let&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&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;printNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we've created a function called &lt;code&gt;printNumber&lt;/code&gt; that takes a number and prints it to the console. The important point is that this function does whatever action we're performing on each item of the array. In this specific example, that just means print it. We'll come back to this function in a bit though.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Our Own
&lt;/h2&gt;

&lt;p&gt;As the name suggests, the &lt;code&gt;forEach&lt;/code&gt; method loops through an array and runs a function &lt;em&gt;for each&lt;/em&gt; item in the array (just like our previous snippet of code). We're actually really close to the implementation of &lt;code&gt;forEach&lt;/code&gt; already.&lt;/p&gt;

&lt;p&gt;To implement our own &lt;code&gt;forEach&lt;/code&gt; method, we need to define a function that takes an array and a function, loops through the array, and passes each item into the function.&lt;/p&gt;

&lt;p&gt;Let's start by defining our 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;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// more to come here ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've declared a function called &lt;code&gt;forEach&lt;/code&gt; that takes two arguments: an array called &lt;code&gt;arr&lt;/code&gt; and a function called &lt;code&gt;fn&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we want to loop through every item in the array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// more to come here ...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've added a &lt;code&gt;for&lt;/code&gt; loop that goes through the passed in array. Note this is the exact same &lt;code&gt;for&lt;/code&gt; loop that we had above.&lt;/p&gt;

&lt;p&gt;Now all that's left to do is to get the current item and pass it into the function, &lt;code&gt;fn&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&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;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it! Now you can see it all together:&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;printNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;forEach&lt;/code&gt; in Action
&lt;/h2&gt;

&lt;p&gt;Now that we have our &lt;code&gt;forEach&lt;/code&gt; method defined, it's just a matter of modifying the function that we pass in to the items in &lt;code&gt;numbers&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We could, for instance, double each number in &lt;code&gt;numbers&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doubleNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&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;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;doubleNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, we could use &lt;code&gt;forEach&lt;/code&gt; to get uppercased versions of an array of strings:&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;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mercedes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Antonio&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sergio&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;José&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Carmen&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dolores&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;uppercaseName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;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;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;())&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;names&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;uppercaseName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Array Methods
&lt;/h2&gt;

&lt;p&gt;The goal of JavaScript's array methods is to provide built-in methods for common tasks and to help you write more readable code. Any &lt;code&gt;for&lt;/code&gt; loop requires really looking at and reading the code in order to understand what it's doing. We can change the meaning of a &lt;code&gt;for&lt;/code&gt; loop a lot by just changing a few characters:&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="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;numbers&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;let&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&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;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;number&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="nx"&gt;numbers&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;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;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;let&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&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;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;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have two &lt;code&gt;for&lt;/code&gt; loops that are nearly identical, but one loops through the array backwards (from last index to first). You can see the difference when you read through the code, but it's not immediately obvious at first glance. Also, the construct of the loop isn't the important part of this block of code, the body is, so we don't want to have to spend time reading and understanding it.&lt;/p&gt;

&lt;p&gt;Think back to the Spanish restaurant and our plate with green stuff on it. The waiter is going to have to check if you mean the salad or the side order of asparagus, just like we have to check what this loop is doing. It's easier for everyone if we just learn how to ask for a salad.&lt;/p&gt;

&lt;p&gt;When we say we want our code to be easy to read, we mean we want it to be easy to understand what it is doing at first glance and without taking the time to work through the syntax piece by piece. Using array methods, like &lt;code&gt;forEach&lt;/code&gt;, makes this a lot easier:&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt;&lt;span class="p"&gt;)&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;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;printNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;That concludes my discussion of &lt;code&gt;forEach&lt;/code&gt;. I hope that you found it helpful, but I also hope that you found the implementation of &lt;code&gt;forEach&lt;/code&gt; fairly simple. I find a lot of JavaScript developers haven't thought about how these methods are implemented. When you do, you see that they're all pretty simple. Going through and thinking about how they're implemented made using them a lot easier, which is what I hope to share with you.&lt;/p&gt;

&lt;p&gt;I'm going to go through and explain and implement every built-in array method, which will help you understand how they work and when and how to use them. To follow along, sign up for my newsletter and follow me on Twitter.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>arraymethods</category>
      <category>arrays</category>
      <category>learningtocode</category>
    </item>
    <item>
      <title>Landing My First Freelance Development Projects</title>
      <dc:creator>Zakk</dc:creator>
      <pubDate>Fri, 18 Sep 2020 17:03:30 +0000</pubDate>
      <link>https://dev.to/zfleischmann/landing-my-first-freelance-development-projects-4bk5</link>
      <guid>https://dev.to/zfleischmann/landing-my-first-freelance-development-projects-4bk5</guid>
      <description>&lt;p&gt;I recently took the plunge and left my job to freelance full-time as a web developer. It’s been an amazing transition and I honestly regret not doing it sooner.&lt;/p&gt;

&lt;p&gt;There are so many reasons why more developers should consider freelancing full-time. For instance, you have the freedom to control your own schedule. I take advantage of this by carving out a few hours in the middle of the day to read or take my dog for a long walk. If demand is high enough, you can pick and choose the projects you take on. I feel fortunate, at the moment, to only be taking on clients who I really want to support. You can also often earn more freelancing than you can working for a single company. I’m on track to earn more than twice what I made at my previous full-time job.&lt;/p&gt;

&lt;p&gt;All that said, getting to the point where you can make the jump takes time. I freelanced on the side for five years before I built up enough work to start doing it full-time. It doesn’t have to take you that long, but it will take a while to build up a steady stream of incoming projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting Your Search
&lt;/h2&gt;

&lt;p&gt;Before you start, you need to realize that the economy is enormous, unfathomably so, and the market for people who can code is huge. If you’re flexible with the kinds of projects you take on and the technologies you use, there’s more demand out there than you can meet.&lt;/p&gt;

&lt;p&gt;My freelance projects have ranged from things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML/CSS emails&lt;/li&gt;
&lt;li&gt;Simple scripts that pull data from a website and store it in a database&lt;/li&gt;
&lt;li&gt;WordPress sites with multiple page templates&lt;/li&gt;
&lt;li&gt;Production applications with user authentication and complex relational data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The challenge is finding the demand for your skill-set.&lt;/p&gt;

&lt;p&gt;When thinking about where to start finding freelance projects, many developers draw a blank. Others jump to networking, whether that’s literally hitting the streets, or messaging people on Twitter, Craigslist, and LinkedIn. Still other developers immediately sign up for sites like Fiverr and Upwork.&lt;/p&gt;

&lt;p&gt;I tried both of these approaches. With mixed results.&lt;/p&gt;

&lt;p&gt;I messaged people on LinkedIn and responded to ads on Craigslist; I also handed out business cards at meetups and other events. Networking can be a good approach. The challenge with networking is that you have to connect with a lot of people in order to get any work.&lt;/p&gt;

&lt;p&gt;So what about Fiverr and Upwork?&lt;/p&gt;

&lt;p&gt;I tried these and was able to get a project or two through these sites. A friend connected me with someone who worked full-time through Fiverr and, over coffee, this person validated the challenge I was experiencing with sites like this: the projects tend to be small and low-paying, so you have to complete a lot of them in order to make a sufficient living. This developer lived in Costa Rica, so he could earn less and comfortable. I lived in Washington, D.C where rent is… higher…&lt;/p&gt;

&lt;h2&gt;
  
  
  A Better Way.
&lt;/h2&gt;

&lt;p&gt;When Willie Sutton, an infamous bank robber, was asked why he robbed banks, his response was, “Because that’s where the money is.” If you’re looking for work as a freelance developer, you have to take a step back and think, where are the projects?&lt;/p&gt;

&lt;p&gt;When someone needs a new website, they research their options and then hire a designer to figure out what the site should look like. Then the designer and the client go out and find a developer who can build and launch the site.&lt;/p&gt;

&lt;p&gt;The best thing you can do to start getting freelance work isn’t to network extensively or sign up for Fiverr, it’s to try and find designers who need a developer.&lt;/p&gt;

&lt;p&gt;Partnering with designers has one huge advantage over networking and Fiverr: it’s where the need for developers is. If you go out and try to find work for yourself, the audience of people you have to network with is huge. And, once you finish building a website for a client, they probably won’t need another one for a couple of years, so every time you finish a project you have to go and find a new client. If you instead focus on finding designers, you only need to find and partner with two or three who can send you a relatively steady stream of work.&lt;/p&gt;

&lt;p&gt;So now the challenge is to find a couple of designers who you can partner with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding and Connecting with Designers
&lt;/h2&gt;

&lt;p&gt;So how do you find and connect with designers? Well, the same way you’d find and connect with anyone: using and expanding your network.&lt;/p&gt;

&lt;p&gt;The first designer I worked with was the edgy older brother of a close friend (a lot of designers try to be edgy). The second had completed the same coding bootcamp, but a previous cohort. The third was a friend-of-a-friend’s wife and the fourth was the husband of one of my wife’s coworkers.&lt;/p&gt;

&lt;p&gt;You should definitely start by tapping your network, as it’s likely to quickly lead to some freelance work. But if you don’t know of many designers, there are still other options. In a pre-COVID world, I would suggest attending meetups, where you would find large rooms filled with designers, many of them full-time freelancers. Post-COVID, you can still try meetups, but connecting with individuals won’t be as easy because of the way events are structured now.&lt;/p&gt;

&lt;p&gt;What you can do instead is reach out to people for “coffee” or a quick chat. I have found that people are far more willing to hop on a 30 minute phone call now (audio, not video) than they were before.&lt;/p&gt;

&lt;p&gt;You will likely still have to connect with a few designers before you find someone willing to pass their work on to you. Once you have partnered with a designer though, they’re yours to lose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping the Designer as a Client
&lt;/h2&gt;

&lt;p&gt;So you’ve partnered with a few designers. What now? How do you keep them happy so they’ll keep sending you work?&lt;/p&gt;

&lt;p&gt;You want to keep two things in mind. First, the designer will always feel exposed, or like there’s some risk in delegating work to you. Second, you are taking on the entire technical side of the project, not just implementing the designs.&lt;/p&gt;

&lt;p&gt;You have to understand the designer’s side of freelance work to see why they’ll always feel nervous about delegating work to you. The client paying both of you is actually the designer’s client, so their reputation is more at risk than yours. The designer is delegating the actual construction of their designs to you for their client.&lt;/p&gt;

&lt;p&gt;This is really important for you to remember. You have a skill set the designer probably doesn’t have and you are doing something for them and their client. Add in the fact that this designer has almost certainly been burnt by developers in the past and you can start to see why they’re nervous.&lt;/p&gt;

&lt;p&gt;Secondly, you’re responsible for the whole technical side of the project. I’ve had friends and former coworkers try to start taking on freelance projects thinking they would only be responsible for implementing the designs. That’s 80% of what you’ll need to do, but that last 20% is what’s going to make or break your relationship with the designer.&lt;/p&gt;

&lt;p&gt;The designer hired you because they can’t build the website themselves. That means they also probably can’t set up hosting or administer the site. In the designer’s mind, that’s part of building the site, which you are expected to handle.&lt;/p&gt;

&lt;p&gt;You’re not likely going to get out of hosting and administering the site. So the best solution is to have easy, standard options that you can use. It’s not sexy by any means, but I’ve set up hosting for clients on Bluehost. I know, it’s not AWS but it’s easy and cheap and I actually had to do very, very little to get the site live. Meanwhile, the designer, who you remember is really my client in this project, didn’t have to do anything and was able to give their client an easy and affordable hosting option.&lt;/p&gt;

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

&lt;p&gt;I fell into this insight by accident. My friend and his older brother needed a website built for their startup and my friend knew I had just graduated from a coding bootcamp. The older brother said that he often had a hard time finishing projects for his clients because he couldn’t code and he didn’t know many reliable people who could.&lt;/p&gt;

&lt;p&gt;So if there’s a formula for success in getting started as a freelance developer it’s this: find people who have work (i.e. designers) and be reliable.&lt;/p&gt;

&lt;p&gt;With time, you can work up to doing this full-time, like I have, or you can just do it on the side, like I did for the last 5 years. The decision is up to you. I made the switch when I had enough current and incoming work that I felt comfortable giving it a shot. Whatever your end goal is, the best thing you can do right now is get started.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you enjoyed this post, you should follow me on &lt;a href="https://twitter.com/ZFleischmann"&gt;Twitter&lt;/a&gt; where I write post programming and freelancing.&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
