<?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"><channel><title><![CDATA[Array Methods You Must Know]]></title><description><![CDATA[Array Methods You Must Know]]></description><link>https://array-method-in-js.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 00:47:25 GMT</lastBuildDate><atom:link href="https://array-method-in-js.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Arrow Functions in JavaScript for Beginners]]></title><description><![CDATA[If you have started learning modern JavaScript, you have probably seen code like this:
const add = (a, b) => a + b;

At first, it may look a little strange.
Where is the function keyword?  
Why is the]]></description><link>https://array-method-in-js.hashnode.dev/arrow-functions-in-javascript-for-beginners</link><guid isPermaLink="true">https://array-method-in-js.hashnode.dev/arrow-functions-in-javascript-for-beginners</guid><dc:creator><![CDATA[Yogayata Verma]]></dc:creator><pubDate>Sun, 15 Mar 2026 13:40:37 GMT</pubDate><content:encoded><![CDATA[<p>If you have started learning modern JavaScript, you have probably seen code like this:</p>
<pre><code class="language-plaintext">const add = (a, b) =&gt; a + b;
</code></pre>
<p>At first, it may look a little strange.</p>
<p>Where is the <code>function</code> keyword?  </p>
<p>Why is there an arrow <code>=&gt;</code>?  </p>
<p>And why does this style look so short?</p>
<p>That is exactly what arrow functions are for.</p>
<p>Arrow functions are a shorter and cleaner way to write functions in JavaScript. They help reduce boilerplate and are very commonly used in modern JavaScript, especially in React and other modern codebases.</p>
<p>In this blog, we will learn:</p>
<ul>
<li><p>what arrow functions are</p>
</li>
<li><p>basic arrow function syntax</p>
</li>
<li><p>arrow functions with one parameter</p>
</li>
<li><p>arrow functions with multiple parameters</p>
</li>
<li><p>implicit return vs explicit return</p>
</li>
<li><p>the basic difference between arrow functions and normal functions</p>
</li>
</ul>
<p>We will keep the examples very simple and beginner-friendly.</p>
<p>Let’s begin.</p>
<h2>What are arrow functions?</h2>
<p>Arrow functions are a shorter syntax for writing functions in JavaScript.</p>
<p>They let us write the same logic with less code.</p>
<p>For example, look at this normal function:</p>
<pre><code class="language-plaintext">function greet(name) {
  return "Hello, " + name;
}
</code></pre>
<p>Now look at the same function written as an arrow function:</p>
<pre><code class="language-plaintext">const greet = (name) =&gt; {
  return "Hello, " + name;
};
</code></pre>
<p>Both do the same job.</p>
<p>The arrow function version is more compact and is widely used in modern JavaScript.</p>
<hr />
<h2>Why arrow functions are useful</h2>
<p>One of the main reasons arrow functions are popular is that they reduce boilerplate.</p>
<p>Let us compare.</p>
<h3>Normal function</h3>
<pre><code class="language-plaintext">function add(a, b) {
  return a + b;
}
</code></pre>
<h3>Arrow function</h3>
<pre><code class="language-plaintext">const add = (a, b) =&gt; {
  return a + b;
};
</code></pre>
<p>Both functions work the same way, but arrow functions often look shorter and cleaner.</p>
<p>This becomes even more useful when writing small functions.</p>
<hr />
<h2>Basic arrow function syntax</h2>
<p>Here is the basic syntax of an arrow function:</p>
<pre><code class="language-plaintext">const functionName = (parameters) =&gt; {
  return value;
};
</code></pre>
<p>Let us break it down:</p>
<ul>
<li><p><code>const</code> stores the function in a variable</p>
</li>
<li><p><code>functionName</code> is the name of the function</p>
</li>
<li><p><code>(parameters)</code> are the input values</p>
</li>
<li><p><code>=&gt;</code> is the arrow</p>
</li>
<li><p><code>{}</code> contains the function body</p>
</li>
<li><p><code>return</code> sends back the result</p>
</li>
</ul>
<h3>Example</h3>
<pre><code class="language-plaintext">const multiply = (a, b) =&gt; {
  return a * b;
};

