<?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>Forem: Robert Hieger</title>
    <description>The latest articles on Forem by Robert Hieger (@rhieger).</description>
    <link>https://forem.com/rhieger</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%2F996784%2Ff268ac4a-3927-4a4b-a4fa-09619fd28053.jpeg</url>
      <title>Forem: Robert Hieger</title>
      <link>https://forem.com/rhieger</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rhieger"/>
    <language>en</language>
    <item>
      <title>A Quick Look at the JavaScript apply() Method</title>
      <dc:creator>Robert Hieger</dc:creator>
      <pubDate>Sun, 12 Nov 2023 02:32:27 +0000</pubDate>
      <link>https://forem.com/rhieger/a-quick-look-at-the-javascript-apply-method-147j</link>
      <guid>https://forem.com/rhieger/a-quick-look-at-the-javascript-apply-method-147j</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8c62crqemlh0x972jja2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8c62crqemlh0x972jja2.png" alt="JPE icon"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://medium.com/javascript-in-plain-english/a-quick-look-at-the-javascript-call-method-b5b2383eb4e6" rel="noopener noreferrer"&gt;JavaScript in Plain English&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotp9f8m5x30ufy2jxzr7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotp9f8m5x30ufy2jxzr7.png" alt="Woman Computer Programmer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
                                               Photo by &lt;a href="https://unsplash.com/@kellysikkema?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Kelly Sikkema&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/javascript-programmers?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;
&lt;/h6&gt;




&lt;h2&gt;
  
  
                                   Table of Contents
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Introductory Remarks
&lt;/h3&gt;

&lt;h3&gt;
  
  
  A Brief Recap
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Let's Get Started
&lt;/h3&gt;

&lt;h3&gt;
  
  
  What Needs to Change?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Now Let's Refactor the Code
&lt;/h3&gt;

&lt;h4&gt;
  
  
       &lt;em&gt;Step 1: Make the Needed Changes to index.html&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
       &lt;em&gt;Step 2: Rename the CSS Style Sheet&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
       &lt;em&gt;Step 3: Refactor the dog Objects to Use the apply() Method&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
       &lt;em&gt;How Does Function.prototype.apply() Differ from Function.prototype.bind()?&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
       &lt;em&gt;Recommended Reading for Study of call(), bind() and apply()&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
       &lt;em&gt;Step 4: Refactor the Event Listeners for button1 through button3&lt;/em&gt;
&lt;/h4&gt;

&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Works Cited
&lt;/h3&gt;




&lt;p&gt;&lt;a id="intro"&gt;&lt;/a&gt;&lt;br&gt;
In the third of three tutorials, we will examine the use of the &lt;code&gt;apply()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Our main focus is refactoring the finished product from the previous tutorial so that it takes advantage of the &lt;code&gt;apply()&lt;/code&gt; method, rather than the &lt;code&gt;bind()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;If you have not read the second tutorial in this series, you would do best to read through &lt;em&gt;&lt;a href="https://dev.to/rhieger/a-quick-look-at-the-javascript-bind-method-understand-the-bind-method-in-javascript-7hh"&gt;A Quick Look at the JavaScript bind() Method&lt;/a&gt;&lt;/em&gt;. Certainly, if you have not worked through the first tutorial in the series, you would do best to review &lt;em&gt;&lt;a href="https://dev.to/rhieger/a-quick-look-at-the-javascript-call-method-5ghc"&gt;A Quick Look at the JavaScript call() Method&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Our starting point will be the finished code from the second tutorial in this series.&lt;/p&gt;

&lt;p&gt;&lt;a id="recap"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  A Brief Recap
&lt;/h3&gt;

&lt;p&gt;Whereas the previous tutorial resulted in a simple demo application that demonstrated the use of the &lt;code&gt;bind()&lt;/code&gt; method, in this tutorial we will refactor the same application to take advantage of the &lt;code&gt;apply()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 1&lt;/strong&gt; below shows the starting point for this tutorial—the opening screen of the application:&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.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%2Fzkk3yv5is1zsqenis2xk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzkk3yv5is1zsqenis2xk.png" alt="Opening Screen My Favorite Dogs"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 1 My Favorite Dogs Opening Screen&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
When finished, the refactored application should produce exactly the same output as its two predecessors.&lt;/p&gt;

&lt;p&gt;&lt;a id="getStarted"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Let's Get Started
&lt;/h3&gt;

&lt;p&gt;To get started, please download the &lt;a href="https://github.com/RHieger/my-favorite-dogs/blob/4b7d6c7a4b71d40a42fa3bc12b0eb25ea7e0c521/starter-code-tutorial-3.zip" rel="noopener noreferrer"&gt;tutorial starter code&lt;/a&gt;. Simply click the download icon found in the Github link as shown below in &lt;strong&gt;Figure 2&lt;/strong&gt;:&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fikwc6w3ymcrb1nomh7d7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fikwc6w3ymcrb1nomh7d7.png" alt="Download Starter Code"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 2 Github Starter Code Download Page&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
You can unzip the archive onto your local drive and you will see the following project files as shown in &lt;strong&gt;Figure 3&lt;/strong&gt; below:&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.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%2F329hd1dlvd0g4ooucvxz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F329hd1dlvd0g4ooucvxz.png" alt="Project Files"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 3 Starter Code Project Files&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a id="changeNeeded"&gt;&lt;/a&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What Needs to Change?
&lt;/h3&gt;

&lt;p&gt;Two main parts of the application will require refactoring. The first is the &lt;strong&gt;index.html&lt;/strong&gt; file, itself, changed mainly for semantic reasons and consistency with the refactored application's purpose. The second is the &lt;strong&gt;app.js&lt;/strong&gt; file, and here the change is more extensive and substantive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 1&lt;/strong&gt; below shows the first block of code that will need to be refactored:&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;// Bound functions:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dog1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed1&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;dog2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed2&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;dog3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Dog Data Arrays&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dogBreed1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dog1&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;dogBreed2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dog2&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;dogBreed3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dog3&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 1 Dog Objects and the dogBreed Arrays Resulting from their Invocation&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
In &lt;strong&gt;Code Listing 1&lt;/strong&gt;, we see three &lt;code&gt;dog&lt;/code&gt; objects. The values stored in these objects are derived from the &lt;code&gt;getDogInfo&lt;/code&gt; object, on which the &lt;code&gt;info()&lt;/code&gt; method is called and bound to the &lt;code&gt;breed&lt;/code&gt; object, which, in turn, contains the properties of each favorite dog breed.&lt;/p&gt;

&lt;p&gt;In the code that follows, &lt;code&gt;dog1&lt;/code&gt; through &lt;code&gt;dog3&lt;/code&gt; are called and return three arrays assigned to &lt;code&gt;dogBreed1&lt;/code&gt; through &lt;code&gt;dogBreed3&lt;/code&gt;. This was the method employed in the last tutorial to assemble the data that populates the three rows of data on favorite dog breeds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 2&lt;/strong&gt; below shows the last block of JavaScript code with which we are concerned in this tutorial:&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;// Button Event Listners:&lt;/span&gt;
&lt;span class="nx"&gt;button1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setRowCotent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dogBreed1&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;button2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setRowCotent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedTwo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dogBreed2&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;button3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setRowCotent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedThree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dogBreed3&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;As in the second tutorial of this series, we will need to refactor the event listeners for &lt;strong&gt;button1&lt;/strong&gt; through &lt;strong&gt;button3&lt;/strong&gt;. &lt;strong&gt;button4&lt;/strong&gt; remains uniform in all three tutorials in this series.&lt;/p&gt;

&lt;p&gt;&lt;a id="refactor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Now Let's Refactor the Code
&lt;/h3&gt;

&lt;p&gt;We can now refactor the code to take advantage of the &lt;code&gt;apply()&lt;/code&gt; method. Once you have unzipped the starter code archive, follow the steps below:&lt;/p&gt;

&lt;p&gt;&lt;a id="neededChange"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Step 1: Make the Needed Changes to&lt;/em&gt; &lt;strong&gt;index.html&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&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;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"IE=edge"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&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;bind() Method Demo&lt;span class="nt"&gt;&amp;lt;/title&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;"./js/app.js"&lt;/span&gt; &lt;span class="na"&gt;defer&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;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;href=&lt;/span&gt;&lt;span class="s"&gt;"./css/bind-demo.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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 3  Section of index.html&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;As in the previous tutorial, we are concerned only with two lines in &lt;strong&gt;Code Listing 3&lt;/strong&gt; above.&lt;/p&gt;

&lt;p&gt;Make the following changes to &lt;strong&gt;index.html:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1) Change the line with the &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; tag so it reads as follows:&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="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;apply() Method Demo&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Change the line with the &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tag so it reads as follows:&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="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;href=&lt;/span&gt;&lt;span class="s"&gt;"./css/apply-demo.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a id="renameStyle"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 2: Rename the CSS Style Sheet&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;If you refer to &lt;strong&gt;Figure 3&lt;/strong&gt; above, you will see that the CSS style sheet is named &lt;strong&gt;bind-demo.css&lt;/strong&gt;. This name does not match the name of the style sheet referenced in our revised &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Rename &lt;strong&gt;bind-demo.css&lt;/strong&gt; to &lt;strong&gt;apply-demo.css&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you open &lt;strong&gt;index.html&lt;/strong&gt; in your browser, everything will display correctly. In fact, if you click the buttons of the app, they will function exactly as they did in the previous tutorial. This is due to the original code in our starter files for the project.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Now we can explore in depth how to refactor the application logic found in&lt;/em&gt; app.js &lt;em&gt;so that it takes advantage of the&lt;/em&gt; &lt;code&gt;apply&lt;/code&gt; &lt;em&gt;method instead of the&lt;/em&gt; &lt;code&gt;bind()&lt;/code&gt; method.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a id="refactorDog"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Refactor the dog Objects to Use the &lt;code&gt;apply()&lt;/code&gt; Method
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Get info for each breed object:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;info&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="k"&gt;return&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;breed&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;origin&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;lifeSpan&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;// Bound functions:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dog1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed1&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;dog2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed2&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;dog3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Dog Data Arrays:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dogBreed1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dog1&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;dogBreed2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dog2&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;dogBreed3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dog3&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 4 The getDogInfo Object and the Derived dog Objects&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;Let's review the code above in &lt;strong&gt;Code Listing 4&lt;/strong&gt; to better understand what we will be changing in the refactored code.&lt;/p&gt;

&lt;p&gt;In our starter code, the &lt;code&gt;info()&lt;/code&gt; method is called on the &lt;code&gt;getDogInfo&lt;/code&gt; object, and the &lt;code&gt;bind()&lt;/code&gt; method is chained to the &lt;code&gt;info()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Three sequentially numbered &lt;code&gt;breed&lt;/code&gt; objects are passed to the &lt;code&gt;bind()&lt;/code&gt; method and the results are store to three sequentially numbered &lt;code&gt;dog&lt;/code&gt; objects.&lt;/p&gt;

&lt;p&gt;Finally, as shown in the finished code from the last tutorial, each &lt;code&gt;dog&lt;/code&gt; object is called and its results stored in the sequentially numbered &lt;code&gt;dogBreed&lt;/code&gt; objects, the contents of which are arrays containing the dog breed information to be injected dynamically in the table rows shown above in &lt;strong&gt;Figure 1&lt;/strong&gt; at the beginning of this article.&lt;/p&gt;

&lt;p&gt;Our objective in refactoring the code seen in &lt;strong&gt;Code Listing 4&lt;/strong&gt; is to replace the &lt;code&gt;bind()&lt;/code&gt; method called on the &lt;code&gt;dog&lt;/code&gt; objects with the &lt;code&gt;apply()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a id="howDiffer"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;How Does&lt;/em&gt; &lt;code&gt;Function.prototype.apply()&lt;/code&gt; &lt;em&gt;Differ from&lt;/em&gt; &lt;code&gt;Function.prototype.bind()&lt;/code&gt;&lt;em&gt;?&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;bind()&lt;/code&gt; method returns a new function that, upon being called, has its reference to which the keyword &lt;code&gt;this&lt;/code&gt; is set. It may also have a set of optional arguments that must precede any that are passed at the time the new returned function is called.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;apply()&lt;/code&gt; method, in contrast, calls a function whose reference for the &lt;code&gt;this&lt;/code&gt; keyword has been set, and then an array (or array-like object) containing any arguments required in the function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Now let's see how to refactor this code in&lt;/em&gt; Code Listing 4 &lt;em&gt;above to take advantage of the&lt;/em&gt; &lt;code&gt;apply()&lt;/code&gt; &lt;em&gt;method&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Make the following changes to the code:&lt;/p&gt;

&lt;p&gt;In place of the &lt;code&gt;dog&lt;/code&gt; object declarations above in &lt;strong&gt;Code Listing 4&lt;/strong&gt;, type in the code listed below in &lt;strong&gt;Code Listing 5&lt;/strong&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="c1"&gt;// Bound functions:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dog1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;breed1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;breed1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="nx"&gt;breed1&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="nx"&gt;breed1&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="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;dog2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;breed2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;breed2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="nx"&gt;breed2&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="nx"&gt;breed2&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="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;dog3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;breed3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;breed3&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="nx"&gt;breed3&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="nx"&gt;breed3&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="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 5 Refactoring of 3 dog Objects&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
There are two notable differences in the code for the three &lt;code&gt;dog&lt;/code&gt; objects above. The first is that the &lt;code&gt;bind()&lt;/code&gt; method is replaced by the &lt;code&gt;apply()&lt;/code&gt; method. The remaining difference in our refactored code has bigger implications, as we will soon see.&lt;/p&gt;

&lt;p&gt;The first argument to the &lt;code&gt;apply()&lt;/code&gt; method in the objects defined above is a reference object to which the &lt;code&gt;this&lt;/code&gt; keyword refers. Then there is an array of arguments, as discussed above. In this case, the arguments are the elements of the array that contains the content to go into the three table cells of each row containing dog breed information.&lt;/p&gt;

&lt;p&gt;The second rather notable difference is that when using the &lt;code&gt;apply()&lt;/code&gt; method, because no new object is returned, as is the case when using &lt;code&gt;bind()&lt;/code&gt;, there is no longer any need to create &lt;code&gt;dogBreed&lt;/code&gt; objects to model the data for the table rows, as shown earlier in &lt;strong&gt;Code Listing 4&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This definitely makes for more compact code, cutting out a full three lines of code. As we directly access the values stored in the &lt;code&gt;dog&lt;/code&gt; objects created with the &lt;code&gt;getDogInfo.info()&lt;/code&gt; method call, also shown above in &lt;strong&gt;Code Listing 5&lt;/strong&gt;, we are able to access the indices in the &lt;code&gt;dog&lt;/code&gt; arrays directly, eliminating an interim step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does this mean that the&lt;/strong&gt; &lt;code&gt;bind()&lt;/code&gt; &lt;strong&gt;method is inefficient or undesirable?&lt;/strong&gt; In this case, the answer is bigger than the question. The concepts involved are fairly advanced, but several articles have been published that explore the question of optimum use cases for the three methods—&lt;code&gt;call()&lt;/code&gt;, &lt;code&gt;bind()&lt;/code&gt; and &lt;code&gt;apply()&lt;/code&gt;—explored in this tutorial series.&lt;/p&gt;

&lt;p&gt;&lt;a id="recommended"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Recommended Reading for Study of&lt;/em&gt; &lt;code&gt;call()&lt;/code&gt;, &lt;code&gt;bind()&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; &lt;code&gt;apply()&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;One fairly comprehensive article that is worth a read is&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;&lt;a href="https://www.eigenx.com/blog/https/mediumcom/eigen-x/how-and-when-to-use-bind-call-and-apply-in-javascript-77b6f42898fb" rel="noopener noreferrer"&gt;How AND When to use bind, call, and apply in JavaScript&lt;/a&gt;. &lt;strong&gt;This article gives a broad overview of the relative utility and good use cases for each method. Though the information in this article is definitely fairly advanced, it is worthwhile to spend some time with the ideas expressed there as they begin to clarify how best to use each of these methods.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a id="refactorButtons"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 4: Refactor the Event Listeners for&lt;/em&gt; button1 &lt;em&gt;through&lt;/em&gt; button3
&lt;/h4&gt;

&lt;p&gt;As one final step in our refactoring, one subtle change must be made to the event listeners for &lt;code&gt;button1&lt;/code&gt; through &lt;code&gt;button3&lt;/code&gt;, as shown earlier in &lt;strong&gt;Code Listing 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Referring back to &lt;strong&gt;Code Listing 2&lt;/strong&gt;, you will note that the last argument in the &lt;code&gt;setRowContent()&lt;/code&gt; callback function reads &lt;code&gt;dogBreed1&lt;/code&gt;, &lt;code&gt;dogBreed2&lt;/code&gt; and &lt;code&gt;dogBreed3&lt;/code&gt;. These arguments hearken back to our previous iteration of the application, which used the &lt;code&gt;bind()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bind()&lt;/code&gt; method, as you will recall, set the value of the &lt;code&gt;this&lt;/code&gt; keyword to the specified object—in our case &lt;code&gt;breed1&lt;/code&gt;, &lt;code&gt;breed2&lt;/code&gt; and &lt;code&gt;breed3&lt;/code&gt;. However, in the process, a new object is returned. In order to access the properties in that object the &lt;code&gt;info()&lt;/code&gt; method was called and the &lt;code&gt;apply()&lt;/code&gt; method was chained to it. The result was stored in brand new arrays stored to the constants &lt;code&gt;dogBreed1&lt;/code&gt;-&lt;code&gt;dogBreed3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;apply()&lt;/code&gt; method, we can cut out this "middleman" step, by allowing access directly to the properties within the object reference set for the &lt;code&gt;this&lt;/code&gt; keyword. We may therefore access properties from &lt;code&gt;dog1&lt;/code&gt;, &lt;code&gt;dog2&lt;/code&gt; and &lt;code&gt;dog3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For convenience, &lt;strong&gt;Code Listing 6&lt;/strong&gt; below shows the relevant code to be refactored:&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;// Button Event Listeners:&lt;/span&gt;
&lt;span class="nx"&gt;button1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;showRowContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dogBreed1&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;button2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;showRowContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedTwo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dogBreed2&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;button3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;showRowContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedThree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dogBreed3&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;h6&gt;
  
  
  &lt;center&gt;Code Listing 6 Event Listeners to be Refactored&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Refactoring the above code is fairly trivial—merely a change of the last argument in each of the &lt;code&gt;setRowContent()&lt;/code&gt; methods. But as discussed earlier, the implications of this change are significant because the extra step of saving the values to be placed in each row to constants is eliminated.&lt;/p&gt;

&lt;p&gt;Now we do the refactoring of the code above, making the minor changes described above. Make sure that your code matches that shown in &lt;strong&gt;Code Listing 7:&lt;/strong&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;// Button Event Listeners:&lt;/span&gt;
&lt;span class="nx"&gt;button1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;showRowContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dog1&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;button2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;showRowContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedTwo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dog2&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;button3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;showRowContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedThree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dog3&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;h6&gt;
  
  
  &lt;center&gt;Code Listing 7 Refactored Event Listeners for button1 through button3&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Now that these changes have been implemented, you can see the results by pointing your browser at the &lt;strong&gt;index.html&lt;/strong&gt; file in your application folder, or by going to the page live on &lt;a href="https://codepen.io/RHieger/full/mdmLLyL" rel="noopener noreferrer"&gt;codepen.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a id="nyext"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;At the end of the first tutorial, I mentioned that the little application we created is simplistic and does not take into account user input. As a result, there is the rather artless paradigm of a button bar with four buttons, three of which fill in details about three pre-determined dog breeds and one that resets the table to its initial state. I indicated that I would be refactoring the current application to be a far more robust, dynamic and user interactive Single Page Application (SPA).&lt;/p&gt;

&lt;p&gt;The current application barely qualifies as an application with dynamic generation of data. I intend to build a new version soon, which will include a modal dialogue in which a user can input the dog breed information of choice. The breeds will therefore not be pre-determined as in this version of the application.&lt;/p&gt;

&lt;p&gt;The implications of these changes are profound from a user interface perspective and also a data validation perspective, as it cannot be predicted ahead of time what a user will enter, and whether the data entered is valid.&lt;/p&gt;

&lt;p&gt;Please stay tuned for my next tutorial—&lt;em&gt;My Favorite Dogs—A Simple Single Page Application (SPA).&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;a id="cited"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;center&gt;Works Citied&lt;/center&gt;
&lt;/h2&gt;

&lt;p&gt;Hieger, Robert. "A Quick Look at the bind() Method:&lt;br&gt;     Understand the bind() Method in JavaScript." &lt;em&gt;dev.to,&lt;/em&gt;&lt;br&gt;     DEV Community, 22 Aug 2021, &lt;a href="https://dev.to/rhieger/a-quick-look-at-the-javascript-bind-method-understand-the-bind-method-in-javascript-7hh"&gt;https://dev.to/rhieger/a-quick-look-at-the-javascript-bind-method-understand-the-bind-method-in-javascript-7hh&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;---. "A Quick Look at the JavaScript call() Method." &lt;em&gt;dev.to,&lt;/em&gt;&lt;br&gt;     DEV Community, 5 May 2021, &lt;a href="https://dev.to/rhieger/a-quick-look-at-the-javascript-call-method-5ghc"&gt;https://dev.to/rhieger/a-quick-look-at-the-javascript-call-method-5ghc&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;"How and When to Use bind, call, and apply in JavaScript."&lt;br&gt;     &lt;em&gt;Eigenx,&lt;/em&gt; Eigen X, 2 Aug. 2018 &lt;a href="https://www.eigenx.com/blog/https/mediumcom/eigen-x/how-and-when-to-use-bind-call-and-apply-in-javascript-77b6f42898fb" rel="noopener noreferrer"&gt;https://www.eigenx.com/blog/https/mediumcom/eigen-x/how-and-when-to-use-bind-call-and-apply-in-javascript-77b6f42898fb&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>dom</category>
    </item>
    <item>
      <title>A Quick Look at the JavaScript bind() Method: Understand the bind() Method in JavaScript</title>
      <dc:creator>Robert Hieger</dc:creator>
      <pubDate>Mon, 06 Nov 2023 17:19:29 +0000</pubDate>
      <link>https://forem.com/rhieger/a-quick-look-at-the-javascript-bind-method-understand-the-bind-method-in-javascript-7hh</link>
      <guid>https://forem.com/rhieger/a-quick-look-at-the-javascript-bind-method-understand-the-bind-method-in-javascript-7hh</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffl28mgvk5me6oq1m2lsk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffl28mgvk5me6oq1m2lsk.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;                                   Photo by &lt;a href="https://www.pexels.com/@negativespace?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels" rel="noopener noreferrer"&gt;Negative Space&lt;/a&gt; from &lt;a href="https://www.pexels.com/photo/gray-laptop-computer-showing-html-codes-in-shallow-focus-photography-160107/?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels" rel="noopener noreferrer"&gt;Pexels&lt;/a&gt;&lt;/p&gt;




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

