<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Sobit Prasad - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Sobit Prasad - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 21 May 2026 04:57:43 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/sobitprasad/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How is == Different from === in JavaScript? Strict vs Loose Equality Explained ]]>
                </title>
                <description>
                    <![CDATA[ If you are reading this blog, you're probably learning JavaScript – and that's awesome. Double equals (==) and triple equals (===) in JavaScript often make beginners scratch their heads. This doesn't mean that you should fear JavaScript, in fact jarg... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/loose-vs-strict-equality-in-javascript/</link>
                <guid isPermaLink="false">66d4614b73634435aafcefe0</guid>
                
                    <category>
                        <![CDATA[ Equality ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Sobit Prasad ]]>
                </dc:creator>
                <pubDate>Tue, 14 Feb 2023 18:09:26 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/shapes--1--1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you are reading this blog, you're probably learning JavaScript – and that's awesome.</p>
<p>Double equals (==) and triple equals (===) in JavaScript often make beginners scratch their heads. This doesn't mean that you should fear JavaScript, in fact jargon like this makes JavaScript even more beautiful once you know how it works.</p>
<h2 id="heading-what-are-and-in-javascript">What are <code>==</code> and <code>===</code> in JavaScript?</h2>
<p>Now, one thing we need to remember is that both <code>==</code> and <code>===</code> are used for comparisons and to find the degree of sameness or equality between the things we are comparing.</p>
<p>Both <code>==</code> and <code>===</code> returns <strong>true</strong> if they find equality and <strong>false</strong> otherwise. But there is a catch: <code>==</code> and <code>===</code> use different criteria to measure the degree of equality.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/shapes--2-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>With that said, let's understand how <code>==</code> (double equals) is different from <code>===</code> (triple equals) using different examples.</p>
<h2 id="heading-how-double-equals-works-with-examples">How Double Equals (==) Works – with Examples</h2>
<p>Double equals (==) is often referred to as 'loose equality' because it performs type coercion before making any comparison.</p>
<p>This means that if the datatypes of the operands we are comparing are different, then the JavaScript Engine automatically converts one of the operands to be the same as the other one in order to make the comparison possible.</p>
<p>Let's understand with the help of an example.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> a = <span class="hljs-number">100</span>;
<span class="hljs-keyword">const</span> b = <span class="hljs-string">'100'</span>;

<span class="hljs-built_in">console</span>.log(a == b) <span class="hljs-comment">// true</span>
</code></pre>
<p>In the above example, we have two variables <code>a</code> and <code>b</code>. The type of variable <code>a</code> is <code>number</code> and the type of variable <code>b</code> is <code>string</code>.</p>
<p>Now, when we compare the two variables using double equals (<code>==</code>) we get <code>true</code> as our output.</p>
<p>This is because the type of the variable <code>a</code> is converted to a <code>string</code> before making the comparison.</p>
<p>After the comparison, the value is checked in both the variables. If it's the same, we will get <code>true</code>, and we'll get <code>false</code> otherwise. In our case, it's <code>true</code>.</p>
<p><strong>It is important to note that the actual values remains unchanged. It only implicitly gets converted while comparing.</strong></p>
<h3 id="heading-rules-for-type-coercion">Rules for Type Coercion</h3>
<p>The above example is quite easy, isn't it? So, let's test it again with an another example. And after that, we will explore the rules for type coercion.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> a = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">const</span> b = <span class="hljs-string">'true'</span>;

<span class="hljs-built_in">console</span>.log(a == b)
</code></pre>
<p>Now, what do you think will be the output? If your answer was <code>true</code>, unfortunately that's not correct. But if you figured out that it was <code>false</code>, then congrats.</p>
<p>If your answer was wrong, don't worry because we are going to learn some rules that will help you understand it even better.</p>
<p>So, here are the rules for type coercion in JavaScript:</p>
<ul>
<li><p>If either operand is a <code>string</code>, the other operand will be converted to a <code>string</code>.</p>
</li>
<li><p>If either operand is a <code>number</code>, the other operand will be converted to a <code>number</code>.</p>
</li>
<li><p>If either operand is a <code>boolean</code>, it will be converted to a <code>number</code> (<code>true</code> becomes <code>1</code> and <code>false</code> becomes <code>0</code>).</p>
</li>
<li><p>If one operand is an <code>object</code> and the other is a primitive value, the object will be converted to a primitive value before the comparison is made.</p>
</li>
<li><p>If one of the operands is <code>null</code> or <code>undefined</code>, the other must also be <code>null</code> or <code>undefined</code> to return <code>true</code>. Otherwise it will return <code>false</code>.</p>
</li>
</ul>
<p>Now, from point three, you know why our answer was <code>false</code> in the above example.</p>
<p>It is because the value of the variable <code>a</code> (<code>true</code>) gets converted to a number before the comparison. So after comparison – where we're now comparing 1 and <code>'true'</code> – we get <code>false</code> because the variables contain different values.</p>
<h2 id="heading-how-triple-equals-works-with-examples">How Triple Equals (===) Works – with Examples</h2>
<p>Triple equals (===), also referred to as "strict equality", works similarly to how double equals (==) works, with one important difference: it does not convert the types of the operands before comparing.</p>
<p>While comparing the variables, it first checks if the types differ. If they do, it returns <code>false</code>. If the types match, then it checks for the value. If the values are same and are not numbers, it returns <code>true</code>.</p>
<p>Finally, if both the operands are numbers and are not <code>NaN</code>, and they have the same value, then it returns <code>true</code>. Otherwise, <code>false</code>.</p>
<p>Let's understand this with the help of examples:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> a = <span class="hljs-number">100</span>;
<span class="hljs-keyword">const</span> b = <span class="hljs-string">'100'</span>;

<span class="hljs-built_in">console</span>.log(a === b);
</code></pre>
<p>We have taken the same example as above, but instead of comparing with double (==) equals we are comparing with triple equals (===).</p>
<p>So, you may have guessed the answer already. Yeah, it's <code>false</code>, Why? Because the type of variable <code>a</code> is number and type of variable <code>b</code> is string.</p>
<p>While comparing, triple equals checks for the types of the operands first – and those types differ in this example. So, it returns <code>false</code>.</p>
<p>Let's look at another example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> a = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">const</span> b = <span class="hljs-number">1</span>;

<span class="hljs-built_in">console</span>.log(a === b);
</code></pre>
<p>In the above example, we have two variables <code>a</code> and <code>b</code>. The type of variable <code>a</code> is boolean and the type of variable <code>b</code> is number.</p>
<p>So, if we're comparing using triple equals (===), it will return <code>false</code> – because again, the variables have different types.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>The <code>==</code> and <code>===</code> operators in JavaScript are comparison operators that we use to determine if two values are equal or not.</p>
<p>The <code>==</code> operator performs a loose equality comparison that performs type coercion if necessary to make the comparison possible.</p>
<p>The <code>===</code> operator, on the other hand, performs a strict equality comparison that does not perform type coercion and requires the operands to have the same type (as well as the same value).</p>
<p>Type coercion in JavaScript can sometimes lead to unexpected results, so it's mostly recommended to use the strict equality operator <code>===</code> instead of the loose equality operator <code>==</code>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Higher Order Functions in JavaScript – Explained with Practical Examples ]]>
                </title>
                <description>
                    <![CDATA[ As a web developer, you should always strive to learn new techniques and discover ways to work smarter with JavaScript. One such technique is using higher order functions. Higher order functions are functions that take one or more functions as argume... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/higher-order-functions-in-javascript-explained/</link>
                <guid isPermaLink="false">66d46149868774922c885020</guid>
                
                    <category>
                        <![CDATA[ functions ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Sobit Prasad ]]>
                </dc:creator>
                <pubDate>Tue, 03 Jan 2023 22:11:58 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/12/higher-order-functions-in-javascript-2.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>As a web developer, you should always strive to learn new techniques and discover ways to work smarter with JavaScript.</p>
<p>One such technique is using higher order functions. Higher order functions are functions that take one or more functions as arguments, or return a function as their result.</p>
<p>In this article, we will delve into what a higher order function is, the benefits of using them, and how to use them in practical applications.</p>
<h2 id="heading-what-is-a-higher-order-function">What is a Higher Order Function?</h2>
<p>A higher order function is a function that takes one or more functions as arguments, or returns a function as its result.</p>
<p>There are several different types of higher order functions like map and reduce. We will discuss these later in this tutorial. But before that let's first dive deep into what higher order functions are.</p>
<pre><code class="lang-js"><span class="hljs-comment">// Callback function, passed as a parameter in the higher order function</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callbackFunction</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'I am  a callback function'</span>);
}

<span class="hljs-comment">// higher order function</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">higherOrderFunction</span>(<span class="hljs-params">func</span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'I am higher order function'</span>)
    func()
}

higherOrderFunction(callbackFunction);
</code></pre>
<p>In the above code <code>higherOrderFunction()</code> is an HOF because we are passing a callback function as a parameter to it.</p>
<p>The above example is quite simple isn't it? So let's expand it further and see how you can use HOFs to write more concise and modular code.</p>
<h3 id="heading-how-higher-order-functions-work">How Higher Order Functions Work</h3>
<p>So, suppose I want you to write a function that calculates the area and diameter of a circle. As a beginner, the first solution that comes to our mind is to write each separate function to calculate area or diameter.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> radius = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// function to calculate area of the circle</span>
<span class="hljs-keyword">const</span> calculateArea =  <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">radius</span>) </span>{
    <span class="hljs-keyword">const</span> output = [];
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i&lt; radius.length; i++){
        output.push(<span class="hljs-built_in">Math</span>.PI * radius[i] * radius[i]);
    }
    <span class="hljs-keyword">return</span> output;
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// function to calculate diameter of the circle</span>
<span class="hljs-keyword">const</span> calculateDiameter =  <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">radius</span>) </span>{
    <span class="hljs-keyword">const</span> output = [];
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i&lt; radius.length; i++){
        output.push(<span class="hljs-number">2</span> * radius[i]);
    }
    <span class="hljs-keyword">return</span> output;
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(calculateArea(radius));
<span class="hljs-built_in">console</span>.log(calculateDiameter(radius))
</code></pre>
<p>But have you noticed the problem with the above code? Aren't we writing almost the same function again and again with slightly different logic? Also, the functions we have written aren't reusable, are they?</p>
<p>So, let's see how we can write the same code using HOFs:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> radius = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// logic to clculate area</span>
<span class="hljs-keyword">const</span> area = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">radius</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.PI * radius * radius;
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// logic to calculate diameter</span>
<span class="hljs-keyword">const</span> diameter = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">radius</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-number">2</span> * radius;
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// a reusable function to calculate area, diameter, etc</span>
<span class="hljs-keyword">const</span> calculate = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">radius, logic</span>)</span>{ 
    <span class="hljs-keyword">const</span> output = [];
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; radius.length; i++){
        output.push(logic(radius[i]))
    }
    <span class="hljs-keyword">return</span> output;
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(calculate(radius, area));
<span class="hljs-built_in">console</span>.log(calculate(radius, diameter));
</code></pre>
<p>Now, as you can see in the above code, we have only written a single function <code>calculate()</code> to calculate the area and diameter of the circle. We only need to write the logic and pass it to <code>calculate()</code> and the function will do the job.</p>
<p>The code that we have written using HOFs is concise and modular. Each function is doing its own job and we are not repeating anything here.</p>
<p>Suppose in the future if we want to write a program to calculate the circumference of the circle. We can simply write the logic to calculate the circumference and pass it to the <code>calculate()</code> function.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> circumeference = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">radius</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-number">2</span> * <span class="hljs-built_in">Math</span>.PI * radius;
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(calculate(radius, circumeference));
</code></pre>
<h2 id="heading-how-to-use-higher-order-functions">How to Use Higher Order Functions</h2>
<p>You can use higher order functions in a variety of ways.</p>
<p>When working with arrays, you can use the <code>map()</code>, <code>reduce()</code>, <code>filter()</code>, and <code>sort()</code> functions to manipulate and transform data in an array.</p>
<p>When working with objects, you can use the <code>Object.entries()</code> function to create a new array from an object.</p>
<p>When working with functions, you can use the <code>compose()</code> function to create complex functions from simpler ones.</p>
<h2 id="heading-how-to-use-some-important-higher-order-functions">How to Use Some Important Higher Order Functions</h2>
<p>There are various built in HOFs, and some of the most common ones are map(), filter() and reduce(). So let's understand each one of these in detail.</p>
<h3 id="heading-how-to-use-map-in-javascript"><strong>How to use</strong> <code>map()</code> in JavaScript</h3>
<p>The <code>map()</code> function takes an array of values and applies a transformation to each value in the array. It does not mutate the original array. It is often used to transform an array of data into a new array with a different structure.</p>
<p>Let's understand with the help of examples.</p>
<p><strong>Example 1</strong>: Suppose we want to add 10 to every element in a array. We can use the <code>map()</code> method to map over every element in the array to add 10 to it.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];
<span class="hljs-keyword">const</span> output = arr.map(<span class="hljs-function">(<span class="hljs-params">num</span>) =&gt;</span> num += <span class="hljs-number">10</span>)
<span class="hljs-built_in">console</span>.log(arr); <span class="hljs-comment">// [1, 2, 3, 4, 5]</span>
<span class="hljs-built_in">console</span>.log(output); <span class="hljs-comment">// [11, 12, 13, 14, 15]</span>
</code></pre>
<p>In the above example, <code>arr</code> is an array with five elements: 1, 2, 3, 4, and 5. <code>map</code> is a method that we use to apply a function to each element in an array, and it returns a new array with the modified elements.</p>
<p>The callback function that is being passed to <code>map</code> uses the arrow function syntax, and it takes a single argument <code>num</code>. This function adds 10 to <code>num</code> (every element in the array) and returns the result.</p>
<p><strong>Example 2</strong>: Here we have an array of users. Suppose we only want their first and last name. We can simply use the <code>map()</code> method to extract it from the <code>users</code> array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> users = [
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'John'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span>},
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'Jane'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">30</span>},
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'Jack'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span>},
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'Jill'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">40</span>},
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'Joe'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">45</span>},
]