console.log(multiply(2, 3));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">6
</code></pre>
<hr />
<h2>Arrow function syntax breakdown</h2>
<pre><code class="language-plaintext">const multiply = (a, b) =&gt; {
  return a * b;
};
</code></pre>
<h3>Visual breakdown</h3>
<pre><code class="language-plaintext">const multiply = (a, b) =&gt; {
      |           |      |
      |           |      └── arrow
      |           └──────── parameters
      └──────────────────── function name
</code></pre>
<h3>Another simple view</h3>
<pre><code class="language-plaintext">const greet = (name) =&gt; {
  return "Hello " + name;
};

greet   → function name
name    → parameter
=&gt;      → arrow function symbol
return  → sends value back
</code></pre>
<hr />
<h2>Arrow functions with one parameter</h2>
<p>If an arrow function has only one parameter, you can write it with or without parentheses.</p>
<h3>With parentheses</h3>
<pre><code class="language-plaintext">const square = (num) =&gt; {
  return num * num;
};

console.log(square(4));
</code></pre>
<h3>Without parentheses</h3>
<pre><code class="language-plaintext">const square = num =&gt; {
  return num * num;
};

console.log(square(4));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">16
</code></pre>
<p>Both are valid.</p>
<p>For beginners, using parentheses is often easier to read at first.</p>
<h3>Example with greeting</h3>
<pre><code class="language-plaintext">const greet = name =&gt; {
  return "Hello, " + name;
};

console.log(greet("Yogayata"));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">Hello, Yogayata
</code></pre>
<hr />
<h2>Arrow functions with multiple parameters</h2>
<p>If there are multiple parameters, parentheses are required.</p>
<h3>Example</h3>
<pre><code class="language-plaintext">const add = (a, b) =&gt; {
  return a + b;
};

console.log(add(5, 3));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">8
</code></pre>
<h3>Another example</h3>
<pre><code class="language-plaintext">const introduce = (firstName, lastName) =&gt; {
  return "Hello, " + firstName + " " + lastName;
};

console.log(introduce("Yogayata", "Verma"));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">Hello, Yogayata Verma
</code></pre>
<hr />
<h2>Converting normal functions into arrow functions</h2>
<p>This is one of the easiest ways to understand arrow functions.</p>
<p>Let us convert some normal functions into arrow functions step by step.</p>
<h3>Example 1: Addition</h3>
<h4>Normal function</h4>
<pre><code class="language-plaintext">function add(a, b) {
  return a + b;
}
</code></pre>
<h4>Arrow function</h4>
<pre><code class="language-plaintext">const add = (a, b) =&gt; {
  return a + b;
};
</code></pre>
<hr />
<h3>Example 2: Greeting</h3>
<h4>Normal function</h4>
<pre><code class="language-plaintext">function greet(name) {
  return "Hello, " + name;
}
</code></pre>
<h4>Arrow function</h4>
<pre><code class="language-plaintext">const greet = (name) =&gt; {
  return "Hello, " + name;
};
</code></pre>
<hr />
<h3>Example 3: Square of a number</h3>
<h4>Normal function</h4>
<pre><code class="language-plaintext">function square(num) {
  return num * num;
}
</code></pre>
<h4>Arrow function</h4>
<pre><code class="language-plaintext">const square = (num) =&gt; {
  return num * num;
};
</code></pre>
<hr />
<h2>Normal function to arrow function transformation</h2>
<pre><code class="language-plaintext">Normal function:
function add(a, b) {
  return a + b;
}

Arrow function:
const add = (a, b) =&gt; {
  return a + b;
};
</code></pre>
<h3>What changed?</h3>
<pre><code class="language-plaintext">1. Removed the "function" keyword
2. Added "=" after the function name
3. Added "=&gt;" before the function body
4. Kept the same logic
</code></pre>
<h3>Another quick transformation</h3>
<pre><code class="language-plaintext">function greet(name) {
  return "Hello, " + name;
}

↓ becomes ↓

const greet = (name) =&gt; {
  return "Hello, " + name;
};
</code></pre>
<hr />
<h2>Explicit return in arrow functions</h2>
<p>An explicit return means you write the <code>return</code> keyword yourself.</p>
<h3>Example</h3>
<pre><code class="language-plaintext">const subtract = (a, b) =&gt; {
  return a - b;
};

console.log(subtract(10, 4));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">6
</code></pre>
<p>In this example:</p>
<ul>
<li><p>curly braces are used</p>
</li>
<li><p><code>return</code> is written clearly</p>
</li>
<li><p>this is called explicit return</p>
</li>
</ul>
<p>This style is useful when the function body has more than one line or when you want the logic to be very clear.</p>
<hr />
<h2>Implicit return in arrow functions</h2>
<p>If an arrow function has only one expression, you can skip:</p>
<ul>
<li><p>the curly braces</p>
</li>
<li><p>the <code>return</code> keyword</p>
</li>
</ul>
<p>JavaScript will automatically return the result.</p>
<p>This is called implicit return.</p>
<h3>Example</h3>
<pre><code class="language-plaintext">const subtract = (a, b) =&gt; a - b;

console.log(subtract(10, 4));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">6
</code></pre>
<p>This is the shorter version of the previous example.</p>
<h3>Compare both</h3>
<h4>Explicit return</h4>
<pre><code class="language-plaintext">const multiply = (a, b) =&gt; {
  return a * b;
};
</code></pre>
<h4>Implicit return</h4>
<pre><code class="language-plaintext">const multiply = (a, b) =&gt; a * b;
</code></pre>
<p>Both work the same way.</p>
<hr />
<h2>Explicit return vs implicit return</h2>
<p>Let us understand this clearly.</p>
<h3>Explicit return</h3>
<p>Use curly braces and write <code>return</code>.</p>
<pre><code class="language-plaintext">const greet = (name) =&gt; {
  return "Hello, " + name;
};
</code></pre>
<h3>Implicit return</h3>
<p>No curly braces and no <code>return</code>.</p>
<pre><code class="language-plaintext">const greet = (name) =&gt; "Hello, " + name;
</code></pre>
<h3>Important rule</h3>
<p>If you use curly braces <code>{}</code>, you usually need to write <code>return</code>.</p>
<p>If you remove the curly braces, JavaScript can return the value automatically.</p>
<h3>Simple comparison</h3>
<pre><code class="language-plaintext">Explicit return:
(param) =&gt; {
  return value;
}

Implicit return:
(param) =&gt; value
</code></pre>
<h3>Example with math</h3>
<pre><code class="language-plaintext">const double1 = (num) =&gt; {
  return num * 2;
};

const double2 = (num) =&gt; num * 2;

console.log(double1(5));
console.log(double2(5));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">10
10
</code></pre>
<hr />
<h2>When to use implicit return</h2>
<p>Implicit return is great for very small functions.</p>
<h3>Good example</h3>
<pre><code class="language-plaintext">const square = num =&gt; num * num;
</code></pre>
<p>This is short and readable.</p>
<h3>Another good example</h3>
<pre><code class="language-plaintext">const sayHi = name =&gt; "Hi " + name;
</code></pre>
<p>But if the function becomes longer, explicit return is often clearer.</p>
<h3>Better with explicit return</h3>
<pre><code class="language-plaintext">const checkNumber = (num) =&gt; {
  if (num &gt; 0) {
    return "Positive";
  }
  return "Not positive";
};
</code></pre>
<p>So for beginners:</p>
<ul>
<li><p>use implicit return for small one-line functions</p>
</li>
<li><p>use explicit return for longer logic</p>
</li>
</ul>
<hr />
<h2>Basic difference between arrow function and normal function</h2>
<p>For beginners, the most important difference is syntax.</p>
<h3>Normal function</h3>
<pre><code class="language-plaintext">function add(a, b) {
  return a + b;
}
</code></pre>
<h3>Arrow function</h3>
<pre><code class="language-plaintext">const add = (a, b) =&gt; a + b;
</code></pre>
<p>Arrow functions are shorter and look more modern.</p>
<p>They are very common in modern JavaScript code.</p>
<h3>Simple comparison table</h3>
<table>
<thead>
<tr>
<th>Normal Function</th>
<th>Arrow Function</th>
</tr>
</thead>
<tbody><tr>
<td>Uses <code>function</code> keyword</td>
<td>Uses <code>=&gt;</code></td>
</tr>
<tr>
<td>More boilerplate</td>
<td>Shorter syntax</td>
</tr>
<tr>
<td>Good for traditional style</td>
<td>Common in modern JS</td>
</tr>
</tbody></table>
<h3>Keep this simple for now</h3>
<p>There are deeper differences too, especially related to <code>this</code>, but you do not need to go deep into that right now.</p>
<p>At the beginner level, focus on:</p>
<ul>
<li><p>syntax</p>
</li>
<li><p>readability</p>
</li>
<li><p>writing small functions cleanly</p>
</li>
</ul>
<hr />
<h2>Using arrow functions with <code>map()</code></h2>
<p>Arrow functions are often used inside array methods like <code>map()</code>.</p>
<h3>Example</h3>
<pre><code class="language-plaintext">const numbers = [1, 2, 3, 4];

const doubled = numbers.map((num) =&gt; num * 2);

console.log(doubled);
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">[2, 4, 6, 8]
</code></pre>
<p>This is a very common modern JavaScript pattern.</p>
<h3>Compare with normal function</h3>
<h4>Using normal function</h4>
<pre><code class="language-plaintext">const numbers = [1, 2, 3, 4];

const doubled = numbers.map(function(num) {
  return num * 2;
});

console.log(doubled);
</code></pre>
<h4>Using arrow function</h4>
<pre><code class="language-plaintext">const numbers = [1, 2, 3, 4];

const doubled = numbers.map((num) =&gt; num * 2);

console.log(doubled);
</code></pre>
<p>The arrow function version is shorter and easier to read once you get used to it.</p>
<hr />
<h2>More simple examples</h2>
<h3>Example 1: Double a number</h3>
<pre><code class="language-plaintext">const double = num =&gt; num * 2;

console.log(double(6));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">12
</code></pre>
<h3>Example 2: Add two numbers</h3>
<pre><code class="language-plaintext">const add = (a, b) =&gt; a + b;

console.log(add(3, 7));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">10
</code></pre>
<h3>Example 3: Greet someone</h3>
<pre><code class="language-plaintext">const greet = name =&gt; "Hello, " + name;

console.log(greet("Aman"));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">Hello, Aman
</code></pre>
<h3>Example 4: Check even or odd</h3>
<pre><code class="language-plaintext">const checkEvenOdd = num =&gt; {
  if (num % 2 === 0) {
    return "Even";
  }
  return "Odd";
};

console.log(checkEvenOdd(8));
console.log(checkEvenOdd(5));
</code></pre>
<h3>Output</h3>
<pre><code class="language-plaintext">Even
Odd
</code></pre>
<hr />
<h2>Practice in browser console</h2>
<p>The best way to understand arrow functions is to try them yourself.</p>
<p>Open your browser console and test these:</p>
<pre><code class="language-plaintext">const square = num =&gt; num * num;
console.log(square(5));

const greet = name =&gt; "Hello " + name;
console.log(greet("Riya"));

const add = (a, b) =&gt; a + b;
console.log(add(2, 8));
</code></pre>
<p>Try changing the values and see what happens.</p>
<p>That is the fastest way to get comfortable.</p>
<hr />
<h2>Mini assignment</h2>
<p>Now it is your turn.</p>
<h3>Step 1: Write a normal function to calculate square of a number</h3>
<p>Example idea:</p>
<pre><code class="language-plaintext">function square(num) {
  return num * num;
}
</code></pre>
<h3>Step 2: Rewrite it using an arrow function</h3>
<p>Example idea:</p>
<pre><code class="language-plaintext">const square = num =&gt; num * num;
</code></pre>
<h3>Step 3: Create an arrow function that returns whether a number is even or odd</h3>
<p>Example idea:</p>
<pre><code class="language-plaintext">const checkEvenOdd = num =&gt; {
  if (num % 2 === 0) {
    return "Even";
  }
  return "Odd";
};
</code></pre>
<h3>Step 4: Use an arrow function inside <code>map()</code> on an array</h3>
<p>Example idea:</p>
<pre><code class="language-plaintext">const numbers = [1, 2, 3, 4];

const doubled = numbers.map(num =&gt; num * 2);

console.log(doubled);
</code></pre>
<h3>Full practice solution</h3>
<pre><code class="language-plaintext">function squareNormal(num) {
  return num * num;
}

const squareArrow = num =&gt; num * num;

const checkEvenOdd = num =&gt; {
  if (num % 2 === 0) {
    return "Even";
  }
  return "Odd";
};

const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num =&gt; num * 2);

