<?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: Pranit Karkera</title>
    <description>The latest articles on DEV Community by Pranit Karkera (@pranit_karkera).</description>
    <link>https://dev.to/pranit_karkera</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%2F2111507%2F989376cd-3b66-404e-a3ef-5fe50779a6bd.jpg</url>
      <title>DEV Community: Pranit Karkera</title>
      <link>https://dev.to/pranit_karkera</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pranit_karkera"/>
    <language>en</language>
    <item>
      <title>Cross-Origin Resource Sharing(CORS). CORS middleware Setup.</title>
      <dc:creator>Pranit Karkera</dc:creator>
      <pubDate>Thu, 30 Jan 2025 07:24:01 +0000</pubDate>
      <link>https://dev.to/pranit_karkera/cross-origin-resource-sharingcors-cors-middleware-setup-mp3</link>
      <guid>https://dev.to/pranit_karkera/cross-origin-resource-sharingcors-cors-middleware-setup-mp3</guid>
      <description>&lt;h3&gt;
  
  
  What is CORS?
&lt;/h3&gt;

&lt;p&gt;CORS stands for &lt;strong&gt;Cross-Origin Resource Sharing&lt;/strong&gt;. It is a security feature implemented by web browsers to prevent web pages from making requests to a different domain than the one that served the web page. This is known as the &lt;strong&gt;Same-Origin Policy&lt;/strong&gt;. Without CORS, a web page hosted on &lt;code&gt;https://example.com&lt;/code&gt; cannot make an API request to &lt;code&gt;https://api.anotherdomain.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;CORS allows servers to specify who can access their resources and under what conditions. It does this by adding specific HTTP headers to the server's responses.&lt;/p&gt;




&lt;h3&gt;
  
  
  CORS in Your Example
&lt;/h3&gt;

