<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mahmoud Mohamed on Medium]]></title>
        <description><![CDATA[Stories by Mahmoud Mohamed on Medium]]></description>
        <link>https://medium.com/@mmahmoud092002?source=rss-d4370bbb7f43------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4gErGLyIQRUgh-YwWwnRfg.jpeg</url>
            <title>Stories by Mahmoud Mohamed on Medium</title>
            <link>https://medium.com/@mmahmoud092002?source=rss-d4370bbb7f43------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 24 May 2026 02:24:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mmahmoud092002/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Understanding Higher Order Functions in Javascript .]]></title>
            <link>https://medium.com/@mmahmoud092002/understanding-higher-order-functions-in-javascript-510a88d75a6b?source=rss-d4370bbb7f43------2</link>
            <guid isPermaLink="false">https://medium.com/p/510a88d75a6b</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[higher-function]]></category>
            <category><![CDATA[function]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Mahmoud Mohamed]]></dc:creator>
            <pubDate>Wed, 29 May 2024 03:09:31 GMT</pubDate>
            <atom:updated>2024-05-29T03:09:31.123Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding Higher Order Functions ( HOF ) in Javascript .!</h3><h4>What is a Higher Order Function ?</h4><p>A higher order function is a function that takes one or more functions as arguments, or returns a function as its result or both.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IZa8jLdJclBO9JiB3Mi6-w.png" /><figcaption>Higeher Order Function</figcaption></figure><h4>How to use Higher Order Functions ?</h4><p>You can use higher order functions in some of ways :</p><ul><li>When you work with arrays, you can use the <strong>map()</strong>, <strong>reduce()</strong>, <strong>filter()</strong>, <strong>find()</strong>, <strong>sort() </strong>and <strong>forEach() </strong>functions to manipulate with data of arrays.</li></ul><p>Now let’s understand each one of these in details.</p><blockquote>How to use <strong><em>map()</em></strong> function ?</blockquote><p>The <strong>map()</strong> function takes an array of values and applies a specific action ( operation ) to each value in the array.</p><p>It does not effect on the original array.</p><p>It is often used to transform an array of data into a new array with a different structure.</p><h4>Example 1</h4><p>Suppose we have an array contains on a set of numbers, and we want to add 10 to every number in our array.</p><pre>const arr = [1, 2, 3, 4, 5];<br>const res_arr = arr.map((num) =&gt; num += 10)<br>console.log(arr); //[1, 2, 3, 4, 5]<br>console.log(res_arr); //[11, 12, 13, 14, 15]</pre><h4>Example 2</h4><p>Suppose we have an array of users objects ( firstName, lastName, age ) we only want their first and last name ( in one form ).</p><pre>const users = [<br>    {firstName: &#39;Mahmoud&#39;, lastName: &#39;Mohamed&#39;, age: 25},<br>    {firstName: &#39;Ahmed&#39;, lastName: &#39;Mohamed&#39;, age: 30},<br>    {firstName: &#39;Omar&#39;, lastName: &#39;Mohamed&#39;, age: 35},<br>]<br><br>const result = users.map((user) =&gt; user.firstName + &#39; &#39; + user.lastName)<br>console.log(result); //[&#39;Mahmoud Mohamed&#39;, &#39;Ahmed Mohamed&#39;, &#39;Omar Mohamed&#39;]</pre><blockquote>How to use <strong><em>filter()</em></strong> function ?</blockquote><p>The <strong>filter()</strong> function takes an array and returns a new array with only the values that meet certain criteria (condition).</p><p>It does not effect on the original array.</p><p>It is often used to select a subset of data from an array based on certain criteria (condition).</p><h4>Example 1</h4><p>Suppose we have an array contains on a set of numbers, and we want to return only the odd numbers from our array.</p><pre>const arr = [1, 2, 3, 4, 5];<br>const res_arr= arr.filter((num) =&gt; num % 2) // filter out odd numbers<br>console.log(arr); // [1, 2, 3, 4, 5]<br>console.log(res_arr); // [1, 3, 5]</pre><h4>Example 2</h4><p>Suppose we have an array of users objects ( firstName, lastName, age ) and we want to return only the users having age greater than 30 from our array.</p><pre>const users = [<br>    {firstName: &#39;Mahmoud&#39;, lastName: &#39;Mohamed&#39;, age: 25},<br>    {firstName: &#39;Ahmed&#39;, lastName: &#39;Mohamed&#39;, age: 30},<br>    {firstName: &#39;Omar&#39;, lastName: &#39;Mohamed&#39;, age: 35},<br>]<br><br>// Find the users with age greater than 30<br>const result = users.filter(({age}) =&gt; age &gt; 30)<br>console.log(result); // [{firstName: &#39;Omar&#39;, lastName: &#39;Mohamed&#39;, age: 35}]</pre><blockquote>How to use <strong><em>reduce()</em></strong> function ?</blockquote><p>The <strong>reduce()</strong> function takes an array and returns result as a single value.</p><p>In the case of the reduce() method, you should used it when you want to perform a specific operation on the elements of an array and return a single value as a result.</p><p>The “single value” refers to the accumulated ( total ) result of applying a function to the elements of an array repeatedly .</p><p>It does not effect on the original array.</p><h4>Example 1</h4><p>Suppose we have an array contains on a set of numbers, and we want to return sum of all numbers in our array.</p><pre>const numbers = [1, 2, 3, 4, 5];<br>const sum = numbers.reduce((total, currentValue) =&gt; {<br>    return total + currentValue;<br>}, 0)<br>console.log(sum); // 15</pre><h4>Example 2</h4><p>Suppose we have an array contains on a set of numbers, and we want to find the maximum value in our array.</p><pre>let numbers = [5, 20, 100, 60, 1];<br>const maxValue = numbers.reduce((max, curr) =&gt; {<br>    if(curr &gt; max) <br>    max = curr;<br>    return max;<br>});<br>console.log(maxValue); // 100</pre><blockquote>How to use <strong><em>find()</em></strong> function ?</blockquote><p>The <strong>find()</strong> function takes an array and allows you to search through the array and return the first element that satisfies a provided testing function ( return first match ).</p><p>If no elements satisfy the testing function, <strong>find()</strong> returns undefined.</p><p>It does not effect on the original array.</p><p>It is often used to serach on something in array and return first match.</p><h4>Example 1</h4><p>Suppose we have an array of students objects representing a set of students, and we want to find the first student who has a grade higher than 85.</p><pre>const students = [<br>    { name: &#39;Mahmoud&#39;, grade: 82 },<br>    { name: &#39;Mohamed&#39;, grade: 90 },<br>    { name: &#39;Omar&#39;, grade: 88 },<br>    { name: &#39;Ahmed&#39;, grade: 75 }<br>];<br><br>const topStudent = students.find(student =&gt; student.grade &gt; 85);<br><br>console.log(topStudent); // { name: &#39;Mohamed&#39;, grade: 90 }</pre><blockquote>How to use <strong><em>sort()</em></strong> function ?</blockquote><p>The <strong>sort()</strong> function takes an array and returns a new sorted array.</p><p>It is often used to sort the elements of an array in place and returns the sorted array.</p><h4>Example 1</h4><p>Suppose we have an array contains on a set of numbers, and we want to return sorted numbers from our array.</p><pre>const numbers = [40, 1, 5, 200];<br>sorted_nums = numbers.sort((a, b) =&gt; a - b);<br>console.log(numbers); // [1, 5, 40, 200]<br>console.log(sorted_nums); // [1, 5, 40, 200]</pre><h4>Example 2</h4><p>Suppose we have an array of students objects representing a set of students, and we want to sort array based on grade.</p><pre>const students = [<br>    { name: &#39;Mahmoud&#39;, grade: 82 },<br>    { name: &#39;Mohamed&#39;, grade: 90 },<br>    { name: &#39;Omar&#39;, grade: 88 },<br>    { name: &#39;Ahmed&#39;, grade: 75 }<br>];<br>students.sort((a, b) =&gt; a.grade - b.grade);<br>console.log(students);<br><br>// [<br>//   { name: &#39;Ahmed&#39;, grade: 75 },<br>//   { name: &#39;Mahmoud&#39;, grade: 82 },<br>//   { name: &#39;Omar&#39;, grade: 88 },<br>//   { name: &#39;Mohamed&#39;, grade: 90 }<br>// ]</pre><blockquote>How to use <strong><em>forEach()</em></strong> function ?</blockquote><p>The <strong>forEach()</strong> function takes an array of values and executes a provided function once for each array element.</p><p>It is often used to perform side effects, such as modifying elements.</p><h4><strong>Example 1</strong></h4><p>Suppose we have an array contains on a set of numbers, and we want to return sum of numbers from our array.</p><pre>const numbers = [1, 2, 3, 4, 5];<br>let sum = 0;<br><br>numbers.forEach(number =&gt; {<br>    sum += number;<br>});<br><br>console.log(sum); // 15</pre><h4>Differences Between forEach() and Other Iteration Methods</h4><p>forEach() vs map()</p><p>forEach() is used for side effects and does not return a new array, whereas map() returns a new array with the results of applying a function to every element.</p><h3>Benefits of Higher Order Functions</h3><p>First, higher order functions can help improve the legibility of your code by making it more easy to understand.<br>This can help speed up the development process and make it easier to debug code.</p><p>Second, higher order functions can help organize your code into smaller parts, making it easier to maintain and extend.</p><h3>Conclusion</h3><p>This article showed what a higher order function is, the benefits of using them, and how to use them in practical applications.</p><p>Now, whenever you try to use <strong>map()</strong>, <strong>filter(), reduce()</strong>, <strong>find()</strong>, <strong>sort()</strong>, and <strong>forEach()</strong> methods and get confused, just remember the following :</p><ul><li>Use map when you want to transform an array.</li><li>Use filter to select a subset of data from an array.</li><li>Use reduce when you want to return a single value as a result.</li><li>Use find when you want to search on a specific value and return the first matach.</li><li>Use sort to sort array elements.</li><li>Use forEach to perform side effects on elements without create a new array in result.</li></ul><p><em>Thanks for reading and see you next time.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=510a88d75a6b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>