&lt;p&gt;               This article was first published on &lt;a href="https://medium.com/javascript-in-plain-english/a-quick-look-at-the-javascript-call-method-b5b2383eb4e6" rel="noopener noreferrer"&gt;Javascript in Plain English&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;center&gt;Table of Contents&lt;/center&gt;
&lt;/h2&gt;



&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Let's Get Started
&lt;/h3&gt;

&lt;h3&gt;
  
  
  What Needs to Change?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Now Let's Refactor the Code
&lt;/h3&gt;

&lt;h4&gt;
  
  
      &lt;em&gt;Step 1: Make the Needed Changes to index.html&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
      &lt;em&gt;Step 2: Rename the CSS Style Sheet&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
      &lt;em&gt;Step 3: Refactor the breed Objects&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
      &lt;em&gt;Step 4. Refactor the dog Objects to use the bind() Method&lt;/em&gt;
&lt;/h4&gt;

&lt;h5&gt;
  
  
          How Does Function.prototype.bind() Differ from Function.prototype.call()?
&lt;/h5&gt;

&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Works Cited
&lt;/h3&gt;




&lt;p&gt;&lt;a id="intro"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;It has been a while, and I am happy top get back to my promised series of tutorials. This is the second in a series of three tutorials on a project to demonstrate the &lt;code&gt;call()&lt;/code&gt;, &lt;code&gt;bind()&lt;/code&gt; and &lt;code&gt;apply()&lt;/code&gt; methods, which bear similarities, but also have distinct differences.&lt;/p&gt;

&lt;p&gt;The first tutorial in this series covered the use of the &lt;code&gt;call()&lt;/code&gt; method in a project that populated a table of favorite dog breeds on the click of buttons beneath the table.&lt;/p&gt;

&lt;p&gt;The text editor used in this tutorial is &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt; (VSC), a very powerful, yet easy-to-use extensible programmer's editor by Microsoft. And it is absolutely free. However, you can use your favorite text editor instead, as long as it features code syntax highlighting for JavaScript, as this will ease in reading your code, and preferably "IntelliSense" to anticipate the code you intend, after a few keystrokes.&lt;/p&gt;

&lt;p&gt;In this second of three tutorials, we will take a look at the &lt;code&gt;bind()&lt;/code&gt; method, which bears similarity to the &lt;code&gt;call()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The code in this project will produce output identical to that of the first tutorial, but using the&lt;/strong&gt; &lt;code&gt;bind()&lt;/code&gt; &lt;strong&gt;method instead&lt;/strong&gt;. This will require several changes to the code from the previous tutorial.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PREREQUISITE&lt;/strong&gt;: If you have not already gone through the first tutorial in this series, because a good deal of the code will not be reviewed here, you would do best to review &lt;em&gt;&lt;a href="https://dev.to/rhieger/a-quick-look-at-the-javascript-call-method-5ghc"&gt;A Quick Look at the JavaScript call() Method&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As we have already built a functioning model of the finished project (as shown below in &lt;strong&gt;Figure 1&lt;/strong&gt;), our focus in this tutorial will not be a walkthrough of the entire process, but rather an exploration of the differences in code for this version of the project.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuas55sou1jv9a848iprm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuas55sou1jv9a848iprm.png" alt="My Favorite Dogs Opening Screen"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 1 Opening Screen of My Favorite Dogs&lt;/center&gt;
&lt;/h6&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;For a brief recap, in the first tutorial, we built a very simple single page application (SPA) with four buttons, the first three of which allowed a user to populate three rows of a table with data on favorite dog breeds. The fourth button reset the table to its original state as shown above.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a id="getStarted"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Let's Get Started
&lt;/h3&gt;

&lt;p&gt;To get started, please download the &lt;a href="https://github.com/RHieger/my-favorite-dogs/blob/fav-dogs-bind/tutorial-2-starter-code.zip" rel="noopener noreferrer"&gt;tutorial starter code&lt;/a&gt;. Simply click the download icon found in the Github Link as shown below in &lt;strong&gt;Figure 2&lt;/strong&gt;:&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbvxepahwte44h8cdxbgj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbvxepahwte44h8cdxbgj.png" alt="Github Download Screen"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 2 Download Starter Code&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
You can unzip the archive onto your local drive and you will see the project files as shown in &lt;strong&gt;Figure 3&lt;/strong&gt; below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrswr388sop23708pkn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrswr388sop23708pkn8.png" alt="Project Files"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 3 Starter Code Project Files&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Figure 3&lt;/strong&gt; shows a directory tree with a &lt;strong&gt;css&lt;/strong&gt; folder containing the external stylesheet for the project, a &lt;strong&gt;js&lt;/strong&gt; folder to contain the application logic, and finally the &lt;strong&gt;index.html&lt;/strong&gt; file with the application content.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a id="changeNeeded"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What Needs to Change?
&lt;/h3&gt;

&lt;p&gt;Several things must change to refactor this application to use the &lt;code&gt;bind()&lt;/code&gt; method, some trivial and some a bit more involved.&lt;/p&gt;

&lt;p&gt;We will need to make changes in all three files that make up the application. In &lt;strong&gt;index.html&lt;/strong&gt; the file reference to the external stylesheet will change for semantic reasons, as will the content in the &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Then, refactoring will occur in the code shown below in &lt;strong&gt;Code Listing 1&lt;/strong&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;dog1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed1&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;dog2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed2&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;dog3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 1 Calls to getDogInfo&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
The code in &lt;strong&gt;Code Listing 1&lt;/strong&gt; above will be refactored to use the &lt;code&gt;bind()&lt;/code&gt; method. This will require an additional segment of code due to the difference between the &lt;code&gt;call()&lt;/code&gt; and &lt;code&gt;bind()&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;&lt;a id="refactorCode"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Now Let's Refactor the Code
&lt;/h3&gt;

&lt;p&gt;Armed with knowledge of the outlined changes above, we can refactor the code to demonstrate use of the &lt;code&gt;bind()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a id="makeChanges"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 1: Make the Needed Changes to&lt;/em&gt; &lt;strong&gt;index.html&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 2&lt;/strong&gt; below shows the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section of the &lt;strong&gt;index.html&lt;/strong&gt; file included in your starter code:&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="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;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"x-UA Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"IE=edge"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&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;call() Method Demo&lt;span class="nt"&gt;&amp;lt;/title&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;"./js/app.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;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;href=&lt;/span&gt;&lt;span class="s"&gt;"./css/call-demo.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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 2  Section of index.html&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;We are concerned only with two lines in &lt;strong&gt;Code Listing 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Make the changes below to your starter code:&lt;/p&gt;

&lt;p&gt;1) Change the line with the &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; tag so that it reads as follows:&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="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;bind() Method Demo&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Change the line with the &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tag to read as follows:&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="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;href=&lt;/span&gt;&lt;span class="s"&gt;"./css/bind-demo.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;

  
  &lt;em&gt;Step 2: Rename the CSS Stylesheet&lt;/em&gt;


&lt;/p&gt;
&lt;p&gt;Referring back to &lt;strong&gt;Figure 3&lt;/strong&gt;, you will see that CSS style sheet is named &lt;strong&gt;call-demo.css&lt;/strong&gt;. This name does not match the name of the style sheet referenced in our revised &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Rename &lt;strong&gt;call-demo.css&lt;/strong&gt; to &lt;strong&gt;bind-demo.css&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you open **index.html in your browser, everything will display correctly. In fact, if you click the buttons of the app, they will function exactly as they did in the previous tutorial. This is due to the original code in our starter files for the project.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Now we have set the stage. Take a deep breath and let's dig into refactoring the application logic found in app.js so that it takes advantage of the&lt;/em&gt; &lt;code&gt;bind()&lt;/code&gt; &lt;em&gt;method instead of the&lt;/em&gt; &lt;code&gt;call()&lt;/code&gt; &lt;em&gt;method.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a id="refactorBreed"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 3: Refactor the&lt;/em&gt; breed &lt;em&gt;Objects&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 3&lt;/strong&gt; below shows the relevant code for this step:&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;breed1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PUG&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CHINA&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lifeSpan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;12-15 YEARS&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;breed2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LABRADOR&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;NEWFOUNDLAND&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lifeSpan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;10-12 YEARS&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;breed3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BEAGLE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ENGLAND&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lifeSpan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;12-15 YEARS&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;h6&gt;
  
  
  &lt;center&gt;Code Listing 3 The breed Objects&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;As &lt;strong&gt;Code Listing 3&lt;/strong&gt; shows, there are three sequentially numbered &lt;code&gt;breed&lt;/code&gt; objects that contain the properties that will be dynamically injected into the columns of the table in our app.&lt;/p&gt;

&lt;p&gt;There is absolutely nothing wrong with the code above and it would work just fine for the purposes of this demo. But I am going to ask you to change the above three objects so that they look like the code in &lt;strong&gt;Code Listing 4&lt;/strong&gt; below:&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;breed1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pug&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;china&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lifeSpan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;12-15 years&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;breed2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newfoundland&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lifeSpan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;10-12 years&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;breed3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;beagle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;england&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lifeSpan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;12-15 years&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;h6&gt;
  
  
  &lt;center&gt;Code Listing 4 Breed Objects Refactored&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;You might be asking yourself, &lt;strong&gt;&lt;em&gt;"Why is this necessary?"&lt;/em&gt;&lt;/strong&gt; And from a certain point of view, you would be correct. I chose to refactor this code for the following reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;As will be seen in the code that follows, refactoring this code as shown above cedes control of the typography to the application logic, rather than hard-coding the uppercase letters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Later on, especially if this application were to be developed into a more functional model that accepted user input, all manner of characters a user might enter could then be cleaned up view transformation of the raw data entered by the user.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In essence, this kind of behavior is more far-reaching and geared toward further development of this simplistic demo.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a id="refactorDog"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 4. Refactor the dog Objects to the use the&lt;/em&gt; &lt;code&gt;bind()&lt;/code&gt; &lt;em&gt;Method&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Code Listing 5&lt;/strong&gt; below shows the code relevant to our task:&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;getDogInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;info&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="c1"&gt;// Array containing properties of each dog object&lt;/span&gt;
    &lt;span class="k"&gt;return&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;breed&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;orgin&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;lifeSpan&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dog1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed1&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;dog2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed2&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;dog3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 5 The getDogInfo Object and the Derived dog Objects&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;To clarify how we will refactor the above, let's review the code in &lt;strong&gt;Code Listing 5&lt;/strong&gt; to clarify what is happening in this listing.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;getDogInfo&lt;/code&gt; Objet contains a single property, a method called &lt;code&gt;info()&lt;/code&gt;, whose sole purpose is to return the values that it will get, albeit indirectly, from the &lt;code&gt;breed&lt;/code&gt; objects. Note that the values returned correspond directly to the properties within the &lt;code&gt;breed&lt;/code&gt; objects shown in &lt;strong&gt;Code Listing 4&lt;/strong&gt; (the refactored &lt;code&gt;breed&lt;/code&gt; objects).&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Code Listing 5&lt;/strong&gt;, three &lt;code&gt;dog&lt;/code&gt; objects are created using the &lt;code&gt;call()&lt;/code&gt; method. In this case, the object created is an array containing the values of the properties. But how does this differ from what we will do when we refactor this code?&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a id="howDiffer"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  &lt;center&gt;How Does Function.prototype.bind() Differ from Function.prototype.call()?&lt;/center&gt;
&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;A Brief Explanation.&lt;/strong&gt; If you are looking for a thorough explanation of just how both these methods work, you'll find the most complete information at the Mozilla Developer Network page for &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call" rel="noopener noreferrer"&gt;Function.prototype.call()&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind" rel="noopener noreferrer"&gt;Function.prototype.bind()&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A simpler summary of the differences can be expressed like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;call()&lt;/code&gt; method invokes a function object and takes an argument that specifies to which object the &lt;code&gt;this&lt;/code&gt; keyword refers. Additional arguments may also be added within the &lt;code&gt;call()&lt;/code&gt; method signature.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;bind()&lt;/code&gt; method, unlike the &lt;code&gt;call()&lt;/code&gt; method, returns a brand new function which may be called, and through which you may pass an array and as many arguments as needed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To refactor some of the code in &lt;strong&gt;Code Listing 5&lt;/strong&gt;, revise the last three lines for objects &lt;code&gt;dog1&lt;/code&gt; through &lt;code&gt;dog3&lt;/code&gt;, so that they look like the code in &lt;strong&gt;Code Listing 6&lt;/strong&gt; below:&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;dog1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed1&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;dog2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed2&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;dog3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 6 Refactored dog Objects&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;So far, the only different is that we substituted &lt;code&gt;bind()&lt;/code&gt; for &lt;code&gt;call()&lt;/code&gt; in the three refactored declarations above.&lt;/p&gt;

&lt;p&gt;But now comes the real difference. Now you should input the code below in &lt;strong&gt;Code Listing 7&lt;/strong&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="c1"&gt;// Dog Data Arrays&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dogBreed1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dog1&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;dogBreed2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dog2&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;dogBreed3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dog3&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 7 dog Functions Invoked and Their Properties Stored&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Why this extra step?&lt;/em&gt;&lt;/strong&gt; Remember that what the &lt;code&gt;bind()&lt;/code&gt; method does is to return a brand new function. An array and any number of arguments may be passed to this new function. For the purposes of our project, we will make use only of an array. But this array must be stored as a constant from which it may be accessed. The three lines of code in &lt;strong&gt;Code Listing 7&lt;/strong&gt; achieve this.&lt;/p&gt;

&lt;p&gt;With the creation of the three constants shown in &lt;strong&gt;Code Listing 7&lt;/strong&gt;, we have finished our refactoring of the application.&lt;/p&gt;

&lt;p&gt;To see the newly-refactored app in action, simply load the &lt;strong&gt;index.html&lt;/strong&gt; file into your browser. You will find that its functionality is identical to that of the application in the first tutorial of this series.&lt;/p&gt;

&lt;p&gt;Or you can visit the finished refactored application on &lt;a href="https://codepen.io/RHieger/full/vYmpZPb" rel="noopener noreferrer"&gt;codepen.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a id="next"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;The third tutorial in this series will refactor this application once again to take advantage of the &lt;code&gt;apply()&lt;/code&gt; method. As you will see, this method also bears some similarity to the &lt;code&gt;bind()&lt;/code&gt; method. We will explore those differences as we refactor the application using this last of the three methods mentioned in the first tutorial.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;See you soon in the next tutorial, A Quick Look at the JavaScript apply() Method.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a id="works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;center&gt; Works Cited&lt;/center&gt;
&lt;/h2&gt;

&lt;p&gt;Hieger, Robert. “A Quick Look At The Javascript call() Method.” &lt;em&gt;Dev&lt;/em&gt;,&lt;br&gt;     DEV Community, 31 July 2021, &lt;a href="https://dev.to/rhieger/a-quick-look-at-the-javascript-call-method-5ghc"&gt;https://dev.to/rhieger/a-quick-look-at-the-javascript-call-method-5ghc&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;“Function.Prototype.call() -Javascript | MDN.” &lt;em&gt;Mozilla Developer&lt;br&gt;     Network&lt;/em&gt;, Mozilla Foundation, 26 Sept. 2023, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“Function.Prototype.bind() - Javascript | MDN.” &lt;em&gt;Mozilla Developer Network&lt;/em&gt;,&lt;br&gt;     Mozilla Foundation, Aug. 2023, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>dom</category>
    </item>
    <item>
      <title>A Quick Look at the JavaScript call() Method</title>
      <dc:creator>Robert Hieger</dc:creator>
      <pubDate>Sun, 29 Oct 2023 02:26:10 +0000</pubDate>
      <link>https://forem.com/rhieger/a-quick-look-at-the-javascript-call-method-5ghc</link>
      <guid>https://forem.com/rhieger/a-quick-look-at-the-javascript-call-method-5ghc</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fft3uqzv6rv2k5ppf049l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fft3uqzv6rv2k5ppf049l.jpg" alt="Photo by James Harrison on Unsplash"&gt;&lt;/a&gt;&lt;br&gt;
Photo by &lt;a href="https://unsplash.com/@jstrippa?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;James Harrison&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/vpOeXr5wmR4?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cugyn603i815mv945rb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cugyn603i815mv945rb.png" alt="JavaScript in Plain English"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://medium.com/javascript-in-plain-english/a-quick-look-at-the-javascript-D:call-method-b5b2383eb4e6" rel="noopener noreferrer"&gt;JavaScript in Plain English&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;&lt;center&gt;Table of Contents&lt;/center&gt;&lt;/h2&gt;
&lt;br&gt;

&lt;h3&gt;Introductory Remarks&lt;/h3&gt;

&lt;h3&gt;What This Tutorial Assumes&lt;/h3&gt;

&lt;h3&gt;What Does the call() Method Do?&lt;/h3&gt;

&lt;h3&gt;Now Let's Build It&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;&lt;h4&gt;    Step 1. Download the Starter Code
&lt;/h4&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;    A Brief Tour of the Starter Code&lt;/em&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;    Step 2. Declaring Objects to Reference the Desired HTML Elements&lt;/em&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;    Step 3. Create the 3 Dog Objects&lt;/em&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;    Step 4. Coding the getDogInfo Object&lt;/em&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;    Step 5. Using the call() method to specify the object whose properties we want returned as an Array&lt;/em&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;    Step 6. Implementing the resetTable() function&lt;/em&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;    Step 7. Implement the setRowContent() function&lt;/em&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;    Step 8. Implementing the event listeners&lt;/em&gt;
&lt;/h4&gt;
&lt;h3&gt;
  
  
  A Final Word
&lt;/h3&gt;
&lt;h3&gt;
  
  
  Works Citied
&lt;/h3&gt;



&lt;p&gt;&lt;a id="introRemarks"&gt;&lt;/a&gt;&lt;br&gt;
I found my way into full-stack web development from an atypical background. I am an actor, writer, director, and musician. Full-stack web development and design felt like a natural choice to me, as it provides the opportunity to exercise my imagination and express aesthetically in visual design for the web.&lt;/p&gt;

&lt;p&gt;Like so many of us working in or studying software engineering and design, I am a firm believer in the idea that one of the best teachers is testing your skills by sharing that knowledge with others.&lt;/p&gt;

&lt;p&gt;This is the first of three articles on three very useful methods exposed by the JavaScript function object—&lt;code&gt;call()&lt;/code&gt;, &lt;code&gt;bind()&lt;/code&gt;, and &lt;code&gt;apply()&lt;/code&gt;. These methods are similar to one another, and thus can be a bit confusing.&lt;/p&gt;

&lt;p&gt;The demo project in this article is not intended to be a treatise on the best and only way to employ the &lt;code&gt;call()&lt;/code&gt; method. Rather, I intend to share my discovery in grappling with the difficulties one encounters when learning the &lt;code&gt;call()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;I have been a dog lover all my life. I thought it might be fun to create a web page that dynamically lists out some favorite dog breeds, their indigenous habitats, and their average lifespans in a table as seen below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe95uenrnbh1a2ehlyuvr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe95uenrnbh1a2ehlyuvr.jpg" alt="My Favorite Dogs Opening Screen"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;h6&gt;&lt;center&gt;Fig. 1 Opening Screen of MY FAVORITE DOGS Demo&lt;/center&gt;&lt;/h6&gt;
&lt;br&gt;

&lt;p&gt;The object of this demo is to employ the &lt;code&gt;call()&lt;/code&gt; method that function objects expose to extract properties from objects and assign them dynamically to text nodes in the &lt;strong&gt;Document Object Model (DOM)&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;I am a firm believer in the idea that one of the best teachers is testing your skills by sharing that knowledge with others.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In &lt;strong&gt;Figure 1&lt;/strong&gt;, you see the opening screen of the finished product, which displays a table with space for three favorite dog breeds, the countries to which they are indigenous, and their average lifespans.&lt;/p&gt;

&lt;p&gt;When the page is loaded, because no favorite dog has been selected, the three items of the &lt;strong&gt;BREED&lt;/strong&gt; column contain the default text &lt;strong&gt;NO BREED SELECTED&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Below the table is a button bar with four buttons, the first three of which allow the user to add a first, second, and third favorite dog breed.&lt;/p&gt;

&lt;p&gt;For the purposes of this demo, we will not seek the input of the user to enter favorite breeds. The three buttons dynamically fill in the three columns for each breed by assigning the properties of three dog breed objects to the table cell text nodes for each breed.&lt;/p&gt;

&lt;p&gt;The fourth button resets the screen to its original state.&lt;/p&gt;

&lt;p&gt;&lt;a id="assume"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What This Tutorial Assumes
&lt;/h3&gt;

&lt;p&gt;This tutorial assumes a basic understanding of HTML, a firm grasp of JavaScript basics, and some knowledge of Object Oriented Programming (OOP). Familiarity with Cascading Style Sheets (CSS) is desirable, but not required. The primary focus of this tutorial is JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;a id="callMethod"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What Does the call() Method Do?
&lt;/h3&gt;

&lt;p&gt;Aye, there's the rub. In order to benefit at all from the powerful &lt;code&gt;call()&lt;/code&gt; method, you really have to have a clear understanding of what it does.&lt;/p&gt;