console.log(squareNormal(4));
console.log(squareArrow(4));
console.log(checkEvenOdd(7));
console.log(doubled);
</code></pre>
<hr />
<h2>Quick recap</h2>
<p>Let us quickly revise.</p>
<h3>Arrow functions are:</h3>
<ul>
<li><p>a shorter way to write functions</p>
</li>
<li><p>cleaner for small functions</p>
</li>
<li><p>common in modern JavaScript</p>
</li>
</ul>
<h3>One parameter</h3>
<pre><code class="language-plaintext">const greet = name =&gt; "Hello " + name;
</code></pre>
<h3>Multiple parameters</h3>
<pre><code class="language-plaintext">const add = (a, b) =&gt; a + b;
</code></pre>
<h3>Explicit return</h3>
<pre><code class="language-plaintext">const square = (num) =&gt; {
  return num * num;
};
</code></pre>
<h3>Implicit return</h3>
<pre><code class="language-plaintext">const square = num =&gt; num * num;
</code></pre>
<hr />
<h2>Final thoughts</h2>
<p>Arrow functions are one of the most useful features in modern JavaScript.</p>
<p>At first, the syntax may look unusual, but after practicing a few examples, it starts feeling natural.</p>
<p>A good approach is:</p>
<ol>
<li><p>start with normal functions</p>
</li>
<li><p>convert them into arrow functions</p>
</li>
<li><p>practice one-parameter and two-parameter examples</p>
</li>
<li><p>learn implicit return with very small functions</p>
</li>
<li><p>use arrow functions inside <code>map()</code></p>
</li>
</ol>
<p>Do not worry about advanced topics yet.</p>
<p>First become comfortable with the syntax and readability.</p>
<p>Once that becomes easy, arrow functions will become a natural part of your JavaScript style.</p>
<hr />
<h2>Quick challenge</h2>
<p>Try converting these normal functions into arrow functions on your own:</p>
<pre><code class="language-plaintext">function greet(name) {
  return "Hello " + name;
}