<span class="hljs-keyword">const</span> result = users.map(<span class="hljs-function">(<span class="hljs-params">user</span>) =&gt;</span> user.firstName + <span class="hljs-string">' '</span> + user.lastName)
<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// ['John Doe', 'Jane Doe', 'Jack Doe', 'Jill Doe', 'Joe Doe']</span>
</code></pre>
<p>In the above code, <code>users</code> is an array of objects representing users. Each object has three properties: <code>firstName</code>, <code>lastName</code>, and <code>age</code>.</p>
<p>We are mapping over each user using the <code>map()</code> method to extract the properties <code>firstName</code> and <code>lastName</code>.</p>
<p>The callback function takes a single argument <code>user</code> which represents an element in the <code>users</code> array (an object).</p>
<p>The function concatenates the <code>firstName</code> and <code>lastName</code> properties of the <code>user</code> object, and returns the result.</p>
<h3 id="heading-how-to-use-filter-in-javascript"><strong>How to Use</strong> <code>filter()</code> in JavaScript</h3>
<p>The <code>filter()</code> function takes an array and returns a new array with only the values that meet certain criteria. It also does not mutate the original array. It is often used to select a subset of data from an array based on certain criteria.</p>
<p><strong>Example 1</strong>: You can use <code>filter()</code> to return only the odd numbers from an array of numbers.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];
<span class="hljs-keyword">const</span> output = arr.filter(<span class="hljs-function">(<span class="hljs-params">num</span>) =&gt;</span> num % <span class="hljs-number">2</span>) <span class="hljs-comment">// filter out odd numbers</span>
<span class="hljs-built_in">console</span>.log(arr); <span class="hljs-comment">// [1, 2, 3, 4, 5]</span>
<span class="hljs-built_in">console</span>.log(output); <span class="hljs-comment">// [1, 3, 5]</span>
</code></pre>
<p>In the above code, <code>arr</code> is an array with five elements: 1, 2, 3, 4, and 5. <code>filter</code> is a method that is used to create a new array with elements that pass a test specified in a provided callback function.</p>
<p>This callback function checks if <code>num</code> is odd by checking if it is not divisible by 2 (<code>num % 2</code>). If <code>num</code> is not divisible by 2, the function returns <code>true</code>, otherwise it returns <code>false</code>.</p>
<p>When <code>filter</code> is called on <code>arr</code>, it applies this function to each element in the array, creating a new array with only the elements that returned <code>true</code> or passed the specified condition when passed to the function. The original <code>arr</code> remains unchanged and returns the result.</p>
<p><strong>Example 2</strong>: You can use <code>filter()</code> to return only the users having age greater than 30 in an array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> users = [
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'John'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span>},
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'Jane'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">30</span>},
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'Jack'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span>},
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'Jill'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">40</span>},
    {<span class="hljs-attr">firstName</span>: <span class="hljs-string">'Joe'</span>, <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">45</span>},
]