&lt;p&gt;Let's start with the &lt;em&gt;de facto&lt;/em&gt; resource for JavaScript study on the web—&lt;a href="https://developer.mozilla.org/en-US/" rel="noopener noreferrer"&gt;Mozilla Developer Network&lt;/a&gt; (MDN)—to get ourselves started:&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The&lt;/em&gt; &lt;code&gt;call()&lt;/code&gt; &lt;em&gt;method calls a function with a given&lt;/em&gt; &lt;code&gt;this&lt;/code&gt; &lt;em&gt;value and any arguments provided to specify to which value&lt;/em&gt; &lt;code&gt;this&lt;/code&gt; &lt;em&gt;refers.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;This is all very good, but what does it mean?&lt;/strong&gt; The &lt;code&gt;this&lt;/code&gt; keyword in JavaScript has been the cause for nightmares for many budding developers, myself included. It is subject to misinterpretation and especially in earlier JavaScript standards, can behave in very strange ways.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt;, in theory, was supposed to refer to the calling function, but because the value of &lt;code&gt;this&lt;/code&gt; cannot be set at the time of the function call, JavaScript will default to the value of the global context, which is &lt;code&gt;window&lt;/code&gt;. This value will be returned, as shown in the example from the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" rel="noopener noreferrer"&gt;MDN article below&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjeyvom9gayrp97qo3r1s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjeyvom9gayrp97qo3r1s.png" alt="MDN article on this keyword"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 2 Excerpt from Mozilla Developer Network article on this keyword&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;As you can see in &lt;strong&gt;Figure 2&lt;/strong&gt;, &lt;code&gt;this&lt;/code&gt; rfefers to the global object, which is &lt;code&gt;window&lt;/code&gt;. As the &lt;code&gt;window&lt;/code&gt; does not yet refer to the context we need, which is &lt;code&gt;document&lt;/code&gt; (the next level down) in the Document Object Model, there is no value for function &lt;code&gt;f1()&lt;/code&gt; to return except &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From the JavaScript code that is part of our project, &lt;code&gt;this&lt;/code&gt; is specified, using the &lt;code&gt;call()&lt;/code&gt; method, and referring to a specified object, as shown in &lt;strong&gt;Code Listing 1&lt;/strong&gt; below:&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;getDogInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;info&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="c1"&gt;// Array containing properties of each dog object&lt;/span&gt;
    &lt;span class="k"&gt;return&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;breed&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;origin&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;lifeSpan&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dog1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed1&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;dog2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed2&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;dog3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 1: Demonstration of call() Method Using Project Code&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Code Listing 1&lt;/strong&gt; above shows an object literal named &lt;code&gt;getDogInfo&lt;/code&gt;. Within the object is a function called &lt;code&gt;info()&lt;/code&gt; that returns an array of the three properties present in three objects designated &lt;code&gt;breed1&lt;/code&gt;, &lt;code&gt;breed2&lt;/code&gt; and &lt;code&gt;breed3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the three statements that follow, the values associated with &lt;code&gt;this&lt;/code&gt; are determined by specifying the objects to which &lt;code&gt;this&lt;/code&gt; refers in the &lt;code&gt;call()&lt;/code&gt; method chained to &lt;code&gt;info&lt;/code&gt;, which in turn refers back to the &lt;code&gt;info()&lt;/code&gt; method exposed by the &lt;code&gt;getDogInfo&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Is your head spinning yet? Don't worry. Mine did, for a while, too. Let's just take things step by step.&lt;/p&gt;

&lt;p&gt;&lt;a id="buildIt"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Now Let's Build It
&lt;/h2&gt;

&lt;p&gt;&lt;a id="starterCode"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;Step 1. Download the Starter Code&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Start by downloading the &lt;a href="https://github.com/RHieger/my-favorite-dogs/releases/tag/v1.00" rel="noopener noreferrer"&gt;starter code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can follow along in the steps to finish the project. As the focus here is JavaScript, and not CSS styling or HTML, the starter code will provide the bare bones structure on which the tutorial project will be built.&lt;/p&gt;

&lt;p&gt;When you download the starter code, you will find a zip archive as shown below in &lt;strong&gt;Figure 3&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhqbmqwey1aqbt2j74ksf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhqbmqwey1aqbt2j74ksf.png" alt="The starter code"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 3 Downloading the Starter Code&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;The screenshot above shows the zip archive in the Downloads folder on a Mac. If you are running Windows or another OS, the location of your download may differ.&lt;/p&gt;

&lt;p&gt;Simply move the zip archive to the location you wish and unzip it.&lt;/p&gt;

&lt;p&gt;Or if you prefer, you can clone the &lt;a href="https://github.com/RHieger/my-favorite-dogs.git" rel="noopener noreferrer"&gt;git repository&lt;/a&gt;, and begin by working with the starter code in the &lt;strong&gt;starter-code&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;I urge you not to look at the finished-project folder until you have followed through the steps of this tutorial. This way, you can gain the best insights, as you piece the project together.&lt;/em&gt;&lt;/strong&gt; That said, should you find yourself stuck, you can certainly check your code against that in the repository.&lt;/p&gt;

&lt;p&gt;Once you unzip the starter code, you will find the following project files as shown below in &lt;strong&gt;Figure 4&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ljgjfb1jqvczg7g9v8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ljgjfb1jqvczg7g9v8w.png" alt="Screenshot of project files"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 4 Project Files as Displayed in Visual Studio Code&lt;/center&gt;
&lt;/h6&gt;



&lt;p&gt;&lt;a id="briefTour"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;A Brief Tour of the Starter Code&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;The starter code includes a &lt;strong&gt;css* folder with a CSS file—&lt;/strong&gt;call-demo.css**—a file that provides the styling for the project.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;js&lt;/strong&gt; folder is a starter &lt;strong&gt;app.js&lt;/strong&gt; JavaScript file, which will eventually contain all the logic for the tutorial.&lt;/p&gt;

&lt;p&gt;Now that all our starter files are in place, we can dive into the project.&lt;/p&gt;

&lt;p&gt;&lt;a id="declareObjects"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 2. Declaring Objects to Reference the Desired HTML Elements&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;In order to add content to an HTML page dynamically, you must identify and capture the elements that you wish to change or augment in a JavaScript DOM object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 5&lt;/strong&gt; below shows the relevant HTML that is part of your starter code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faohtqcdr03h15obpcnan.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faohtqcdr03h15obpcnan.png" alt="HTML Listing"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 5 HTML Listing—Elements to be Referenced
&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;strong&gt;Figure 5&lt;/strong&gt; shows the &lt;strong&gt;elements&lt;/strong&gt; that need to be referenced in the DOM. To do this, we must create JavaScript objects that point to the elements (or &lt;strong&gt;nodes&lt;/strong&gt;, as they are known in the DOM).&lt;/p&gt;

&lt;p&gt;Note that every &lt;code&gt;&amp;lt;td&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; element above has an &lt;code&gt;id&lt;/code&gt; attribute. Though there are many ways to access these nodes in the DOM using JavaScript, we will approach this using the simplest and most basic method of all—that is the &lt;code&gt;getElementById()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 6&lt;/strong&gt; below shows our starter &lt;strong&gt;app.js&lt;/strong&gt; file, which will be the brain center of our project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2eolol4kl6qlrv2kzf3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2eolol4kl6qlrv2kzf3x.png" alt="Starter Code app.js"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  Fig. 6 Starter code for app.js
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
To provide access to the DOM nodes, each of the breed columns and the buttons in the button bar must be stored in DOM objects. To do this, replace &lt;strong&gt;/* YOUR CODE HERE */&lt;/strong&gt; with the code in &lt;strong&gt;Code Listing 2&lt;/strong&gt; below:&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;breedOne&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;favBreed1&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;breedOrigin1&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;origin1&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;breedAvgLife1&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;avgLife1&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;breedTwo&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;favBreed2&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;breedOrigin2&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;origin2&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;breedAvgLife2&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;avgLife2&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;breedThree&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;favBreed3&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;breedOrigin3&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;origin3&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;breedAvgLife3&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;avgLife3&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;button1&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;breed1&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;button2&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;breed2&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;button3&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;breed3&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;button4&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;reset&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;h6&gt;
  
  
  &lt;center&gt;Code Listing 2 Creating the DOM Objects&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;Now that we have created the necessary DOM objects, we can reference and manipulate them directly through JavaScript. To prove that we can gain access to any of the nodes thus captured by the DOM constants, you can use the JavaScript console of your browser to see what one of the DOM objects returns, as shown in &lt;strong&gt;Figure 7&lt;/strong&gt; below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Favaoxiy6g5yuuzzcx49q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Favaoxiy6g5yuuzzcx49q.png" alt="Console output for button4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  &lt;center&gt;Fig. 7 JavaScript Console Showing the Output of button4
&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a id="dogObjects"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 3. Create the 3 Dog Objects&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;The next leg of our journey brings us to the heart of the problem. We have created objects that reference the three tables rows that will contain information about our favorite dogs. But how do we get that information into a form that JavaScript can use to populate the rows?&lt;/p&gt;

&lt;p&gt;There are several ways to do this. For the purposes of this tutorial, we declare 3 dog objects using the object literal syntax as shown below in &lt;strong&gt;Code Listing 3&lt;/strong&gt; below:&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;breed1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PUG&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CHINA&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lifeSpan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;12-15 YEARS&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;breed1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LABRADOR&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;NEWFOUNDLAND&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lifeSpan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;10-12 YEARS&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;breed3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BEAGLE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ENGLAND&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lifeSpan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;12-15 YEARS&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;h6&gt;
  
  
  &lt;center&gt;Code Listing 3 Declaring the Dog Objects&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 3&lt;/strong&gt; shows &lt;strong&gt;breed1&lt;/strong&gt;, &lt;strong&gt;breed2&lt;/strong&gt; and &lt;strong&gt;breed3&lt;/strong&gt; objects with the properties &lt;strong&gt;breed&lt;/strong&gt;, &lt;strong&gt;origin&lt;/strong&gt; and &lt;strong&gt;lifeSpan&lt;/strong&gt; defined. You might ask why I did not declare a &lt;strong&gt;Dog&lt;/strong&gt; class and then instantiate three &lt;strong&gt;Dog&lt;/strong&gt; objects from that.&lt;/p&gt;

&lt;p&gt;While using classes is an efficient and elegant way to code the objects, the definition of individual object literals as shown above will demonstrate what the &lt;code&gt;call()&lt;/code&gt; method does. In fact, the use of classes would obviate using the &lt;code&gt;call()&lt;/code&gt; method, as properties and methods are incapsulated in one object.&lt;/p&gt;

&lt;p&gt;In another tutorial, I will show the same project refactored to use class declaration and object instantiation.&lt;/p&gt;

&lt;p&gt;You should now append the above code to the end of your &lt;strong&gt;app.js&lt;/strong&gt; starter file.&lt;/p&gt;

&lt;p&gt;We now have the raw materials we need. &lt;em&gt;But how do we extract the properties from our&lt;/em&gt; &lt;strong&gt;breed&lt;/strong&gt; _objects above and inject them, one-by-one, into the table cells defined above?&lt;/p&gt;

&lt;p&gt;For this, we will need a function that gets this information and transforms it into a form we can use to inject the desired content.&lt;/p&gt;

&lt;p&gt;&lt;a id="dogInfo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Step 4. Coding the&lt;/em&gt; getDogInfo &lt;em&gt;Object&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Now we implement the &lt;code&gt;getDogInfo&lt;/code&gt; object, whose sole property is a method called &lt;code&gt;info&lt;/code&gt;. This method extracts the properties from our &lt;strong&gt;breed&lt;/strong&gt; objects defined above, and returns an Array with the desired values from those properties. We already saw this object in &lt;strong&gt;Code Listing 1&lt;/strong&gt;, but it is reproduced here for ease in reading. &lt;strong&gt;Code Listing 4&lt;/strong&gt; below is the implementation of the &lt;code&gt;getDogInfo&lt;/code&gt; object:&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;getDogInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;info&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="c1"&gt;// Array containing properties of each dog object&lt;/span&gt;
    &lt;span class="k"&gt;return&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;breed&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;origin&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;lifeSpan&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;h6&gt;
  
  
  &lt;center&gt;Code Listing 4 The getDogInfo Object&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
In &lt;strong&gt;Code Listing 4&lt;/strong&gt;, we declare the &lt;code&gt;getDogInfo&lt;/code&gt; object. the &lt;code&gt;info()&lt;/code&gt; method returns an Array containing the &lt;strong&gt;dog&lt;/strong&gt; object properties.&lt;/p&gt;

&lt;p&gt;You should now append the code above to the end of your &lt;strong&gt;app.js&lt;/strong&gt; starter file.&lt;/p&gt;

&lt;p&gt;In order to make use of the returned Array, we will need to store the returned value of &lt;code&gt;getDogInfo&lt;/code&gt; in a variable. This is what we will do next.&lt;/p&gt;

&lt;p&gt;&lt;a id="usingCall"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 5. Using the&lt;/em&gt; &lt;code&gt;call()&lt;/code&gt; &lt;em&gt;method to specify the object whose properties we want returned as an Array&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Though the &lt;code&gt;getDogInfo&lt;/code&gt; object and its &lt;code&gt;info()&lt;/code&gt; method provide the logic to extract the object properties we need in order to inject the content on the web page, on its own, &lt;code&gt;getDogInfo&lt;/code&gt; does not have the functionality to specify from which object we wish the property values returned.&lt;/p&gt;

&lt;p&gt;Enter the &lt;code&gt;call&lt;/code&gt; method, which is a prototype method exposed by all function objects. The basic syntax employed in the variable declarations needed at this point will take the following form:&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;desiredObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's pick this statement apart to clarify what it is doing. On the left side we declare a constant called &lt;code&gt;obj&lt;/code&gt; and store to it the returned values from the &lt;code&gt;getDogInfo&lt;/code&gt; object. Using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#dot_notation" rel="noopener noreferrer"&gt;object dot notation&lt;/a&gt;, we chain the &lt;code&gt;info()&lt;/code&gt; method on &lt;code&gt;getDogInfo&lt;/code&gt;, followed by the built-in &lt;code&gt;call()&lt;/code&gt; method, which takes a single argument of the name of the object you wish referenced to &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 5&lt;/strong&gt; below shows the resulting code needed to return the desired Arrays of values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dog1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed1&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;dog2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed2&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;dog3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDogInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breed3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 5 Using the call() Method&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
You should now append the code in &lt;strong&gt;Code Listing 5&lt;/strong&gt; to the end of your &lt;strong&gt;app.js&lt;/strong&gt; starter file.&lt;/p&gt;

&lt;p&gt;Now almost everything is in place, but there are still a few critical missing pieces. The first of these is a function that will be called by the &lt;strong&gt;RESET&lt;/strong&gt; button that appears in &lt;strong&gt;Figure 1&lt;/strong&gt; at the beginning of this article. Its purpose is to return the table of dog breeds back to its original state after the other three buttons to its left have been clicked.&lt;/p&gt;

&lt;p&gt;For this, we will build a function called &lt;code&gt;resetTable()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a id="resetTable"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 6. Implementing the&lt;/em&gt; resetTable() &lt;em&gt;function&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 6&lt;/strong&gt; below shows the implementation of the &lt;code&gt;resetTable()&lt;/code&gt; function, which performs a lot of behind-the-scenes work to reset our table to its beginning state:&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;resetTable&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;noSelection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;NO BREED SELECTED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;breedOne&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;noSelection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;breedOrigin1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;breedAvgLife1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;breedTwo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;noSelection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;breedOrigin2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;breedAvgLife2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;breedThree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;noSelection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;breedOrigin3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;breedAvgLife3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;h6&gt;
  
  
  &lt;center&gt;Code Listing 6 The resetTable() Function&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;code&gt;resetTable()&lt;/code&gt; is implemented as an arrow function, but it could just as easily have used the &lt;code&gt;function myFunction() {}&lt;/code&gt; syntax that existed prior to &lt;strong&gt;ECMAScript 6&lt;/strong&gt;. Either will work fine. I happen to prefer the elegant syntax of the arrow function.&lt;/p&gt;

&lt;p&gt;A lot is going on here, but let's briefly pick this apart. You'll recall that in &lt;strong&gt;Figure 1&lt;/strong&gt;, the leftmost column of the table is filled on each row with the content &lt;strong&gt;NO BREED SELECTED&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When the &lt;strong&gt;ADD FIRST FAVORITE&lt;/strong&gt;, &lt;strong&gt;ADD SECOND FAVORITE&lt;/strong&gt; and &lt;strong&gt;ADD THIRD FAVORITE&lt;/strong&gt; buttons are clicked these entries are replaced with the name of the dog breed. Rather than individually specifying this value for each table row, it makes sense to declare a constant with the value, hence the constant &lt;code&gt;noSelection&lt;/code&gt; that appears in the above listing.&lt;/p&gt;

&lt;p&gt;As a reminder of what all the DOM objects in the above listing reference, you can refer both to &lt;strong&gt;Code Listing 2&lt;/strong&gt;, and also the matching code in your &lt;strong&gt;index.html&lt;/strong&gt; starter code.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Code Listing 6&lt;/strong&gt;, the &lt;code&gt;textContent&lt;/code&gt; property of each of the defined DOM objects for each table row is set to its original value. In the case of the &lt;strong&gt;BREED&lt;/strong&gt; column, the column is set to the value stored in the &lt;code&gt;noSelection&lt;/code&gt; constant. The remaining two columns are simply set to null, removing the content injected when the first three buttons were clicked.&lt;/p&gt;

&lt;p&gt;You should now append the code in &lt;strong&gt;Code Listing 6&lt;/strong&gt; to the end of your &lt;strong&gt;app.js&lt;/strong&gt; starter file.&lt;/p&gt;

&lt;p&gt;There is still one other critical function needed to set the content of each table row when the &lt;strong&gt;ADD FIRST FAVORITE&lt;/strong&gt; through &lt;strong&gt;ADD THIRD FAVORITE&lt;/strong&gt; buttons are clicked. This will be accomplished by the &lt;code&gt;setRowContent()&lt;/code&gt; function that will act as a &lt;strong&gt;callback function&lt;/strong&gt; to the buttons' associated event listeners, as will become evident in &lt;strong&gt;Step 8&lt;/strong&gt;. But now, on to &lt;strong&gt;Step 7&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a id="setRowContent"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 7. Implement the&lt;/em&gt; setRowContent() &lt;em&gt;function&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 7&lt;/strong&gt; below shows the implementation of the &lt;code&gt;setRowContent()&lt;/code&gt; 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;const&lt;/span&gt; &lt;span class="nx"&gt;setRowContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;column2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;column3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;breed&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;column1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nx"&gt;column2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breed&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="nx"&gt;column3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breed&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 7 The setRowContent() function&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
As you can see, the &lt;code&gt;setRowContent()&lt;/code&gt; function takes four arguments, one for each of the three columns in a table row, and a fourth to specify the object from which we will extract our needed properties. We pass these arguments to the function at runtime to populate each row of the table.&lt;/p&gt;

&lt;p&gt;We are almost done now, but there is still one critical component missing. If you click on any of the buttons, absolutely nothing will happen. We need to wire the buttons to the functionality we have thus far defined. This is done, of course, by virtue of &lt;strong&gt;event listeners&lt;/strong&gt;. We will define an event listener for the click of each button on the screen.&lt;/p&gt;

&lt;p&gt;&lt;a id="eventListeners"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 8. Implementing the event listeners&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;And now we arrive at the last step to enable the dynamic content on our web page.&lt;/p&gt;

&lt;p&gt;There are four buttons on the page, and thus we need four event listeners. The type of event for which we are listening is a &lt;code&gt;click&lt;/code&gt; event when the user clicks on any of the four buttons.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 8&lt;/strong&gt; below shows the implementation:&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;//  Button box event listeners populate&lt;/span&gt;
&lt;span class="c1"&gt;//  table with dog breed information.&lt;/span&gt;