function multiply(a, b) {
  return a * b;
}
</code></pre>
<p>Then try writing:</p>
<ul>
<li><p>an arrow function for division</p>
</li>
<li><p>an arrow function for checking if a number is positive</p>
</li>
<li><p>an arrow function inside <code>map()</code> to add <code>5</code> to every number in an array</p>
</li>
</ul>
<p>Practice them in your console before checking any solution.</p>
<hr />
<h2>Conclusion</h2>
<p>Arrow functions help make JavaScript cleaner, shorter, and more modern.</p>
<p>They are especially useful for small functions, simple logic, and array methods like <code>map()</code>.</p>
<p>Keep practicing with very small examples like:</p>
<ul>
<li><p>square of a number</p>
</li>
<li><p>greeting a name</p>
</li>
<li><p>adding two values</p>
</li>
<li><p>checking even or odd</p>
</li>
</ul>
<p>That is the best way to build confidence.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Array Methods for Beginners]]></title><description><![CDATA[Arrays are one of the most important parts of JavaScript.
They help us store multiple values in a single variable, and once you start working with arrays, you quickly realize that you need easy ways t]]></description><link>https://array-method-in-js.hashnode.dev/javascript-array-methods-for-beginners</link><guid isPermaLink="true">https://array-method-in-js.hashnode.dev/javascript-array-methods-for-beginners</guid><dc:creator><![CDATA[Yogayata Verma]]></dc:creator><pubDate>Sun, 15 Mar 2026 13:32:39 GMT</pubDate><content:encoded><![CDATA[<p>Arrays are one of the most important parts of JavaScript.</p>
<p>They help us store multiple values in a single variable, and once you start working with arrays, you quickly realize that you need easy ways to add items, remove items, loop through them, transform them, and calculate results from them.</p>
<p>That is where JavaScript array methods become very useful.</p>
<p>In this blog, we will understand these beginner-friendly array methods:</p>
<ul>
<li><p><code>push()</code> and <code>pop()</code></p>
</li>
<li><p><code>shift()</code> and <code>unshift()</code></p>
</li>
<li><p><code>map()</code></p>
</li>
<li><p><code>filter()</code></p>
</li>
<li><p><code>reduce()</code></p>
</li>
<li><p><code>forEach()</code></p>
</li>
</ul>
<p>We will keep the examples simple, practical, and beginner-friendly.</p>
<p>We will also see:</p>
<ul>
<li><p>before and after array state</p>
</li>
<li><p>traditional <code>for</code> loop vs <code>map()</code></p>
</li>
<li><p>traditional <code>for</code> loop vs <code>filter()</code></p>
</li>
<li><p>a very basic explanation of <code>reduce()</code></p>
</li>
<li><p>small text-based diagrams using plain Markdown</p>
</li>
<li><p>a practice assignment at the end</p>
</li>
</ul>
<p>Let’s begin.</p>
<h2>Why learn array methods?</h2>
<p>Imagine you have an array of:</p>
<ul>
<li><p>marks</p>
</li>
<li><p>product prices</p>
</li>
<li><p>student names</p>
</li>
<li><p>tasks</p>
</li>
<li><p>cart items</p>
</li>
</ul>
<p>You may want to:</p>
<ul>
<li><p>add a new value</p>
</li>
<li><p>remove a value</p>
</li>
<li><p>print each value</p>
</li>
<li><p>create a modified version of the array</p>
</li>
<li><p>select only certain values</p>
</li>
<li><p>calculate the total sum</p>
</li>
</ul>
<p>Array methods help us do all of this in a cleaner and easier way.</p>
<hr />
<h2><code>push()</code> — Add an element to the end</h2>
<p>The <code>push()</code> method adds one or more elements to the end of an array.</p>
<h3>Example</h3>
<pre><code class="language-js">let fruits = ["apple", "banana"];