&lt;p&gt;In your code, you are using the &lt;code&gt;cors&lt;/code&gt; middleware in an Express.js application to enable CORS. Here's a breakdown of how it works:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;CORS Middleware Setup&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;corsOptions&lt;/span&gt; &lt;span class="o"&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="s2"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;optionSuccessStatus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;corsOptions&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;origin: "*"&lt;/code&gt;&lt;/strong&gt;: This allows requests from &lt;strong&gt;any domain&lt;/strong&gt;. The  is a wildcard that means "all origins are allowed." This is useful for public APIs but should be used cautiously in production, as it can expose your API to security risks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;credentials: true&lt;/code&gt;&lt;/strong&gt;: This allows the server to accept credentials (e.g., cookies, authorization headers) from the client. If the client includes credentials in the request (e.g., with &lt;code&gt;withCredentials: true&lt;/code&gt; in an XMLHttpRequest or Fetch API call), the server must respond with the &lt;code&gt;Access-Control-Allow-Credentials: true&lt;/code&gt; header.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;optionSuccessStatus: 200&lt;/code&gt;&lt;/strong&gt;: This sets the HTTP status code for &lt;code&gt;OPTIONS&lt;/code&gt; requests (preflight requests) to &lt;code&gt;200&lt;/code&gt;. By default, some browsers expect a &lt;code&gt;204&lt;/code&gt; status code for preflight requests, but this setting ensures compatibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How CORS Works in Your Application&lt;/strong&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%2Fivuc8o4tero9bp5i4rdq.gif" 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%2Fivuc8o4tero9bp5i4rdq.gif" alt="CORS Request Flow" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a client (e.g., a browser) makes a request to your server, the following happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Preflight Request (if applicable)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;For certain types of requests (e.g., those with custom headers or non-standard HTTP methods), the browser sends an &lt;code&gt;OPTIONS&lt;/code&gt; request to the server to check if the actual request is allowed.&lt;/li&gt;
&lt;li&gt;The server responds with CORS headers (e.g., &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;, &lt;code&gt;Access-Control-Allow-Methods&lt;/code&gt;, etc.) to indicate whether the request is permitted.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Actual Request&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If the preflight request is successful, the browser sends the actual request (e.g., &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;The server processes the request and includes the appropriate CORS headers in the response.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;CORS Headers in Responses&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When the &lt;code&gt;cors&lt;/code&gt; middleware is used, it automatically adds the following headers to responses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Access-Control-Allow-Origin: *&lt;/code&gt;&lt;/strong&gt;: Allows requests from any origin.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Access-Control-Allow-Credentials: true&lt;/code&gt;&lt;/strong&gt;: Allows credentials to be included in requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Access-Control-Allow-Methods&lt;/code&gt;&lt;/strong&gt;: Specifies which HTTP methods are allowed (e.g., &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Access-Control-Allow-Headers&lt;/code&gt;&lt;/strong&gt;: Specifies which headers can be used in the actual request.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Example Scenario
&lt;/h3&gt;

&lt;p&gt;Suppose your Express server is running at &lt;code&gt;http://localhost:3000&lt;/code&gt;, and a web application hosted at &lt;code&gt;https://example.com&lt;/code&gt; tries to make a request to your API.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The browser sends a request to &lt;code&gt;http://localhost:3000/api/products&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The server responds with the appropriate CORS headers:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The browser checks these headers and allows the response to be accessed by the web application at &lt;code&gt;https://example.com&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Key Points to Remember
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Security Considerations&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Using &lt;code&gt;origin: "*"&lt;/code&gt; is convenient but can expose your API to cross-site request forgery (CSRF) attacks. In production, it's better to specify trusted origins explicitly, e.g., &lt;code&gt;origin: "https://example.com"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Credentials&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;credentials: true&lt;/code&gt; is set, the &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; header cannot be . You must specify a specific origin.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Preflight Requests&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Not all requests trigger a preflight request. Simple requests (e.g., &lt;code&gt;GET&lt;/code&gt; or &lt;code&gt;POST&lt;/code&gt; with standard headers) do not require a preflight.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing CORS&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;You can test CORS behavior using tools like Postman or by running a frontend application and inspecting network requests in the browser's developer tools.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Example of a More Secure CORS Configuration
&lt;/h3&gt;

&lt;p&gt;If you want to restrict access to specific domains, you can modify the &lt;code&gt;corsOptions&lt;/code&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;corsOptions&lt;/span&gt; &lt;span class="o"&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="s2"&gt;https://example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Allow only this domain&lt;/span&gt;
  &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;optionSuccessStatus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&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;This ensures that only requests from &lt;code&gt;https://example.com&lt;/code&gt; are allowed, improving security.&lt;/p&gt;




&lt;p&gt;By using the &lt;code&gt;cors&lt;/code&gt; middleware in your Express application, you enable cross-origin requests while controlling access to your API resources.&lt;/p&gt;




&lt;p&gt;More information about javascript array methods can be found in the React docs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Leave comment if you have any questions or feedback.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>Map(), Filter(), Reduce() javascript array method</title>
      <dc:creator>Pranit Karkera</dc:creator>
      <pubDate>Thu, 30 Jan 2025 06:48:32 +0000</pubDate>
      <link>https://dev.to/pranit_karkera/map-filter-reduce-javascript-array-method-2khj</link>
      <guid>https://dev.to/pranit_karkera/map-filter-reduce-javascript-array-method-2khj</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;What Are These Methods?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, and &lt;code&gt;reduce&lt;/code&gt; are methods that help you work with arrays in JavaScript. They are used to process and transform data in arrays without needing to write long &lt;code&gt;for&lt;/code&gt; loops.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The &lt;code&gt;map()&lt;/code&gt; Method&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  What does it do?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;map()&lt;/code&gt; method creates a &lt;strong&gt;new array&lt;/strong&gt; by applying a function to every element in the original array.&lt;/li&gt;
&lt;li&gt;It does not change the original array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to use it?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;map()&lt;/code&gt; when you want to &lt;strong&gt;transform&lt;/strong&gt; every element in an array (e.g., doubling numbers, converting strings to uppercase).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Double each number in the array&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doubled&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 4, 6, 8]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How it works:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;map()&lt;/code&gt; method loops through each element in the &lt;code&gt;numbers&lt;/code&gt; array.&lt;/li&gt;
&lt;li&gt;For each element, it applies the function &lt;code&gt;(num) =&amp;gt; num * 2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It collects the results into a new array (&lt;code&gt;doubled&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The &lt;code&gt;filter()&lt;/code&gt; Method&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  What does it do?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;filter()&lt;/code&gt; method creates a &lt;strong&gt;new array&lt;/strong&gt; with only the elements that pass a certain condition.&lt;/li&gt;
&lt;li&gt;It does not change the original array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to use it?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;filter()&lt;/code&gt; when you want to &lt;strong&gt;select&lt;/strong&gt; specific elements from an array (e.g., filtering even numbers, finding names that start with "A").&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Filter out only the even numbers&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;evens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evens&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How it works:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;filter()&lt;/code&gt; method loops through each element in the &lt;code&gt;numbers&lt;/code&gt; array.&lt;/li&gt;
&lt;li&gt;For each element, it checks the condition &lt;code&gt;(num) =&amp;gt; num % 2 === 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the condition is &lt;code&gt;true&lt;/code&gt;, it adds the element to the new array (&lt;code&gt;evens&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The &lt;code&gt;reduce()&lt;/code&gt; Method&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  What does it do?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;reduce()&lt;/code&gt; method combines all the elements of an array into a &lt;strong&gt;single value&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It does not change the original array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to use it?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;reduce()&lt;/code&gt; when you want to &lt;strong&gt;aggregate&lt;/strong&gt; data (e.g., summing numbers, finding the maximum value).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Sum all the numbers in the array&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How it works:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;reduce()&lt;/code&gt; method takes two arguments:

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;reducer function&lt;/strong&gt; (&lt;code&gt;(acc, num) =&amp;gt; acc + num&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;initial value&lt;/strong&gt; (&lt;code&gt;0&lt;/code&gt; in this case).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;It loops through each element in the &lt;code&gt;numbers&lt;/code&gt; array.&lt;/li&gt;
&lt;li&gt;For each element, it updates the &lt;code&gt;acc&lt;/code&gt; (accumulator) with the result of the reducer function.&lt;/li&gt;
&lt;li&gt;At the end, it returns the final value of &lt;code&gt;acc&lt;/code&gt; (&lt;code&gt;sum&lt;/code&gt; in this case).&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Key Differences Between &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, and &lt;code&gt;reduce&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Method&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Input&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;map()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Array&lt;/td&gt;
&lt;td&gt;New array&lt;/td&gt;
&lt;td&gt;Transform each element&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;filter()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Array&lt;/td&gt;
&lt;td&gt;New array&lt;/td&gt;
&lt;td&gt;Select elements that meet a condition&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;reduce()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Array&lt;/td&gt;
&lt;td&gt;Single value&lt;/td&gt;
&lt;td&gt;Combine elements into one value&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Combining These Methods&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You can chain these methods together to perform complex operations on arrays.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Step 1: Double each number (map)&lt;/span&gt;
&lt;span class="c1"&gt;// Step 2: Filter out numbers greater than 5 (filter)&lt;/span&gt;
&lt;span class="c1"&gt;// Step 3: Sum the remaining numbers (reduce)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [2, 4, 6, 8]&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [6, 8]&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num&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="c1"&gt;// 14&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 14&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Practice Time!&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s practice with a small exercise:&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;You have an array of products:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.5&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mango&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Use &lt;code&gt;map()&lt;/code&gt; to create a new array with only the names of the products.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;filter()&lt;/code&gt; to find products with a price greater than 1.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;reduce()&lt;/code&gt; to calculate the total price of all products.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1. Map to get product names&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;productNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;productNames&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["Apple", "Banana", "Orange", "Mango"]&lt;/span&gt;

&lt;span class="c1"&gt;// 2. Filter to find products with price &amp;gt; 1&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;expensiveProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;&amp;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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expensiveProducts&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// [{ name: "Apple", price: 1.5 }, { name: "Orange", price: 2 }, { name: "Mango", price: 3 }]&lt;/span&gt;

&lt;span class="c1"&gt;// 3. Reduce to calculate total price&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;totalPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalPrice&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Summary&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;map()&lt;/code&gt;&lt;/strong&gt;: Transform each element in an array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;filter()&lt;/code&gt;&lt;/strong&gt;: Select elements that meet a condition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;reduce()&lt;/code&gt;&lt;/strong&gt;: Combine elements into a single value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More information about javascript array methods can be found in the React docs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Leave comment if you have any questions or feedback.&lt;/p&gt;

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