&lt;span class="nx"&gt;button1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setRowContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dog1&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;button1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setRowContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedTwo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dog2&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;button1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setRowContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;breedThree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedOrigin3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;breedAvgLife3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;dog3&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;button4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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;resetTable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;center&gt;Code Listing 8 The event listeners&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
As shown in &lt;strong&gt;Code Listing 8&lt;/strong&gt;, an event listener is declared for each of the four buttons shown in &lt;strong&gt;Figure 1&lt;/strong&gt; at the beginning of this tutorial. In the first three event listeners, we use an &lt;a href=""&gt;anonymous function&lt;/a&gt;, which in turn calls the &lt;code&gt;setRowContent(https://www.javascripttutorial.net/javascript-anonymous-functions/)&lt;/code&gt; function implemented in &lt;strong&gt;Step 7&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Because these are &lt;code&gt;click&lt;/code&gt; events, when the user clicks on the button, the dynamic content attached to that button will be injected into the table rows and cells specified in the function call.&lt;/p&gt;

&lt;p&gt;Finally, in the event listener for &lt;strong&gt;button4&lt;/strong&gt;, we use a callback function—namely the &lt;code&gt;resetTable()&lt;/code&gt; function defined earlier in &lt;strong&gt;Step 6&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our project is now complete. If you want to see the project in action, you can find it on &lt;a href="https://codepen.io/RHieger/pen/jOyYVKP" rel="noopener noreferrer"&gt;Codepen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a id="finalWord"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  A Final Word
&lt;/h3&gt;

&lt;p&gt;The end product is, of course, a bit artless, in that the user is only able to add the hard-coded properties of the three dogs included in the JavaScript code. In a real-world example, you would probably want a modal dialogue box to pop up asking a user to input the data for the favorite dog breed of choice.&lt;/p&gt;

&lt;p&gt;You would almost certainly not want a pre-determined number of rows in the table, but only add the rows to the table dynamically as a user enters choices. I plan to build this more elaborate application and post a tutorial for it in the near future.&lt;/p&gt;

&lt;p&gt;The purpose of this tutorial, however, is to provide a working, and at least somewhat meaningful example of how the &lt;code&gt;call()&lt;/code&gt; function, exposed by the built-in JavaScript function object, works. I am open to any suggestions for future articles or projects.&lt;/p&gt;

&lt;p&gt;I hope you have found this tutorial enjoyable.&lt;/p&gt;




&lt;p&gt;&lt;a id="worksCited"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;center&gt;Works Cited&lt;/center&gt;

&lt;p&gt;"this - JavaScript | MDN", _Mozilla Developer Network, Mozilla Foundation,&lt;br&gt;        25 Sept. 2023, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/&lt;br&gt;JavaScript/Reference/Operators/this&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;"Property Accessors - JavaScript | MDN." &lt;em&gt;Mozilla Developer Network&lt;/em&gt;,&lt;br&gt;        Mozilla Foundation, 6 Sept. 2023, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#dot_notation" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#dot_notation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;"JavaScript Anonymous Functions." &lt;em&gt;JavaScript Tutorial&lt;/em&gt;,&lt;br&gt;         &lt;a href="https://www.javascripttutorial.net/javascript-anonymous-functions/" rel="noopener noreferrer"&gt;https://www.javascripttutorial.net/javascript-anonymous-functions/&lt;/a&gt;.&lt;br&gt;        Accessed 22 Oct. 2023.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Regular Expressions—a Rite of Passage: Completing the Application</title>
      <dc:creator>Robert Hieger</dc:creator>
      <pubDate>Tue, 28 Feb 2023 01:04:19 +0000</pubDate>
      <link>https://forem.com/rhieger/regular-expressions-a-rite-of-passage-completing-the-application-6kj</link>
      <guid>https://forem.com/rhieger/regular-expressions-a-rite-of-passage-completing-the-application-6kj</guid>
      <description>&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%2F18l7uah4jdiyqik5wbge.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%2F18l7uah4jdiyqik5wbge.png" alt="JavaScript in Plain English logo" width="325" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;This article was first published on &lt;a href="https://javascript.plainenglish.io" rel="noopener noreferrer"&gt;JavaScript in Plain English&lt;/a&gt;.&lt;/h6&gt;




&lt;p&gt;&lt;b&gt;Part 3: Building from Infrastructure to Completed Application&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvomi2lenwle8l86fdwk.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%2Fnvomi2lenwle8l86fdwk.jpg" alt="Woman with Computer Code Superimposed" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Photo by &lt;a href="https://unsplash.com/@thisisengineering?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;ThisisEngineering RAEng&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/software?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;
&lt;/center&gt;&lt;/h6&gt;




&lt;h2&gt;Table of Contents&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;WELCOME BACK!&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Completing the Application
&lt;/h4&gt;

&lt;h4&gt;
  
  
       &lt;em&gt;Step 7. Coding the Callback Functions&lt;/em&gt;
&lt;/h4&gt;

&lt;h5&gt;
  
  
          &lt;em&gt;The populateResultBox() Function&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
          &lt;em&gt;The depopulateResultBox() Function&lt;/em&gt;
&lt;/h5&gt;

&lt;h4&gt;
  
  
       &lt;em&gt;Step 8. Coding the Event Listeners&lt;/em&gt;
&lt;/h4&gt;

&lt;h5&gt;
  
  
          &lt;em&gt;Add an Event Listener to the Validate Zip Codes Button&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
          &lt;em&gt;Add an Event Listener to the Reset Button&lt;/em&gt;
&lt;/h5&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Conclusion&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Next Steps
&lt;/h4&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;




&lt;h2&gt;
  
  
  &lt;em&gt;WELCOME BACK!&lt;/em&gt;&lt;a id="welcome"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Our moment has arrived. We are now ready to finish our application started in &lt;strong&gt;&lt;em&gt;Regular Expressions—a Rite of Passage: From Theory to Practice&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Completing the Application&lt;/em&gt;&lt;a id="completeApp"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We left off at Step 6 in Part 2 of this tutorial, in which we built out the infrastructure of our application, providing all necessary data structures and helper functions.&lt;/p&gt;

&lt;p&gt;Two steps remain to complete the application:&lt;/p&gt;

&lt;p&gt;     1. We need to code the callback functions that will be used by the two&lt;br&gt;         event listeners mentioned in Part 2.&lt;/p&gt;


&lt;p&gt;     2. We need to write the event listeners that will call the callback functions&lt;br&gt;         in order to carry out their work.&lt;/p&gt;





&lt;h3&gt;&lt;center&gt;Step 7. Coding the Callback Functions&lt;/center&gt;&lt;/h3&gt;
&lt;a id="codeCallback"&gt;&lt;/a&gt;

&lt;p&gt;Let’s think for a moment about what these callback functions need to accomplish. We know that they are attached as arguments to two event listeners, one listening for a &lt;strong&gt;click event&lt;/strong&gt; on the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button and the other listening for a click event on the &lt;strong&gt;Reset&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;We will be picking up exactly where you left off in your code at the end of&lt;br&gt;Part 2.&lt;/p&gt;

&lt;p&gt;For the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button, we need a callback function that will swap contents with the default message of &lt;strong&gt;NO MATCH&lt;/strong&gt; and populate its parent instead with a &lt;strong&gt;scrolling window&lt;/strong&gt; of valid zip code matches.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;center&gt;The &lt;code&gt;populateResultBox()&lt;/code&gt; Function&lt;/center&gt;
&lt;a id="populateResult"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Search in the starter code&lt;/strong&gt; for the line that reads:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Code populateResultBox() callback function.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with the following lines of 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;populateResultBox&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="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;placeHolder&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&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="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;custom&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;scrollbar&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;createParagraphText&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// Create paragraphs and set textContent only&lt;/span&gt;
  &lt;span class="c1"&gt;// if not already created. Prevents doubling&lt;/span&gt;
  &lt;span class="c1"&gt;// of result data if old paragraphs have not&lt;/span&gt;
  &lt;span class="c1"&gt;// yet been garbage collected.&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;paragraphs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;createParagraphs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Append all result paragraphs to resultBox.&lt;/span&gt;

  &lt;span class="nf"&gt;appendParagraphs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;paragraphs&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;h6&gt;&lt;center&gt;Code Listing 1. The populateResultBox() Callback Function&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;A lot is happening in this function, though much of it has been broken down in our discussion of the helper functions. Nonetheless, there are still a few remaining new concepts here.&lt;/p&gt;

&lt;p&gt;The first three lines of code are manipulation of the DOM as we have not seen before in this tutorial. To fully understand these three lines, we will need to look at some code snippets from our HTML and CSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 2&lt;/strong&gt; below shows the HTML element to which &lt;strong&gt;resultBox&lt;/strong&gt; refers:&lt;/p&gt;

&lt;p&gt;&amp;lt;div id="results"&lt;br&gt;
class="results-data-dimensions default-result"&amp;gt;&lt;br&gt;
  &amp;lt;p class="default-message" id="message"&amp;gt;no match&amp;lt;/p&amp;gt;&lt;br&gt;
  &amp;lt;!-- Dynamic content lives here. --&amp;gt;&lt;br&gt;
&amp;lt;/div&amp;gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Code Listing 2. HTML Elements Captured by resultBox&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;You might recall from Part 2 a code listing that started with the element&lt;br&gt;&lt;code&gt;&amp;lt;div id= “getResults”&amp;gt;&lt;/code&gt;, which contains the entire widget with the two buttons and the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; element with the &lt;strong&gt;NO MATCH&lt;/strong&gt; message shown in&lt;br&gt;&lt;strong&gt;Code Listing 2&lt;/strong&gt; above.&lt;/p&gt;

&lt;p&gt;We need to take a brief dip into the &lt;strong&gt;CSS&lt;/strong&gt; code to understand fully what is being changed by &lt;code&gt;populateResultBox()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Take a look at &lt;strong&gt;Code Listing 3&lt;/strong&gt; below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.default-result&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#808080&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;h6&gt;&lt;center&gt;Code Listing 3. CSS Class Controlling resultBox Background Color&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;As you can see, the &lt;code&gt;.default-result&lt;/code&gt; class specifies a gray background. As this background is part of a placeholder for the results about to populate &lt;code&gt;resultBox&lt;/code&gt;, we want to clear it.&lt;/p&gt;

&lt;p&gt;Now let’s look at the first line of code in the &lt;code&gt;populateResultBox()&lt;/code&gt; 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;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;As we know, the &lt;code&gt;resultBox&lt;/code&gt; object captures &lt;code&gt;&amp;lt;div id=“results”&amp;gt;&lt;/code&gt; shown in &lt;strong&gt;Code Listing 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Document Object Model (DOM)&lt;/strong&gt;, HTML elements have the &lt;code&gt;classList&lt;/code&gt; property, which keeps track of all CSS classes associated with them.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;remove()&lt;/code&gt; method chained to the classList property does exactly what it says—it removes the class passed to it from the classList. This does &lt;em&gt;not&lt;/em&gt; mean the class is deleted from the linked CSS file. What it &lt;em&gt;does&lt;/em&gt; mean is that the web page displayed in your browser will no longer show a gray background in the scrolling window of results.&lt;/p&gt;

&lt;p&gt;Now let’s review our second line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;placeHolder&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;This line of code is concerned with nodes in the &lt;strong&gt;DOM tree&lt;/strong&gt;. As you&lt;br&gt;
might recall from earlier, &lt;code&gt;placeHolder&lt;/code&gt; is the DOM node that captures&lt;br&gt;
&lt;code&gt;&amp;lt;p class=“default-message” id=“message”&amp;gt;no match&amp;lt;/p&amp;gt;.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This paragraph is the &lt;strong&gt;NO MATCH&lt;/strong&gt; message shown on the opening screen of our application. Obviously, we need to remove this from our display.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;removeChild()&lt;/code&gt; method removes a &lt;strong&gt;child node&lt;/strong&gt; from a &lt;strong&gt;parent node&lt;/strong&gt;. &lt;code&gt;placeHolder&lt;/code&gt; is a child node of &lt;code&gt;resultBox&lt;/code&gt;. As in the case of the &lt;code&gt;remove()&lt;/code&gt; method above, &lt;code&gt;removeChild()&lt;/code&gt; leaves the paragraph in the HTML untouched. It is merely removed from our live display.&lt;/p&gt;

&lt;p&gt;Next let’s review our third line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&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="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;custom&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;scrollbar&lt;/span&gt;&lt;span class="err"&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 are doing something quite different. Here, we add a CSS class&lt;br&gt;to &lt;code&gt;reasultBox&lt;/code&gt;. &lt;strong&gt;Code Listing 4&lt;/strong&gt; below shows the code for CSS class &lt;code&gt;.custom-scrollbar&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.custom-scrollbar&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--scrollbar-foreground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#a239ca&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--scrollbar-background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;162&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;57&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;202&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c"&gt;/* Foreground and Background Colors */&lt;/span&gt;
  &lt;span class="py"&gt;scrollbar-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--scrollbar-foreground&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--scrollbar-background&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;h6&gt;&lt;center&gt;Code Listing 4. CSS Class Controlling Scrollbar Styling for resultBox&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;Without diving deeply into the above code, all we need to know is that this class sets foreground and background styling for the scrollbar in &lt;code&gt;resultBox&lt;/code&gt; once it has been populated. For those who are interested in delving deeper into the styling, the above style was informed by the work of &lt;em&gt;CSS Tricks&lt;/em&gt; (Coyier, &lt;em&gt;“Using Custom Properties for Styling Scrollbars in CSS”&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;add()&lt;/code&gt; method chained to the &lt;code&gt;classList&lt;/code&gt; property, in contrast to the &lt;code&gt;remove()&lt;/code&gt; method we saw moments ago, adds a CSS class to &lt;code&gt;resultBox&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The remaining code in the &lt;code&gt;populateResultBox()&lt;/code&gt; callback function has already been covered in &lt;strong&gt;Part 2, Step 6&lt;/strong&gt; of this tutorial.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;center&gt;The &lt;code&gt;depopulateResultBox()&lt;/code&gt; Function&lt;/center&gt;
&lt;a id="depopulateResult"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now we can turn our attention to coding the &lt;code&gt;depopulateResultBox()&lt;/code&gt; callback function. As you might guess, the purpose of this function is to remove the paragraphs that populated &lt;code&gt;resultBox&lt;/code&gt; and restore the default message and styling associated therewith.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Search in the starter code&lt;/strong&gt; for the line that reads:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Code depopulateResultBox() callback function.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with the following lines of 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;depopulateResultBox&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;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstChild&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstChild&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;custom&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;scrollbar&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&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="err"&gt;‘&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;resultBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;placeholder&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;h6&gt;&lt;center&gt;Code Listing 5. The depopulateResultBox() Function&lt;/center&gt;&lt;/h6&gt;

&lt;p&gt;We've met every method seen in this function when we coded the &lt;code&gt;depopulateResultBox()&lt;/code&gt; helper function above. So here is a quick breakdown of what is happening in this code:&lt;/p&gt;

&lt;p&gt;     1. We start with a while loop that takes a single argument—&lt;br&gt;         &lt;code&gt;resultBox.firstChild&lt;/code&gt;. We already know that resultBox is a&lt;br&gt;         DOM node. As such it exposes properties and methods. The          &lt;code&gt;firstChild&lt;/code&gt; property captures the first child of resultBox,&lt;br&gt;         which would be the first paragraph in resultBox.&lt;/p&gt;

&lt;p&gt;     2. The argument in this while loop  is a Boolean that evaluates to&lt;br&gt;          true if there is a &lt;code&gt;firstChild&lt;/code&gt; paragraph node in resultBox. Each&lt;br&gt;          time through the loop,&lt;/p&gt;

&lt;p&gt;         &lt;code&gt;resultBox.removeChild(resultBox.firstChild);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;         is executed, it removes one paragraph. On each iteration of the&lt;br&gt;         loop the next paragraph in line becomes the firstChild. Once&lt;br&gt;         there is no &lt;code&gt;firstChild&lt;/code&gt; left, the loop terminates because no&lt;br&gt;         paragraphs remain.&lt;/p&gt;

&lt;p&gt;     3. Once all paragraphs have been removed the three remaining&lt;br&gt;          lines of code outside of the while loop remove the&lt;br&gt;         &lt;code&gt;.custom-scrollbar&lt;/code&gt; class, thus restoring the gray background,&lt;br&gt;         and finally uses the appendChild() method to restore the&lt;br&gt;         &lt;strong&gt;NO MATCH&lt;/strong&gt; message.&lt;/p&gt;

&lt;p&gt;We have now completed our callback functions and are ready to proceed&lt;br&gt;to the last step—coding the event listeners for the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt;&lt;br&gt;and &lt;strong&gt;Reset&lt;/strong&gt; buttons.&lt;/p&gt;





&lt;h3&gt;&lt;center&gt;Step 8. Coding the Event Listeners&lt;/center&gt;&lt;/h3&gt;
&lt;a id="eventListeners"&gt;&lt;/a&gt;

&lt;p&gt;Now we approach the ultimate step in completing our application—that of coding the event listeners&lt;br&gt;
for our buttons.&lt;/p&gt;

&lt;p&gt;To start, I would like to recall two declared node objects that we created at the beginning of &lt;strong&gt;&lt;em&gt;Part 2, Regular Expressions—a Rite of Passage: From Theory to Practice&lt;/em&gt;&lt;/strong&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;resultButton&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;validate&lt;/span&gt;&lt;span class="err"&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;resetButton&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;reset&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;&lt;center&gt;Code Listing 6. Nodes in Need of Event Listeners&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;These two lines of code are our starting point as we will need them to build our event listeners.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;resultButton&lt;/code&gt; is the DOM node that captures the&lt;br&gt;&lt;code&gt;&amp;lt;button class=“controlButton” id=“validate”&amp;gt;&lt;/code&gt;  element in our &lt;strong&gt;index.html&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;resetButton&lt;/code&gt; is the DOM node that captures the&lt;br&gt;&lt;code&gt;&amp;lt;button class=“controlButton” id=“reset”&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;With this new information we can begin.&lt;/p&gt;




&lt;h3&gt;
  
  
  Add an Event Listener to the Validate Zip Codes Button&lt;a id="validateZip"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Search in the starter code&lt;/strong&gt; for the line that reads:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Code resultButton event listener.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with the following line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;resultButton&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;populateResultBox&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Using the &lt;code&gt;resultButton&lt;/code&gt; DOM node, we chain the &lt;code&gt;addEventListener()&lt;/code&gt; method to it. This method takes two arguments, as you see.&lt;/p&gt;

&lt;p&gt;The first specifies what type of event to be listened for (there are a number of different kinds). Ours is a &lt;strong&gt;click event&lt;/strong&gt;, which means that JavaScript will listen for any mouse clicks on &lt;code&gt;resultButton&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second is the callback function  to be executed when the click event is detected. The callback function can be an anonymous function included as the second argument. In our case, the callback function is &lt;code&gt;populateResultBox()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With this event listener in place, we should now be able to populate the &lt;code&gt;resultBox&lt;/code&gt; with valid zip code matches when we click the &lt;strong&gt;Validate Zip Codes button&lt;/strong&gt;. Let’s test this.&lt;/p&gt;

&lt;p&gt;If you have not already done so, open the &lt;strong&gt;index.html&lt;/strong&gt; file in your favorite browser. If you have the file open already, make sure to refresh your browser.&lt;/p&gt;

&lt;p&gt;Click on the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;Your screen should look much like that shown below in &lt;strong&gt;Figure 1&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe3y2c7ob9pz5ezr1x30f.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%2Fe3y2c7ob9pz5ezr1x30f.png" alt="Fig. 1. resultBox with Scrollbar and Populated with Valid Zip Codes" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 1. resultBox with Scrollbar and Populated with Valid Zip Codes&lt;/center&gt;&lt;/h6&gt;




&lt;h3&gt;
  
  
  Add an Event Listener to the Reset Button&lt;a id="reset"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now let’s code the event listener for the Reset button.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Search in the starter code&lt;/strong&gt; for the line that reads:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: CODE resetButton event listener.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with the following line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;resetButton&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;depopulateResultBox&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;This event listener also listens for a click event, this time on our &lt;strong&gt;Reset&lt;/strong&gt; button, and calls the &lt;code&gt;depopulateResultBox()&lt;/code&gt; callback function to reset our page to its beginning state.&lt;/p&gt;

&lt;p&gt;To test this out, refresh your browser. Now click the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button. Once you see that the resultBox has been populated with the zip code matches, click on the &lt;strong&gt;Reset&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;If all goes according to plan, your screen should reset to its initial state with the message &lt;strong&gt;NO MATCH&lt;/strong&gt; displayed where the scrolling list of zip code matches were.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;em&gt;Conclusion&lt;/em&gt;&lt;a id="conclusion"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;CONGRATULATIONS!&lt;/em&gt;&lt;/strong&gt; You have completed the demo zip code validation Single Page Application (SPA).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;If your code is not working and you are not sure why&lt;/em&gt;&lt;/strong&gt;, &lt;em&gt;you can download my completed code and compare yours with it to find any errors.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can also view the live application here on &lt;a href="https://codepen.io/RHieger/full/yLvLJPO" rel="noopener noreferrer"&gt;codepen.io&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Next Steps&lt;a id="next"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now that you have a functional demo web app that returns valid zip codes from a test string, &lt;strong&gt;&lt;em&gt;where might you go from here?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Play around with the finished code you have. Here are a couple suggestions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Experiment with further de-coupling of helper functions from the data upon which they operate&lt;/strong&gt;, as in my example of the &lt;code&gt;createParagraphs()&lt;/code&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Think about how you might make this a truly interactive application&lt;/strong&gt; in which a user would simply input a test string and click on the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button to get the result(s).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, feel free to fork [my repository(&lt;a href="https://github.com/RHieger/regex-zip-code-tutorial" rel="noopener noreferrer"&gt;https://github.com/RHieger/regex-zip-code-tutorial&lt;/a&gt;) and create your own version.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Happy coding!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
                                                    References&lt;a id="references"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Coyier, Chris. &lt;em&gt;“The Current State of Styling Scrollbars in CSS (2022&lt;br&gt;     Update): CSS-Tricks.”&lt;/em&gt; CSS Tricks, 21 Feb. 2022,&lt;br&gt;     &lt;a href="https://css-tricks.com/the-current-state-of-styling-scrollbars-in-css/#aa-using-custom-properties-for-styling-scrollbars-in-css" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://css-tricks.com/the-current-state-of-styling-scrollbars-in-css/#aa-using-custom-properties-for-styling-scrollbars-in-css" rel="noopener noreferrer"&gt;https://css-tricks.com/the-current-state-of-styling-scrollbars-in-css/#aa-using-custom-properties-for-styling-scrollbars-in-css&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>leadership</category>
      <category>discuss</category>
      <category>productivity</category>
      <category>career</category>
    </item>
    <item>
      <title>Regular Expressions—a Rite of Passage: From Theory to Practice</title>
      <dc:creator>Robert Hieger</dc:creator>
      <pubDate>Sun, 05 Feb 2023 00:34:39 +0000</pubDate>
      <link>https://forem.com/rhieger/regular-expressions-a-rite-of-passage-from-theory-to-practice-2ii5</link>
      <guid>https://forem.com/rhieger/regular-expressions-a-rite-of-passage-from-theory-to-practice-2ii5</guid>
      <description>&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%2Fufpuszxu8iarolp0ytwo.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%2Fufpuszxu8iarolp0ytwo.png" alt="JavaScript in Plain English logo" width="325" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;This article was first published on &lt;a href="https://javascript.plainenglish.io" rel="noopener noreferrer"&gt;JavaScript in Plain English&lt;/a&gt;.&lt;/h6&gt;

&lt;p&gt;&lt;b&gt;Part 2: Applying the theory to build a demo single-page app that validates zip codes.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyc02qzncu9hrkeydpaek.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%2Fyc02qzncu9hrkeydpaek.jpg" alt="Photo of woman's hands doing system design sketch" width="758" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Photo by Kelly Sikkema on Unsplash&lt;/center&gt;&lt;/h6&gt;




&lt;h2&gt;Table of Contents&lt;/h2&gt;




&lt;h4&gt;
  
  
  &lt;em&gt;WELCOME BACK!&lt;/em&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Getting Started
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Let's Build It!
&lt;/h4&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Step 1. Unzip the Starter Code Archive&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Step 2. Putting the Project Pieces Together&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Step 3. Capture Necessary DOM Nodes&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
            &lt;em&gt;Verify that Nodes Are Visible to the DOM&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Step 4. Declare the Test String and Regular Expression&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Step 5. Use Regex to Return the Valid Zip Code Matches&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Step 6. Creating the Helper Functions&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
          &lt;em&gt;The createParagraphText() Helper Function&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
          &lt;em&gt;The createParagraphs() Helper Function&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
          &lt;em&gt;The appendParagraphs() Helper Function&lt;/em&gt;
&lt;/h5&gt;

&lt;h4&gt;
  
  
  What's Next?
&lt;/h4&gt;




&lt;p&gt;&lt;a id="welcome"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;WELCOME BACK!&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;For those of you who have read the first part in this tutorial series, thanks for hanging in through a theoretical article. It will pay off as we proceed from theory to practice.&lt;/p&gt;

&lt;p&gt;In Part 2 we apply what we learned in Part 1, &lt;strong&gt;&lt;em&gt;Regular Expressions—a Rite of Passage for Web Developers&lt;/em&gt;&lt;/strong&gt;, which gave a quick introduction to some key concepts of regular expressions.&lt;/p&gt;

&lt;p&gt;We will transform theory into practice by building a demo &lt;strong&gt;Single Page Application (SPA)&lt;/strong&gt; for the web.&lt;/p&gt;




&lt;h3&gt;
  
  
  Getting Started &lt;a id="getStarted"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;For those who have not already seen what our SPA will look like, &lt;strong&gt;Figure 1&lt;/strong&gt; below shows the landing page:&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%2Fbgv6xhc152pukn67cdj9.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%2Fbgv6xhc152pukn67cdj9.png" alt="Fig. 1 Application Landing Page" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 1 Application Landing Page&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;The opening screen above presents the following components:&lt;/p&gt;

&lt;p&gt;     1. The main heading, which states what the application does—namely,&lt;br&gt;          matching valid zip codes.&lt;/p&gt;

&lt;p&gt;     2. Immediately below this, a callout box explains the aims of the&lt;br&gt;          application in more detail.&lt;/p&gt;

&lt;p&gt;     3. Next is a scrolling list of the multiline test string that will be passed to&lt;br&gt;          the regular expression shown below it.&lt;/p&gt;

&lt;p&gt;     4. Beneath this we have a &lt;strong&gt;widget&lt;/strong&gt; that, on the left side, has a group of 2&lt;br&gt;          vertically-stacked buttons. The top one, &lt;strong&gt;Validate Zip Codes&lt;/strong&gt;, triggers&lt;br&gt;          an event that matches and extracts valid zip codes from the test&lt;br&gt;          string. When clicked, the window to its right is populated with valid zip&lt;br&gt;          codes returned by the regular expression.&lt;/p&gt;

&lt;p&gt;     5. The second button—&lt;strong&gt;Reset&lt;/strong&gt;—resets the text box to its default state as&lt;br&gt;          seen in &lt;strong&gt;Figure 1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;How are we going to do all of this?&lt;/em&gt;&lt;/strong&gt; Let’s think about this for a second.&lt;/p&gt;

&lt;p&gt;We provide some pre-fabricated test data to our application (though the principle would remain the same if a user were to upload a file with test data). In either case, the &lt;strong&gt;regex engine&lt;/strong&gt; is presented with the test data and asked to return matches only for valid zip codes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is more complicated than it sounds&lt;/strong&gt;. The regular expression specified to the regex engine must be able to do all of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make certain that user input consists only of numbers and optionally a hyphen for +4 zip codes. (Obviously in the case of this demo application, there is no user input. But further development of this project could make the application fully interactive.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No alphabetical, punctuation, or other special symbols—save the hyphen needed in a +4 zip code—may be part of the returned match.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make certain that no other characters precede or follow either variety of zip codes (5-digit or +4).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In every case, the zip code must start with a sequence of 5 digits.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optionally, the regex engine must be able to match a 5-character sequence followed by a hyphen and ending with a sequence of 4 numbers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, no characters must occur after the last 4 digits.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All this seems a tall order. But as we saw in the previous tutorial, &lt;strong&gt;regular expressions&lt;/strong&gt; are more than capable of fulfilling all these requirements.&lt;br&gt;
 &lt;br&gt;
&lt;strong&gt;&lt;em&gt;The moment has finally arrived!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Let’s Build It! &lt;a id="build"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;After all this theory, let’s put it into practice and build this application.&lt;/p&gt;

&lt;p&gt;To start, please download the &lt;a href="https://github.com/RHieger/regex-zip-code-tutorial/raw/starter-code/regex-starter-code.zip" rel="noopener noreferrer"&gt;starter code&lt;/a&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 1. Unzip the Starter Code Archive&lt;/em&gt; &lt;a id="starterCode"&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;The archive is named &lt;strong&gt;regex-starter-code.zip&lt;/strong&gt; You can rename it anything more to your liking, such as &lt;strong&gt;regex-demo-web-app.zip&lt;/strong&gt;. Be sure to keep the &lt;strong&gt;.zip&lt;/strong&gt; extension in the name as some zip archive programs might not recognize the file if the extension is missing.&lt;/p&gt;

&lt;p&gt;For purposes of this tutorial, I use &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Microsoft Visual Studio Code&lt;/a&gt;, but you can also use your favorite text editor.&lt;/p&gt;

&lt;p&gt;Once you have unzipped the renamed &lt;strong&gt;regex-demo-web-app.zip&lt;/strong&gt; (or whatever name you choose), you will have a file structure similar to what is shown below in &lt;strong&gt;Figure 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zashaxzsg4a2y2idyyz.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%2F9zashaxzsg4a2y2idyyz.png" alt="Fig. 2 Project File Structure" width="684" height="913"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 2 Project File Structure&lt;/center&gt;&lt;/h6&gt;


&lt;h4&gt;
  
  
  &lt;em&gt;Step 2. Putting the Project Pieces Together&lt;/em&gt;&lt;a id="projectPieces"&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Our project is comprised of &lt;strong&gt;HTML&lt;/strong&gt; content, &lt;strong&gt;CSS&lt;/strong&gt; styling, and a single &lt;strong&gt;JavaScript&lt;/strong&gt; file—&lt;strong&gt;app.js&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Though I have provided styling for you so that we can focus primarily on &lt;strong&gt;JavaScript&lt;/strong&gt;, we will start at the bare-bones structure provided by the &lt;strong&gt;index.html&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Let’s start by opening the application’s &lt;strong&gt;index.html&lt;/strong&gt; file in your favorite&lt;br&gt;web browser. For purposes of this tutorial, I use &lt;strong&gt;Google Chrome&lt;/strong&gt; for&lt;br&gt;its superb developer tools. &lt;strong&gt;Figure 3&lt;/strong&gt; below shows our barebones&lt;br&gt;application framework.&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%2F18lnch1z38csdvejz4o2.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%2F18lnch1z38csdvejz4o2.png" alt="Fig 3. Bare Bones HTML" width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 3. Bare Bones HTML&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;&lt;strong&gt;Figure 3&lt;/strong&gt; reveals the barebones structure of the document in which our application resides. Not all that comprehensible. Let’s fix that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Listing 1&lt;/strong&gt; below shows a fragment of our &lt;strong&gt;index.html&lt;/strong&gt; file.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- INSERT link to zipcodes.css stylesheet. --&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Regex Demo-Zip Code&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- INSERT app.js JavaScript --&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;h6&gt;&lt;center&gt;Code Listing 1. index.html Showing Relevant &amp;lt;head&amp;gt; Content&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;We will replace the comment lines now with the code that will knit together the three components of our application.&lt;/p&gt;

&lt;p&gt;As you know, in order for your browser to render the application landing page as we wish, we have to link our &lt;strong&gt;cascading stylesheet&lt;/strong&gt; to the &lt;strong&gt;HTML&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;We also must link our &lt;strong&gt;JavaScript&lt;/strong&gt; code to the HTML file. Follow these steps:&lt;/p&gt;

&lt;p&gt;     1. Open &lt;strong&gt;index.html&lt;/strong&gt; if you have not already done so.&lt;br&gt;&lt;br&gt;
     2. Search for the first comment in &lt;strong&gt;Listing 1&lt;/strong&gt; above and replace it with the&lt;br&gt;          following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link rel=“stylesheet” href=“./css/zipcodes.css”&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;     3. Now we integrate the last component of our project. Search for the&lt;br&gt;          second comment in  &lt;strong&gt;Code Listing 1&lt;/strong&gt; above and replace it with the&lt;br&gt;          following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src=“./js/app.js” defer&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The keyword &lt;strong&gt;defer&lt;/strong&gt; in the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag assures that the browser loads the DOM for the index.html document before script execution begins. This prevents JavaScript from throwing errors because a DOM object references a node that has not yet been defined.&lt;/p&gt;

&lt;p&gt;Remember to save both these changes in your &lt;strong&gt;index.html&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;     4. Refresh your browser.&lt;br&gt;
 &lt;br&gt;
Great! If all goes according to plan, your web page should appear exactly as in &lt;strong&gt;Figure 1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However, if you click on either of the buttons, &lt;em&gt;absolutely nothing happens at this point.&lt;/em&gt; Obviously, this is because we have yet to write any of the code that provides functionality to these buttons.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Step 3. Capture Necessary DOM Nodes&lt;/em&gt;&lt;a id="captureNodes"&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Start by opening the &lt;strong&gt;app.js&lt;/strong&gt; file included in the starter code archive.&lt;/p&gt;

&lt;p&gt;At this point, the entire file consists of nothing more than comments. We start by capturing the necessary &lt;strong&gt;DOM&lt;/strong&gt; nodes. We will use the &lt;strong&gt;Document Object Model (DOM)&lt;/strong&gt; to pinpoint the parts of our screen that will need to change when the user interacts with our application.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Which of the HTML elements need to be captured in DOM Objects?&lt;/em&gt; First, we can reason that because nothing happens when we click on either button in our application, we need to gain access to those nodes. Thus, we need 2 DOM objects to capture the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; and &lt;strong&gt;Reset&lt;/strong&gt; buttons.&lt;br&gt;
Next we need two things to happen. We need the default message to the right of the buttons  that now reads &lt;strong&gt;NO MATCH&lt;/strong&gt; to disappear and be replaced with a scrolling list of matches returned by the regex engine.&lt;/p&gt;

&lt;p&gt;In order to do this, we need to be able to capture the default message so that it may be removed. We also need access to its parent element, which will contain the scrolling list once the default message is removed. Thus we need 2 DOM objects to capture these elements.&lt;/p&gt;

&lt;p&gt;In total, we must declare 4 DOM objects to capture the nodes that reference the HTML elements to which we need access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 4&lt;/strong&gt; below shows the relevant lines of HTML containing the elements we will capture in DOM objects:&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%2Fjbg3g7kw9qgd5p456v72.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%2Fjbg3g7kw9qgd5p456v72.png" alt="Fig. 4 Elements to Be Captured in the DOM" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 4 Elements to Be Captured in the DOM&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;The critical pieces of markup in &lt;strong&gt;Figure 4&lt;/strong&gt; are the buttons, the paragraph reading “no match,” and its parent element—the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; with an &lt;strong&gt;id&lt;/strong&gt; attribute of &lt;strong&gt;“results.”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each element has an &lt;strong&gt;id property&lt;/strong&gt; set on it so that it may be captured using the &lt;code&gt;document.getElementById()&lt;/code&gt; method. There are other means that could be employed, such as the &lt;code&gt;querySelector()&lt;/code&gt; and &lt;code&gt;querySelectorAll()&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;For this tutorial, I have chosen to use &lt;code&gt;getElementById()&lt;/code&gt; for its directness and simplicity.&lt;/p&gt;

&lt;p&gt;To create the necessary DOM objects, &lt;strong&gt;search in your starter code&lt;/strong&gt; for the line  that reads &lt;code&gt;// TO BE DONE: Declare necessary DOM nodes&lt;/code&gt; and replace it with the following 4 lines of code:&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;resultButton&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;validate&lt;/span&gt;&lt;span class="err"&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;resetButton&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;reset&lt;/span&gt;&lt;span class="err"&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;resultBox&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="err"&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;placeHolder&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;h6&gt;&lt;center&gt;Code Listing 2: Code to Capture Required DOM Nodes&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;&lt;em&gt;Still&lt;/em&gt;, nothing will happen if you refresh your browser and click on the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button. &lt;strong&gt;We have not yet provided this functionality&lt;/strong&gt;. What we will do now is to test that the elements we captured are really captured, using the &lt;strong&gt;Google Chrome Developer Tools&lt;/strong&gt;.&lt;/p&gt;


&lt;h4&gt;
  
  
  &lt;em&gt;Verify that the Nodes Are Visible to the DOM&lt;/em&gt;&lt;a id="verifyNodes"&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;To do this, refresh your browser and open the &lt;strong&gt;JavaScript Console&lt;/strong&gt; by pressing &lt;strong&gt;Ctrl+Shift+J&lt;/strong&gt; for Windows/Linux/Unix and &lt;strong&gt;Command+Option+J&lt;/strong&gt; for Mac. You should see something like &lt;strong&gt;Figure 5&lt;/strong&gt; below:&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%2Frzt7scydmc5dsknxkdex.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%2Frzt7scydmc5dsknxkdex.png" alt="Fig. 5 Google Chrome Showing JavaScript Console on Right" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 5 Google Chrome Showing JavaScript Console on Right&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;To verify our node objects, click an insertion point in the JavaScript Console and type the following commands followed by the &lt;strong&gt;Enter/Return&lt;/strong&gt; key:&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resultButton &amp;lt;Enter&amp;gt;
resetButton &amp;lt;Enter&amp;gt;
resultBox &amp;lt;Enter&amp;gt;
placeHolder &amp;lt;Enter&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;Be sure to expand the output for &lt;strong&gt;resultBox&lt;/strong&gt; by clicking on the right-pointing arrow next to the collapsed information.&lt;/p&gt;

&lt;p&gt;You should see a result like that shown in &lt;strong&gt;Figure 6&lt;/strong&gt; below:&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%2F6h8uuj0x6n6n24hbdz9z.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%2F6h8uuj0x6n6n24hbdz9z.png" alt="Fig. 6 Browser with JavaScript Console Confirming Access to the Desired Nodes" width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 6 Browser with JavaScript Console Confirming Access to the Desired Nodes&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;As of now, we have our entry point into the application because we can access the portions of the page we need to manipulate. Next, we need to declare our test string and our regular expression.&lt;/p&gt;


&lt;h3&gt;
  
  
  Step 4. Declare the Test String and Regular Expression&lt;a id="testRegExp"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;We need to declare two &lt;strong&gt;constants&lt;/strong&gt;—one that will store the &lt;strong&gt;test string&lt;/strong&gt; we pass to our &lt;strong&gt;regular expression&lt;/strong&gt; and one that stores the &lt;strong&gt;regular expression object&lt;/strong&gt; to which the &lt;strong&gt;test string&lt;/strong&gt; will be passed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Search in your starter code&lt;/strong&gt; for the line that reads &lt;code&gt;// TBD: Declare testString&lt;/code&gt; and replace it with the following code:&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;testString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="s2"&gt;`10003
asdf10003
10003asdf
jklm10003^$@%
10003-8924
one zero zero zero three
9101-94015
94015-9101
20012
08735
KbdsD$%^&amp;amp;*
sSd070031jkl;m
70122
\sKu2034
98108
75381
asdfjkl;
60661
!9004!@#$5^&amp;amp;*
97218
#,.$$&amp;amp;&amp;amp;%@
10022-3337
eNuhfF!.`&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;h6&gt;&lt;center&gt;Code Listing 3. The Test String&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;&lt;strong&gt;Search in your starter code&lt;/strong&gt; for the line that reads &lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Declare regular expression.&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;and replace it with the following code:&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;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]{5}(&lt;/span&gt;&lt;span class="sr"&gt;-&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]{4})?&lt;/span&gt;&lt;span class="sr"&gt;$/gm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;The stage is set for our demo application. We have captured the &lt;strong&gt;DOM nodes&lt;/strong&gt; that will be necessary to inject dynamic data to the page when the user clicks on the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button. We have also provided our &lt;strong&gt;testString&lt;/strong&gt; constant and our &lt;strong&gt;regex&lt;/strong&gt; constant.&lt;/p&gt;

&lt;p&gt;Next, we have to move on to building the functionality that will provide the matches of valid zip codes that we want injected on our page.&lt;/p&gt;


&lt;h3&gt;
  
  
  Step 5. Use Regex to Return the Valid Zip Code Matches&lt;a id="matches"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Before we actually add the necessary code to &lt;strong&gt;app.js&lt;/strong&gt;, let’s take a step back to get a clear understanding of how the matching process will work.&lt;/p&gt;

&lt;p&gt;We already have our regex object that was declared above using the object literal notation. This object exposes methods that perform different kinds of searches.&lt;/p&gt;

&lt;p&gt;There are several methods exposed by the regex object. You can find them on the  Mozilla Developer Network regular expressions page (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#using_regular_expressions_in_javascript" rel="noopener noreferrer"&gt;&lt;em&gt;Using Regular Expressions in JavaScript"&lt;/em&gt;&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;What will be relevant to us, however, is the &lt;code&gt;match()&lt;/code&gt; method. The syntax for this method is:&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;regular&lt;/span&gt; &lt;span class="nx"&gt;expression&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;Our string object is the constant &lt;strong&gt;testString&lt;/strong&gt;. The &lt;code&gt;match()&lt;/code&gt; method, in turn, takes one argument, and that is the regular expression object.&lt;/p&gt;

&lt;p&gt;The return value for &lt;code&gt;match()&lt;/code&gt; is an array comprised of every match returned by the regular expression.&lt;/p&gt;

&lt;p&gt;The return value &lt;strong&gt;must be returned to something&lt;/strong&gt; in order for it to be accessible. In the syntax shown above, the object returned by the match() method is not addressable, though memory has been reserved for it.&lt;/p&gt;

&lt;p&gt;Once the method is executed JavaScript will &lt;strong&gt;garbage collect&lt;/strong&gt; the values stored therein as soon as it determines that they have not been used or are no longer in use. If you are interested in learning more about this,  you might want to consult this brief, but informative article on &lt;a href="https://javascript.info/garbage-collection" rel="noopener noreferrer"&gt;garbage collection&lt;/a&gt; (&lt;em&gt;“Garbage Collection”&lt;/em&gt;).&lt;br&gt;
The actual syntax we will employ is as shown below:&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;myConstant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;regular&lt;/span&gt; &lt;span class="nx"&gt;expression&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="o"&gt;&amp;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;strong&gt;Search in your starter code&lt;/strong&gt; for the line that reads&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Save regex match to matches variable.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with:&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;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;testString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;This one line of code accomplishes a lot. In one fell swoop, &lt;strong&gt;testString&lt;/strong&gt; is passed to &lt;strong&gt;regex&lt;/strong&gt; (our regular expression object), the pattern match is performed, valid zip codes are extracted, and the results are stored in an array with the identifier of &lt;strong&gt;matches&lt;/strong&gt;. &lt;em&gt;Very powerful stuff.&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;Now let’s test this out by taking the steps that follow:&lt;/p&gt;

&lt;p&gt;     1. Refresh your browser. If you have not already opened the JavaScript&lt;br&gt;         Console, do so now.&lt;/p&gt;

&lt;p&gt;     2. Click an insertion point in the JavaScript Console and type &lt;strong&gt;matches&lt;/strong&gt;,&lt;br&gt;         then hit the &lt;strong&gt;Enter/Return&lt;/strong&gt; Key. Once you expand the content&lt;br&gt;         returned in the console, your display should look like that shown in&lt;br&gt;         &lt;strong&gt;Figure 7&lt;/strong&gt; below:&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%2Fb5h83jb7e5pm6gygfknd.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%2Fb5h83jb7e5pm6gygfknd.png" alt="Fig. 7 Browser with JavaScript Console confirming valid zip code matches" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 7 Browser with JavaScript Console confirming valid zip code matches&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;As &lt;strong&gt;Figure 7&lt;/strong&gt; confirms there are 11 valid zip code matches returned by the &lt;strong&gt;regular expression object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Obviously, we cannot ask an end-user to enter the JavaScript Console to get the results of this validation process.&lt;/em&gt; For this to be a truly dynamic application, we have to build some infrastructure preparatory to creating two &lt;strong&gt;event listeners&lt;/strong&gt;—one that listens for a click on the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button, the other for a click on the &lt;strong&gt;Reset&lt;/strong&gt; button.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 6. Creating the Helper Functions&lt;a id="helpers"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Of what is this infrastructure comprised?&lt;/em&gt; If we start with the most obvious assumption, we need something to happen when an end-user clicks either of the buttons.&lt;/p&gt;

&lt;p&gt;Our application is &lt;strong&gt;event-driven&lt;/strong&gt;. Thus we need to code two &lt;strong&gt;event listeners&lt;/strong&gt; for the buttons. As we will see, these event listeners are powered by several &lt;strong&gt;helper functions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Before we code the first helper, we need to declare an empty array, which its code will populate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Search in your starter code&lt;/strong&gt; for the line that reads&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Declare empty array for text nodes.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with:&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;paragraphText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;The above empty array will be populated by our first &lt;strong&gt;helper function:&lt;/strong&gt;&lt;/p&gt;



&lt;p&gt;&lt;a id="createParagraphText"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;The &lt;strong&gt;createParagraphText()&lt;/strong&gt; Helper Function&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Search in your starter code&lt;/strong&gt; for the line that reads&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Code createParagraphText() helper function.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with:&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;createParagraphText&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="nx"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;matchNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;paragraphText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;` Match #&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;matchNumber&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="nx"&gt;match&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;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;h6&gt;&lt;center&gt;Code Listing 4. The createParagraphText() Helper Function&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;&lt;em&gt;What is happening in this helper function?&lt;/em&gt; Let’s break it down line by line.&lt;/p&gt;

&lt;p&gt;The code in &lt;code&gt;createParagraphText()&lt;/code&gt; is comprised of a &lt;code&gt;forEach()&lt;/code&gt; loop. &lt;code&gt;forEach()&lt;/code&gt; is exposed by the &lt;strong&gt;Array&lt;/strong&gt; object. In the code above, our array is the &lt;strong&gt;matches&lt;/strong&gt; constant.&lt;/p&gt;

&lt;p&gt;We call the &lt;code&gt;forEach()&lt;/code&gt; method on &lt;strong&gt;matches&lt;/strong&gt;. &lt;strong&gt;match&lt;/strong&gt;, which is found in the &lt;strong&gt;argument&lt;/strong&gt; portion of the method, is a reference to each element found in the &lt;strong&gt;matches&lt;/strong&gt; array, over which it loops.&lt;/p&gt;

&lt;p&gt;There is also an &lt;strong&gt;anonymous callback function&lt;/strong&gt; as the second parameter, which extracts data from the &lt;code&gt;matches[]&lt;/code&gt; array created by the &lt;code&gt;match()&lt;/code&gt; method above. In the first line of the callback function, we declare the variable &lt;strong&gt;matchNumber&lt;/strong&gt;, which stores the value of the &lt;strong&gt;index + 1&lt;/strong&gt; within the &lt;code&gt;matches[]&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;We call the &lt;code&gt;indexOf()&lt;/code&gt; method on &lt;strong&gt;match&lt;/strong&gt; to get the array index of &lt;strong&gt;match&lt;/strong&gt; in each iteration of the loop. &lt;strong&gt;1&lt;/strong&gt; is added to the index value because, as we know, arrays are &lt;strong&gt;0-indexed&lt;/strong&gt;, and for humans, it makes more sense to refer to matches &lt;strong&gt;1&lt;/strong&gt; through &lt;strong&gt;11&lt;/strong&gt;, rather than &lt;strong&gt;0&lt;/strong&gt; through &lt;strong&gt;10&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the final line of our function, we populate the &lt;code&gt;paragraphText[]&lt;/code&gt; array, using the &lt;code&gt;push()&lt;/code&gt; method exposed by the Array object.&lt;/p&gt;

&lt;p&gt;The data that we specify to be pushed to the array for each item makes use of both template literal notation and string interpolation to create an array element that looks like this:&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Match #1: 10003
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;&lt;strong&gt;You might be wondering why I didn’t create the full paragraph in one fell swoop.&lt;/strong&gt; Building the full paragraph comes as a result of our next helper function.&lt;/p&gt;

&lt;p&gt;Before we build that, we need to declare another empty &lt;strong&gt;array&lt;/strong&gt; to contain the completed paragraphs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Search in your starter code&lt;/strong&gt; for the line that reads&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Declare paragraphs array to hold complete paragraphs.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with:&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;paragraphs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;The &lt;code&gt;paragraphs[]&lt;/code&gt; array will be populated by complete paragraphs with child text nodes extracted from the &lt;code&gt;paragraphText[]&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;&lt;a id="createParagraphs"&gt;&lt;/a&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;The&lt;/em&gt; &lt;code&gt;createParagraphs()&lt;/code&gt; &lt;em&gt;Helper Function&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Search in your starter code&lt;/strong&gt; for the line that reads&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Code createParagrahs() helper function.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with:&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;createParagraphs&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="nx"&gt;paragraphText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;paragraph&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="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;paragraphs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paragraph&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;h6&gt;&lt;center&gt;Code Listing 5. the CreateParagraphs() Helper Function&lt;/center&gt;&lt;/h6&gt;



&lt;p&gt;&lt;em&gt;So what exactly is this code doing?&lt;/em&gt; The entire logic of the &lt;code&gt;createParagraphs()&lt;/code&gt; function is found in a single call to the &lt;code&gt;forEach()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;The first line in the loop is:&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;paragraph&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="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s think about what’s happening here for a moment.&lt;/p&gt;

&lt;p&gt;Each time through the loop, &lt;strong&gt;JavaScript&lt;/strong&gt; finds a &lt;strong&gt;text node&lt;/strong&gt; that is a member of the &lt;code&gt;paragraphText[]&lt;/code&gt; array. This line of code declares a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; element to which the &lt;strong&gt;text node&lt;/strong&gt; will be appended as a &lt;strong&gt;child&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The next line in the loop is:&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;paragraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;This line appends the text node at the current index to the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; element created in the previous line.&lt;/p&gt;

&lt;p&gt;The final line of code in the loop is:&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;paragraphs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;This line of code pushes a complete paragraph to the &lt;code&gt;paragraphs[]&lt;/code&gt; array. This sequence is repeated until the last element within the &lt;code&gt;paragraphText[]&lt;/code&gt; array has been read into the loop.&lt;/p&gt;

&lt;p&gt;An array of completed paragraphs now wait to be appended to the &lt;strong&gt;resultBox&lt;/strong&gt; object. One final helper function will accomplish this task.&lt;/p&gt;




&lt;p&gt;&lt;a id="appendParagraphs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;The&lt;/em&gt; &lt;code&gt;appendParagraphs()&lt;/code&gt; &lt;em&gt;Helper Function&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Let’s review &lt;strong&gt;Figure 1&lt;/strong&gt; for a moment. The two buttons shown have to their right a box with a message that reads &lt;strong&gt;NO MATCH&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We want to swap the contents of this box now, removing the &lt;strong&gt;NO MATCH&lt;/strong&gt; placeholder message with a scrolling window that will contain as its children all of the paragraphs in the &lt;code&gt;paragraphs[]&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Search in your starter code&lt;/strong&gt; for the line that reads&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TO BE DONE: Code createParagrahs() helper function.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and replace it with:&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;appendParagraphs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;children&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;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&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;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&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;h6&gt;&lt;center&gt;Code Listing 6. The appendParagraphs() Helper Function&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;In only 5 lines of code, &lt;code&gt;appendParagraphs()&lt;/code&gt; accomplishes quite a lot. The function takes two arguments, &lt;strong&gt;parent&lt;/strong&gt; and &lt;strong&gt;children&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When calling this function, &lt;strong&gt;parent&lt;/strong&gt; refers to &lt;strong&gt;resultBox&lt;/strong&gt;, the node object that captures &lt;code&gt;&amp;lt;div id=“results”&amp;gt;&lt;/code&gt; in our &lt;strong&gt;index.html&lt;/strong&gt; file. &lt;strong&gt;children&lt;/strong&gt; refers to the &lt;code&gt;paragraphs[]&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;Our final helper function also uses the &lt;code&gt;forEach()&lt;/code&gt; method. Here all elements of the &lt;code&gt;paragraphs[]&lt;/code&gt; array are appended to the &lt;strong&gt;resultBox&lt;/strong&gt; node, repeating this process until all 11 matches have been appended to &lt;strong&gt;resultBox&lt;/strong&gt;.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; &lt;em&gt;If you try to log these nodes to the&lt;/em&gt;  &lt;strong&gt;JavaScript Console&lt;/strong&gt; &lt;em&gt;in your browser, you will receive an error message. This is because each of the&lt;/em&gt; &lt;strong&gt;helper functions&lt;/strong&gt; &lt;em&gt;we have created up to this point is called by the&lt;/em&gt; &lt;strong&gt;callback functions&lt;/strong&gt; &lt;em&gt;of the&lt;/em&gt; &lt;strong&gt;event listeners&lt;/strong&gt; &lt;em&gt;we have yet to code.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;We have now completed our infrastructure comprised of arrays and our 3 helper functions.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;AN IMPORTANT ASIDE:&lt;/strong&gt; &lt;em&gt;You will note that in the&lt;/em&gt; &lt;code&gt;appendParagraphs()&lt;/code&gt; &lt;em&gt;function, we have a fully abstracted function that is&lt;/em&gt; &lt;strong&gt;de-coupled&lt;/strong&gt; &lt;em&gt;from the data sets in our application.&lt;br&gt;&lt;br&gt;Though this could have been done for all the helper functions, in the interest of brevity, I have given this one example instead. This technique of&lt;/em&gt; &lt;strong&gt;de-coupling&lt;/strong&gt; &lt;em&gt;functions from the data on which they operate makes the code more reusable and should be practice as a rule in public application releases.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  What’s Next?&lt;a id="next"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;When you have completed all the steps above successfully, you can continue on with the third and concluding part of our tutorial—&lt;a href="https://dev.tourl"&gt;&lt;strong&gt;&lt;em&gt;Regular Expressions—a Rite of Passage: Completing the Application.&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;&lt;center&gt;References&lt;/center&gt;&lt;/h2&gt;

&lt;p&gt;“Regular Expressions - JavaScript: MDN, Using Regular Expressions in&lt;br&gt;     in  JavaScript” &lt;em&gt;JavaScript | MDN&lt;/em&gt;, Mozilla Developer Network,&lt;br&gt;     &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#using_regular_expressions_in_javascript" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#using_regular_expressions_in_javascript" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#using_regular_expressions_in_javascript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“Garbage Collection.” &lt;em&gt;The Modern JavaScript Tutorial,&lt;/em&gt; JavaScript.info,&lt;br&gt;     23 May 2022, &lt;a href="https://javascript.info/garbage-collection" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://javascript.info/garbage-collection" rel="noopener noreferrer"&gt;https://javascript.info/garbage-collection&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>devmeme</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Regular Expressions—A Rite of Passage for Web Developers</title>
      <dc:creator>Robert Hieger</dc:creator>
      <pubDate>Thu, 12 Jan 2023 06:13:36 +0000</pubDate>
      <link>https://forem.com/rhieger/regular-expressions-a-rite-of-passage-for-web-developers-3pfj</link>
      <guid>https://forem.com/rhieger/regular-expressions-a-rite-of-passage-for-web-developers-3pfj</guid>
      <description>&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%2Fmrtgbnlvvja71z4x1wes.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%2Fmrtgbnlvvja71z4x1wes.png" alt="JavaScript in Plain English Logo" width="325" height="70"&gt;&lt;/a&gt; This article was originally published on &lt;a href="https://medium.com/javascript-in-plain-english/regular-expressions-a-rite-of-passage-for-web-developers-b24181465096" rel="noopener noreferrer"&gt;JavaScript in Plain English&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Part 1: Regular Expressions Crash Course—Explaining the Theory Behind Regular Expressions&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fevrh7znk6lu1iygg9tej.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%2Fevrh7znk6lu1iygg9tej.jpg" alt="Woman Programmer in Front of Multiple Displays" width="760" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Photo by ThisIsEngineering (pexels.com)&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;&lt;strong&gt;Intended Audience&lt;/strong&gt;: Intermediate Developers—a strong grasp of &lt;strong&gt;HTML&lt;/strong&gt;, some understanding of &lt;strong&gt;CSS&lt;/strong&gt;, and a strong intermediate grasp of the &lt;strong&gt;JavaScript&lt;/strong&gt; language are required.&lt;/p&gt;




&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;h4&gt;Introduction&lt;/h4&gt;

&lt;h5&gt;What Are We Going to Do in This Tutorial?&lt;/h5&gt;

&lt;h5&gt;Grappling with Theory&lt;/h5&gt;

&lt;h5&gt;Just Enough Regex&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;What Are Regular Expressions?&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;A Thumbnail Historical Sketch of Regex&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Table 1. Some Well-Known Flavors of Regex&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;Nuts &amp;amp; Bolts of Regular Expressions in JavaScript&lt;/h5&gt;

&lt;h5&gt;The Anatomy of a Regular Expression&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Delimiters&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Anchors&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Character Sets&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Ranges&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Quantifiers&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Capturing Groups&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Flags&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;The Regex Tear-Down&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Taking it Apart&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;The First Half of the Pattern&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;The Second Half of the Pattern&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;Sometimes a Hyphen is Just a Hyphen&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;Putting It Back Together&lt;/h5&gt;

&lt;h5&gt;
  
  
       &lt;em&gt;The Test String&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;What Next?&lt;/h5&gt;

&lt;h5&gt;References&lt;/h5&gt;




&lt;h2&gt;
  
  
  Introduction&lt;a id="intro"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The entire software edngineering profession, regardless of discipline, eventually faces a challenge along the learning pathway. When a&lt;br&gt;budding or even experienced developer encounters this challenge, it presents as somewhat of a monolithic barrier. It is almost as if that&lt;br&gt;moment is the discovery of a new and threatening four-letter word&lt;br&gt;(though it’s really five letters)—&lt;strong&gt;regex&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When I first encountered &lt;strong&gt;regex&lt;/strong&gt; (or &lt;strong&gt;regular expressions&lt;/strong&gt;), my experience was a disheartening struggle to learn what seemed a mysterious language, which despite its obvious power, did not make obvious its utility.&lt;/p&gt;

&lt;p&gt;Yet its utility is quite obvious any time we use a web browser to search out an item, whether we search on Google or within the content of a website. Its utility is also quite evident when we fill in online forms and click the &lt;strong&gt;Submit&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;Before the data in your form is submitted, it is validated to make certain that it is correctly formed and/or sanitized (stripped of erroneous content).&lt;/p&gt;




&lt;h2&gt;
  
  
  What Are We Going to Do in This Tutorial?&lt;a id="whatToDo"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We will be building a simple demo &lt;strong&gt;Single Page Application (SPA)&lt;/strong&gt;, but with fairly complex underpinnings.&lt;/p&gt;

&lt;p&gt;This tutorial is the first of a three-part series in which we will proceed from theory to practice to the realization of a complete demo SPA.&lt;/p&gt;




&lt;h2&gt;
  
  
  Grappling with Theory&lt;a id="grappleTheory"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;If you are completely new to regular expressions, then this first part of the tutorial will be for you. If you are fairly experienced with them, then you can skip ahead to the second article in this series—&lt;em&gt;Regular Expressions—a Rite of Passage: From Theory to Practice&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;However, because this part of the tutorial uses the data in our web application to give a quick crash course in regular expressions, you might still find it useful.&lt;/p&gt;

&lt;p&gt;By the end of the third tutorial in this series, we will build a small web application the purpose of which will be to extract from a string of text valid zip codes and display them in a result box that populates upon clicking the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 1&lt;/strong&gt; below shows the opening screen of our finished application:&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%2Fzuf9eue5j9rqsp3hb9om.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%2Fzuf9eue5j9rqsp3hb9om.png" alt="Application Landing Page" width="800" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 1 Application Landing Page&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;In short, the test data frame scrolls to reveal a multiline string of characters within which are found valid zip codes. When the user clicks the &lt;strong&gt;Validate Zip Codes&lt;/strong&gt; button, using the regular expression shown just below the test data, the application will replace the &lt;strong&gt;NO MATCH&lt;/strong&gt; message displayed in the frame to its right with a scrolling list of validated zip code matches.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Reset&lt;/strong&gt; button restores the screen to its original state.&lt;/p&gt;

&lt;p&gt;To realize this project, we will need to take a dive into the theory of regular expressions.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;But this app seems simple enough. Why devote a whole article to theoretical underpinnings? Shouldn’t we learn by doing?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Well, yes…but not really. We will learn by doing, but doing without some minimal basis of understanding would be a painful pursuit. In writing this application, we will slam head-on into the barrier of &lt;strong&gt;Regex&lt;/strong&gt;, which at least initially, might feel like bumping into a &lt;strong&gt;&lt;em&gt;Tyrannosaurus Regex&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;All kidding aside, without this rite of passage, any attempts to come up with a similar application to the one we are going to create would require writing code that likely would not work as well as that made possible by the inclusion of regular expressions.&lt;/p&gt;

&lt;p&gt;A thorough exploration of Regex and its implementation in JavaScript is beyond the scope of this article. Such exploration could easily require a course of its own.&lt;/p&gt;

&lt;p&gt;However, to facilitate some understanding of how it will come into play for our demo web application, I will give a quick crash course in just the concepts of regular expressions necessary for the realization of this project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Just Enough Regex&lt;a id="enoughRegex"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;To begin, we need to define our terms. Thus far, we have only named our concept—&lt;strong&gt;regular expressions&lt;/strong&gt;. But what are they?&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;What Are Regular Expressions?&lt;/em&gt;&lt;a id="whatIsRegex"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;As defined by the Mozilla Developer Network—&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“Regular expressions are patterns used to match character combinations in strings” (“Regular expressions—JavaScript”)&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;em&gt;What does this mean?&lt;/em&gt; This is a very abstract statement. Let’s take yet a further step back to set the theoretical context for regular expressions.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;A Thumbnail Historical Sketch of Regular Expressions&lt;/em&gt;&lt;a id="thumbNail"&gt;&lt;/a&gt;
&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%2Fp8s9us46elshsaswdhw2.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%2Fp8s9us46elshsaswdhw2.jpg" alt="Mathematician Stephen Cole Kleene" width="325" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Mathematician Stephen Cole Kleene&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;The roots of regular expressions are in a 1951 paper entitled &lt;em&gt;Representation of Events in Nerve Nets and Finite Automata&lt;/em&gt;, by mathematician Stephen Cole Kleene. The theories defined in this paper are far beyond the scope of this tutorial, but should you be interested in exploring what he has to say, you can download the paper &lt;a href="https://www.rand.org/content/dam/rand/pubs/research_memoranda/2008/RM704.pdf" rel="noopener noreferrer"&gt;here&lt;/a&gt; (Kleene, &lt;em&gt;“Representation of Events”&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Practical application of Kleene’s theories came into their own around 1968 when they were used to facilitate pattern matching in text editors, and by compilers in their lexical analysis of source code. For more on this, you might want to consult this &lt;a href="https://en.wikipedia.org/wiki/Regular_expression" rel="noopener noreferrer"&gt;article&lt;/a&gt; (Wikipedia, &lt;em&gt;“Regular Expressions”&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Suffice it to say that in the years to come, many different implementations of regular expressions were developed, all of them owing their heritage to the advent of these concepts in the &lt;strong&gt;UNIX&lt;/strong&gt; operating system. &lt;strong&gt;Table 1&lt;/strong&gt; below is a sampling of some of the different flavors of regular expressions found on Wikipedia (&lt;em&gt;“Comparison of Regular Expression Engines”&lt;/em&gt;):&lt;/p&gt;




&lt;p&gt;&lt;a id="table1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Regex Engine (Library)&lt;/th&gt;
&lt;th&gt;Where It is Used&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;PCRE (Perl Compatible Regular Expressions)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;This implementation of regular expressions derives from the Perl language often used for server-side scripts in web applications. Perl is compatible to the &lt;strong&gt;POSIX (Portable Operating System Interface)&lt;/strong&gt; standard established by the &lt;strong&gt;IEEE&lt;/strong&gt;. UNIX was used as the model for this standard.&lt;br&gt;&lt;br&gt;The PCRE implementation found its way into PHP, the Apache HTTP Server, and C and C++ languages, to name but a few.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;FREJ (Fuzzy Regular Expressions for Java)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;This implementation is a library specific to the Java programming language.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;EXMAScript (JavaScript Regular Expressions)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;This is the reference library standard used by most JavaScript engines across many web browsers.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;XRegExp (Extended JavaScript Regular Expressions)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;This library, which may be used with JavaScript, is a superset of the standard implementation of regular expressions in the JavaScript engine. Coded and maintained by &lt;a href="https://blog.stevenlevithan.com/about" rel="noopener noreferrer"&gt;Steven Levithan&lt;/a&gt;, the releases of this library may be found on this repository:&lt;br&gt;&lt;br&gt;&lt;a href="https://github.com/slevithan/xregexp/releases" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://github.com/slevithan/xregexp/releases" rel="noopener noreferrer"&gt;https://github.com/slevithan/xregexp/releases&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h6&gt;&lt;center&gt;Table 1: Some Well Known Flavors of Regex&lt;/center&gt;&lt;/h6&gt;




&lt;h3&gt;
  
  
  &lt;em&gt;Nuts and Bolts of Regular Expressions in JavaScript&lt;/em&gt;&lt;a id="nutsBolts"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;In JavaScript, regular expressions are represented in one of two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;An object literal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An object declared with a constructor.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;strong&gt;object literal notation&lt;/strong&gt; is what we see above in &lt;strong&gt;Figure 1&lt;/strong&gt; minus its declaration. The code that you will be writing soon uses the notation shown here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-z&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;This expression will search for and match any instances of lowercase letters &lt;strong&gt;a&lt;/strong&gt; through &lt;strong&gt;z&lt;/strong&gt; in a specified test string. More on this later.&lt;/p&gt;

&lt;p&gt;This same regular expression, declared using its &lt;strong&gt;object constructor&lt;/strong&gt;, has the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;regex&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;RegExp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="err"&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;Both of these syntaxes work identically. &lt;em&gt;Is there a time one or the other should be used?&lt;/em&gt; Yes, there is. According to the Mozilla Developer Network:&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“The literal notation results in compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won’t be recompiled on each iteration” (“RegExp –JavaScript | MDN”).&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You will find more complete information on this in the full &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor" rel="noopener noreferrer"&gt;Mozilla Developer Network article&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a id="anatomyRegex"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;The Anatomy of a Regular Expression&lt;/center&gt;&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;/^&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;})?&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;gm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Looking at this cryptic line of code, you might be asking yourself, &lt;em&gt;“What is this Gobbledygook?”&lt;/em&gt; I assure you that in a few minutes, this will look less intimidating than&lt;br&gt;
it does now.&lt;/p&gt;

&lt;p&gt;If we think of Regex as a symbolic representation of character patterns we wish to match, we can begin to classify different kinds of symbolic characters.&lt;/p&gt;

&lt;p&gt;We will take a quick look at the regular expression above, pick it apart piece by piece and then put it back together to understand what is being asked of the regex engine.&lt;br&gt;
The component parts of our regular expression can be broken down as follows:&lt;/p&gt;

&lt;p&gt;     1. Delimiters&lt;br&gt;
     2. Anchors&lt;br&gt;
     3. Character Sets and Ranges&lt;br&gt;
     4. Quantifiers&lt;br&gt;
     5. Capturing Groups&lt;br&gt;
     6. Flags&lt;/p&gt;

&lt;p&gt;All six of these components come into play for our regular expression. Let’s take them one at a time.&lt;/p&gt;

&lt;p&gt;&lt;a id="delimiters"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;Delimiters&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;In JavaScript object literal notation, regular expressions are delimited on either side by forward slashes. Everything that appears between these slashes is a representation of the pattern for which we wish to find matches in our test string.&lt;/p&gt;

&lt;p&gt;&lt;a id="anchors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
&lt;center&gt;Anchors&lt;/center&gt;

&lt;/h4&gt;
&lt;p&gt;An &lt;strong&gt;anchor&lt;/strong&gt; does not match any specific character in a test string. Rather it defines the beginning or endpoint of the match we wish to see returned by the regex engine.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;caret&lt;/strong&gt; symbol (&lt;strong&gt;^&lt;/strong&gt;) represents the beginning of the match for which we are searching. For example, if we want to match for any instance of the letter &lt;strong&gt;J&lt;/strong&gt; at the beginning of our pattern, the syntax would be &lt;code&gt;^J&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On the other hand, the &lt;strong&gt;dollar sign&lt;/strong&gt; (&lt;strong&gt;$&lt;/strong&gt;) represents the end of the match for which we are searching. This symbol is used at the end of the string pattern and affects the character or character set immediately preceding it.&lt;/p&gt;

&lt;p&gt;&lt;a id="charSets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;Character Sets&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;A &lt;strong&gt;character set&lt;/strong&gt; is a specified collection of characters, which may be character literals or a specified progression of characters against which&lt;br&gt;we want to test a specific test string. Character Sets are enclosed by&lt;br&gt;square brackets.&lt;/p&gt;

&lt;p&gt;&lt;a id="ranges"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;Ranges&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;When a series of characters defines a range of characters, we have a &lt;strong&gt;range&lt;/strong&gt;. Ranges are comprised of a set of sequential digits or alphabetical characters. For example, &lt;strong&gt;0&lt;/strong&gt; through &lt;strong&gt;9&lt;/strong&gt; is what we would consider (in the decimal system, anyway) the complete range of digits possible in a number. This would also be notated in square brackets, 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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For alphabetical characters we also have the ranges &lt;strong&gt;[a-z]&lt;/strong&gt; and &lt;strong&gt;[A-Z]&lt;/strong&gt;. But we could just as easily specify a range of digits such as &lt;strong&gt;[1-5]&lt;/strong&gt; or alphabetical ranges such as &lt;strong&gt;[a-g]&lt;/strong&gt; or &lt;strong&gt;[A-G]&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a id="quantifiers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;Quantifiers&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;A &lt;strong&gt;quantifier&lt;/strong&gt; specifies the minimum instances of a given character (or character within a character set) desired in a match. It may also specify&lt;br&gt;the exact number of characters desired in a pattern match.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table 2&lt;/strong&gt; below shows some of the uses for these quantifiers:&lt;/p&gt;

&lt;p&gt;&lt;a id="quantifiers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Quantifier&lt;/th&gt;
&lt;th&gt;                                                       Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Represents &lt;strong&gt;0&lt;/strong&gt; or &lt;strong&gt;more&lt;/strong&gt; instances of the character or character set that immediately precedes it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Represents &lt;strong&gt;1&lt;/strong&gt; or &lt;strong&gt;more&lt;/strong&gt; instances of the character or character set that immediately precedes it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;?&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Represents &lt;strong&gt;0&lt;/strong&gt; or &lt;strong&gt;1&lt;/strong&gt; instance of the character or character set that immediately precedes it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;{ }&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;This quantifier represents exactly the number of character matches as the number within it. For example, &lt;strong&gt;{5}&lt;/strong&gt; means exactly &lt;strong&gt;5&lt;/strong&gt; instances of the character that immediately precedes it. &lt;strong&gt;{2,}&lt;/strong&gt; means &lt;strong&gt;2&lt;/strong&gt; or more instances. &lt;strong&gt;{1, 5}&lt;/strong&gt; means between &lt;strong&gt;1&lt;/strong&gt; and &lt;strong&gt;5&lt;/strong&gt; instances.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h6&gt;&lt;center&gt;Table 2: Regex Quantifiers&lt;/center&gt;&lt;/h6&gt;




&lt;p&gt;&lt;a id="captureGroups"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;Capturing Groups&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;Notated by the use of parentheses, &lt;strong&gt;capturing groups&lt;/strong&gt; set a pattern sequence to be taken as a whole, rather than its constituent parts. In&lt;br&gt;other words, if we have a capturing group such as 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="nx"&gt;A&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;G&lt;/span&gt;&lt;span class="p"&gt;]&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="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;…what we are saying is to match any complete pattern that starts with any capital letter between &lt;strong&gt;A&lt;/strong&gt; and &lt;strong&gt;G&lt;/strong&gt;, followed by a &lt;strong&gt;hyphen&lt;/strong&gt; and &lt;strong&gt;0&lt;/strong&gt;, then finally exactly &lt;strong&gt;3&lt;/strong&gt; digits between &lt;strong&gt;0&lt;/strong&gt; and &lt;strong&gt;9&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How does this differ from a plain character set?&lt;/em&gt; In a character set, unless a quantifier follows it immediately such as in the sequence &lt;strong&gt;[0-9]{3}&lt;/strong&gt; above, the regex engine will match only &lt;strong&gt;1&lt;/strong&gt; character that falls within the range specified.&lt;/p&gt;

&lt;p&gt;With a capturing group, on the other hand, a sequence is analyzed as a whole and must be matched as a whole. This proves very useful in our mini-application, as you will see.&lt;/p&gt;




&lt;p&gt;&lt;a id="flags"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;Flags&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;With &lt;strong&gt;flags&lt;/strong&gt;, we reach perhaps the easiest to understand of all the syntax thus far described. Flags have an impact on the way the regex engine parses the test string passed to it.&lt;br&gt;
The JavaScript flavor of regular expressions has six flags, but we will confine our examination to the three you will probably most often encounter. These flags occur at the end of a regular expression after the closing forward slash. They are:&lt;br&gt;&lt;br&gt;&lt;br&gt;
     1. &lt;strong&gt;i—&lt;/strong&gt;the &lt;strong&gt;case-insensitive flag&lt;/strong&gt; specifies that alphabetical characters will&lt;br&gt;         be matched whether they are upper or lower case.&lt;br&gt;&lt;br&gt;
     2. &lt;strong&gt;g—&lt;/strong&gt;the &lt;strong&gt;global flag&lt;/strong&gt; specifies that all matches from beginning to end of&lt;br&gt;         the test string will be returned. By default, the regex engine reads the&lt;br&gt;         test string from left to right, and once it returns the first match, exits,&lt;br&gt;         ignoring any matches beyond the first.&lt;/p&gt;

&lt;p&gt;     3. &lt;strong&gt;m—&lt;/strong&gt;Stefan Judis clarifies that “the multiline flag changes the&lt;br&gt;          meaning…” of the &lt;strong&gt;^&lt;/strong&gt; and &lt;strong&gt;$&lt;/strong&gt; anchors mentioned above (Judis, &lt;em&gt;Multiline&lt;br&gt;         mode in JavaScript regular expressions&lt;/em&gt;). By default, the regex engine&lt;br&gt;         treats a test string  as one long uninterrupted string of characters to&lt;br&gt;         be searched.&lt;/p&gt;

&lt;p&gt;         The boundaries of this string are defined by the &lt;strong&gt;^&lt;/strong&gt; anchor, which denotes&lt;br&gt;         the start of a string, and the &lt;strong&gt;$ anchor&lt;/strong&gt;, which denotes the end of a string.&lt;br&gt;         With the &lt;strong&gt;m&lt;/strong&gt; (&lt;strong&gt;multiline&lt;/strong&gt;) flag appended to the end of an expression, these&lt;br&gt;         two anchors define the beginning and end of a line rather than the string&lt;br&gt;         as a whole.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;OKAY&lt;/strong&gt;. I know that was an awful lot of stuff and some of it might have even been a bit confusing. That’s about to change.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;The Regex Tear-Down&lt;/em&gt;&lt;a id="regexTeardown"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;We are now ready to analyze the &lt;strong&gt;regular expression&lt;/strong&gt; used in the web application we are going to build. Let’s look at it again in all its glory:&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="o"&gt;/^&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;})?&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;gm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="takeItApart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;Taking it Apart&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;Let’s start by examining the outermost parts of this expression. The shell of our regular expression 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="c1"&gt;//gm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;What does this little fragment say on its own?&lt;/em&gt; Not much, as no pattern has been specified as yet. Nonetheless, this fragment sets the stage for how any pattern appearing between the forward slash delimiters will be treated.&lt;/p&gt;

&lt;p&gt;Using the definitions of regex flags above, this fragment says to the regex engine &lt;em&gt;“Match all instances of the pattern specified between the delimiters&lt;/em&gt; (&lt;strong&gt;global matching&lt;/strong&gt;) &lt;em&gt;and consider each line of the test string separately&lt;/em&gt; (&lt;strong&gt;multiline matching&lt;/strong&gt;)&lt;em&gt;.”&lt;/em&gt; Not much yet, but the engine now knows how to treat whatever pattern is defined for it between the delimiters.&lt;/p&gt;




&lt;p&gt;&lt;a id="firstHalf"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;The First Half of the Pattern&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;The first piece of this pattern makes use of the &lt;strong&gt;^&lt;/strong&gt; anchor about which we learned earlier, differently. Let’s take a look at the first half of the pattern with this in mind:&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="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we have the &lt;strong&gt;^ beginning of string anchor&lt;/strong&gt;, which declares the start of our pattern. Next we have a character set enclosed in square brackets. This character set is comprised entirely of a range of digits &lt;strong&gt;0&lt;/strong&gt; through &lt;strong&gt;9&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This range is followed immediately by a quantifier of &lt;strong&gt;{5}&lt;/strong&gt;. Taken together, the range and the quantifier specify that any set of exactly 5 digits in the range of &lt;strong&gt;0&lt;/strong&gt; through &lt;strong&gt;9&lt;/strong&gt; will be returned as a match.&lt;/p&gt;

&lt;p&gt;Now we have the complete first half of our regular expression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/^[0-9]{5}/gm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this says all together to the regex engine is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“Search for and return all matches across all lines of the test string, that start with any combination of exactly &lt;strong&gt;5&lt;/strong&gt; digits.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Taken on its own, the first half of our regular expression is complete in and of itself and will return any valid 5-digit zip code that it finds in our test string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But there’s still a problem&lt;/strong&gt;. What we have so far will bypass any valid +4 zip codes embedded in the text. The second half of our regular expression will address this problem.&lt;/p&gt;

&lt;p&gt;&lt;a id="secondHalf"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;The Second Half of the Pattern&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;Now let’s approach the second half of the regular expression:&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;})?&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Here we encounter a capturing group, which was explained earlier. The presence of the parentheses around the hyphen, range and quantifier indicates that the group is to be analyzed only as a whole. &lt;strong&gt;No&lt;/strong&gt; constituent part of the capturing group will be considered on its own.&lt;/p&gt;