console.log("Before:", fruits);

fruits.push("mango");

console.log("After:", fruits);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">Before: ["apple", "banana"]
After: ["apple", "banana", "mango"]
</code></pre>
<h3>Before and after</h3>
<pre><code class="language-text">Before: ["apple", "banana"]
After:  ["apple", "banana", "mango"]
</code></pre>
<h3>Practical example</h3>
<p>Suppose you are building a to-do list app. When a user adds a new task, you can use <code>push()</code>.</p>
<pre><code class="language-js">let tasks = ["Study JavaScript", "Practice coding"];
tasks.push("Revise arrays");

console.log(tasks);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">["Study JavaScript", "Practice coding", "Revise arrays"]
</code></pre>
<hr />
<h2><code>pop()</code> — Remove an element from the end</h2>
<p>The <code>pop()</code> method removes the last element from an array.</p>
<h3>Example</h3>
<pre><code class="language-js">let fruits = ["apple", "banana", "mango"];

console.log("Before:", fruits);

fruits.pop();

console.log("After:", fruits);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">Before: ["apple", "banana", "mango"]
After: ["apple", "banana"]
</code></pre>
<h3>Before and after</h3>
<pre><code class="language-text">Before: ["apple", "banana", "mango"]
After:  ["apple", "banana"]
</code></pre>
<h3>Practical example</h3>
<p>You can think of <code>pop()</code> like removing the most recent item from a list.</p>
<hr />
<h2><code>unshift()</code> — Add an element to the beginning</h2>
<p>The <code>unshift()</code> method adds one or more elements to the beginning of an array.</p>
<h3>Example</h3>
<pre><code class="language-js">let colors = ["blue", "green"];

console.log("Before:", colors);

colors.unshift("red");

console.log("After:", colors);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">Before: ["blue", "green"]
After: ["red", "blue", "green"]
</code></pre>
<h3>Before and after</h3>
<pre><code class="language-text">Before: ["blue", "green"]
After:  ["red", "blue", "green"]
</code></pre>
<h3>Practical example</h3>
<p>This is useful when you want to add the newest item at the top, like a latest notification.</p>
<hr />
<h2><code>shift()</code> — Remove an element from the beginning</h2>
<p>The <code>shift()</code> method removes the first element from an array.</p>
<h3>Example</h3>
<pre><code class="language-js">let colors = ["red", "blue", "green"];

console.log("Before:", colors);

colors.shift();

