<?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[JavaScript]]></title><description><![CDATA[JavaScript]]></description><link>https://revathi-js.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 20 Sep 2026 09:56:13 GMT</lastBuildDate><atom:link href="https://revathi-js.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Mastering JavaScript Array Methods with a Fun Pani Puri Example]]></title><description><![CDATA[Arrays are one of the most fundamental data structures in JavaScript, and they come with a variety of built-in methods to make handling data easier. To make learning more fun, let's explore JavaScript array methods using a delicious example: making P...]]></description><link>https://revathi-js.hashnode.dev/mastering-javascript-array-methods-with-a-fun-pani-puri-example</link><guid isPermaLink="true">https://revathi-js.hashnode.dev/mastering-javascript-array-methods-with-a-fun-pani-puri-example</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[array]]></category><category><![CDATA[array methods]]></category><dc:creator><![CDATA[Revathi P]]></dc:creator><pubDate>Mon, 17 Feb 2025 18:11:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/HlNcigvUi4Q/upload/2bf6fe0faea812c5ca3dd56a36c392b1.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Arrays are one of the most fundamental data structures in JavaScript, and they come with a variety of built-in methods to make handling data easier. To make learning more fun, let's explore JavaScript array methods using a delicious example: making Pani Puri 😋!</p>
<h2 id="heading-lets-make-pani-puri">Let's Make Pani Puri🫕</h2>
<p>Imagine we have an array that represents all the ingredients needed to make Pani Puri🥣:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> paniPuri = [<span class="hljs-string">'Puri'</span>, <span class="hljs-string">'Masala'</span>, <span class="hljs-string">'Meetha pani'</span>, <span class="hljs-string">'Pudina pani'</span>, <span class="hljs-string">'Jeera'</span>, <span class="hljs-string">'Garlic'</span>, <span class="hljs-string">'Tamarind'</span>, <span class="hljs-string">'Hing pani'</span>, <span class="hljs-string">'Amchur pani'</span>];
</code></pre>
<p>Now, let's apply different JavaScript array methods to this list.</p>
<h3 id="heading-1-find-the-total-number-of-ingredients">1. Find the Total Number of Ingredients</h3>
<p>To determine how many ingredients we have in our paniPuri array, we use the <code>.length</code> property:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(paniPuri.length); <span class="hljs-comment">// Output: 9</span>
</code></pre>
<h3 id="heading-2-find-the-first-ingredient">2. Find the First Ingredient</h3>
<p>Arrays are zero-indexed, meaning the first item is at index <code>0</code>. Let’s see what is the first item required for a pani puri🔎. We can access it like this:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(paniPuri[<span class="hljs-number">0</span>]); <span class="hljs-comment">// Output: 'Puri'</span>
</code></pre>
<h3 id="heading-3-replace-an-ingredient">3. Replace an Ingredient</h3>
<p>I don't like Garlic water🚫 and want to replace it with Ginger Water😅. We can do this by directly modifying the array:</p>
<pre><code class="lang-javascript">paniPuri[<span class="hljs-number">5</span>] = <span class="hljs-string">'Ginger Water'</span>;
</code></pre>
<h3 id="heading-4-find-the-index-of-an-ingredient">4. Find the Index of an Ingredient</h3>
<p>I love Meetha Pani😍, it is my favorite flavor. Let’s search where it is in the ingredients array🔭, we use <code>.indexOf()</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(paniPuri.indexOf(<span class="hljs-string">'Meetha pani'</span>)); <span class="hljs-comment">// Output: 2</span>
</code></pre>
<h3 id="heading-5-add-a-new-ingredient">5. Add a New Ingredient</h3>
<p>What if we want to add Bundi to our Pani Puri? 🤔 We can use <code>.push()</code> to add it to the end of the array:</p>
<pre><code class="lang-javascript">paniPuri.push(<span class="hljs-string">'Bundi'</span>);
</code></pre>
<h3 id="heading-6-remove-the-last-ingredient">6. Remove the Last Ingredient</h3>
<p>Too many items are there, let’s remove the last item from our array, we can use <code>.pop()</code>:</p>
<pre><code class="lang-javascript">paniPuri.pop();
</code></pre>
<h3 id="heading-7-remove-the-first-ingredient">7. Remove the First Ingredient</h3>
<p>To remove the first item from the array, we can use <code>.shift()</code>:</p>
<pre><code class="lang-javascript">paniPuri.shift();
</code></pre>
<h3 id="heading-8-add-an-ingredient-to-the-beginning">8. Add an Ingredient to the Beginning</h3>
<p>If we want to add an ingredient to the beginning of the array, we can use <code>.unshift()</code>:</p>
<pre><code class="lang-javascript">paniPuri.unshift(<span class="hljs-string">'Sev'</span>);
</code></pre>
<h3 id="heading-9-extract-a-portion-of-the-ingredients">9. Extract a Portion of the Ingredients</h3>
<p>I just want Meetha pani, Pudina pani and Jeera🫡, let’s see how to get that. To extract a portion of the array, we can use <code>.slice()</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> selectedPaniPuri = paniPuri.slice(<span class="hljs-number">2</span>, <span class="hljs-number">5</span>);
<span class="hljs-built_in">console</span>.log(selectedPaniPuri); <span class="hljs-comment">// Output: ['Meetha pani', 'Pudina pani', 'Jeera']</span>
</code></pre>
<h3 id="heading-10-remove-and-replace-ingredients">10. Remove and Replace Ingredients</h3>
<p>Let’s remove the pudina pani and jeera with Chaat Masala and Black Salt 🧂. To remove or replace elements at specific positions, we can use <code>.splice()</code>:</p>
<pre><code class="lang-javascript">paniPuri.splice(<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'Chaat Masala'</span>, <span class="hljs-string">'Black Salt'</span>);<span class="hljs-comment">//</span>
</code></pre>
<h3 id="heading-11-check-if-an-ingredient-exists">11. Check if an Ingredient Exists</h3>
<p>We can check if an item exists in the array using <code>.includes()</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(paniPuri.includes(<span class="hljs-string">'Jeera'</span>)); <span class="hljs-comment">// Output: false</span>
</code></pre>
<h3 id="heading-12-reverse-the-order-of-ingredients">12. Reverse the Order of Ingredients</h3>
<p>To reverse the array, we can use <code>.reverse()</code>:</p>
<pre><code class="lang-javascript">paniPuri.reverse();
</code></pre>
<h3 id="heading-13-sort-the-ingredients-alphabetically">13. Sort the Ingredients Alphabetically</h3>
<p>We can sort the ingredients in alphabetical order using <code>.sort()</code>:</p>
<pre><code class="lang-javascript">paniPuri.sort();
</code></pre>
<h3 id="heading-14-display-all-ingredients">14. Display All Ingredients</h3>
<p>To list all the ingredients in our Pani Puri one by one, we can use <code>.forEach()</code>:</p>
<pre><code class="lang-javascript">paniPuri.forEach(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(item);
});
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>JavaScript provides several built-in methods to work with arrays efficiently. By using <code>.length</code>, <code>.indexOf()</code>, <code>.push()</code>, <code>.pop()</code>, <code>.shift()</code>, <code>.unshift()</code>, <code>.slice()</code>, <code>.splice()</code>, <code>.includes()</code>, <code>.reverse()</code>, <code>.sort()</code>, and <code>.forEach()</code>, we can easily manipulate and manage arrays. With this fun Pani Puri example, you now have a practical understanding of how to work with JavaScript arrays!</p>
<p>Happy coding (and happy eating)😋!</p>
]]></content:encoded></item></channel></rss>