&lt;p&gt;First let’s take on the contents of the capturing group.&lt;/p&gt;

&lt;p&gt;&lt;a id="justHyphen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Sometimes a Hyphen is Just a Hyphen&lt;/em&gt;&lt;br&gt;
&lt;/h4&gt;

&lt;p&gt;We start with the &lt;strong&gt;hyphen&lt;/strong&gt;. When a hyphen occurs within square brackets that delimit a character set, it denotes a range, e.g. &lt;code&gt;[0-9]&lt;/code&gt;. When it occurs as the first character in a capturing group and outside of square brackets, it is a &lt;strong&gt;character literal&lt;/strong&gt;. Thus the hyphen is a required character at the beginning of the pattern to be matched in the capturing group.&lt;/p&gt;

&lt;p&gt;Next is the range &lt;code&gt;[0-9]&lt;/code&gt;. So far our group will only return a match if it starts with a hyphen and is followed by &lt;strong&gt;4&lt;/strong&gt; digits between &lt;strong&gt;0&lt;/strong&gt; and &lt;strong&gt;9&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Outside the parentheses of the capturing group is the &lt;strong&gt;?&lt;/strong&gt; quantifier, which specifies that there can be &lt;strong&gt;0&lt;/strong&gt; or &lt;strong&gt;1&lt;/strong&gt; instance of what immediately preceded it. It’s important to note  that this &lt;strong&gt;?&lt;/strong&gt; quantifier applies to the entire capturing group, not just the single character to its left.&lt;/p&gt;