<span class="hljs-comment">// Find the users with age greater than 30</span>
<span class="hljs-keyword">const</span> output = users.filter(<span class="hljs-function">(<span class="hljs-params">{age}</span>) =&gt;</span> age &gt; <span class="hljs-number">30</span>)
<span class="hljs-built_in">console</span>.log(output); <span class="hljs-comment">// [{firstName: 'Jack', lastName: 'Doe', age: 35}, {firstName: 'Jill', lastName: 'Doe', age: 40}, {firstName: 'Joe', lastName: 'Doe', age: 45}]</span>
</code></pre>
<p>In the above code, <code>users</code> is an array of objects representing users. Each object has three properties: <code>firstName</code>, <code>lastName</code>, and <code>age</code>.</p>
<p><code>filter</code> is called on the <code>users</code> array and it applies a callback function to each element in the array.</p>
<p>The function takes a single argument, an object destructured to a single property <code>age</code>. This function checks if <code>age</code> is greater than 30. If it is, the function returns <code>true</code>, otherwise it returns <code>false</code>.</p>
<p>When <code>filter</code> is called on <code>users</code>, it applies this function to each element in the array, creating a new array with only the elements that returned <code>true</code> when passed to the function and returns the result. The original <code>users</code> array remains unchanged.</p>
<h3 id="heading-how-to-use-reduce-in-javascript"><strong>How to use</strong> <code>reduce()</code> in JavaScript</h3>
<p>The <code>reduce()</code> method is kind of overwhelming. If you have came across <code>reduce()</code> method before and haven't understood it at first, it's totally fine.</p>
<p>But don't worry – here, we will learn it through quite a few examples and I will try my best to make you understand this method.</p>
<p>Now, one doubt that might comes to your mind is why we use the <code>reduce()</code> method. As there are already lots of methods, how can we decide which one to use and when?</p>
<p>In the case of the <code>reduce()</code> method, you should is used it when you want to perform some operation on the elements of an array and return a single value as a result. The "single value" refers to the accumulated result of repeatedly applying a function to the elements of a sequence.</p>
<p>For example, you might use <code>reduce()</code> to sum up all the elements in an array, to find the maximum or minimum value, to merge multiple objects into a single object, or to group different elements in an array.</p>
<p>Now let's understand all these with the help of examples.</p>
<p><strong>Example 1</strong>: Using <code>reduce()</code> to sum up all the elements in an array:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];