console.log("After:", colors);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">Before: ["red", "blue", "green"]
After: ["blue", "green"]
</code></pre>
<h3>Before and after</h3>
<pre><code class="language-text">Before: ["red", "blue", "green"]
After:  ["blue", "green"]
</code></pre>
<h3>Practical example</h3>
<p>This is useful in queue-like situations where the first item should be removed first.</p>
<hr />
<h2><code>forEach()</code> — Do something for every element</h2>
<p>The <code>forEach()</code> method runs a function once for every element in the array.</p>
<p>It is useful when you want to perform an action like printing values.</p>
<h3>Example</h3>
<pre><code class="language-js">let numbers = [10, 20, 30];

numbers.forEach(function(num) {
  console.log(num);
});
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">10
20
30
</code></pre>
<h3>Practical example</h3>
<p>Suppose you want to greet every student in an array.</p>
<pre><code class="language-js">let students = ["Aman", "Riya", "Kunal"];

students.forEach(function(student) {
  console.log("Welcome " + student);
});
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">Welcome Aman
Welcome Riya
Welcome Kunal
</code></pre>
<h3>Important point</h3>
<p><code>forEach()</code> is used when you want to do something with each item.</p>
<p>It does <strong>not</strong> create a new array like <code>map()</code>.</p>
<hr />
<h2><code>map()</code> — Create a new array by changing each element</h2>
<p>The <code>map()</code> method creates a <strong>new array</strong> by applying a function to every element of the original array.</p>
<p>This is very useful when you want to transform data.</p>
<h3>Example</h3>
<pre><code class="language-js">let numbers = [1, 2, 3, 4];

console.log("Original:", numbers);

let doubled = numbers.map(function(num) {
  return num * 2;
});

console.log("New array:", doubled);
console.log("Original after map:", numbers);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">Original: [1, 2, 3, 4]
New array: [2, 4, 6, 8]
Original after map: [1, 2, 3, 4]
</code></pre>
<h3>What to notice?</h3>
<ul>
<li><p><code>map()</code> does not change the original array</p>
</li>
<li><p>it returns a new array</p>
</li>
<li><p>every item gets transformed</p>
</li>
</ul>
<h3>Before and after</h3>
<pre><code class="language-text">Original array: [1, 2, 3, 4]
New array:      [2, 4, 6, 8]
</code></pre>
<h3>Practical example</h3>
<p>Suppose you want to increase every product price by 10.</p>
<pre><code class="language-js">let prices = [100, 200, 300];

let updatedPrices = prices.map(function(price) {
  return price + 10;
});

console.log(updatedPrices);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">[110, 210, 310]
</code></pre>
<h3>Visualizing <code>map()</code></h3>
<p><code>map()</code> takes every value, transforms it, and puts the result into a new array.</p>
<pre><code class="language-text">Original array: [1, 2, 3]

1  ──&gt; 2
2  ──&gt; 4
3  ──&gt; 6

New array: [2, 4, 6]
</code></pre>
<h3>Another simple view</h3>
<pre><code class="language-text">[1, 2, 3]
    ↓
Pick 1 → transform it → add to new array
Pick 2 → transform it → add to new array
Pick 3 → transform it → add to new array
    ↓
[2, 4, 6]
</code></pre>
<hr />
<h2>Traditional <code>for</code> loop vs <code>map()</code></h2>
<p>Let us compare both approaches.</p>
<h3>Using a traditional <code>for</code> loop</h3>
<pre><code class="language-js">let numbers = [1, 2, 3, 4];
let doubled = [];

for (let i = 0; i &lt; numbers.length; i++) {
  doubled.push(numbers[i] * 2);
}

console.log(doubled);
</code></pre>
<h3>Using <code>map()</code></h3>
<pre><code class="language-js">let numbers = [1, 2, 3, 4];

let doubled = numbers.map(function(num) {
  return num * 2;
});

console.log(doubled);
</code></pre>
<h3>Why <code>map()</code> feels cleaner</h3>
<p>With <code>map()</code>:</p>
<ul>
<li><p>the code is shorter</p>
</li>
<li><p>the purpose is easier to read</p>
</li>
<li><p>it clearly shows that we are transforming data</p>
</li>
</ul>
<p>If your goal is to create a changed version of every element, <code>map()</code> is usually a great choice.</p>
<hr />
<h2><code>filter()</code> — Create a new array with selected elements</h2>
<p>The <code>filter()</code> method creates a new array containing only the elements that match a condition.</p>
<h3>Example</h3>
<pre><code class="language-js">let numbers = [5, 12, 8, 20, 3];

console.log("Original:", numbers);

let greaterThanTen = numbers.filter(function(num) {
  return num &gt; 10;
});

console.log("Filtered:", greaterThanTen);
console.log("Original after filter:", numbers);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">Original: [5, 12, 8, 20, 3]
Filtered: [12, 20]
Original after filter: [5, 12, 8, 20, 3]
</code></pre>
<h3>What to notice?</h3>
<ul>
<li><p><code>filter()</code> does not change the original array</p>
</li>
<li><p>it creates a new array</p>
</li>
<li><p>only matching values stay in the new array</p>
</li>
</ul>
<h3>Before and after</h3>
<pre><code class="language-text">Original array: [5, 12, 8, 20, 3]
New array:      [12, 20]
</code></pre>
<h3>Practical example</h3>
<p>Suppose you want only the students who passed.</p>
<pre><code class="language-js">let marks = [35, 80, 25, 90, 40];