&lt;p&gt;This is the power of capturing groups. They make it possible to require of a match a specified group of characters or character sets. Because the &lt;strong&gt;?&lt;/strong&gt; quantifier accepts either &lt;strong&gt;0&lt;/strong&gt; or &lt;strong&gt;1&lt;/strong&gt; match of what precedes it, it is sometimes called an &lt;strong&gt;optional&lt;/strong&gt;. In other words, a match of &lt;strong&gt;0&lt;/strong&gt; or &lt;strong&gt;1&lt;/strong&gt; instance of the character or pattern specified will be returned.&lt;/p&gt;

&lt;p&gt;This behavior is what makes it possible for the regex engine to return matches of either a 5-digit zip code or a +4 zip code.&lt;/p&gt;

&lt;p&gt;Finally, we have the &lt;strong&gt;$&lt;/strong&gt; anchor, which defines the end of the string to be returned as a match. This very unambiguously specifies that a valid zip code match will begin with a sequence of &lt;strong&gt;5&lt;/strong&gt; digits and have an optional hyphen followed by exactly &lt;strong&gt;4&lt;/strong&gt; digits, but absolutely nothing following this sequence.&lt;/p&gt;




&lt;p&gt;&lt;a id="putTogether"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;&lt;center&gt;Putting it Back Together&lt;/center&gt;&lt;/h4&gt;

