<?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: Devin Golden</title>
    <description>The latest articles on DEV Community by Devin Golden (@basedenergy).</description>
    <link>https://dev.to/basedenergy</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%2F111276%2F84b87f3e-2e9a-4c12-8b8a-3264be690a91.jpeg</url>
      <title>DEV Community: Devin Golden</title>
      <link>https://dev.to/basedenergy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/basedenergy"/>
    <language>en</language>
    <item>
      <title>Tasty Testing with Mocha and Chai ☕️</title>
      <dc:creator>Devin Golden</dc:creator>
      <pubDate>Tue, 27 Nov 2018 23:54:36 +0000</pubDate>
      <link>https://dev.to/basedenergy/tasty-testing-with-mocha-and-chai--51ek</link>
      <guid>https://dev.to/basedenergy/tasty-testing-with-mocha-and-chai--51ek</guid>
      <description>&lt;p&gt;Today I'm going to briefly introduce a simple workflow for automating testing that uses &lt;a href="https://mochajs.org/" rel="noopener noreferrer"&gt;Mocha&lt;/a&gt; and &lt;a href="https://www.chaijs.com/" rel="noopener noreferrer"&gt;Chai&lt;/a&gt;. Mocha is a test framework that sets up test suites, and Chai is an expectation framework that adds helpers to make it easier to build tests against code. &lt;/p&gt;

&lt;h2&gt;
  
  
  But why do I need to do this??
&lt;/h2&gt;

&lt;p&gt;Writing solid tests saves a developer the hassle of clicking around in an app to manually test every single feature of the application. It's also important when utilizing Test Driven Development, or TDD. While I'm not going to fully dive into TDD here, it basically is a style of development in which:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Tests are written first. Only write enough of a test to fail.&lt;/li&gt;
&lt;li&gt;Production code is then written with the express purpose of making a failing unit test pass. &lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  So let's get started by installing Mocha and Chai
&lt;/h4&gt;

&lt;p&gt;▶︎ &lt;a href="https://www.chaijs.com/guide/installation/" rel="noopener noreferrer"&gt;Chai installation instructions&lt;/a&gt;&lt;br&gt;
▶︎ &lt;a href="https://mochajs.org/#installation" rel="noopener noreferrer"&gt;Mocha installation instructions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we have these installed, we have two ways in which to run our tests: through our console with Node, or in the browser. Using the browser method requires creating a special html file, which we will get into in a bit. But first:&lt;/p&gt;
&lt;h2&gt;
  
  
  Testing in Node
&lt;/h2&gt;

&lt;p&gt;Say we were building an addition function that needed to be able to add either an array of numbers or two numbers passed in as parameters. Let's use an &lt;code&gt;npm init&lt;/code&gt; to create a package.json file. &lt;/p&gt;

&lt;p&gt;Next we need to add chai and mocha to our dev-dependencies, and for convenience sake, add a script for running mocha. My package.json for this exercise looks 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="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&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;mochachai-intro&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;version&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;1.0.0&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;description&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="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;main&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;index.js&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;scripts&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test&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;mocha&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;license&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;ISC&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;devDependencies&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chai&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;^4.2.0&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;mocha&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;^5.2.0&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;dependencies&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can do an &lt;code&gt;npm install&lt;/code&gt; to get all of our fun dependencies installed and get to work!&lt;/p&gt;

&lt;p&gt;For the sake of this example, we're going to create two Javascript files, one called &lt;code&gt;add.js&lt;/code&gt; and one called &lt;code&gt;test.js&lt;/code&gt;. The test.js file is going to contain our tests that will be run against our add.js function. &lt;/p&gt;

&lt;p&gt;Since we're utilizing TDD, let's come up with the tests first. Our test.js should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;expect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;expect&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;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./add.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;add&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should return the sum of two positive numbers&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&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;4&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should return the sum of two negative numbers&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should return the sum of an array of numbers&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we just need to write a function that will do ONLY what is required by these test guidelines. Mine looks 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;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&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;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we run &lt;code&gt;npm test&lt;/code&gt;, we should get back a message that all of our tests are passing:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxsb0vum1aiwq7d633to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxsb0vum1aiwq7d633to.png" alt="passing tests" width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;
VERY COOL!



&lt;h2&gt;
  
  
  Testing in the Browser
&lt;/h2&gt;