let passed = marks.filter(function(mark) {
  return mark &gt;= 40;
});

console.log(passed);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">[80, 90, 40]
</code></pre>
<h3>Visualizing <code>filter()</code></h3>
<p><code>filter()</code> checks every value and only keeps the ones that match the condition.</p>
<pre><code class="language-text">Original array: [5, 12, 8, 20]
Condition: value &gt; 10

5   ──&gt; skip
12  ──&gt; keep
8   ──&gt; skip
20  ──&gt; keep

New array: [12, 20]
</code></pre>
<h3>Another simple view</h3>
<pre><code class="language-text">[5, 12, 8, 20]
      ↓
Check 5   → does not match
Check 12  → matches
Check 8   → does not match
Check 20  → matches
      ↓
[12, 20]
</code></pre>
<hr />
<h2>Traditional <code>for</code> loop vs <code>filter()</code></h2>
<h3>Using a traditional <code>for</code> loop</h3>
<pre><code class="language-js">let numbers = [5, 12, 8, 20, 3];
let result = [];

for (let i = 0; i &lt; numbers.length; i++) {
  if (numbers[i] &gt; 10) {
    result.push(numbers[i]);
  }
}

console.log(result);
</code></pre>
<h3>Using <code>filter()</code></h3>
<pre><code class="language-js">let numbers = [5, 12, 8, 20, 3];

let result = numbers.filter(function(num) {
  return num &gt; 10;
});

console.log(result);
</code></pre>
<h3>Why <code>filter()</code> is better here</h3>
<p>With <code>filter()</code>, the purpose becomes very clear:</p>
<p>we are selecting only the values that match a condition.</p>
<hr />
<h2><code>reduce()</code> — Turn the whole array into one final value</h2>
<p>The <code>reduce()</code> method takes all elements of an array and combines them into a single result.</p>
<p>That final result can be:</p>
<ul>
<li><p>a total sum</p>
</li>
<li><p>a total price</p>
</li>
<li><p>a count</p>
</li>
<li><p>one final value</p>
</li>
</ul>
<p>For beginners, the easiest use case is finding the total sum.</p>
<h3>Example</h3>
<pre><code class="language-js">let numbers = [10, 20, 30];

let total = numbers.reduce(function(accumulator, currentValue) {
  return accumulator + currentValue;
}, 0);

console.log(total);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">60
</code></pre>
<h3>Beginner-friendly explanation</h3>
<p>Inside <code>reduce()</code>:</p>
<ul>
<li><p><code>accumulator</code> stores the running result</p>
</li>
<li><p><code>currentValue</code> is the current array element</p>
</li>
<li><p><code>0</code> is the starting value</p>
</li>
</ul>
<p>So the calculation happens like this:</p>
<pre><code class="language-text">Start with accumulator = 0

0 + 10 = 10
10 + 20 = 30
30 + 30 = 60
</code></pre>
<p>Final result: <code>60</code></p>
<h3>Practical example</h3>
<p>Suppose you want to calculate the total bill of products in a cart.</p>
<pre><code class="language-js">let cartPrices = [199, 299, 499];

let totalBill = cartPrices.reduce(function(total, price) {
  return total + price;
}, 0);

console.log(totalBill);
</code></pre>
<h3>Output</h3>
<pre><code class="language-js">997
</code></pre>
<h3>Visualizing <code>reduce()</code></h3>
<p><code>reduce()</code> combines all values into one final result.</p>
<pre><code class="language-text">Array: [10, 20, 30]

acc = 0
acc + 10 = 10
acc + 20 = 30
acc + 30 = 60

Final result = 60
</code></pre>
<h3>Another simple view</h3>
<pre><code class="language-text">[10, 20, 30]
    ↓
acc = 0
acc = acc + 10  → 10
acc = acc + 20  → 30
acc = acc + 30  → 60
    ↓