&lt;p&gt;Let’s put everything back together now. &lt;strong&gt;Figure 2&lt;/strong&gt; below shows the entire sequence of our regular expression.&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="o"&gt;/^&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;})?&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;gm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;&lt;center&gt;Fig. 2 The Reassembled Regular Expression&lt;/center&gt;&lt;/h6&gt;

&lt;p&gt;This sequence should now be clearer to us. Read from left to right, here is the meaning of the complete regular expression:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“Search for and return all matches across all lines of the test string that start with any combination of exactly 5 digits, and conclude optionally with a hyphen and exactly 4 digits in the range of &lt;strong&gt;0&lt;/strong&gt; through &lt;strong&gt;9&lt;/strong&gt;.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There we have it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How about a Real-World Example?
&lt;/h2&gt;

&lt;p&gt;Here is a very stripped down example of what we will implement for our application:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;The Test String&lt;/em&gt;&lt;a id="testString"&gt;&lt;/a&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10003
asdf10003
10003-8924
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How many matches do you think will be returned from this &lt;strong&gt;search string&lt;/strong&gt;? 1, 2, 3 or 4?&lt;/p&gt;

&lt;p&gt;Using the very useful online tool called &lt;a href="https://regexpal.com/" rel="noopener noreferrer"&gt;Regex Pal&lt;/a&gt;, we obtain the results found in &lt;strong&gt;Figure 3&lt;/strong&gt; below:&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%2Fcari4y303x2ai2lk1m7m.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%2Fcari4y303x2ai2lk1m7m.png" alt="Fig. 3 Test Results from RegexPal.com" width="800" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;&lt;center&gt;Fig. 3 Test Results from RegexPal.com&lt;/center&gt;&lt;/h6&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;On the first line, &lt;strong&gt;10003&lt;/strong&gt; is highlighted as a match. This is because it meets the criteria for a 5 digit zip code.&lt;/p&gt;

&lt;p&gt;On the second line, &lt;strong&gt;asdf10003&lt;/strong&gt; has not been highlighted. This is because even though there are 5 digits on this line, they are preceded by &lt;strong&gt;asdf&lt;/strong&gt;. It therefore does not meet the criteria, as the beginning of the matched string is not a sequence of 5 digits as required.&lt;/p&gt;

&lt;p&gt;On the third line, &lt;strong&gt;10003asdf&lt;/strong&gt; has also not been highlighted. This is because, even though  there are 5 digits at the beginning of the line, &lt;strong&gt;asdf&lt;/strong&gt; follows this sequence. It therefore does not meet the criteria, as the end of the matched string must either be the last digit of the 5-digit sequence at the beginning, or the optional hyphen followed by exactly 4 digits. Nothing else may follow.&lt;/p&gt;

&lt;p&gt;Finally, on the fourth line, &lt;strong&gt;10003-8924&lt;/strong&gt; &lt;em&gt;is&lt;/em&gt; highlighted. This is because it meets the criteria of a string that begins with exactly 5 digits, and ends with the optional sequence of a hyphen followed by exactly 4 digits.&lt;/p&gt;

&lt;p&gt;There are therefore 2 matches.&lt;/p&gt;

&lt;p&gt;Though this example is admittedly a bit simpler than the one we will be using in our finished application, the principles remain the same.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Next?&lt;a id="whatNext"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Once you have come up for air, please continue to the second part of this tutorial series in which we will begin building our application—&lt;a href="https://dev.to/rhieger/regular-expressions-a-rite-of-passage-from-theory-to-practice-2ii5"&gt;&lt;strong&gt;&lt;em&gt;Regular Expressions—a Rite of Passage: From Theory to Practice&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;





&lt;h2&gt;&lt;center&gt;References&lt;/center&gt;&lt;/h2&gt;
&lt;a id="references"&gt;&lt;/a&gt;

&lt;p&gt;“Regular Expressions - JavaScript: MDN.” &lt;em&gt;JavaScript | MDN&lt;/em&gt;,&lt;br&gt;     &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Kleene, Stephen Cole. &lt;em&gt;Representation of Events in Nerve Nets and Finite…&lt;br&gt;     - Rand Corporation&lt;/em&gt;.      &lt;a href="https://www.rand.org/content/dam/rand/pubs/research_memoranda/2008/RM704.pdf" rel="noopener noreferrer"&gt;https://www.rand.org/content/dam/rand/pubs/research_memoranda/2008/RM704.pdf&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“Regular Expression.” &lt;em&gt;Wikipedia&lt;/em&gt;, Wikimedia Foundation, 30 May 2022,&lt;br&gt;     &lt;a href="https://en.wikipedia.org/wiki/Regular_expression" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Regular_expression&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“Comparison of Regular Expression Engines.” &lt;em&gt;Wikipedia&lt;/em&gt;, Wikimedia&lt;br&gt;     Foundation, 29 Apr. 2022,&lt;br&gt;     &lt;a href="https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“RegExp - JavaScript: MDN.” &lt;em&gt;JavaScript | MDN&lt;/em&gt;,&lt;br&gt;     &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Judis, Stefan. “Multiline Mode in JavaScript Regular Expressions.” &lt;em&gt;Stefan&lt;br&gt;     Judis Web Development&lt;/em&gt;, 23 Jan. 2022,&lt;br&gt;     &lt;a href="https://www.stefanjudis.com/today-i-learned/multiline-mode-in-javascript-regular-expressions/" rel="noopener noreferrer"&gt;https://www.stefanjudis.com/today-i-learned/multiline-mode-in-javascript-regular-expressions/&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Numbers According to Computers</title>
      <dc:creator>Robert Hieger</dc:creator>
      <pubDate>Fri, 06 Jan 2023 05:12:49 +0000</pubDate>
      <link>https://forem.com/rhieger/numbers-according-to-computers-3beb</link>
      <guid>https://forem.com/rhieger/numbers-according-to-computers-3beb</guid>
      <description>&lt;p&gt;&lt;strong&gt;This article was first published on &lt;a href="https://medium.com/nerd-for-tech/numbers-according-to-computers-7c64de47348e" rel="noopener noreferrer"&gt;medium.com&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Intended Audience: Beginner to Intermediate Developers
&lt;/h6&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Many might consider the topic of this article superfluous and not worthy of lengthy examination if one is looking to become a skilled web developer/designer.&lt;/p&gt;

&lt;p&gt;I find that in academic circles and, to a great extent, among budding developers, the importance of understanding numbers as they appear to computers, among other mathematical concepts germane to software development, encounters a kind of flippant dismissal—an assessment that they are not truly necessary in order to become a good software engineer.&lt;/p&gt;

&lt;p&gt;I would argue that these rudimentary concepts not only warrant review, but are of increasing importance in the arena of full stack web development/design.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;So why spend time on such a boring aspect of computer science? What relevance does it have to a web developer/designer or student of programming? It sounds more like what a computer engineer needs to understand.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, yes, a computer engineer definitely must understand binary numbers as microprocessors make us of this at the machine language level to represent data and instructions they receive.&lt;/p&gt;

&lt;p&gt;The subject of number representation in computers is of great importance to developers as well, and can have significant ramifications in any programming language. Consider for a moment the shorthand employed to represent numbers in both high level programing languages such as Java, JavaScript or Python and lower level languages such as assembler code or machine language.&lt;/p&gt;

&lt;p&gt;Of course, computers don't really know the difference between 10, 100, 35 or 70, for that matter. They don't even know what numbers are. At the level of machine language—the only language of which computers are capable—all the microprocessor understands is electrical current or lack thereof when circuit is closed or open.&lt;/p&gt;

&lt;p&gt;At the machine language level, which communicates directly with the microprocessor, &lt;strong&gt;0&lt;/strong&gt; represents an open circuit, or one through which the flow of current is interrupted. &lt;strong&gt;1&lt;/strong&gt;, on the other hand, represents a circuit through which the flow of electricity is not impeded.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Micro-Exploration of the Microprocessor
&lt;/h2&gt;

&lt;p&gt;In the typical microprocessor there are billions of microcircuits employing silicon semiconductor components.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;What is a semiconductor?&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;At its root, a semiconductor is a&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"... material [that] has an electrical conductivity value falling between that of a conductor, such as metallic copper, and an insulator, such as glass. Its resistivity falls as its temperature rises; metals behave the opposite way. Its conducting properties may be altered in useful ways by introducing impurities ('doping') into the crystal structure. When two differently doped regions exist in the same crystal, a semiconductor junction is created"_Semiconductor -Wikipedia&lt;/em&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As further clarified by the &lt;em&gt;Encyclopaedia Britannica&lt;/em&gt;, the introduction of impurities through the process of doping starts with&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“pure crystalline silicon…which is abundant in ordinary beach sand…[and] has a very high resistance to electrical current…[With the introduction of] certain impurities, known as dopants, the silicon can be made to conduct usable currents… [most notably]…as a switch, turning current off and on as desired” (“Microprocessor Circuits”).&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I took this detour because the duality of circuits that can close or open leads logically into how numbers are processed by the microprocessor. The utility of a switch circuit in a computer should be self-evident.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Binary System (Base 2)
&lt;/h2&gt;

&lt;p&gt;First, we have the binary system, the basis for the &lt;strong&gt;ASCII Code&lt;/strong&gt; (American Standard Code for Information Interchange), which codifies the Latin alphabet, the digits &lt;strong&gt;0-9&lt;/strong&gt;, punctuation marks, some other special characters and unprintable control characters.&lt;/p&gt;

&lt;p&gt;For the purposes of this article, we will concern ourselves solely with the representation of numbers. The &lt;strong&gt;ASCII&lt;/strong&gt; code employs &lt;strong&gt;8 bits&lt;/strong&gt; to represent a single character. These &lt;strong&gt;8 bits&lt;/strong&gt; are our starting point to understand the representation of numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do Computers Count?
&lt;/h2&gt;

&lt;p&gt;I am by no means a mathematician, but the math concepts needed in order to understand binary representation of numbers are present in basic math.&lt;/p&gt;