&lt;p&gt;If you decide that you want a fancy UI to show off your passing tests (and honestly who wouldn't), you simply have to add in an html page with some scripting for mocha and chai. I use a boilerplate test.html file that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Tests&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;media=&lt;/span&gt;&lt;span class="s"&gt;"all"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/ajax/libs/mocha/5.2.0/mocha.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"mocha"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;p&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"."&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Index&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"messages"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"fixtures"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/ajax/libs/mocha/5.2.0/mocha.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"add.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;mocha&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bdd&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;mocha&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reporter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;html&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;expect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;chai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"test.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;mocha&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All we have to do is add this html page to our app and open it up in a browser and we should get a nice looking results page that even shows how long it's taking for our tests to run! The page for our add function should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6og6ke7j96y0ybnq41eo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6og6ke7j96y0ybnq41eo.png" alt="mochachai ui" width="788" height="460"&gt;&lt;/a&gt;&lt;/p&gt;
Beautifully illustrated and functional!



&lt;h2&gt;
  
  
  Now what!?
&lt;/h2&gt;

&lt;p&gt;Now we have a very basic idea of how to use Mocha and Chai to write tests, but what do we do with this information!? In Part 2, I will go into some more uses for testing that are more applicable to real-life scenarios. &lt;/p&gt;

&lt;h4&gt;
  
  
  Be sure to react and follow 🤠✌️
&lt;/h4&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>On That 'New' New!</title>
      <dc:creator>Devin Golden</dc:creator>
      <pubDate>Mon, 19 Nov 2018 09:46:54 +0000</pubDate>
      <link>https://dev.to/basedenergy/on-that-new-new-5bik</link>
      <guid>https://dev.to/basedenergy/on-that-new-new-5bik</guid>
      <description>&lt;h2&gt;
  
  
  Simplifying the 'new' Keyword!
&lt;/h2&gt;

&lt;p&gt;So, this week I'm gonna be breaking down another essential Javascript operator to become familiar with: the &lt;code&gt;new&lt;/code&gt; operator. I think that the best way to start off an explanation of &lt;code&gt;new&lt;/code&gt; is by first describing what happens when it is used. &lt;/p&gt;

&lt;h4&gt;
  
  
  Four things happen when &lt;code&gt;new&lt;/code&gt; is used:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;A new, empty object is created.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; is bound to the new object.&lt;/li&gt;
&lt;li&gt;The newly created object's internal &lt;code&gt;[[prototype]]&lt;/code&gt;, or &lt;code&gt;__proto__&lt;/code&gt;, is set to be the same as the &lt;code&gt;[[prototype]]&lt;/code&gt; of the constructor function's.&lt;/li&gt;
&lt;li&gt;The new object is returned with a &lt;code&gt;return&lt;/code&gt; &lt;code&gt;this&lt;/code&gt; at the end of the function.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  I know that might not make much sense!
&lt;/h2&gt;

&lt;p&gt;Let's create a really simple constructor function so we can get a more practical idea of how &lt;code&gt;new&lt;/code&gt; works. This one I'm going to call &lt;code&gt;Homie&lt;/code&gt; and it's going to take in a couple of parameters and set them to the value of &lt;code&gt;this&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;const&lt;/span&gt; &lt;span class="nx"&gt;Homie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;thingInCommon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isChill&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;likesMyJokes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;thingInCommon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;thingInCommon&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isChill&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isChill&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;likesMyJokes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;likesMyJokes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can use &lt;code&gt;new&lt;/code&gt; to invoke the constructor:&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;homie1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Homie &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sweaty Steve&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;Weightlifting&lt;/span&gt;&lt;span class="dl"&gt;'&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;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;So in this example, we have:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Created an object, called 'homie1', that seems like a decent gym buddy. You probably wouldn't want to hang out with Sweaty Steve in your spare time, but he seems like he'd give you tips about your form and spot you if you needed him to. &lt;/li&gt;
&lt;li&gt;Bound &lt;code&gt;this&lt;/code&gt; to homie1.&lt;/li&gt;
&lt;li&gt;Added a &lt;code&gt;__proto__&lt;/code&gt; to homie1 that will point to our &lt;code&gt;Homie.prototype&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Returned our homie1 object. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We can see this in action with a quick console log:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;homie1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="cm"&gt;/* Homie {name: "Sweaty Steve", thingInCommon: "Weightlifting", isChill: false, likesMyJokes: true}
isChill: false
likesMyJokes: true
name: "Sweaty Steve"
thingInCommon: "Weightlifting"
__proto__: Object */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The commented code is what I get back when running all of the prior code in a Google Chrome console window. So we can clearly see that an object has been created with all of the values that we input when we called the &lt;code&gt;new&lt;/code&gt; keyword. We can also see the &lt;code&gt;__proto__&lt;/code&gt; object, which I will dive further into in a future post. &lt;/p&gt;

&lt;p&gt;We could continue using the &lt;code&gt;new&lt;/code&gt; keyword and passing in new parameters to quickly create a whole gang of homies to kick it with!&lt;/p&gt;

&lt;p&gt;I hope this gives a small example of how &lt;code&gt;new&lt;/code&gt; can be used to quickly create new objects from a constructor function and some of the properties that carry over to these objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  I really love it when people react and follow!
&lt;/h3&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is "This"?</title>
      <dc:creator>Devin Golden</dc:creator>
      <pubDate>Fri, 09 Nov 2018 07:25:56 +0000</pubDate>
      <link>https://dev.to/basedenergy/what-is-this-4onl</link>
      <guid>https://dev.to/basedenergy/what-is-this-4onl</guid>
      <description>&lt;h2&gt;
  
  
  Simplifying the &lt;strong&gt;"this"&lt;/strong&gt; Javascript keyword
&lt;/h2&gt;

&lt;p&gt;The "this" keyword always refers to an &lt;strong&gt;object&lt;/strong&gt;, but the value of "this" changes depending on where it is called. When a function is called, an execution context is created that contains a reference to "this". Because the value of "this" changes depending on execution context, we can determine the value of "this" by examining the function's &lt;strong&gt;call-site&lt;/strong&gt;, or where it is called.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conveniently, there are four simple rules to help determine the value of "this"!
&lt;/h4&gt;

&lt;h3&gt;
  
  
  Rule #1: Default Binding
&lt;/h3&gt;

&lt;p&gt;When called alone, or within a function, "this" will refer the Global object. In a browser, this will refer to the Window object. One exception to this is if the user is in strict mode, in which case "this" will be undefined. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bm6pgcg7l2tiars2yiq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bm6pgcg7l2tiars2yiq.png" alt="default example" width="588" height="462"&gt;&lt;/a&gt;&lt;br&gt;In this example, you would get a console log of the global object
 &lt;/p&gt;

&lt;h3&gt;
  
  
  Rule #2: Implicit Binding
&lt;/h3&gt;

&lt;p&gt;If a function is contained within an object, "this" will refer to the nearest parent object. Even if the function is only referenced in the object, "this" will still refer to the object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxagtuloadd6yp0vtafo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxagtuloadd6yp0vtafo.png" alt="implicit example" width="672" height="678"&gt;&lt;/a&gt;&lt;/p&gt;
In this example, we console log out "😸 Meow!"



&lt;h3&gt;
  
  
  Rule #3: Explicit Binding
&lt;/h3&gt;

&lt;p&gt;There are three functions &lt;em&gt;call&lt;/em&gt;, &lt;em&gt;bind&lt;/em&gt;, and &lt;em&gt;apply&lt;/em&gt; that can be used to set the value of "this". Call and apply work largely the same way, the first argument passed to each will be what "this" refers to. Bind actually creates a new function which a permanently assigned "this" value. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzcqcoravgh6i2reeh3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzcqcoravgh6i2reeh3b.png" alt="explicit example" width="638" height="894"&gt;&lt;/a&gt;&lt;/p&gt;
In this example, all three functions set "this" explicitly.



&lt;h3&gt;
  
  
  Rule #4: New Binding
&lt;/h3&gt;

&lt;p&gt;When using the &lt;strong&gt;new&lt;/strong&gt; keyword, "this" will refer to the new object that is being created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzmlf3nijuj7wy584l1bs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzmlf3nijuj7wy584l1bs.png" alt="new example" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
In this example, "this" points to the new object and prints out my kitty's name



&lt;h3&gt;
  
  
  The Arrow Function Exception!
&lt;/h3&gt;

&lt;p&gt;One exception to these general rules of thumb is that when using an arrow function, "this" will retain the value of its parent scope. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimyhpid0xzwcgqs94omy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimyhpid0xzwcgqs94omy.png" alt="arrow example" width="800" height="782"&gt;&lt;/a&gt;&lt;/p&gt;
Here, "this" retains the value of its parent function.



&lt;h3&gt;
  
  
  The Dev Recap:
&lt;/h3&gt;

&lt;p&gt;So a quick summary of the "this" keyword:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;"This" is usually determined by a function's execution context.&lt;/li&gt;
&lt;li&gt;By default, "this" refers to the global object (window object in a browser).&lt;/li&gt;
&lt;li&gt;When the "new" keyword is used, "this" will refer to the new object.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Call&lt;/em&gt;, &lt;em&gt;bind&lt;/em&gt;, and &lt;em&gt;apply&lt;/em&gt; can be used to set "this" value.&lt;/li&gt;
&lt;li&gt;Arrow functions do not re-bind "this".&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hopefully this is helpful to someone new to Javascript who is trying to wrap their head around the "this" keyword! Thanks for reading and don't forget to &lt;strong&gt;follow&lt;/strong&gt;!!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>this</category>
      <category>webdev</category>
    </item>
    <item>
      <title>For Loops vs. forEach: A Far Reach!</title>
      <dc:creator>Devin Golden</dc:creator>
      <pubDate>Thu, 01 Nov 2018 20:02:10 +0000</pubDate>
      <link>https://dev.to/basedenergy/for-loops-vs-foreach-a-far-reach-59j1</link>
      <guid>https://dev.to/basedenergy/for-loops-vs-foreach-a-far-reach-59j1</guid>
      <description>&lt;p&gt;One of the major concepts that was reiterated over and over when I was first learning about Javascript arrays is that when we need a function to hit every object in an array, we need a loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  Brother, may I loop thru an array?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6nhj6z41fj7f2nx1yvxg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6nhj6z41fj7f2nx1yvxg.jpg" alt="brother loops" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So in a world where we are designing an app that needs to go through a list of kitties (hopefully my future career), a typical &lt;strong&gt;for loop&lt;/strong&gt; would look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftpsqkpux6ymnwiv1sioh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftpsqkpux6ymnwiv1sioh.png" alt="kitty loop" width="644" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This works great, but we also have the option of using a nifty method that our array will inherit from Array.prototype called &lt;strong&gt;forEach()&lt;/strong&gt;. We can invoke forEach() to effectively do the same thing that our for loop accomplished, and it will look a little something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4wj9mxm5snr78k2xpm4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4wj9mxm5snr78k2xpm4.png" alt="kitty forEach" width="574" height="116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know what you're thinking, "Why would a developer prefer to use one over the other? Are there advantages and disadvantages of each??". &lt;br&gt;
Well boy do I have a treat for you, because there absolutely ARE.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Case for Loops
&lt;/h3&gt;

&lt;p&gt;There are some real Joseph Gordon Levitt style Loopers out there that will tell you that for loops are much more efficient than using the forEach method, and they make some good points!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some Javascript engines and browsers are more optimized for for loops.&lt;/li&gt;
&lt;li&gt;Break can be used to terminate the loop early.&lt;/li&gt;
&lt;li&gt;Gives more control over the conditions of the loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The forEach() Rebuttal
&lt;/h3&gt;

&lt;p&gt;On the other hand, proponents of forEach() will be quick to tell you that the "forEach() method is #1!!" (this happens to me on the street all the time). I understand the passion that forEachers™️ have for their favorite Array method.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;forEach is much more readable. When you factor in the temporary variables that are needed in for loops, it makes for a lot of code to look at! forEach looks cleaner and takes less time to read.&lt;/li&gt;
&lt;li&gt;Less chance for small errors in code since you don't have to specify a condition statement.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Dev Take!
&lt;/h3&gt;

&lt;p&gt;While I really don't want to catapult myself into this debate, and I typically avoid talking politics, I have to say that I do have a preference here...&lt;/p&gt;

&lt;p&gt;I think that in most circumstances where an array needs to be looped through, the &lt;strong&gt;forEach() method&lt;/strong&gt; is better practice! It looks a lot nicer and makes code more readable and maintainable. One of the only times I would use a for loop is if I knew I needed to break out of the loop early. Otherwise, I go forEach() every time. &lt;/p&gt;

&lt;h4&gt;
  
  
  Bring on the hate mail from "for loopers".
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flmfxcbmzzhy4howswvxc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flmfxcbmzzhy4howswvxc.png" alt="loop earth" width="161" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>arraymethods</category>
    </item>
  </channel>
</rss>