Final result = 60
</code></pre>
<h3>Keep this in mind</h3>
<p>At first, <code>reduce()</code> may feel a little confusing.</p>
<p>That is normal.</p>
<p>Start by practicing only sum-related examples. Once that becomes easy, you can explore more advanced cases later.</p>
<hr />
<h2>Quick summary of before and after array states</h2>
<h3><code>push()</code></h3>
<pre><code class="language-text">Before: ["a", "b"]
After:  ["a", "b", "c"]
</code></pre>
<h3><code>pop()</code></h3>
<pre><code class="language-text">Before: ["a", "b", "c"]
After:  ["a", "b"]
</code></pre>
<h3><code>unshift()</code></h3>
<pre><code class="language-text">Before: ["b", "c"]
After:  ["a", "b", "c"]
</code></pre>
<h3><code>shift()</code></h3>
<pre><code class="language-text">Before: ["a", "b", "c"]
After:  ["b", "c"]
</code></pre>
<h3><code>map()</code></h3>
<pre><code class="language-text">Original: [1, 2, 3]
New:      [2, 4, 6]
</code></pre>
<h3><code>filter()</code></h3>
<pre><code class="language-text">Original: [5, 12, 8, 20]
New:      [12, 20]
</code></pre>
<h3><code>reduce()</code></h3>
<pre><code class="language-text">Original: [10, 20, 30]
Result:   60
</code></pre>
<h3><code>forEach()</code></h3>
<pre><code class="language-text">Original: [1, 2, 3]
Action: prints each value one by one
</code></pre>
<hr />
<h2>When should you use which method?</h2>
<p>Use:</p>
<ul>
<li><p><code>push()</code> when you want to add an item at the end</p>
</li>
<li><p><code>pop()</code> when you want to remove an item from the end</p>
</li>
<li><p><code>unshift()</code> when you want to add an item at the beginning</p>
</li>
<li><p><code>shift()</code> when you want to remove an item from the beginning</p>
</li>
<li><p><code>forEach()</code> when you want to perform an action on every item</p>
</li>
<li><p><code>map()</code> when you want to transform every item into a new array</p>
</li>
<li><p><code>filter()</code> when you want only the items that match a condition</p>
</li>
<li><p><code>reduce()</code> when you want one final value from the whole array</p>
</li>
</ul>
<hr />
<h2>Try these examples in the browser console</h2>
<p>One of the best ways to learn JavaScript is by running examples yourself.</p>
<p>Open your browser console and try this:</p>
<pre><code class="language-js">let nums = [2, 4, 6, 8];

nums.push(10);
console.log(nums);

let doubled = nums.map(function(n) {
  return n * 2;
});
console.log(doubled);

let bigNums = nums.filter(function(n) {
  return n &gt; 5;
});
console.log(bigNums);
</code></pre>
<p>When you try code on your own, the concepts become much easier to remember.</p>
<hr />
<h2>Mini assignment</h2>
<p>Now it is your turn.</p>
<h3>Step 1: Create an array of numbers</h3>
<pre><code class="language-js">let numbers = [4, 7, 12, 15, 3];
</code></pre>
<h3>Step 2: Use <code>map()</code> to double each number</h3>
<p>Expected result:</p>
<pre><code class="language-js">[8, 14, 24, 30, 6]
</code></pre>
<h3>Step 3: Use <code>filter()</code> to get numbers greater than 10</h3>
<p>Expected result:</p>
<pre><code class="language-js">[12, 15]
</code></pre>
<h3>Step 4: Use <code>reduce()</code> to calculate the total sum</h3>
<p>Expected result:</p>
<pre><code class="language-js">41
</code></pre>
<h3>Full practice solution</h3>
<pre><code class="language-js">let numbers = [4, 7, 12, 15, 3];

let doubled = numbers.map(function(num) {
  return num * 2;
});

let greaterThanTen = numbers.filter(function(num) {
  return num &gt; 10;
});

let total = numbers.reduce(function(sum, num) {
  return sum + num;
}, 0);

console.log("Original:", numbers);
console.log("Doubled:", doubled);
console.log("Greater than 10:", greaterThanTen);
console.log("Total sum:", total);
</code></pre>
<hr />
<h2>Final thoughts</h2>
<p>JavaScript array methods make your code cleaner, shorter, and easier to understand.</p>
<p>If you are just starting out, focus on understanding these three things for each method:</p>
<ul>
<li><p>what it does</p>
</li>
<li><p>whether it changes the original array or creates a new one</p>
</li>
<li><p>when to use it in a real problem</p>
</li>
</ul>
<p>A good order for beginners is:</p>
<ol>
<li><p><code>push()</code> and <code>pop()</code></p>
</li>
<li><p><code>shift()</code> and <code>unshift()</code></p>
</li>
<li><p><code>forEach()</code></p>
</li>
<li><p><code>map()</code></p>
</li>
<li><p><code>filter()</code></p>
</li>
<li><p><code>reduce()</code></p>
</li>
</ol>
<p>Also, avoid method chaining in the beginning.</p>
<p>First become comfortable using one method at a time.</p>
<p>Once the basics are clear, you will find arrays much easier to work with in JavaScript.</p>
<hr />
<h2>Quick challenge</h2>
<p>Take this array:</p>
<pre><code class="language-js">let nums = [1, 5, 10, 20];
</code></pre>
<p>Try to do these on your own:</p>
<ul>
<li><p>add <code>50</code> at the end</p>
</li>
<li><p>remove the first element</p>
</li>
<li><p>create a new array with all values tripled</p>
</li>
<li><p>create another array with only values greater than <code>10</code></p>
</li>
<li><p>find the total sum</p>
</li>
</ul>
<p>Try them in your console before looking at any answer.</p>
<hr />
<h2>Conclusion</h2>
<p>Array methods are a big step toward writing better JavaScript.</p>
<p>At first, some of them may look confusing, especially <code>map()</code>, <code>filter()</code>, and <code>reduce()</code>. But with a little practice, they become very natural.</p>
<p>Keep experimenting with small arrays, run examples in the console, and try changing the values yourself.</p>
<p>That is the fastest way to learn.</p>
]]></content:encoded></item></channel></rss>