<span class="hljs-keyword">const</span> sum = numbers.reduce(<span class="hljs-function">(<span class="hljs-params">total, currentValue</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> total + currentValue;
}, <span class="hljs-number">0</span>)

<span class="hljs-built_in">console</span>.log(sum); <span class="hljs-comment">// 15</span>
</code></pre>
<p>In this example, the <code>reduce()</code> method is called on the <code>numbers</code> array and is passed a callback function that takes two arguments: <code>total</code> and <code>currentValue</code>.</p>
<p>The <code>total</code> argument is the accumulation of the values that have been returned from the function so far, and the <code>currentValue</code> is the current element being processed in the array.</p>
<p>The <code>reduce()</code> method also takes an initial value as the second argument, in this case <code>0</code>, which is used as the initial value of <code>total</code> for the first iteration.</p>
<p>In each iteration, the function adds the current value to the total and returns the new value of the total.</p>
<p>The <code>reduce()</code> method then uses the returned value as the <code>total</code> for the next iteration, until it has processed all the elements in the array.</p>
<p>Finally, it returns the final value of the total, which is the sum of all the elements in the array.</p>
<p><strong>Example 2</strong>: Using <code>reduce()</code> to find the maximum value in an array:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">5</span>, <span class="hljs-number">20</span>, <span class="hljs-number">100</span>, <span class="hljs-number">60</span>, <span class="hljs-number">1</span>];
<span class="hljs-keyword">const</span> maxValue = numbers.reduce(<span class="hljs-function">(<span class="hljs-params">max, curr</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span>(curr &gt; max) max = curr;
    <span class="hljs-keyword">return</span> max;
});
<span class="hljs-built_in">console</span>.log(maxValue); <span class="hljs-comment">// 100</span>
</code></pre>
<p>In this example, again we have two arguments <code>max</code> and <code>curr</code> in the callback function. Notice we haven't passed the second parameter in the <code>reduce()</code> method this time. So, the default value will be the first element in the array.</p>
<p>The callback function first checks if the current element <code>curr</code> is greater than the current maximum value <code>max</code>. If it is, it updates the value of <code>max</code> to be the current element. If it is not, <code>max</code> is not updated. Finally, the function returns the value of <code>max</code>.</p>
<p>In this case, the <code>reduce()</code> method will start by setting <code>max</code> to 5 and <code>curr</code> to 20. It will then check if 20 is greater than 5, which it is, so it updates <code>max</code> to 20.</p>
<p>It will then set <code>curr</code> to 100 and check if 100 is greater than 20, which it is, so it updates <code>max</code> to 100.</p>
<p>It will continue this process until it has processed all the elements in the array. The final value of <code>max</code> will be the maximum value in the array, which is 100 in this case.</p>
<p><strong>Example 3</strong>: Using <code>reduce()</code> to merge different objects in a single object:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> obj1 = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> };
<span class="hljs-keyword">const</span> obj2 = { <span class="hljs-attr">c</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">d</span>: <span class="hljs-number">4</span> };
<span class="hljs-keyword">const</span> obj3 = { <span class="hljs-attr">e</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">f</span>: <span class="hljs-number">6</span> };
<span class="hljs-keyword">const</span> mergedObj = [obj1, obj2, obj3].reduce(<span class="hljs-function">(<span class="hljs-params">acc, curr</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> { ...acc, ...curr };
}, {});
<span class="hljs-built_in">console</span>.log(mergedObj); <span class="hljs-comment">// { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }</span>
</code></pre>
<p>In this example, we have two arguments <code>acc</code> and <code>curr</code> in the callback function. The <code>acc</code> represents the current merged object that has been created so far, while the <code>curr</code> represents the current object being processed in the array.</p>
<p>The function uses the spread operator (<code>...</code>) to create a new object that combines the properties of the current merged object <code>acc</code> and the current object <code>curr</code>. It then returns this new object.</p>
<p>In this case, the <code>reduce()</code> method will start by setting <code>acc</code> to an empty object (which is the value passed as the second argument to <code>reduce()</code>). It will then set <code>curr</code> to <code>obj1</code>, and create a new object that combines the properties of the empty object and <code>obj1</code>. It will then set <code>curr</code> to <code>obj2</code> and create a new object that combines the properties of the previous merged object and <code>obj2</code>. It will continue this process until it has processed all the objects in the array.</p>
<p>The final value of <code>acc</code> will be the merged object, which will contain all the properties of the original objects.</p>
<p><strong>Example 4</strong>: Using <code>reduce()</code> to group objects in an array. For example, grouping products in a shopping cart according to their brand name.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> shoppingCart = [
    {<span class="hljs-attr">name</span>: <span class="hljs-string">'Apple'</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">1.99</span>, <span class="hljs-attr">quantity</span>: <span class="hljs-number">3</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">'Apple'</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">1.99</span>, <span class="hljs-attr">quantity</span>: <span class="hljs-number">3</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">'Xiomi'</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">2.99</span>, <span class="hljs-attr">quantity</span>: <span class="hljs-number">2</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">'Samsung'</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">3.99</span>, <span class="hljs-attr">quantity</span>: <span class="hljs-number">1</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">'Tesla'</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">3.99</span>, <span class="hljs-attr">quantity</span>: <span class="hljs-number">1</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">'Tesla'</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">4.99</span>, <span class="hljs-attr">quantity</span>: <span class="hljs-number">4</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">'Nokia'</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">4.99</span>, <span class="hljs-attr">quantity</span>: <span class="hljs-number">4</span>},
]

<span class="hljs-keyword">const</span> products = shoppingCart.reduce(<span class="hljs-function">(<span class="hljs-params">productGroup, product</span>) =&gt;</span> {
    <span class="hljs-keyword">const</span> name = product.name;
    <span class="hljs-keyword">if</span>(productGroup[name] == <span class="hljs-literal">null</span>) {
        productGroup[name] = [];
    }
    productGroup[name].push(product);

    <span class="hljs-keyword">return</span> productGroup;
}, {});

<span class="hljs-built_in">console</span>.log(products);
</code></pre>
<pre><code class="lang-json"><span class="hljs-comment">// OUTPUT</span>
{
  Apple: [
    { name: 'Apple', price: <span class="hljs-number">1.99</span>, quantity: <span class="hljs-number">3</span> },
    { name: 'Apple', price: <span class="hljs-number">1.99</span>, quantity: <span class="hljs-number">3</span> }
  ],
  Xiomi: [ { name: 'Xiomi', price: <span class="hljs-number">2.99</span>, quantity: <span class="hljs-number">2</span> } ],
  Samsung: [ { name: 'Samsung', price: <span class="hljs-number">3.99</span>, quantity: <span class="hljs-number">1</span> } ],
  Tesla: [
    { name: 'Tesla', price: <span class="hljs-number">3.99</span>, quantity: <span class="hljs-number">1</span> },
    { name: 'Tesla', price: <span class="hljs-number">4.99</span>, quantity: <span class="hljs-number">4</span> }
  ],
  Nokia: [ { name: 'Nokia', price: <span class="hljs-number">4.99</span>, quantity: <span class="hljs-number">4</span> } ]
}
</code></pre>
<p>In this example, we have <code>shoppingCart</code> array representing different products and two arguments <code>productGroup</code> and <code>product</code> in the callback function.</p>
<p>The <code>productGroup</code> argument represents the current group of products that have been found so far, while the <code>product</code> argument represents the current product being processed in the array.</p>
<p>The function first gets the name of the current product using <code>product.name</code>. It then checks if there is already a group for this product name in the <code>productGroup</code> object using the <code>if</code> statement. If there is not, it creates a new group by initializing the <code>productGroup[name]</code> property to an empty array.</p>
<p>Finally, the function pushes the current product into the group using the <code>push()</code> method, and returns the updated <code>productGroup</code> object.</p>
<p>After the <code>reduce()</code> method has processed all the elements in the <code>shoppingCart</code> array, the resulting <code>productGroup</code> object will contain keys for each product name, and values that are arrays of products with that name.</p>
<h2 id="heading-benefits-of-higher-order-functions">Benefits of Higher Order Functions</h2>
<p>Using higher order functions has some important benefits for web developers.</p>
<p>First, higher order functions can help improve the legibility of your code by making it more concise and easy to understand. 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 chunks, making it easier to maintain and extend.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This article explored what a higher order function is, the benefits of using them, and how to use them in practical applications.</p>
<p>By using higher order functions, web developers can work smarter by organizing their code into smaller chunks and making it more legible and easier to debug.</p>
<p>Now, whenever you try to use map(), filter() and reduce() methods and get confused, just remember the following:</p>
<ul>
<li><p>Use map when you want to transform an array</p>
</li>
<li><p>Use filter to select a subset of data from an array, and</p>
</li>
<li><p>Use reduce when you want to return a single value as a result.</p>
</li>
</ul>
<p>For further reading on higher order functions, check out this awesome video by Akshay Saini on <a target="_blank" href="https://www.youtube.com/watch?v=HkWxvB1RJq0">YouTube</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