&lt;p&gt;For humans, counting seems most natural in the &lt;strong&gt;decimal&lt;/strong&gt; (&lt;strong&gt;Base 10&lt;/strong&gt;) system, perhaps because we have 10 fingers and 10 toes. The number &lt;strong&gt;10&lt;/strong&gt; is an easy concept for us to grasp. &lt;strong&gt;Base 10&lt;/strong&gt;, on the other hand, is completely alien to computers. At the primitive level, there are &lt;strong&gt;2 states&lt;/strong&gt; for a circuit—closed, where the flow of electricity is uninterrupted, or open, where the flow of current is interrupted, as we discovered in &lt;strong&gt;&lt;em&gt;“A Micro-Exploration of the Microprocessor.”&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The smallest unit of storage a computer can recognize is called a &lt;strong&gt;bit&lt;/strong&gt; (short for &lt;strong&gt;binary digit&lt;/strong&gt;). A &lt;strong&gt;binary digit&lt;/strong&gt; can be one of two numbers—&lt;strong&gt;0&lt;/strong&gt; or &lt;strong&gt;1&lt;/strong&gt;. Because the first widely-used personal computers had microprocessors such as the &lt;strong&gt;Intel 8088&lt;/strong&gt;, whose word length was &lt;strong&gt;8 bits&lt;/strong&gt;, standard representation for a single character or number now uses the convention of &lt;strong&gt;8 bits&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8 bit&lt;/strong&gt;s are known as a &lt;strong&gt;byte&lt;/strong&gt;. One smaller unit, that of &lt;strong&gt;4 bits&lt;/strong&gt; is known as a &lt;strong&gt;nibble&lt;/strong&gt;, used only on very early processors that had a &lt;strong&gt;4-bit word length&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What if you need to represent larger numbers or floating point numbers?&lt;/em&gt;&lt;/strong&gt; It is possible to represent larger numbers or floating point numbers. This requires more bytes. On early &lt;strong&gt;8-bit processors&lt;/strong&gt;, this also requires more than one &lt;strong&gt;processing cycle&lt;/strong&gt;. In this case,  the processor’s “[add] instruction set[s] the ‘carry’ flag” and the microprocessor sees the next cycle as part of the same number or calculation (&lt;em&gt;“How Did Old Games and Computers Display Large Numbers?”&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Different programming languages handle representation of numbers differently. We can examine the representation of numeric data types in the &lt;strong&gt;C language&lt;/strong&gt;, which arguably provides the seminal reference types upon which many other languages have built &lt;strong&gt;static typing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As Ritchie and Kernighan state in &lt;em&gt;The C Programming Language,&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F71ggaghxiyevq7wo5xf5.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%2F71ggaghxiyevq7wo5xf5.png" alt="Chart from The C Programming Language" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;"2.2 Data Types and Sizes"&lt;/em&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  What Numbers Can be Expressed by a Single Byte?
&lt;/h3&gt;

&lt;p&gt;To put this in basic mathematical terms, a single byte is capable of representing &lt;strong&gt;2&lt;sup&gt;8&lt;/sup&gt;-1&lt;/strong&gt; or &lt;strong&gt;(2 ×2 ×2 ×2 ×2 ×2 ×2 ×2)-1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2&lt;sup&gt;8&lt;/sup&gt;=256&lt;/strong&gt;. Why, then, is the largest possible number &lt;strong&gt;256-1&lt;/strong&gt;? We need one digit to express &lt;strong&gt;0&lt;/strong&gt;. Therefore, the representable range of numbers expressed by a single byte is &lt;strong&gt;0-255&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Representing Signed Numbers&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;We cut the possible range of numbers in half if we want to represent negative numbers, with “[the] lowest [negative] number that can be stored [being]&lt;br&gt;&lt;strong&gt;-(2&lt;sup&gt;&lt;em&gt;(k-1)&lt;/em&gt;&lt;/sup&gt;-1)&lt;/strong&gt; and [the] largest [positive] number that can be stored being&lt;br&gt;&lt;strong&gt;(2&lt;sup&gt;&lt;em&gt;(k-1)&lt;/em&gt;&lt;/sup&gt;-1)&lt;/strong&gt; (&lt;em&gt;“Representation of Negative Binary Numbers”&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;With this formula, we then have &lt;strong&gt;-128&lt;/strong&gt; through &lt;strong&gt;127&lt;/strong&gt; as the range of numbers one can represent in a single &lt;strong&gt;byte&lt;/strong&gt; (&lt;strong&gt;8 bits&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;We use the first bit to determine whether a number is negative or positive. We render this notation “with the help of an extra bit or flag called sign bit or sign flag in the Binary number representation system for signed numbers…[and it] has a value of…0 for positive numbers and 1 for negative binary numbers” (&lt;em&gt;“Negative Binary Numbers”&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Using the first bit to represent the sign of the integer has a couple of consequences that bear examination. We now have only &lt;strong&gt;7 bits&lt;/strong&gt; of the &lt;strong&gt;byte&lt;/strong&gt; to represent the integer. There is also an unsettling ambiguity in this encoding of numbers.&lt;/p&gt;

&lt;p&gt;Remember, we have one less &lt;strong&gt;binary digit&lt;/strong&gt; with which to work because we need it to represent &lt;strong&gt;0&lt;/strong&gt;. If we set the &lt;strong&gt;MSB&lt;/strong&gt; (&lt;strong&gt;most significant bit&lt;/strong&gt;), or the leftmost digit to &lt;strong&gt;0&lt;/strong&gt;, it represents positive 0. If, on the other hand, the MSB is &lt;strong&gt;1&lt;/strong&gt;, we now have the theoretical construct of &lt;strong&gt;negative 0&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Though the computer will not care one way or the other, this is an unsettling idea for humans, as we conceive of &lt;strong&gt;0&lt;/strong&gt; as neither negative, nor positive, but occurring at the center of a number line continuum between the stream of positive integers on the right and the stream of negative integers on the left.&lt;/p&gt;

&lt;p&gt;For this reason, the most prevalent representation of signed binary numbers uses the &lt;strong&gt;2s Complement Method&lt;/strong&gt;, as it produces the least ambiguity of value. In short, &lt;strong&gt;2s Complement&lt;/strong&gt; represents negative numbers by “invert[ing] the bits (0 goes to 1, and 1 to 0) and add[ing] one to the resulting number” (Finley, &lt;em&gt;“Twos Complement”&lt;/em&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;What Does this Look Like in Practice?&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s say we want to represent the decimal number &lt;strong&gt;-34&lt;/strong&gt; using &lt;strong&gt;8 bits&lt;/strong&gt;. We would start with the unsigned binary representation of &lt;strong&gt;34&lt;/strong&gt;, which is&lt;/p&gt;


&lt;center&gt;&lt;b&gt;0010 0010&lt;/b&gt;&lt;/center&gt;


&lt;center&gt;&lt;b&gt;OR&lt;/b&gt;&lt;/center&gt;


&lt;center&gt;&lt;b&gt;0(2&lt;sup&gt;7&lt;/sup&gt;) + 0(2&lt;sup&gt;6&lt;/sup&gt;) + 1(2&lt;sup&gt;5&lt;/sup&gt;) + 0(2&lt;sup&gt;4&lt;/sup&gt;) +&lt;/b&gt;&lt;/center&gt;



&lt;center&gt;&lt;b&gt;0(2&lt;sup&gt;3&lt;/sup&gt;) + 0(2&lt;sup&gt;2&lt;/sup&gt;) + 0(2&lt;sup&gt;1&lt;/sup&gt;) + 1(2&lt;sup&gt;0&lt;/sup&gt;) =&lt;/b&gt;&lt;/center&gt;



&lt;center&gt;&lt;b&gt;010 + 010 + 3210 + 010 +&lt;/b&gt;&lt;/center&gt;



&lt;center&gt;&lt;b&gt;010 + 210 + 010 = 3410&lt;/b&gt;&lt;/center&gt;


&lt;p&gt; &lt;/p&gt;

&lt;p&gt;To change the sign of this integer to negative, first we invert each &lt;strong&gt;binary digit&lt;/strong&gt; arriving at&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;


&lt;center&gt;&lt;b&gt;1101 1101&lt;/b&gt;&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Finally, we add &lt;strong&gt;1&lt;/strong&gt; to the rightmost &lt;strong&gt;7 digits&lt;/strong&gt; (remembering that the leftmost digit is the &lt;strong&gt;&lt;em&gt;MSB&lt;/em&gt;&lt;/strong&gt; representing the sign of the integer). The resulting &lt;strong&gt;binary number&lt;/strong&gt; is&lt;/p&gt;


&lt;center&gt;&lt;b&gt;1101 1110&lt;/b&gt;&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;which now equals &lt;b&gt;-3410.&lt;/b&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Other Number Bases Relevant to Computers
&lt;/h2&gt;

&lt;p&gt;There are two other number bases that come into play with computers. By far, the more prevalent of the two is &lt;strong&gt;hexadecimal notation&lt;/strong&gt;, which is &lt;strong&gt;Base 16&lt;/strong&gt;. The other less frequently used number base is &lt;strong&gt;octal notation&lt;/strong&gt;, or &lt;strong&gt;Base 8&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Hexadecimal Notation&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Hexadecimal notation&lt;/strong&gt; must represent the numbers &lt;strong&gt;0-15&lt;/strong&gt; (16 digits) and does so using the Arabic numerals &lt;strong&gt;0-9&lt;/strong&gt; and the &lt;strong&gt;Latin alphabetical characters&lt;/strong&gt; of &lt;strong&gt;A-F&lt;/strong&gt;. As we will see, &lt;strong&gt;hexadecimal notation&lt;/strong&gt; has an intimate relationship to web development and design.&lt;/p&gt;

&lt;h3&gt;&lt;center&gt;Relevance to Web Development/Design&lt;/center&gt;&lt;/h3&gt;

&lt;p&gt;How is &lt;strong&gt;hexadecimal notation&lt;/strong&gt; relevant to &lt;strong&gt;web development&lt;/strong&gt; and design? From a certain perspective, this question could be likened unto the question of how oxygen is relevant to our pulmonary system. &lt;strong&gt;Hexadecimal notation&lt;/strong&gt; is related fundamentally to both back-end web development and front-end development/design.&lt;/p&gt;

&lt;p&gt;In addition to being quite useful to data structures employed on the back-end of web applications, &lt;strong&gt;hexadecimal notation&lt;/strong&gt; is linked intimately to &lt;strong&gt;CSS&lt;/strong&gt; (&lt;strong&gt;Cascading Style Sheets&lt;/strong&gt;) where developers use it to specify colors.&lt;/p&gt;

&lt;p&gt;How a monitor displays colors on a web page depends first and foremost upon the color depth afforded by standards of color encoding, and additionally upon what &lt;strong&gt;color spaces&lt;/strong&gt; are supported by the hardware and the browser on which one views the page. Most typical in present day applications is &lt;strong&gt;32-bit color&lt;/strong&gt;, which supports up to &lt;strong&gt;16,777,216&lt;/strong&gt; colors plus “…an alpha channel [that] can create…convincing gradients, shadows and transparencies” (&lt;em&gt;“What's the Difference between 16-Bit, 24-Bit, and 32-Bit Color?”&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;24-bit color&lt;/strong&gt; already provides &lt;strong&gt;16,777,216 colors&lt;/strong&gt;, but lacks the nuance of the &lt;strong&gt;alpha channel&lt;/strong&gt; provided in 32-bit color encoding.&lt;/p&gt;

&lt;h3&gt;&lt;center&gt;CSS Color Encoding&lt;/center&gt;&lt;/h3&gt;

&lt;p&gt;Color encoding in CSS is accomplished in several different ways, the most prevalent of which are &lt;strong&gt;RGB&lt;/strong&gt; (red-green-blue), &lt;strong&gt;RGBA&lt;/strong&gt; (red-green-blue-alpha), and &lt;strong&gt;HSL&lt;/strong&gt; (hue-saturation-lightness). There are other means of encoding color. The Mozilla Developer Network published a fascinating article that elucidates the &lt;strong&gt; CSS&lt;/strong&gt; data type, and enumerates keywords that define color using different color spaces, such as &lt;strong&gt;sRGB&lt;/strong&gt; (standard RGB) and &lt;strong&gt;HSL&lt;/strong&gt;, as we’ve already seen. Added to the list, to name but a few, are &lt;strong&gt;HSLA&lt;/strong&gt; (hue-saturation-lightness-alpha) and &lt;strong&gt;HWB/HWBA&lt;/strong&gt; (hue-whiteness-blackness/hue-whiteness-blackness-alpha)  (&lt;em&gt;“&amp;lt;color&amp;gt; - CSS: Cascading Style Sheets: MDN”&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;All these methods of encoding share one thing in common. They may be expressed in decimal or hexadecimal notation.&lt;br&gt;
For example, the &lt;strong&gt;rgb()CSS&lt;/strong&gt; property uses &lt;strong&gt;8 bits&lt;/strong&gt; per &lt;strong&gt;color channel&lt;/strong&gt; (red, green and blue).&lt;/p&gt;

&lt;p&gt;The syntax to encode the color &lt;strong&gt;yellow&lt;/strong&gt; is &lt;code&gt;rgb(255, 255, 0);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The equivalent syntax to express &lt;strong&gt;yellow&lt;/strong&gt; with &lt;strong&gt;hexadecimal&lt;/strong&gt; notation in &lt;strong&gt;CSS&lt;/strong&gt; is &lt;code&gt;color: #ffff00;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that the &lt;strong&gt;rgb()&lt;/strong&gt; property has &lt;strong&gt;3 decimal&lt;/strong&gt; numbers with a range of &lt;strong&gt;0-255&lt;/strong&gt;. Each of these decimal numbers is represented as &lt;strong&gt;8 bits&lt;/strong&gt;, for a total of &lt;strong&gt;24 bits&lt;/strong&gt;, corresponding to the &lt;strong&gt;24-bit&lt;/strong&gt; color encoding mentioned above.&lt;/p&gt;

&lt;p&gt;Since we are dealing with unsigned integers, we can dispense with &lt;strong&gt;2s complement&lt;/strong&gt; encoding for the moment.&lt;/p&gt;

&lt;p&gt;This would give us the following &lt;strong&gt;binary&lt;/strong&gt; representation of the color &lt;strong&gt;yellow&lt;/strong&gt;:&lt;/p&gt;


&lt;center&gt;&lt;b&gt;11111111 11111111 00000000&lt;/b&gt;&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The hexadecimal equivalent is &lt;strong&gt;three groupings of 2 hexadecimal digits&lt;/strong&gt;:&lt;/p&gt;


&lt;center&gt;&lt;b&gt;FF FF 00&lt;/b&gt;&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Note that two &lt;strong&gt;hexadecimal digits&lt;/strong&gt; are equivalent to &lt;strong&gt;8 bits&lt;/strong&gt;. Consequently, a total of &lt;strong&gt;6 hexadecimal digits&lt;/strong&gt; do the same job that &lt;strong&gt;24 binary digits&lt;/strong&gt; do. Quite a reduction in time spent on encoding color.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Three Different Ways of Counting&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;To show the means of counting provided by number bases historically used in computing, let’s take a look at their efficiency (from the human perspective, anyway) of notation from least to most:&lt;/p&gt;

&lt;h2&gt;&lt;center&gt;Counting in Binary&lt;/center&gt;&lt;/h2&gt;


&lt;center&gt;&lt;b&gt;0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010&lt;/b&gt;&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;And what does the large binary number of &lt;strong&gt;1010&lt;/strong&gt; equal in &lt;strong&gt;Base 10&lt;/strong&gt;? Here is the astonishing equivalency between the two:&lt;/p&gt;


&lt;center&gt;&lt;b&gt;10102 = 1(2&lt;sup&gt;3&lt;/sup&gt;) + 0(2&lt;sup&gt;2&lt;/sup&gt;) + 1(2&lt;sup&gt;1&lt;/sup&gt;) + 0(2&lt;sup&gt;0&lt;/sup&gt;) = 1010&lt;/b&gt;&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;&lt;center&gt;Counting in Octal&lt;/center&gt;&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Octal notation&lt;/strong&gt; or &lt;strong&gt;Base 8&lt;/strong&gt; is perhaps a bit less daunting. Here is how we count in &lt;strong&gt;Base 8&lt;/strong&gt;:&lt;/p&gt;


&lt;center&gt;&lt;b&gt;0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 14, 15, 16, 17, 20…77, 100&lt;/b&gt;&lt;/center&gt;

&lt;p&gt;Here, we have a total of &lt;strong&gt;8&lt;/strong&gt; digits representing the values from &lt;strong&gt;0-7&lt;/strong&gt;. So what is the equivalent of &lt;strong&gt;1008&lt;/strong&gt;? Here is the equation:&lt;/p&gt;


&lt;center&gt;&lt;b&gt;1008 = 1(8&lt;sup&gt;2&lt;/sup&gt;) + 0(8&lt;sup&gt;1&lt;/sup&gt;) + 0(8&lt;sup&gt;0&lt;/sup&gt;) =&lt;br&gt;6410 + 010 + 010 = 6410&lt;/b&gt;&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;As mentioned earlier, &lt;strong&gt;octal notation&lt;/strong&gt; does not come into a lot of use in present computer platforms. Its chief usage was with old minicomputers and mainframes such as the &lt;strong&gt;IBM System/360&lt;/strong&gt;, “announced by IBM on April 7, 1965…[and]…designed to cover both commercial and scientific applications and to cover a complete range of applications from small to large” (&lt;em&gt;“IBM System/360 - Wikipedia”&lt;/em&gt;).&lt;/p&gt;

&lt;h2&gt;&lt;center&gt;Counting in Hexadecimal&lt;/center&gt;&lt;/h2&gt;

&lt;p&gt;At first glance, counting in &lt;strong&gt;hexadecimal&lt;/strong&gt; (&lt;strong&gt;Base 16&lt;/strong&gt;) is daunting because one must represent the numbers &lt;strong&gt;0-15&lt;/strong&gt; with single digits. Thus we count like this:&lt;/p&gt;


&lt;center&gt;&lt;b&gt;0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, &lt;br&gt;10, 11, 12, 13, 14, 15, 1A, 1B, 1C, 1D, 1E, 1F, 20…&lt;/b&gt;&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;This schema challenges our perception. After all, we do not have &lt;strong&gt;16&lt;/strong&gt; fingers with which we can count &lt;strong&gt;16&lt;/strong&gt; digits. To our way of thinking, &lt;strong&gt;16&lt;/strong&gt; is &lt;strong&gt;1&lt;/strong&gt; unit of ten plus 6 ones. But now let’s see the equivalent of &lt;strong&gt;1A&lt;/strong&gt; in decimal:&lt;/p&gt;


&lt;center&gt;&lt;b&gt;1A16 = 1(16&lt;sup&gt;1&lt;/sup&gt;) + A(16&lt;sup&gt;0&lt;/sup&gt;) =&lt;br&gt;1610 + 1010 = 2610&lt;/b&gt;&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;This representation is nice and compact. But its real advantage becomes more obvious in the representation of larger numbers, such as those uses to represent color in &lt;strong&gt;CSS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As we know, each &lt;strong&gt;hexadecimal&lt;/strong&gt; digit is equivalent to &lt;strong&gt;4 bits&lt;/strong&gt; (&lt;strong&gt;1 nibble&lt;/strong&gt;). Because a &lt;strong&gt;byte&lt;/strong&gt; is comprised of &lt;strong&gt;8 bits&lt;/strong&gt;, what is represented in &lt;strong&gt;8&lt;/strong&gt; digits in &lt;strong&gt;binary&lt;/strong&gt; can be represented in only &lt;strong&gt;2&lt;/strong&gt; digits in &lt;strong&gt;hexadecimal&lt;/strong&gt;. This proves quite advantageous for coding in &lt;strong&gt;CSS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each &lt;strong&gt;channel—red, green and blue—&lt;/strong&gt;have a decimal value of &lt;strong&gt;192&lt;/strong&gt;, which when notated using the &lt;code&gt;rgb()&lt;/code&gt; property of &lt;strong&gt;CSS&lt;/strong&gt; is &lt;code&gt;rgb(192, 192, 192);&lt;/code&gt; Though not as cumbersome as the &lt;strong&gt;binary notation&lt;/strong&gt; specified above, I think we can agree that it is yet more compact to use the &lt;strong&gt;hexadecimal CSS rule&lt;/strong&gt; for the color &lt;b&gt;silver—&lt;/b&gt;&lt;code&gt;color: #c0c0c0;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

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

&lt;p&gt;We have covered in some depth the seemingly arcane, yet vital concept of number encoding on computers and distilled this knowledge through the looking glass of web design/development. Though not linked pronouncedly to the technologies of web development and design, the importance of number encoding to web technologies is irrefutable and overarching.&lt;/p&gt;

&lt;p&gt;In today’s &lt;strong&gt;world wide web&lt;/strong&gt;, what we used to call web sites are undeniably trending toward &lt;strong&gt;web applications&lt;/strong&gt; with far more user interaction than ever before, and beginning to look more like their desktop counterparts.&lt;/p&gt;

&lt;p&gt;As a result, &lt;strong&gt;web applications&lt;/strong&gt; can involve manipulation of numbers, often at a more sophisticated level than previously encountered.&lt;/p&gt;

&lt;p&gt;Finally, if we are truly honest with ourselves, sometimes when we do need to bother with the complexities of binary or hexadecimal calculations, it can become tedious and frustrating because our skills have become a bit rusty.&lt;br&gt;
The intention of this article is to give some underpinning, and perhaps a new appreciation for these concepts by those who have long been away from rudimentary theory.&lt;/p&gt;




&lt;h3&gt;&lt;center&gt;References&lt;/center&gt;&lt;/h3&gt;

&lt;p&gt;“Semiconductor.” Wikipedia, Wikimedia Foundation, 30 May 2022,&lt;br&gt;     &lt;a href="https://en.wikipedia.org/wiki/Semiconductor" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Semiconductor&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“Microprocessor Circuits.” &lt;em&gt;Encyclopædia Britannica&lt;/em&gt;, Encyclopædia&lt;br&gt;     Britannica, Inc.,&lt;br&gt;     &lt;a href="https://www.britannica.com/technology/integrated-circuit/Microprocessor-circuits" rel="noopener noreferrer"&gt;https://www.britannica.com/technology/integrated-circuit/Microprocessor-circuits&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“R/Explain Like I’m Five - ELI5: How Did Old Games and Computers Display&lt;br&gt;     Large Numbers?” &lt;em&gt;Reddit&lt;/em&gt;, Reddit.com,&lt;br&gt;     &lt;a href="https://www.reddit.com/r/explainlikeimfive/comments/5vqye0/eli5_how_did_old_games_and_computers_display/." rel="noopener noreferrer"&gt;https://www.reddit.com/r/explainlikeimfive/comm ents/5vqye0/eli5_how_did_old_games_and_computers_display/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kernighan, Brian W., and Dennis M. Ritchie. “2.2 Data Types and Sizes.” &lt;em&gt;The&lt;br&gt;     C Programming Language&lt;/em&gt;, Prentice Hall, Englewood Cliffs, NJ, 2016.&lt;/p&gt;

&lt;p&gt;“Representation of Negative Binary Numbers.” &lt;em&gt;GeeksforGeeks&lt;/em&gt;, 22 Apr. 2020,&lt;a href="https://www.geeksforgeeks.org/representation-of-negative-binary-numbers/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/representation-of-negative-binary-numbers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finley, Thomas. “Two's Complement.” &lt;em&gt;Duke CPS 104 Codex&lt;/em&gt;,&lt;br&gt;      Cornell University,&lt;br&gt;     Apr. 2000,&lt;br&gt;     &lt;a href="https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html" rel="noopener noreferrer"&gt;https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“What's the Difference between 16-Bit, 24-Bit, and 32-Bit Color?”&lt;br&gt;     Computer Hope'sFree Computer Help, Computer Hope, 13 Nov. 2018, &lt;br&gt;     &lt;a href="https://www.computerhope.com/issues/ch001557.htm#:~:text=Like%2024%2Dbit%20color%2C%2032,colors%2C%20more%20memory%20is%20required" rel="noopener noreferrer"&gt;https://www.computerhope.com/issues/ch001557.htm#:~:text=Like%2024%Dbit%20olor%2C%2032,colors%2C%20more%20memory%20is%20required&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“&amp;lt;color&amp;gt; - CSS: Cascading Style Sheets: MDN.”&lt;br&gt;     &lt;em&gt;CSS: Cascading Style Sheets | MDN&lt;/em&gt;, Mozilla Developer Network,&lt;br&gt;     &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/color_value" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/CSS/color_value&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;“IBM System/360.” Wikipedia, Wikimedia Foundation, 22 Feb. 2021,&lt;br&gt;     &lt;a href="https://en.wikipedia.org/wiki/IBM_System/360" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/IBM_System/360&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>gamedev</category>
    </item>
  </channel>
</rss>
