<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: cathrynbmcdonald</title>
    <description>The latest articles on DEV Community by cathrynbmcdonald (@cathrynbmcdonald).</description>
    <link>https://dev.to/cathrynbmcdonald</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%2F1068565%2F034857e5-54a3-4fed-87b6-8e192f826f3f.jpeg</url>
      <title>DEV Community: cathrynbmcdonald</title>
      <link>https://dev.to/cathrynbmcdonald</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cathrynbmcdonald"/>
    <language>en</language>
    <item>
      <title>Higher-Order Functions in Javascript</title>
      <dc:creator>cathrynbmcdonald</dc:creator>
      <pubDate>Thu, 04 May 2023 20:09:02 +0000</pubDate>
      <link>https://dev.to/cathrynbmcdonald/higher-order-functions-in-javascript-5gbf</link>
      <guid>https://dev.to/cathrynbmcdonald/higher-order-functions-in-javascript-5gbf</guid>
      <description>&lt;h2&gt;
  
  
  What are &lt;code&gt;HoF&lt;/code&gt;'s and Why Should We Use Them?
&lt;/h2&gt;

&lt;p&gt;Javascript functions that take in another function as an argument or return another function are called higher-order functions. Using these functions allows us to save time, write cleaner code, and reduce the possibility of typing errors. Let’s take a look at each type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type 1: What are Functions That Take Another Function as an Argument?
&lt;/h2&gt;

&lt;p&gt;Suppose we need to write several functions so we can add different types of punctuation to an array of names. Instead of writing similar code multiple times, we can write a single &lt;code&gt;HoF&lt;/code&gt; that takes in a function as an argument. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function arrayPunctuation(array, func) {
   var newArray = [];
   for (let i = 0; i &amp;lt; array.length; i++) {
      newArray.push(func(array[i]));
   }
   return newArray;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function can then be used to add different types of punctuation to each element in an array, depending on the function we pass in as an argument. For example, we might pass in a function which &lt;code&gt;capitalizes&lt;/code&gt; each element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function capitalize(string) {
   return string.toUpperCase();
}

var capital = arrayPunctuation(['John', 'Mary'], capitalize);
console.log(capital);  // output: ['JOHN', 'MARY']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, we can call the function and pass in a function that adds a &lt;code&gt;comma&lt;/code&gt; after each element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addComma(string) {
   return `${string},`;
}

var comma = arrayPunctuation(['John', 'Mary'], addComma);
console.log(comma);  // output: ['John,', 'Mary,']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or call the function and pass in a function that adds the title of &lt;code&gt;Dr.&lt;/code&gt; before each element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addTitle(string) {
   return `Dr. ${string}`;
}

var title = arrayPunctuation(['John', 'Mary'], addTitle);
console.log(title);  // output: ['Dr. John', 'Dr. Mary']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Type 2: What are Functions That Return Another Function?
&lt;/h2&gt;

&lt;p&gt;Now suppose we need to write several functions so we can add different types of punctuation to the end of a string. Instead of writing similar code multiple times, we can write a single &lt;code&gt;HoF&lt;/code&gt; that returns a function. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function stringPunctuation(string) {
   return function(punctuation) {
      return string + punctuation;
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function can then be used to add different types of punctuation to the end of a string, depending on the arguments we pass in. For example, we can call the function and pass in the strings &lt;code&gt;Where&lt;/code&gt; and &lt;code&gt;?&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var question = stringPunctuation(`Where`)(`?`);
console.log(question);  // output: `Where?`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or we can call the function and pass in the strings &lt;code&gt;Wow&lt;/code&gt; and &lt;code&gt;!&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var exclamation = stringPunctuation(`Wow`)(`!`);
console.log(exclamation);  // output: `Wow!`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we've learned about the importance of using higher order functions. By using these functions we can break our code into smaller chunks, which makes it easier to understand and debug. We can also speed up the development process.  &lt;code&gt;HoF&lt;/code&gt;'s are a powerful addition to our coding toolbox.  &lt;/p&gt;

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