<?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: Christine Harper</title>
    <description>The latest articles on DEV Community by Christine Harper (@harp0131).</description>
    <link>https://dev.to/harp0131</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%2F727307%2Fdad4ac17-2f22-48b2-ad1a-ad008f197bf5.jpeg</url>
      <title>DEV Community: Christine Harper</title>
      <link>https://dev.to/harp0131</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harp0131"/>
    <language>en</language>
    <item>
      <title>Javascript Basics - Introduction to forEach()</title>
      <dc:creator>Christine Harper</dc:creator>
      <pubDate>Sat, 16 Oct 2021 22:43:06 +0000</pubDate>
      <link>https://dev.to/harp0131/javascript-basics-introduction-to-foreach-2lf6</link>
      <guid>https://dev.to/harp0131/javascript-basics-introduction-to-foreach-2lf6</guid>
      <description>&lt;p&gt;First, lets take a quick look at a basic 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;let&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;element0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;element1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;element2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each element in an array also has a corresponding index position. The first element in an array is at index 0, the second element is at index 1, and so on.&lt;/p&gt;

&lt;h2&gt;
  
  
  forEach() Syntax
&lt;/h2&gt;

&lt;p&gt;We can use the &lt;code&gt;forEach()&lt;/code&gt; method to execute a callback function &lt;em&gt;for each&lt;/em&gt; non-empty element in an array. Let's look at the basic syntax of the forEach() method 🧐&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;array&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;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&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 for each element&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The callback function takes up to three parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;element&lt;/strong&gt; - refers to the current element at each iteration. This is a required parameter, but you can name it whatever you like!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;index&lt;/strong&gt; - this parameter is optional, but it refers to the index position of each element. Remember that the first index position in an array is 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;array&lt;/strong&gt; - this parameter is optional and refers to the array that the elements that you are iterating over are from&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  forEach() In Action
&lt;/h2&gt;

&lt;p&gt;Now let's see the &lt;code&gt;forEach()&lt;/code&gt; method in action!&lt;/p&gt;

&lt;p&gt;Let's pretend we have a class with a group of students. We will put their first names into an array 🍎&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;classList&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;Linda&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;Ainslie&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;Tracey&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;Rick&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's use the &lt;code&gt;forEach()&lt;/code&gt; method to iterate over this array and say 'Hello' to each student 🙋‍♀️&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;classList&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;student&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="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;output:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// Hello, Linda!&lt;/code&gt;&lt;br&gt;
&lt;code&gt;// Hello, Ainslie!&lt;/code&gt;&lt;br&gt;
&lt;code&gt;// Hello, Tracey!&lt;/code&gt;&lt;br&gt;
&lt;code&gt;// Hello, Rick!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this example we are using the &lt;code&gt;forEach()&lt;/code&gt; method on the classList array. The parameter 'student' refers to the student names in the array. During the first iteration, 'student' refers to the element 'Linda'. In the second iteration it refers to 'Ainslie', and so on. The callback function in this example will print 'Hello' and the student name in the console. The function is called for every student in the classList array. It runs sequentially in ascending order from the first element in the array to the last.&lt;/p&gt;

&lt;p&gt;Now let's add another parameter and see how we can access the index position for each element in the array 📍&lt;/p&gt;

&lt;p&gt;When you add the index parameter, it must be the second parameter passed into the callback 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="nx"&gt;classList&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;student&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;! Your index number is: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;output:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// Hello, Linda! Your index number is: 0.&lt;/code&gt;&lt;br&gt;
&lt;code&gt;// Hello, Ainslie! Your index number is: 1.&lt;/code&gt;&lt;br&gt;
&lt;code&gt;// Hello, Tracey! Your index number is: 2.&lt;/code&gt;&lt;br&gt;
&lt;code&gt;// Hello, Rick! Your index number is: 3.&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  forEach and Arrow Functions
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;forEach()&lt;/code&gt; method uses a very clean and concise syntax compared to a regular for loop. You will often see people use arrow functions passed in as the callback. This is how our example could be written using an arrow 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="nx"&gt;classList&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;student&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="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="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;! Your index number is: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are only accessing the elements, you can tidy up the code even further 🧹&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;classList&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;student&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="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Special Characteristics of forEach()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;forEach()&lt;/code&gt; method syntax is a little easier to read and write than some of the other looping methods. There are some key differences to be aware of: 🕵️‍♀️&lt;/p&gt;

&lt;p&gt;The following are some characteristics that are specific to &lt;code&gt;forEach()&lt;/code&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;loops over every non-empty element in an array&lt;/li&gt;
&lt;li&gt;loops in ascending order from index 0 to the end of the array&lt;/li&gt;
&lt;li&gt;you cannot break or end the loop early&lt;/li&gt;
&lt;li&gt;it will not change the original array (&lt;em&gt;unless&lt;/em&gt; your callback function executes an operation to do so)&lt;/li&gt;
&lt;li&gt;it will always return undefined&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Read more on MDN to become a &lt;code&gt;forEach()&lt;/code&gt; wizard 🧙‍♀️&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach'"&gt;MDN - Array.prototype.forEach()&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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