<?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[Learn with Prakash]]></title><description><![CDATA[My name is 𝐏𝐫𝐚𝐤𝐚𝐬𝐡 and I talk about 𝗧𝗲𝗰𝗵-𝐊𝐧𝐨𝐰𝐥𝐞𝐝𝐠𝐞, 𝗪𝗲𝗯𝗗𝗲𝘃, 𝗗𝗲𝘃𝗢𝗽𝘀 and 𝗟𝗶𝗳𝗲𝘀𝘁𝘆𝗹𝗲.]]></description><link>https://blog.prakashtsx.me</link><generator>RSS for Node</generator><lastBuildDate>Sat, 06 Jun 2026 22:28:51 GMT</lastBuildDate><atom:link href="https://blog.prakashtsx.me/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Map and Set in JavaScript: Beyond Objects and Arrays]]></title><description><![CDATA[JavaScript gives us powerful primitives like objects and arrays, and for a long time they were enough to solve most problems. But as applications grew in complexity, certain limitations became clear. ]]></description><link>https://blog.prakashtsx.me/map-set</link><guid isPermaLink="true">https://blog.prakashtsx.me/map-set</guid><category><![CDATA[map-set]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Wed, 13 May 2026 04:28:56 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/6fec86f4-4c95-4154-87a1-f2589448c08e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>JavaScript gives us powerful primitives like objects and arrays, and for a long time they were enough to solve most problems. But as applications grew in complexity, certain limitations became clear. That is where <code>Map</code> and <code>Set</code> come in.</p>
<p>They are not just alternatives. They are purpose-built data structures designed to solve specific problems more cleanly, more predictably, and often more efficiently.</p>
<p>Understanding them properly is a strong signal of maturity as a JavaScript developer.</p>
<h2>The Problem with Traditional Objects and Arrays</h2>
<p>Before jumping into Map and Set, it is important to understand why they exist.</p>
<h3>Problem 1: Objects are not true dictionaries</h3>
<pre><code class="language-js">const user = {};
user["name"] = "Prakash";
user[1] = "one";

console.log(user);
</code></pre>
<p>Looks fine, but internally JavaScript converts keys into strings.</p>
<pre><code class="language-js">console.log(Object.keys(user)); 
// ["1", "name"]
</code></pre>
<p>Even if you use numbers or objects as keys, they get coerced into strings. This creates limitations when you need precise key control.</p>
<h3>Problem 2: Arrays allow duplicates</h3>
<pre><code class="language-js">const numbers = [1, 2, 2, 3, 3, 3];
</code></pre>
<p>There is no built-in guarantee of uniqueness. You must manually filter duplicates.</p>
<p>These problems led to the introduction of Map and Set.</p>
<h2>What is a Map?</h2>
<p>A <code>Map</code> is a collection of key-value pairs where keys can be of any type.</p>
<p>Unlike objects, keys are not restricted to strings or symbols.</p>
<h3>Syntax</h3>
<pre><code class="language-js">const map = new Map();
</code></pre>
<h3>Example</h3>
<pre><code class="language-js">const userMap = new Map();

userMap.set("name", "Prakash");
userMap.set(1, "one");

console.log(userMap.get("name")); // Prakash
console.log(userMap.get(1));      // one
</code></pre>
<h2>Key Characteristics of Map</h2>
<h3>1. Keys can be anything</h3>
<pre><code class="language-js">const objKey = { id: 1 };

const map = new Map();
map.set(objKey, "data");

console.log(map.get(objKey)); // data
</code></pre>
<p>This is impossible with normal objects.</p>
<h3>2. Maintains insertion order</h3>
<pre><code class="language-js">const map = new Map();
map.set("a", 1);
map.set("b", 2);

for (let [key, value] of map) {
  console.log(key, value);
}
</code></pre>
<p>Output will always follow insertion order.</p>
<h3>3. Built-in methods for better control</h3>
<pre><code class="language-js">map.has("a");   // true
map.delete("a");
map.size;       // number of elements
map.clear();
</code></pre>
<h2>Conceptual Visualization of Map</h2>
<p>Think of Map like a structured table:</p>
<p>Key → Value "name" → "Prakash" 1 → "one" { id: 1 } → "data"</p>
<p>Each key directly maps to a value without type restrictions.</p>
<h2>What is a Set?</h2>
<p>A <code>Set</code> is a collection of unique values.</p>
<p>It automatically removes duplicates and ensures that each value appears only once.</p>
<h3>Syntax</h3>
<pre><code class="language-js">const set = new Set();
</code></pre>
<h3>Example</h3>
<pre><code class="language-js">const numbers = new Set();

numbers.add(1);
numbers.add(2);
numbers.add(2);

console.log(numbers); // Set {1, 2}
</code></pre>
<h2>Key Characteristics of Set</h2>
<h3>1. Only unique values</h3>
<pre><code class="language-js">const set = new Set([1, 2, 2, 3]);

console.log(set); // Set {1, 2, 3}
</code></pre>
<h3>2. No indexing</h3>
<p>Unlike arrays, you cannot access elements by index.</p>
<pre><code class="language-js">set[0]; // undefined
</code></pre>
<h3>3. Useful methods</h3>
<pre><code class="language-js">set.has(1);    // true
set.delete(1);
set.size;      
set.clear();
</code></pre>
<h2>Conceptual Visualization of Set</h2>
<p>Think of Set as a filter that removes duplicates automatically:</p>
<p>Input: [1, 2, 2, 3, 3] Output: {1, 2, 3}</p>
<p>No duplicates survive.</p>
<h2>Map vs Object</h2>
<table>
<thead>
<tr>
<th>Feature</th>
<th>Map</th>
<th>Object</th>
</tr>
</thead>
<tbody><tr>
<td>Key types</td>
<td>Any type</td>
<td>String or Symbol</td>
</tr>
<tr>
<td>Order</td>
<td>Maintains insertion order</td>
<td>Not strictly guaranteed</td>
</tr>
<tr>
<td>Performance</td>
<td>Optimized for frequent add/remove</td>
<td>Not optimized for that</td>
</tr>
<tr>
<td>Size</td>
<td><code>map.size</code></td>
<td><code>Object.keys(obj).length</code></td>
</tr>
<tr>
<td>Iteration</td>
<td>Directly iterable</td>
<td>Requires methods</td>
</tr>
</tbody></table>
<h3>Example Comparison</h3>
<pre><code class="language-js">const obj = {};
obj[{}] = "value";

console.log(obj); 
// key becomes "[object Object]"
</code></pre>
<pre><code class="language-js">const map = new Map();
const key = {};

map.set(key, "value");
console.log(map.get(key)); // correct
</code></pre>
<p>Map preserves key identity. Object does not.</p>
<h2>Set vs Array</h2>
<table>
<thead>
<tr>
<th>Feature</th>
<th>Set</th>
<th>Array</th>
</tr>
</thead>
<tbody><tr>
<td>Duplicates</td>
<td>Not allowed</td>
<td>Allowed</td>
</tr>
<tr>
<td>Order</td>
<td>Maintained</td>
<td>Maintained</td>
</tr>
<tr>
<td>Indexing</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Performance (search)</td>
<td>Faster (<code>has</code>)</td>
<td>Slower (<code>includes</code>)</td>
</tr>
</tbody></table>
<h3>Example Comparison</h3>
<pre><code class="language-js">const arr = [1, 2, 2, 3];
const unique = [...new Set(arr)];

console.log(unique); // [1, 2, 3]
</code></pre>
<p>Set is often used to remove duplicates from arrays.</p>
<h2>When to Use Map</h2>
<p>Use Map when:</p>
<p>You need key-value storage with flexible key types You need reliable insertion order You frequently add and remove entries You want better performance for lookups</p>
<h3>Example Use Case</h3>
<pre><code class="language-js">const cache = new Map();

function getData(key) {
  if (cache.has(key)) {
    return cache.get(key);
  }

  const result = "expensive computation";
  cache.set(key, result);
  return result;
}
</code></pre>
<h2>When to Use Set</h2>
<p>Use Set when:</p>
<p>You need unique values You want to remove duplicates You want fast existence checks</p>
<h3>Example Use Case</h3>
<pre><code class="language-js">const visited = new Set();

function visit(page) {
  if (visited.has(page)) {
    console.log("Already visited");
    return;
  }

  visited.add(page);
  console.log("Visiting:", page);
}
</code></pre>
<h2>Deep Insight</h2>
<p>Map and Set are not just new syntax. They represent a shift in thinking.</p>
<p>Objects were designed for structure. Arrays were designed for ordered lists.</p>
<p>But modern applications need:</p>
<p>Precise control over keys Guaranteed uniqueness Efficient lookups</p>
<p>Map and Set solve these problems directly.</p>
<hr />
<h3><strong>❊ Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[The Magic of this, call(), apply(), and bind() in JavaScript]]></title><description><![CDATA[Introduction
In JavaScript, one concept controls how functions behave depending on how they are called. That concept is the this keyword. Along with this, three powerful methods — call(), apply(), and]]></description><link>https://blog.prakashtsx.me/call-apply-and-bind-in-javascript</link><guid isPermaLink="true">https://blog.prakashtsx.me/call-apply-and-bind-in-javascript</guid><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Tue, 05 May 2026 10:52:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/cddc6506-a42e-44d2-b306-5cd71d86b7e8.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Introduction</h2>
<p>In JavaScript, one concept controls how functions behave depending on how they are called. That concept is the <code>this</code> keyword. Along with <code>this</code>, three powerful methods — <code>call()</code>, <code>apply()</code>, and <code>bind()</code> — allow you to control what <code>this</code> should be.</p>
<p>If you understand these properly, you gain full control over function execution in JavaScript.</p>
<h2>What <code>this</code> Means in JavaScript</h2>
<p><code>this</code> refers to the object that is calling the function.</p>
<p>It is not decided where the function is written. It is decided when the function is called.</p>
<p><em><strong>Caller → Function →</strong></em> <code>this</code></p>
<p>Whoever calls the function becomes the value of <code>this</code>.</p>
<h2><code>this</code> Inside Normal Functions</h2>
<pre><code class="language-js">function show() {
  console.log(this);
}

show();
</code></pre>
<ul>
<li><p>In non-strict mode → <code>this</code> is the global object</p>
</li>
<li><p>In strict mode → <code>this</code> is <code>undefined</code></p>
</li>
</ul>
<h2><code>this</code> Inside Objects</h2>
<pre><code class="language-js">const user = {
  name: "Prakash",
  greet() {
    console.log(this.name);
  }
};

user.greet();
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Prakash
</code></pre>
<p>Here, <code>user</code> is calling the function, so <code>this</code> refers to <code>user</code>.</p>
<h2>Calling Context Changes <code>this</code> (Very Important)</h2>
<pre><code class="language-js">function show() {
  console.log(this);
}

const obj = {
  show
};

// Direct call
show();        // global / undefined

// Object call
obj.show();    // obj
</code></pre>
<p>This proves one rule:</p>
<p><code>this</code> is decided at call time, not at definition time.</p>
<h2>Function Borrowing Problem</h2>
<p>Sometimes you want to use a method of one object with another object without copying it.</p>
<pre><code class="language-js">const user1 = {
  name: "Prakash",
  greet() {
    console.log(this.name);
  }
};

const user2 = {
  name: "Rahul"
};

user1.greet(); // Prakash
</code></pre>
<p>Now we want <code>user2</code> to use <code>greet</code>. This is where <code>call()</code>, <code>apply()</code>, and <code>bind()</code> help.</p>
<h2>What <code>call()</code> Does</h2>
<p><code>call()</code> allows you to set <code>this</code> manually and immediately execute the function.</p>
<h3>Syntax</h3>
<pre><code class="language-js">functionName.call(thisArg, arg1, arg2)
</code></pre>
<h3>Example</h3>
<pre><code class="language-js">user1.greet.call(user2);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Rahul
</code></pre>
<h2>What <code>apply()</code> Does</h2>
<p><code>apply()</code> is similar to <code>call()</code>, but it takes arguments as an array.</p>
<h3>Syntax</h3>
<pre><code class="language-js">functionName.apply(thisArg, [arg1, arg2])
</code></pre>
<h3>Example</h3>
<pre><code class="language-js">function introduce(city, country) {
  console.log(`\({this.name} lives in \){city}, ${country}`);
}

const user = { name: "Prakash" };

introduce.apply(user, ["Roorkee", "India"]);
</code></pre>
<h2>What <code>bind()</code> Does</h2>
<p><code>bind()</code> does not execute the function immediately. It returns a new function with <code>this</code> permanently set.</p>
<h3>Syntax</h3>
<pre><code class="language-js">const newFn = functionName.bind(thisArg)
</code></pre>
<h3>Example</h3>
<pre><code class="language-js">function greet() {
  console.log(this.name);
}

const user = { name: "Prakash" };

const boundFn = greet.bind(user);
boundFn();
</code></pre>
<h2>Real-World Use of <code>bind()</code></h2>
<pre><code class="language-js">const user = {
  name: "Prakash",
  greet() {
    console.log(this.name);
  }
};

setTimeout(user.greet.bind(user), 1000);
</code></pre>
<p>Without <code>bind()</code>, <code>this</code> would be lost.</p>
<h2>Arrow Functions and <code>this</code> (Important)</h2>
<pre><code class="language-js">const user = {
  name: "Prakash",
  greet: () =&gt; {
    console.log(this.name);
  }
};

user.greet(); // undefined
</code></pre>
<p>Explanation:</p>
<ul>
<li><p>Arrow functions do not have their own <code>this</code></p>
</li>
<li><p>They take <code>this</code> from their surrounding scope</p>
</li>
</ul>
<h2>Visual Understanding</h2>
<img src="https://images.openai.com/static-rsc-4/yHKtjOUlQVGJIC94qJruuiOWz69qIcZF9dGByLQ0vTtVHNlOGCj-8WmH3kmeF7JdbhrO5uZKG5qxdNFgL63AvGqpqcDLMbVFakwMOnGeFAeq5hy0qthfzksTDQQFaxQHfoKs3jS_GGQV2j13lfue07NdTBhs40D0rsWNBjXiTwBrlaAzklWNVkrfF2U5IdWl?purpose=fullsize" alt="Image" style="display:block;margin:0 auto" />

<img src="https://images.openai.com/static-rsc-4/0pu3317ObduCCn2vCGdKzQ0WvQOYDCHvNNq07Jr-Gz1DWMYlfsrGI_CxKU-fu77OMh-gYvMPY87ukR0dho475UG6PAlImCYdiu5p56UdPn58GjBFQIRDSvPAIfeIdC5JRRyj8SliW3ncHps7QEZBe4wUeowvvuS_unqKq3jvSfHGw44KETWCnICAf61rJjeP?purpose=fullsize" alt="Image" style="display:block;margin:0 auto" />

<ul>
<li><p><code>call()</code> → sets <code>this</code> and runs immediately</p>
</li>
<li><p><code>apply()</code> → sets <code>this</code>, runs immediately, takes array</p>
</li>
<li><p><code>bind()</code> → returns a new function</p>
</li>
</ul>
<h2>Difference Between <code>call</code>, <code>apply</code>, and <code>bind</code></h2>
<table>
<thead>
<tr>
<th>Feature</th>
<th>call()</th>
<th>apply()</th>
<th>bind()</th>
</tr>
</thead>
<tbody><tr>
<td>Executes immediately</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
</tr>
<tr>
<td>Arguments format</td>
<td>Comma-separated</td>
<td>Array</td>
<td>Comma-separated</td>
</tr>
<tr>
<td>Returns function</td>
<td>No</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Use case</td>
<td>Quick invocation</td>
<td>Array-based arguments</td>
<td>Reusable function</td>
</tr>
</tbody></table>
<h2>Combined Example</h2>
<pre><code class="language-js">const person1 = { name: "Prakash" };
const person2 = { name: "Aman" };

function greet(age) {
  console.log(`\({this.name} is \){age} years old`);
}

// call
greet.call(person1, 21);

// apply
greet.apply(person2, [22]);

// bind
const fn = greet.bind(person1);
fn(25);
</code></pre>
<h2>Practice Example :</h2>
<pre><code class="language-js">const car = {
  brand: "Tesla",
  show() {
    console.log(this.brand);
  }
};

const bike = {
  brand: "Yamaha"
};

// call
car.show.call(bike);

// apply
function info(speed, type) {
  console.log(`\({this.brand} runs at \){speed} and is ${type}`);
}

info.apply(bike, ["120km/h", "sports"]);

// bind
const boundInfo = info.bind(car);
boundInfo("200km/h", "electric");
</code></pre>
<h2>Common Mistakes</h2>
<h3>Losing <code>this</code></h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  greet() {
    console.log(this.name);
  }
};

const fn = user.greet;
fn(); // undefined
</code></pre>
<h3>Fix</h3>
<pre><code class="language-js">const fixed = user.greet.bind(user);
fixed();
</code></pre>
<h2>Quick Summary of Rules</h2>
<ul>
<li><p>Normal function → depends on how it is called</p>
</li>
<li><p>Object method → <code>this</code> is the object</p>
</li>
<li><p>call/apply → manually set <code>this</code></p>
</li>
<li><p>bind → returns a function with fixed <code>this</code></p>
</li>
<li><p>Arrow function → no own <code>this</code></p>
</li>
</ul>
<h2>Conclusion</h2>
<p>The <code>this</code> keyword becomes simple when you focus on one rule:</p>
<p><code>this</code> depends on the caller</p>
<p>The methods <code>call()</code>, <code>apply()</code>, and <code>bind()</code> give you full control over that behavior.</p>
<p>If you master these concepts, you will write cleaner code, reuse functions efficiently, and avoid common bugs in JavaScript applications.</p>
<hr />
<h3><strong>❊ Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Understanding the this Keyword in JavaScript ]]></title><description><![CDATA[Introduction
The this keyword is one of the most confusing concepts in JavaScript for beginners. The reason is simple: its value is not fixed. It changes depending on how and where a function is calle]]></description><link>https://blog.prakashtsx.me/this-keyword</link><guid isPermaLink="true">https://blog.prakashtsx.me/this-keyword</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[this keyword]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Tue, 05 May 2026 10:48:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/0970f7f5-2983-47d2-9a3e-8b0affbe2d13.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Introduction</h2>
<p>The <code>this</code> keyword is one of the most confusing concepts in JavaScript for beginners. The reason is simple: its value is not fixed. It changes depending on how and where a function is called.</p>
<p>Instead of thinking of <code>this</code> as something complicated, you should understand one simple rule:</p>
<p><code>this</code> refers to the object that is calling the function.</p>
<p>Once you understand the caller, you understand <code>this</code>.</p>
<h2>What is <code>this</code></h2>
<p>In JavaScript, <code>this</code> is a special keyword that refers to the execution context of a function. In simpler terms, it points to the object that is currently calling the function.</p>
<h3>Think of it like this:</h3>
<p>Caller → Function → <code>this</code></p>
<p>Whoever calls the function becomes the value of <code>this</code>.</p>
<h2><code>this</code> in Global Context</h2>
<p>In the global scope, <code>this</code> refers to the global object.</p>
<h3>In Browser</h3>
<pre><code class="language-js">console.log(this); 
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">window
</code></pre>
<h3>In Node.js</h3>
<pre><code class="language-js">console.log(this);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">{}
</code></pre>
<p>The value depends on the environment, but the idea remains the same: global context means <code>this</code> points to the global object.</p>
<h2><code>this</code> Inside Objects</h2>
<p>When a function is inside an object and is called using that object, <code>this</code> refers to the object.</p>
<h3>Example</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  greet: function () {
    console.log(this.name);
  }
};

user.greet();
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Prakash
</code></pre>
<p>Here, <code>user</code> is calling <code>greet()</code>, so <code>this</code> refers to <code>user</code>.</p>
<h2>Visual Understanding</h2>
<img src="https://images.openai.com/static-rsc-4/GtvZjO8jDeDA9GsXjZFcupntJP9EcC3U1vFjG4GQHgcQgSEu0L6xjO8xSsW6jpaLZcq4lYnXCgwQ5FULaExEAYTrU_Fm4vMSmPsrlKa-yAyIEXVoSVXwsziByEAlb2VGm3qXkT4jYSaBk1lw4LFy4u8ziG5lez1OGkNoagFUG6dH00_rAY5HJgq5Nsu0koeC?purpose=fullsize" alt="Image" style="display:block;margin:0 auto" />

<img src="https://images.openai.com/static-rsc-4/ZuyQaBpMRsVY-oxRuaXFC8NmaoY9_jkh4lWytGcWTsaeNhsQfYgPQYNrrVPLrTHalgVcEeFbOLWAubjIoZgrAuaFXEO38UONEzi4k9feWEq94LFMmzkUosP_hfqRoRjenjGrH6ajlE1k1Ck3_dCT4-wEomAKZdniIlFmEpgZmgTUbRj4W2WIumeef8vPfDCh?purpose=fullsize" alt="Image" style="display:block;margin:0 auto" />

<img src="https://images.openai.com/static-rsc-4/B2QXsQOvi8h8yLDBEqdsnJ-JAAXyEuwG8Y204NSF3XTTxquf3LLAP_6rH5-8BhVlO1I2GSD--Gxg__-K7MtKIsCcxuCM6gFIEA4dTsiWx8Tkmqz1Dv2-IPmrdcPSIdXi2RGKGhMEpahPSuNSXg_jBkx0PgCew7-Ny7g8bcYFS4QncTnTuPvKxaZHWQbVm0lb?purpose=fullsize" alt="Image" style="display:block;margin:0 auto" />

<img src="https://images.openai.com/static-rsc-4/bJ7avFso6qM9dq6l6yJTS6B_8YmTk2He6Rd8NFBsC93GB9BBHZlqEaOk-_cR3ofzAHuGvR9f50NxgH45LyS5jDx2OKJOc-BTlS5d2JS3QkjdrLwKfa5Nq-N5IKzyuID6jErE7fEd-mqHazE2U1cWn--uA1zINIS0bX_F6WnvQEi2zOnH3BLTI2DIMe-Ok_Hi?purpose=fullsize" alt="Image" style="display:block;margin:0 auto" />

<img src="https://images.openai.com/static-rsc-4/-kUIN8xMweijAbh-lPeFGjJ-4oMh00EKkk-3uhEiflJejbpGqvE2IH1GAAh78kU_eIBI956DdPSP6iMnIMbJm-xEJjYdIUW3utEmkC2LaoIa9tmbNY0ne9GkWl7gT5OwiASt6bRQnYNaYTB4cW42Y9RvtaUllhZv367QiBjQJnD0zTb1XjJ_PGOMyJAmi3dm?purpose=fullsize" alt="Image" style="display:block;margin:0 auto" />

<img src="https://images.openai.com/static-rsc-4/z--0Wt3BQxGmcwFPY8bZInLyzDgM6PI5MgSDaCyVZ8zh932U7bGFAu37A4HjXOgS4rj9j0CzePxg_QHKymg6Trcbj7omgLODoJH-9oOb51wiTyD_WENsZap45DfWXamPEU6_LXObKEVR7zA96IdqwKqcEk4E2ZPv2GREOF8MBy4yDcea3GmD92Bo7tqv2dFK?purpose=fullsize" alt="Image" style="display:block;margin:0 auto" />

<p>Object → Method → <code>this = object</code></p>
<h2><code>this</code> Inside Functions</h2>
<p>This is where most confusion happens.</p>
<h3>Example</h3>
<pre><code class="language-js">function show() {
  console.log(this);
}

show();
</code></pre>
<p>In non-strict mode (browser), this will point to the global object.</p>
<p>In strict mode:</p>
<pre><code class="language-js">"use strict";

function show() {
  console.log(this);
}

show();
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">undefined
</code></pre>
<p>So inside normal functions, <code>this</code> depends on how the function is called, not where it is written.</p>
<h2>How Calling Context Changes <code>this</code></h2>
<p>This is the most important concept.</p>
<p>The value of <code>this</code> depends on the call site.</p>
<h3>Case 1: Direct Function Call</h3>
<pre><code class="language-js">function test() {
  console.log(this);
}

test();
</code></pre>
<p><code>this</code> → global object (or undefined in strict mode)</p>
<h3>Case 2: Object Method Call</h3>
<pre><code class="language-js">const obj = {
  name: "JavaScript",
  show() {
    console.log(this.name);
  }
};

obj.show();
</code></pre>
<p><code>this</code> → obj</p>
<h3>Case 3: Assigning Method to Variable</h3>
<pre><code class="language-js">const obj = {
  name: "JavaScript",
  show() {
    console.log(this.name);
  }
};

const fn = obj.show;
fn();
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">undefined
</code></pre>
<p>Here, the caller is no longer <code>obj</code>. It becomes a normal function call.</p>
<h2>Caller-Based Model</h2>
<p>Different calls → Different <code>this</code></p>
<ul>
<li><p><code>obj.method()</code> → <code>this = obj</code></p>
</li>
<li><p><code>function()</code> → <code>this = global/undefined</code></p>
</li>
<li><p><code>detached function</code> → <code>this lost</code></p>
</li>
</ul>
<h2>Before vs After Understanding</h2>
<h3>Confusing Approach</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  greet: function () {
    console.log(this.name);
  }
};

const greetFn = user.greet;
greetFn(); // undefined
</code></pre>
<h3>Correct Thinking</h3>
<p>Do not look at where the function is defined. Look at how it is called.</p>
<h2>Common Mistakes</h2>
<h3>Mistake 1: Thinking <code>this</code> is fixed</h3>
<pre><code class="language-js">const obj = {
  value: 10,
  show() {
    return this.value;
  }
};

const fn = obj.show;
console.log(fn()); // undefined
</code></pre>
<h3>Mistake 2: Losing Context</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  greet() {
    console.log(this.name);
  }
};

setTimeout(user.greet, 1000); // undefined
</code></pre>
<p>Because the function is passed as a callback, it loses its original caller.</p>
<h2>Simple Rule to Remember</h2>
<p>If you remember only one thing, remember this:</p>
<p><code>this</code> = the object that calls the function</p>
<p>If there is no object, <code>this</code> becomes global or undefined.</p>
<h2>Benefits of Understanding <code>this</code></h2>
<p>Understanding <code>this</code> helps you:</p>
<ul>
<li><p>Write correct object-oriented code</p>
</li>
<li><p>Avoid bugs in callbacks and event handlers</p>
</li>
<li><p>Work effectively with frameworks like React and Node.js</p>
</li>
<li><p>Debug unexpected undefined values</p>
</li>
</ul>
<h2>Conclusion</h2>
<p>The <code>this</code> keyword is not complicated once you stop thinking about where the function is written and start focusing on how it is called.</p>
<p>Every time you see <code>this</code>, ask one question: Who is calling this function?</p>
<p>That answer will always give you the correct value of <code>this</code>.</p>
<hr />
<h3><strong>❊ Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Destructuring in JavaScript ]]></title><description><![CDATA[Introduction
When writing real-world JavaScript applications, extracting values from arrays and objects often becomes repetitive and verbose. Destructuring solves this problem by allowing you to unpac]]></description><link>https://blog.prakashtsx.me/destructuring-in-javascript</link><guid isPermaLink="true">https://blog.prakashtsx.me/destructuring-in-javascript</guid><category><![CDATA[destructuring in JavaScript]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Tue, 05 May 2026 10:43:18 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/013b13ac-c81c-43da-a4e3-35abc41101cd.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Introduction</h2>
<p>When writing real-world JavaScript applications, extracting values from arrays and objects often becomes repetitive and verbose. Destructuring solves this problem by allowing you to unpack values into variables in a concise and readable way. It is one of the most useful features introduced in modern JavaScript and is widely used in frontend and backend development.</p>
<h2>What is Destructuring</h2>
<p>Destructuring is a syntax that allows you to extract values from arrays or properties from objects and assign them directly to variables.</p>
<h3>Without Destructuring</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  age: 21,
  city: "Roorkee"
};

const name = user.name;
const age = user.age;
const city = user.city;

console.log(name, age, city);
</code></pre>
<h3>With Destructuring</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  age: 21,
  city: "Roorkee"
};

const { name, age, city } = user;

console.log(name, age, city);
</code></pre>
<p>Destructuring removes repetition and makes code easier to read.</p>
<h2>Destructuring Arrays</h2>
<p>Array destructuring is based on position (index).</p>
<h3>Basic Example</h3>
<pre><code class="language-js">const numbers = [10, 20, 30];

const [a, b, c] = numbers;

console.log(a); // 10
console.log(b); // 20
console.log(c); // 30
</code></pre>
<h3>Skipping Values</h3>
<pre><code class="language-js">const numbers = [10, 20, 30];

const [first, , third] = numbers;

console.log(first); // 10
console.log(third); // 30
</code></pre>
<h3>Using Rest Operator</h3>
<pre><code class="language-js">const numbers = [1, 2, 3, 4, 5];

const [first, ...rest] = numbers;

console.log(first); // 1
console.log(rest);  // [2, 3, 4, 5]
</code></pre>
<h3>Swapping Variables</h3>
<pre><code class="language-js">let a = 5;
let b = 10;

[a, b] = [b, a];

console.log(a); // 10
console.log(b); // 5
</code></pre>
<h2>Destructuring Objects</h2>
<p>Object destructuring is based on property names, not position.</p>
<h3>Basic Example</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  age: 21
};

const { name, age } = user;

console.log(name); // Prakash
console.log(age);  // 21
</code></pre>
<h3>Renaming Variables</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  age: 21
};

const { name: userName, age: userAge } = user;

console.log(userName); // Prakash
console.log(userAge);  // 21
</code></pre>
<h3>Nested Object Destructuring</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  address: {
    city: "Roorkee",
    state: "Uttarakhand"
  }
};

const { address: { city, state } } = user;

console.log(city);  // Roorkee
console.log(state); // Uttarakhand
</code></pre>
<h3>Destructuring in Function Parameters</h3>
<pre><code class="language-js">function printUser({ name, age }) {
  console.log(`Name: \({name}, Age: \){age}`);
}

const user = { name: "Prakash", age: 21 };

printUser(user);
</code></pre>
<p>This pattern is heavily used in React components and backend APIs.</p>
<h2>Default Values in Destructuring</h2>
<p>Default values help when a property or index is missing.</p>
<h3>Object Default Values</h3>
<pre><code class="language-js">const user = {
  name: "Prakash"
};

const { name, age = 18 } = user;

console.log(name); // Prakash
console.log(age);  // 18
</code></pre>
<h3>Array Default Values</h3>
<pre><code class="language-js">const numbers = [10];

const [a, b = 20] = numbers;

console.log(a); // 10
console.log(b); // 20
</code></pre>
<h2>Before vs After Comparison</h2>
<h3>Without Destructuring</h3>
<pre><code class="language-js">function getUserData(user) {
  const name = user.name;
  const age = user.age;
  const city = user.city;

  console.log(name, age, city);
}
</code></pre>
<h3>With Destructuring</h3>
<pre><code class="language-js">function getUserData({ name, age, city }) {
  console.log(name, age, city);
}
</code></pre>
<h3>Array Example Without</h3>
<pre><code class="language-js">const arr = [1, 2, 3];

const first = arr[0];
const second = arr[1];
</code></pre>
<h3>Array Example With</h3>
<pre><code class="language-js">const [first, second] = [1, 2, 3];
</code></pre>
<h2>Benefits of Destructuring</h2>
<p>Destructuring reduces repetitive code and improves readability. It allows you to extract only the data you need, making functions cleaner and easier to maintain. It is especially useful when working with APIs, React props, and configuration objects. It also helps prevent errors caused by repeatedly accessing deeply nested properties.</p>
<h2>Advanced Patterns</h2>
<h3>Nested with Default</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  address: {}
};

const {
  address: { city = "Unknown" }
} = user;

console.log(city); // Unknown
</code></pre>
<h3>Rest with Objects</h3>
<pre><code class="language-js">const user = {
  name: "Prakash",
  age: 21,
  city: "Roorkee"
};

const { name, ...rest } = user;

console.log(name); // Prakash
console.log(rest); // { age: 21, city: "Roorkee" }
</code></pre>
<h3>Function Return Destructuring</h3>
<pre><code class="language-js">function getData() {
  return {
    id: 1,
    title: "JavaScript"
  };
}

const { id, title } = getData();

console.log(id, title);
</code></pre>
<h2>Mental Model</h2>
<p>For objects, destructuring works by matching property names to variables. For arrays, destructuring works based on index positions. Understanding this difference helps avoid most common mistakes.</p>
<h2>Common Mistakes</h2>
<h3>Wrong Property Name</h3>
<pre><code class="language-js">const user = { name: "Prakash" };

const { username } = user; // undefined
</code></pre>
<h3>Unsafe Nested Destructuring</h3>
<pre><code class="language-js">const user = {};

const { address: { city } } = user; // error
</code></pre>
<h3>Safe Fix</h3>
<pre><code class="language-js">const { address: { city } = {} } = user;
</code></pre>
<h2>Conclusion</h2>
<p>Destructuring is not just a syntax improvement but a better way of writing JavaScript. It simplifies data extraction, reduces code repetition, and makes your logic more expressive. Once you start using it consistently, it becomes a natural part of how you write modern JavaScript.</p>
<hr />
<h3><strong>❊ Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Array Flattening in JavaScript]]></title><description><![CDATA[Introduction
Arrays are one of the most fundamental data structures in JavaScript. In simple cases, they store values in a single, linear structure. But as applications grow, data often becomes more c]]></description><link>https://blog.prakashtsx.me/array-flattening-in-javascript</link><guid isPermaLink="true">https://blog.prakashtsx.me/array-flattening-in-javascript</guid><category><![CDATA[Array Flattening]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Sun, 26 Apr 2026 12:26:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/9557037a-2466-47d4-90ad-167e1b6ed431.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Introduction</h2>
<p>Arrays are one of the most fundamental data structures in JavaScript. In simple cases, they store values in a single, linear structure. But as applications grow, data often becomes more complex — and that’s where <strong>nested arrays</strong> come into play.</p>
<p>Nested arrays allow us to represent hierarchical or grouped data. However, working with deeply nested structures can quickly become difficult.</p>
<p>This is where <strong>array flattening</strong> becomes an essential concept.</p>
<p>Flattening transforms a nested array into a single-level array, making it easier to process, iterate, and manipulate.</p>
<h2>Understanding Nested Arrays</h2>
<p>A nested array is simply an array that contains other arrays as its elements.</p>
<pre><code class="language-js">const data = [1, 2, [3, 4], [5, [6, 7]]];
</code></pre>
<p>In this example:</p>
<ul>
<li><p>The array contains numbers</p>
</li>
<li><p>It also contains arrays inside it</p>
</li>
<li><p>Those arrays can themselves contain more arrays</p>
</li>
</ul>
<p>This creates a <strong>multi-level structure</strong>, similar to a tree.</p>
<h3>Conceptual View</h3>
<p>Think of it like this:</p>
<pre><code class="language-plaintext">[1, 2, [3, 4], [5, [6, 7]]]
                ↓
          [5, [6, 7]]
                ↓
             [6, 7]
</code></pre>
<p>Each level introduces another layer of depth.</p>
<h2>Why Flattening Matters</h2>
<p>At first glance, nested arrays might seem harmless. But in real-world scenarios, they introduce complexity.</p>
<h3>1. Simplifying Data Processing</h3>
<p>Most array operations in JavaScript — like <code>map</code>, <code>filter</code>, and <code>reduce</code> — work best on flat arrays.</p>
<pre><code class="language-js">const arr = [[1, 2], [3, 4]];
</code></pre>
<p>If you want to process all values uniformly, flattening becomes necessary:</p>
<pre><code class="language-js">[1, 2, 3, 4]
</code></pre>
<h3>2. Working with API Data</h3>
<p>APIs often return deeply nested JSON structures.</p>
<pre><code class="language-js">const users = [
  ["Prakash", "Rahul"],
  ["Aman", ["Riya", "Neha"]]
];
</code></pre>
<p>To extract meaningful data, flattening helps convert it into a usable format.</p>
<h3>3. Cleaner Logic and Readability</h3>
<p>Flat arrays:</p>
<ul>
<li><p>Reduce nested loops</p>
</li>
<li><p>Simplify conditions</p>
</li>
<li><p>Improve readability</p>
</li>
</ul>
<h3>4. Real Application Use Cases</h3>
<ul>
<li><p>Rendering lists in UI frameworks</p>
</li>
<li><p>Data normalization before storing in databases</p>
</li>
<li><p>Processing logs or analytics data</p>
</li>
<li><p>Handling recursive structures like comments or folders</p>
</li>
</ul>
<h2>The Core Idea of Flattening</h2>
<p>Flattening is not just a function — it’s a way of thinking.</p>
<p>At its core:</p>
<blockquote>
<p>“If an element is an array, break it down further. If it’s not, keep it.”</p>
</blockquote>
<h3>Example</h3>
<pre><code class="language-js">Input:
[1, [2, [3, 4]], 5]

Output:
[1, 2, 3, 4, 5]
</code></pre>
<h3>Step-by-Step Thought Process</h3>
<pre><code class="language-plaintext">Start:
[1, [2, [3, 4]], 5]

Take 1 → keep it

Encounter [2, [3, 4]] → open it

Take 2 → keep it

Encounter [3, 4] → open it

Take 3, 4 → keep both

Take 5 → keep it

Final:
[1, 2, 3, 4, 5]
</code></pre>
<p>This mental model is the foundation of all flattening techniques.</p>
<h2>Approaches to Flatten Arrays</h2>
<p>There are multiple ways to flatten arrays in JavaScript. Each approach reflects a different way of thinking.</p>
<h2>1. Using <code>flat()</code> — The Built-in Method</h2>
<p>JavaScript provides a built-in method for flattening arrays.</p>
<pre><code class="language-js">const arr = [1, [2, [3, 4]]];

arr.flat(2);
// [1, 2, 3, 4]
</code></pre>
<p>The number passed defines the depth.</p>
<p>For unknown depth:</p>
<pre><code class="language-js">arr.flat(Infinity);
</code></pre>
<h3>Why It’s Useful</h3>
<ul>
<li><p>Clean and readable</p>
</li>
<li><p>No manual logic required</p>
</li>
<li><p>Ideal for everyday usage</p>
</li>
</ul>
<h2>2. Recursive Approach — Breaking the Problem Down</h2>
<p>Recursion mirrors the structure of nested arrays perfectly.</p>
<pre><code class="language-js">function flattenArray(arr) {
  let result = [];

  for (let item of arr) {
    if (Array.isArray(item)) {
      result = result.concat(flattenArray(item));
    } else {
      result.push(item);
    }
  }

  return result;
}
</code></pre>
<h3>Deep Understanding</h3>
<p>This works because:</p>
<ul>
<li><p>Each nested array is treated as a smaller version of the same problem</p>
</li>
<li><p>The function keeps calling itself until no arrays remain</p>
</li>
</ul>
<p>This is similar to how tree traversal works.</p>
<h2>3. Functional Approach with <code>reduce()</code></h2>
<p>This method uses a more declarative style.</p>
<pre><code class="language-js">function flatten(arr) {
  return arr.reduce((acc, curr) =&gt; {
    return Array.isArray(curr)
      ? acc.concat(flatten(curr))
      : acc.concat(curr);
  }, []);
}
</code></pre>
<h3>Insight</h3>
<p>Instead of building step-by-step manually:</p>
<ul>
<li><p>You accumulate results</p>
</li>
<li><p>Combine them recursively</p>
</li>
</ul>
<p>This approach is common in functional programming.</p>
<h2>4. Iterative Approach Using a Stack</h2>
<p>Recursion is elegant, but not always ideal for very deep structures.</p>
<p>An alternative is using a stack:</p>
<pre><code class="language-js">function flatten(arr) {
  const stack = [...arr];
  const result = [];

  while (stack.length) {
    const item = stack.pop();

    if (Array.isArray(item)) {
      stack.push(...item);
    } else {
      result.push(item);
    }
  }

  return result.reverse();
}
</code></pre>
<h3>Key Idea</h3>
<ul>
<li><p>Replace recursion with manual control</p>
</li>
<li><p>Use a stack to simulate depth traversal</p>
</li>
</ul>
<h2>Understanding Depth in Flattening</h2>
<p>Not all flattening needs to be complete.</p>
<p>Sometimes you only want to flatten one or two levels.</p>
<pre><code class="language-js">const arr = [1, [2, [3, 4]]];

arr.flat(1);
// [1, 2, [3, 4]]
</code></pre>
<p>This is useful when:</p>
<ul>
<li><p>You want partial transformation</p>
</li>
<li><p>You want to preserve some structure</p>
</li>
</ul>
<h2>Edge Cases to Consider</h2>
<p>Real-world data is rarely clean.</p>
<h3>1. Empty Arrays</h3>
<pre><code class="language-js">[1, [], [2, []]]
</code></pre>
<h3>2. Mixed Data Types</h3>
<pre><code class="language-js">[1, "text", [true, [null]]]
</code></pre>
<h3>3. Deeply Nested Structures</h3>
<pre><code class="language-js">[[[[[1]]]]]
</code></pre>
<h3>4. Sparse Arrays</h3>
<pre><code class="language-js">[1, , [2, , [3]]]
</code></pre>
<p>Good implementations handle all of these gracefully.</p>
<h2>Performance Considerations</h2>
<p>Different approaches behave differently depending on data size.</p>
<h3>Recursion</h3>
<ul>
<li><p>Easy to write</p>
</li>
<li><p>May cause stack overflow for very deep arrays</p>
</li>
</ul>
<h3>Iterative (Stack)</h3>
<ul>
<li><p>More control</p>
</li>
<li><p>Safer for large data</p>
</li>
</ul>
<h3>flat()</h3>
<ul>
<li><p>Optimized internally</p>
</li>
<li><p>Best for most practical use cases</p>
</li>
</ul>
<h2>Conceptual Connection</h2>
<p>Flattening is more than just an array problem.</p>
<p>It connects to:</p>
<ul>
<li><p>Tree traversal</p>
</li>
<li><p>Recursion patterns</p>
</li>
<li><p>Depth-first search</p>
</li>
<li><p>Data transformation pipelines</p>
</li>
</ul>
<p>Understanding flattening deeply strengthens your ability to solve complex problems.</p>
<h2>Final Thoughts</h2>
<p>Array flattening might look like a small utility problem, but it teaches an important lesson:</p>
<blockquote>
<p>Complex structures can often be simplified by breaking them down step by step.</p>
</blockquote>
<p>Once you understand the idea of:</p>
<ul>
<li><p>Identifying structure</p>
</li>
<li><p>Decomposing it</p>
</li>
<li><p>Rebuilding it</p>
</li>
</ul>
<p>You unlock a powerful way of thinking that applies far beyond arrays.</p>
<hr />
<p><em>I write articles on</em> <a href="http://blog.prakashtsx.com"><em><strong>blog.prakashtsx.com</strong></em></a> <em>and also post development-related content on:</em></p>
<ul>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>Twitter / X</strong></a></p>
</li>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Template Literals in JavaScript]]></title><description><![CDATA[Introduction
Strings are one of the most commonly used data types in JavaScript. In the early days of JavaScript, developers relied on string concatenation using the + operator to build dynamic string]]></description><link>https://blog.prakashtsx.me/template-literals-in-javascript</link><guid isPermaLink="true">https://blog.prakashtsx.me/template-literals-in-javascript</guid><category><![CDATA[template literals]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[@hiteshchoudharylco]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Sun, 26 Apr 2026 11:19:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/7ba749ca-0ef4-4ac1-ab75-38f42d6546ef.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3>Introduction</h3>
<p>Strings are one of the most commonly used data types in JavaScript. In the early days of JavaScript, developers relied on string concatenation using the <code>+</code> operator to build dynamic strings. While functional, this approach quickly becomes difficult to read and maintain as applications grow.</p>
<p>With the introduction of ES6 (ECMAScript 2015), JavaScript provided a better alternative: <strong>Template Literals</strong>. They simplify string handling, improve readability, and allow embedding expressions directly inside strings.</p>
<p>This article explores template literals in depth, from basics to real-world usage.</p>
<h2>Problems with Traditional String Concatenation</h2>
<p>Before template literals, developers used <code>+</code> to combine strings and variables.</p>
<h3>Example: Basic Concatenation</h3>
<pre><code class="language-javascript">const name = "Prakash";
const age = 21;

const message = "My name is " + name + " and I am " + age + " years old.";
console.log(message);
</code></pre>
<p>This works, but introduces several issues:</p>
<ul>
<li><p>Reduced readability</p>
</li>
<li><p>Repetitive syntax</p>
</li>
<li><p>Higher chance of missing quotes or operators</p>
</li>
</ul>
<h3>Example: Complex UI String</h3>
<pre><code class="language-javascript">const product = "Laptop";
const price = 50000;

const html = "&lt;div&gt;" +
               "&lt;h1&gt;" + product + "&lt;/h1&gt;" +
               "&lt;p&gt;Price: ₹" + price + "&lt;/p&gt;" +
             "&lt;/div&gt;";
</code></pre>
<p>Problems:</p>
<ul>
<li><p>Structure is hard to visualize</p>
</li>
<li><p>Debugging becomes difficult</p>
</li>
<li><p>Not scalable for larger templates</p>
</li>
</ul>
<h3>Example: Multi-line Strings</h3>
<pre><code class="language-javascript">const text = "Line 1\n" +
             "Line 2\n" +
             "Line 3";
</code></pre>
<p>Problems:</p>
<ul>
<li><p>Use of <code>\n</code> reduces clarity</p>
</li>
<li><p>Not visually aligned with output</p>
</li>
<li><p>Harder to maintain</p>
</li>
</ul>
<h2>What are Template Literals?</h2>
<p>Template literals are string literals enclosed by backticks (<code>`</code>) instead of single (<code>'</code>) or double (<code>"</code>) quotes.</p>
<p>They provide:</p>
<ul>
<li><p>Built-in string interpolation</p>
</li>
<li><p>Multi-line string support</p>
</li>
<li><p>Embedded expressions</p>
</li>
<li><p>Cleaner syntax</p>
</li>
</ul>
<h2>Template Literal Syntax</h2>
<pre><code class="language-javascript">const str = `This is a template literal`;
</code></pre>
<p>Key distinction:</p>
<ul>
<li><p><code>" "</code> and <code>' '</code> → traditional strings</p>
</li>
<li><p><code>` `</code> → template literals</p>
</li>
</ul>
<h2>Embedding Variables (String Interpolation)</h2>
<p>Instead of concatenation, template literals allow embedding variables using <code>${}</code>.</p>
<pre><code class="language-javascript">const name = "Prakash";
const age = 21;

const message = `My name is \({name} and I am \){age} years old.`;
console.log(message);
</code></pre>
<p>Benefits:</p>
<ul>
<li><p>Eliminates <code>+</code></p>
</li>
<li><p>Improves readability</p>
</li>
<li><p>Makes strings more natural to write</p>
</li>
</ul>
<h2>Embedding Expressions</h2>
<p>Template literals allow execution of JavaScript expressions inside <code>${}</code>.</p>
<pre><code class="language-javascript">const a = 10;
const b = 20;

console.log(`Sum is ${a + b}`);
</code></pre>
<pre><code class="language-javascript">console.log(`Random number: ${Math.random()}`);
</code></pre>
<pre><code class="language-javascript">console.log(`Is adult: ${age &gt;= 18}`);
</code></pre>
<p>This makes template literals highly flexible and powerful.</p>
<h2>Multi-line Strings</h2>
<p>Template literals support multi-line strings without special characters.</p>
<h3>Traditional approach</h3>
<pre><code class="language-javascript">const text = "Line 1\n" +
             "Line 2\n" +
             "Line 3";
</code></pre>
<h3>Template literal approach</h3>
<pre><code class="language-js">const text = `Line 1
Line 2
Line 3`;
</code></pre>
<p>Advantages:</p>
<ul>
<li><p>Cleaner syntax</p>
</li>
<li><p>Matches actual output visually</p>
</li>
<li><p>Easier to edit and maintain</p>
</li>
</ul>
<h2>Real-World Use Cases</h2>
<h3>1. Dynamic HTML Generation</h3>
<pre><code class="language-javascript">const user = {
  name: "Prakash",
  role: "Developer"
};

const card = `
  &lt;div class="card"&gt;
    &lt;h2&gt;${user.name}&lt;/h2&gt;
    &lt;p&gt;${user.role}&lt;/p&gt;
  &lt;/div&gt;
`;
</code></pre>
<p>Used in:</p>
<ul>
<li><p>Frontend rendering</p>
</li>
<li><p>Server-side templates</p>
</li>
<li><p>Email generation</p>
</li>
</ul>
<h3>2. Logging and Debugging</h3>
<pre><code class="language-js">const id = 101;
const status = "success";

console.log(`Request \({id} completed with status: \){status}`);
</code></pre>
<p>Improves clarity in logs and debugging.</p>
<h3>3. API Responses</h3>
<pre><code class="language-js">function greet(user) {
  return `Welcome \({user.name}, your balance is ₹\){user.balance}`;
}
</code></pre>
<h3>4. SQL Query Construction (with caution)</h3>
<pre><code class="language-js">const table = "users";

const query = `SELECT * FROM ${table}`;
</code></pre>
<p>Note: Avoid direct interpolation in production queries. Use parameterized queries to prevent SQL injection.</p>
<h3>5. Conditional Rendering</h3>
<pre><code class="language-js">const isLoggedIn = true;

const message = `User is ${isLoggedIn ? "logged in" : "not logged in"}`;
</code></pre>
<h2>Before vs After Comparison</h2>
<h3>Before (Concatenation)</h3>
<pre><code class="language-js">const name = "Prakash";
const msg = "Hello " + name + ", welcome to our platform!";
</code></pre>
<h3>After (Template Literal)</h3>
<pre><code class="language-js">const msg = `Hello ${name}, welcome to our platform!`;
</code></pre>
<h3>Before (Complex Structure)</h3>
<pre><code class="language-js">const html = "&lt;ul&gt;" +
               "&lt;li&gt;" + item1 + "&lt;/li&gt;" +
               "&lt;li&gt;" + item2 + "&lt;/li&gt;" +
             "&lt;/ul&gt;";
</code></pre>
<h3>After (Cleaner Version)</h3>
<pre><code class="language-js">const html = `
  &lt;ul&gt;
    &lt;li&gt;${item1}&lt;/li&gt;
    &lt;li&gt;${item2}&lt;/li&gt;
  &lt;/ul&gt;
`;
</code></pre>
<h2>Readability Improvement</h2>
<p>Template literals significantly improve:</p>
<ul>
<li><p>Code clarity</p>
</li>
<li><p>Maintainability</p>
</li>
<li><p>Team collaboration</p>
</li>
<li><p>Debugging efficiency</p>
</li>
</ul>
<p>They reduce visual noise and make code closer to natural language.</p>
<h2>Advanced Concept: Tagged Templates</h2>
<p>Template literals can be customized using tagged functions.</p>
<pre><code class="language-js">function highlight(strings, value) {
  return `\({strings[0]}**\){value}**${strings[1]}`;
}

const result = highlight`Hello ${"Prakash"}!`;
console.log(result);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Hello **Prakash**!
</code></pre>
<p>Use cases:</p>
<ul>
<li><p>Styling libraries (e.g., styled-components)</p>
</li>
<li><p>Internationalization</p>
</li>
<li><p>Security sanitization</p>
</li>
</ul>
<h2>Common Mistakes</h2>
<h3>1. Using quotes instead of backticks</h3>
<pre><code class="language-js">// Incorrect
const msg = "Hello ${name}";

// Correct
const msg = `Hello ${name}`;
</code></pre>
<h3>2. Overcomplicating expressions</h3>
<pre><code class="language-js">// Hard to read
`${user.age &gt; 18 ? user.name.toUpperCase() : "Minor"}`
</code></pre>
<p>Better approach:</p>
<pre><code class="language-js">const displayName = user.age &gt; 18 ? user.name.toUpperCase() : "Minor";
`${displayName}`
</code></pre>
<h2>Why Template Literals Matter in Modern JavaScript</h2>
<p>Template literals are widely used in:</p>
<ul>
<li><p>React applications</p>
</li>
<li><p>Node.js backends</p>
</li>
<li><p>Logging systems</p>
</li>
<li><p>API responses</p>
</li>
<li><p>Dynamic UI rendering</p>
</li>
</ul>
<p>They are considered standard practice in modern JavaScript development.</p>
<h2>Conclusion</h2>
<p>Template literals are more than a syntactic improvement. They fundamentally change how developers work with strings by making code cleaner, more expressive, and easier to maintain.</p>
<p>They:</p>
<ul>
<li><p>Replace complex concatenation</p>
</li>
<li><p>Enable dynamic string creation</p>
</li>
<li><p>Improve readability</p>
</li>
<li><p>Support scalable development</p>
</li>
</ul>
<p>Adopting template literals is essential for writing modern JavaScript.</p>
<hr />
<p><em>I write articles on</em> <a href="http://blog.prakashtsx.com"><em><strong>blog.prakashtsx.com</strong></em></a> <em>and also post development-related content on:</em></p>
<ul>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>Twitter / X</strong></a></p>
</li>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[JavaScript Operators: The Basics You Need to Know]]></title><description><![CDATA[Introduction
When you begin learning JavaScript, one of the most important concepts you encounter is operators. Operators are fundamental because they allow your program to perform actions such as cal]]></description><link>https://blog.prakashtsx.me/javascript-operators</link><guid isPermaLink="true">https://blog.prakashtsx.me/javascript-operators</guid><category><![CDATA[javascript operators]]></category><category><![CDATA[operators-in-javascript]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Sat, 25 Apr 2026 16:53:47 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/85f0de96-cb57-4438-abd1-801b9d118e86.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3>Introduction</h3>
<p>When you begin learning JavaScript, one of the most important concepts you encounter is <strong>operators</strong>. Operators are fundamental because they allow your program to perform actions such as calculations, comparisons, and logical decisions.</p>
<p>Every real-world application—whether it is an e-commerce website, authentication system, or dashboard relies heavily on operators.</p>
<p>In this blog, we will move step by step:</p>
<ul>
<li><p>From basic understanding</p>
</li>
<li><p>To real-world usage</p>
</li>
<li><p>To practical coding patterns</p>
</li>
</ul>
<h3>What Are Operators?</h3>
<p>Operators are <strong>symbols used to perform operations on values (operands)</strong>.</p>
<pre><code class="language-javascript">let result = 10 + 5;
</code></pre>
<p>In this example:</p>
<ul>
<li><p><code>10</code> and <code>5</code> are operands</p>
</li>
<li><p><code>+</code> is the operator</p>
</li>
<li><p>The result is <code>15</code></p>
</li>
</ul>
<p>Operators tell JavaScript what action needs to be performed.</p>
<h3>1. Arithmetic Operators</h3>
<p>Arithmetic operators are used for mathematical calculations.</p>
<table>
<thead>
<tr>
<th>Operator</th>
<th>Meaning</th>
</tr>
</thead>
<tbody><tr>
<td>+</td>
<td>Addition</td>
</tr>
<tr>
<td>-</td>
<td>Subtraction</td>
</tr>
<tr>
<td>*</td>
<td>Multiplication</td>
</tr>
<tr>
<td>/</td>
<td>Division</td>
</tr>
<tr>
<td>%</td>
<td>Modulus (remainder)</td>
</tr>
</tbody></table>
<h3>Basic Example</h3>
<pre><code class="language-javascript">let a = 10;
let b = 3;

console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.33
console.log(a % b); // 1
</code></pre>
<h3>Real-World Example: Shopping Cart Calculation</h3>
<p>In an e-commerce application, arithmetic operators are used to calculate total price:</p>
<pre><code class="language-js">let price = 499;
let quantity = 2;
let discount = 100;

let total = (price * quantity) - discount;

console.log("Total price:", total);
</code></pre>
<p>This is exactly how most online platforms calculate totals before checkout.</p>
<h3>Modulus Operator in Real Use</h3>
<pre><code class="language-javascript">console.log(10 % 2); // 0
console.log(7 % 2);  // 1
</code></pre>
<p>Use cases:</p>
<ul>
<li><p>Checking even or odd numbers</p>
</li>
<li><p>Alternating UI elements (for example, table rows)</p>
</li>
<li><p>Pagination logic</p>
</li>
</ul>
<h3>2. Comparison Operators</h3>
<p>Comparison operators compare two values and return a boolean (<code>true</code> or <code>false</code>).</p>
<table>
<thead>
<tr>
<th>Operator</th>
<th>Meaning</th>
</tr>
</thead>
<tbody><tr>
<td>==</td>
<td>Equal (loose comparison)</td>
</tr>
<tr>
<td>===</td>
<td>Strict equal</td>
</tr>
<tr>
<td>!=</td>
<td>Not equal</td>
</tr>
<tr>
<td>&gt;</td>
<td>Greater than</td>
</tr>
<tr>
<td>&lt;</td>
<td>Less than</td>
</tr>
</tbody></table>
<h3>Basic Example</h3>
<pre><code class="language-js">console.log(5 &gt; 3); // true
console.log(2 &lt; 1); // false
</code></pre>
<hr />
<h3>Critical Concept: <code>==</code> vs <code>===</code></h3>
<pre><code class="language-js">console.log(5 == "5");   // true
console.log(5 === "5");  // false
</code></pre>
<ul>
<li><p><code>==</code> performs type conversion</p>
</li>
<li><p><code>===</code> checks both value and type</p>
</li>
</ul>
<hr />
<h3>Real-World Problem</h3>
<pre><code class="language-javascript">let input = "0";

if (input == 0) {
  console.log("Accepted");
}
</code></pre>
<p>This may lead to unexpected behavior because <code>"0"</code> is converted to <code>0</code>.</p>
<h3>Best Practice</h3>
<p>Always prefer strict comparison:</p>
<pre><code class="language-js">if (input === 0) {
  // safer comparison
}
</code></pre>
<h3>Real-World Example: Authentication</h3>
<pre><code class="language-javascript">let enteredPassword = "1234";
let actualPassword = 1234;

if (enteredPassword === actualPassword) {
  console.log("Login successful");
} else {
  console.log("Invalid credentials");
}
</code></pre>
<p>This prevents logical and security issues.</p>
<h2>3. Logical Operators</h2>
<p>Logical operators are used to combine multiple conditions.</p>
<table>
<thead>
<tr>
<th>Operator</th>
<th>Meaning</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>&amp;&amp;</td>
<td>AND</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>OR</td>
</tr>
<tr>
<td>!</td>
<td>NOT</td>
<td></td>
<td></td>
</tr>
</tbody></table>
<h3>AND Operator (<code>&amp;&amp;</code>)</h3>
<p>All conditions must be true.</p>
<pre><code class="language-javascript">let age = 20;
let hasID = true;

if (age &gt; 18 &amp;&amp; hasID) {
  console.log("Access granted");
}
</code></pre>
<p>Real-world usage:</p>
<ul>
<li><p>Verification systems</p>
</li>
<li><p>Entry conditions</p>
</li>
<li><p>Form validation</p>
</li>
</ul>
<h3>OR Operator (<code>||</code>)</h3>
<p>At least one condition must be true.</p>
<pre><code class="language-javascript">let isAdmin = false;
let isEditor = true;

if (isAdmin || isEditor) {
  console.log("Access granted");
}
</code></pre>
<p>Real-world usage:</p>
<ul>
<li><p>Role-based permissions</p>
</li>
<li><p>Feature access control</p>
</li>
</ul>
<h3>NOT Operator (<code>!</code>)</h3>
<p>Reverses a boolean value.</p>
<pre><code class="language-javascript">let isLoggedIn = false;

if (!isLoggedIn) {
  console.log("Please log in");
}
</code></pre>
<h2>Truth Tables</h2>
<h3>AND (<code>&amp;&amp;</code>)</h3>
<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>Result</th>
</tr>
</thead>
<tbody><tr>
<td>true</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td>false</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td>false</td>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td>false</td>
</tr>
</tbody></table>
<h3>OR (<code>||</code>)</h3>
<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>Result</th>
</tr>
</thead>
<tbody><tr>
<td>true</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td>true</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td>false</td>
</tr>
</tbody></table>
<h2>4. Assignment Operators</h2>
<p>Assignment operators are used to assign and update values.</p>
<table>
<thead>
<tr>
<th>Operator</th>
<th>Meaning</th>
</tr>
</thead>
<tbody><tr>
<td>=</td>
<td>Assign value</td>
</tr>
<tr>
<td>+=</td>
<td>Add and assign</td>
</tr>
<tr>
<td>-=</td>
<td>Subtract and assign</td>
</tr>
</tbody></table>
<h3>Example</h3>
<pre><code class="language-javascript">let balance = 1000;

balance += 500; // deposit
balance -= 200; // withdrawal

console.log(balance); // 1300
</code></pre>
<h3>Real-World Use Case</h3>
<p>These operators are commonly used in:</p>
<ul>
<li><p>Banking systems</p>
</li>
<li><p>Counters (likes, views)</p>
</li>
<li><p>Inventory updates</p>
</li>
</ul>
<h2>Mini Project: Combining All Operators</h2>
<p>Let’s simulate a small real-world scenario.</p>
<h3>Problem</h3>
<ul>
<li><p>Calculate total price</p>
</li>
<li><p>Apply discount if user is eligible</p>
</li>
<li><p>Display final result</p>
</li>
</ul>
<h3>Solution</h3>
<pre><code class="language-javascript">let price = 200;
let quantity = 3;
let age = 22;
let isMember = true;

// Step 1: Calculate total
let total = price * quantity;

// Step 2: Apply discount
if (isMember &amp;&amp; age &gt; 18) {
  total -= 100;
}

// Step 3: Output result
console.log("Final amount:", total);
</code></pre>
<h2>Common Mistakes</h2>
<h3>1. Using <code>==</code> instead of <code>===</code></h3>
<pre><code class="language-js">// Incorrect
if (value == "10")

// Correct
if (value === "10")
</code></pre>
<h3>2. Unexpected Type Conversion</h3>
<pre><code class="language-javascript">console.log("5" + 2); // "52"
</code></pre>
<p>JavaScript converts numbers to strings in this case.</p>
<h3>3. Misunderstanding Logical Conditions</h3>
<pre><code class="language-js">if (true &amp;&amp; false) // always false
</code></pre>
<p>Understanding truth tables is important.</p>
<h2>Operator Categories Summary</h2>
<table>
<thead>
<tr>
<th>Category</th>
<th>Purpose</th>
</tr>
</thead>
<tbody><tr>
<td>Arithmetic</td>
<td>Perform calculations</td>
</tr>
<tr>
<td>Comparison</td>
<td>Compare values</td>
</tr>
<tr>
<td>Logical</td>
<td>Combine conditions</td>
</tr>
<tr>
<td>Assignment</td>
<td>Update values</td>
</tr>
</tbody></table>
<h2>Conclusion</h2>
<p>Operators are the foundation of JavaScript logic. They allow you to:</p>
<ul>
<li><p>Perform calculations</p>
</li>
<li><p>Make decisions</p>
</li>
<li><p>Control application flow</p>
</li>
</ul>
<p>Without operators, writing meaningful programs would not be possible.</p>
<hr />
<p><em>I write articles on</em> <a href="http://blog.prakashtsx.com"><em><strong>blog.prakashtsx.com</strong></em></a> <em>and also post development-related content on:</em></p>
<ul>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>Twitter / X</strong></a></p>
</li>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Linux File System Exploration Looking Inside the System]]></title><description><![CDATA[Introduction
When we use Linux, we mostly interact with it through commands. But those commands are only interfaces.
The real system exists underneath, inside the filesystem.
Linux follows a powerful ]]></description><link>https://blog.prakashtsx.me/linux-file-system</link><guid isPermaLink="true">https://blog.prakashtsx.me/linux-file-system</guid><category><![CDATA[Linux]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Wed, 22 Apr 2026 18:05:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/aefb6c56-cdf5-4fac-aac7-51fc372501c0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Introduction</h2>
<p>When we use Linux, we mostly interact with it through commands. But those commands are only interfaces.</p>
<p>The real system exists underneath, inside the filesystem.</p>
<p>Linux follows a powerful design philosophy: the operating system exposes its internal state through files. If you know where to look, you can understand how everything works — users, processes, networking, services, and even hardware.</p>
<p>In this exploration, I moved beyond basic commands and investigated how Linux actually works internally by studying its filesystem structure.</p>
<h3>Starting Point — The Root of Everything</h3>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/4a53e7d0-5cfb-4ab2-9f01-d95bdbf4e40a.png" alt="" style="display:block;margin:0 auto" />

<p>Linux begins from a single root directory:</p>
<pre><code class="language-plaintext">/
</code></pre>
<p>Unlike operating systems that separate drives (like C:, D:), Linux builds everything under one unified tree.</p>
<p>This structure organizes the system clearly:</p>
<ul>
<li><p>Configuration → <code>/etc</code></p>
</li>
<li><p>Processes → <code>/proc</code></p>
</li>
<li><p>Devices → <code>/dev</code></p>
</li>
<li><p>Logs → <code>/var/log</code></p>
</li>
<li><p>Boot system → <code>/boot</code></p>
</li>
</ul>
<p>This unified design solves fragmentation problems and makes the system easier to inspect, debug, and understand.</p>
<h3>1. <code>/etc</code> — Where System Behavior Is Defined</h3>
<p>The <code>/etc</code> directory contains configuration files that control how the system behaves.</p>
<h3>User Management — <code>/etc/passwd</code> and <code>/etc/shadow</code></h3>
<ul>
<li><p><code>/etc/passwd</code> → stores user information (username, UID, home directory, shell)</p>
</li>
<li><p><code>/etc/shadow</code> → stores encrypted passwords</p>
</li>
</ul>
<h3>Problem</h3>
<p>If password data were stored in a publicly readable file, it would create a major security risk.</p>
<h3>Solution</h3>
<p>Linux separates user identity and authentication:</p>
<ul>
<li><p><code>/etc/passwd</code> → readable by all programs</p>
</li>
<li><p><code>/etc/shadow</code> → restricted access</p>
</li>
</ul>
<h3>Insight</h3>
<p>Security in Linux is not only about permissions, but also about <strong>data separation and system design</strong>.</p>
<h3>2. DNS Resolution — How Linux Finds Websites</h3>
<p><strong>To understand how Linux connects to websites, I explored:</strong></p>
<ul>
<li><p><code>/etc/hosts</code></p>
</li>
<li><p><code>/etc/resolv.conf</code></p>
</li>
<li><p><code>/etc/nsswitch.conf</code></p>
</li>
</ul>
<p><strong>How it works</strong></p>
<p>When you access a domain:</p>
<ol>
<li><p>Linux checks <code>/etc/hosts</code> (local mapping)</p>
</li>
<li><p>If not found, it queries DNS servers from <code>/etc/resolv.conf</code></p>
</li>
<li><p>The lookup order is defined in <code>/etc/nsswitch.conf</code></p>
</li>
</ol>
<p><strong>Problem</strong></p>
<p>Systems need a reliable way to resolve domain names, even without external DNS.</p>
<p><strong>Solution</strong></p>
<p>Linux provides a flexible, file-based resolution system.</p>
<p><strong>Real-world use</strong></p>
<p>Developers use <code>/etc/hosts</code> to override domains locally for testing applications.</p>
<p><strong>Insight</strong></p>
<p>Networking behavior in Linux is not hidden — it is fully configurable through simple files.</p>
<h3>3. <code>/proc</code> — The Kernel’s Live Interface</h3>
<p>The <code>/proc</code> directory is not stored on disk. It is a virtual filesystem created by the kernel in real time.</p>
<p><strong>What it exposes</strong></p>
<ul>
<li><p>Process information → <code>/proc/[PID]/status</code></p>
</li>
<li><p>Open files → <code>/proc/[PID]/fd</code></p>
</li>
<li><p>System stats → <code>/proc/cpuinfo</code>, <code>/proc/meminfo</code></p>
</li>
</ul>
<p><strong>Problem</strong></p>
<p>Applications and tools need access to internal system state (processes, memory, CPU usage).</p>
<p><strong>Solution</strong></p>
<p>Instead of complex APIs, Linux exposes this data as files.</p>
<p><strong>Investigation</strong></p>
<p>When I explored <code>/proc/$$</code>, I could directly see details about the current running shell process.</p>
<p><strong>Insight</strong></p>
<p>Tools like <code>ps</code>, <code>top</code>, and <code>lsof</code> are not special — they simply read from <code>/proc</code>. This means the system is transparent and you can build your own monitoring tools.</p>
<h3>4. Network Routing — How Data Finds Its Path</h3>
<p>Routing information is available in:</p>
<ul>
<li><p><code>/proc/net/route</code></p>
</li>
<li><p><code>ip route</code> output</p>
</li>
</ul>
<p><strong>What it represents</strong></p>
<p>The routing table defines:</p>
<ul>
<li><p>Where packets should go</p>
</li>
<li><p>Which interface to use</p>
</li>
<li><p>Which gateway to use</p>
</li>
</ul>
<p><strong>Problem</strong></p>
<p>Without routing rules, the system would not know how to send data across networks.</p>
<p><strong>Solution</strong></p>
<p>Linux maintains routing information in structured, readable form.</p>
<p><strong>Insight</strong></p>
<p>Even complex networking decisions are exposed as data. Linux does not hide its networking logic — it makes it inspectable.</p>
<h3>5. <code>systemd</code> — Service Management Through Files</h3>
<p>Modern Linux systems use <strong>systemd</strong> to manage services.</p>
<p><strong>Where configuration lives</strong></p>
<ul>
<li><p><code>/etc/systemd/system/</code></p>
</li>
<li><p><code>/lib/systemd/system/</code></p>
</li>
</ul>
<p>Each service is defined using a configuration file.</p>
<p><strong>Problem</strong></p>
<p>Managing system services manually would be complex and inconsistent.</p>
<p><strong>Solution</strong></p>
<p>Linux uses declarative configuration files to define:</p>
<ul>
<li><p>How services start</p>
</li>
<li><p>Dependencies</p>
</li>
<li><p>Restart policies</p>
</li>
</ul>
<p><strong>Insight</strong></p>
<p>Enabling a service does not install anything new. It simply creates links that include the service in the boot process.</p>
<h3>6. <code>/var/log</code> — The System’s Memory</h3>
<p>Logs are stored in <code>/var/log</code>.</p>
<p><strong>What logs contain</strong></p>
<ul>
<li><p>Login attempts</p>
</li>
<li><p>System errors</p>
</li>
<li><p>Service activity</p>
</li>
<li><p>Kernel messages</p>
</li>
</ul>
<p><strong>Problem</strong></p>
<p>Without logs, debugging and monitoring system behavior would be extremely difficult.</p>
<p><strong>Solution</strong></p>
<p>Linux records system activity in structured log files.</p>
<p><strong>Real-world use</strong></p>
<p>System administrators use logs to debug production issues and detect security threats.</p>
<p><strong>Insight</strong></p>
<p>Linux is highly observable. If something goes wrong, the evidence is usually already recorded.</p>
<h3>7. <code>/dev</code> — Everything Is a Device File</h3>
<p>The <code>/dev</code> directory represents hardware and virtual devices as files.</p>
<p><strong>Examples</strong></p>
<ul>
<li><p><code>/dev/null</code> → discards output</p>
</li>
<li><p><code>/dev/zero</code> → generates zero bytes</p>
</li>
<li><p><code>/dev/urandom</code> → generates random data</p>
</li>
</ul>
<p><strong>Problem</strong></p>
<p>Programs need a consistent way to interact with hardware.</p>
<p><strong>Solution</strong></p>
<p>Linux abstracts devices into files, allowing standard operations like <code>read</code> and <code>write</code>.</p>
<p><strong>Insight</strong></p>
<p>This reflects a core Unix philosophy: <strong>everything is treated as a file</strong>, making the system consistent and simple.</p>
<h3>8. File Permissions — The Security Model</h3>
<p>Linux controls access using permissions:</p>
<ul>
<li><p>Owner</p>
</li>
<li><p>Group</p>
</li>
<li><p>Others</p>
</li>
</ul>
<p><strong>Special Case — SUID</strong></p>
<p>Some binaries have a special permission:</p>
<pre><code class="language-plaintext">-rwsr-xr-x
</code></pre>
<p>This allows them to run with the owner’s privileges.</p>
<p><strong>Problem</strong></p>
<p>Some programs require elevated privileges but giving full root access is dangerous.</p>
<p><strong>Solution</strong></p>
<p>Linux allows controlled privilege escalation using mechanisms like SUID.</p>
<p><strong>Insight</strong></p>
<p>Linux security is granular. It enables limited privilege access instead of unrestricted control.</p>
<h3>9. <code>/boot</code> — How the System Starts</h3>
<p>The <code>/boot</code> directory contains files required to start the system.</p>
<p><strong>Contents</strong></p>
<ul>
<li><p>Kernel (<code>vmlinuz</code>)</p>
</li>
<li><p>Initial RAM disk (<code>initrd</code>)</p>
</li>
<li><p>Bootloader files</p>
</li>
</ul>
<p><strong>Boot process</strong></p>
<ol>
<li><p>Firmware starts the system</p>
</li>
<li><p>Bootloader loads the kernel</p>
</li>
<li><p>Kernel initializes hardware</p>
</li>
<li><p>systemd starts services</p>
</li>
</ol>
<p><strong>Problem</strong></p>
<p>The system needs a way to load essential components before the full OS is available.</p>
<p><strong>Solution</strong></p>
<p>Linux uses an initial temporary environment (<code>initrd</code>) to prepare the system.</p>
<p><strong>Insight</strong></p>
<p>Linux solves complex startup problems using staged initialization rather than a single step.</p>
<h3>10. Hardware Information — Direct from the Kernel</h3>
<p>System information is available in:</p>
<ul>
<li><p><code>/proc/cpuinfo</code></p>
</li>
<li><p><code>/proc/meminfo</code></p>
</li>
</ul>
<p><strong>Observation</strong></p>
<p>Memory may appear “used” even when the system is idle.</p>
<p><em><strong>Problem</strong></em></p>
<p>Unused memory is wasted memory.</p>
<p><strong>Solution</strong></p>
<p>Linux uses free memory for caching to improve performance.</p>
<p><strong>Insight</strong></p>
<p>Linux prioritizes efficiency — it uses available resources instead of leaving them idle.</p>
<h3>Conclusion</h3>
<p><strong>This exploration changed how I understand Linux.</strong></p>
<p>Instead of seeing it as a command-line system, I now see it as a system built around files:</p>
<ul>
<li><p>Configuration is stored in files</p>
</li>
<li><p>System state is exposed through files</p>
</li>
<li><p>Hardware is accessed through files</p>
</li>
</ul>
<p>This design makes Linux:</p>
<ul>
<li><p>Transparent</p>
</li>
<li><p>Debuggable</p>
</li>
<li><p>Flexible</p>
</li>
</ul>
<p>Linux is not a black box. It is a system you can inspect, understand, and even reconstruct from its filesystem.</p>
<p>Understanding the filesystem is not just a skill — it is the key to understanding how Linux actually works.</p>
]]></content:encoded></item><item><title><![CDATA[Spread vs Rest Operators in JavaScript]]></title><description><![CDATA[JavaScript has evolved a lot over the years, and with ES6 came two incredibly useful features: the spread (...) and rest (...) operators. At first glance, they look identical—but they serve very diffe]]></description><link>https://blog.prakashtsx.me/spread-vs-rest</link><guid isPermaLink="true">https://blog.prakashtsx.me/spread-vs-rest</guid><category><![CDATA[Spread operator]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Tue, 21 Apr 2026 10:41:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/392794a7-8d17-49b2-8d20-ca88ad83b1f0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>JavaScript has evolved a lot over the years, and with ES6 came two incredibly useful features: the <strong>spread (</strong><code>...</code><strong>)</strong> and <strong>rest (</strong><code>...</code><strong>) operators</strong>. At first glance, they look identical—but they serve very different purposes.</p>
<p>If you’ve ever been confused about when to use each one, this guide will clear things up with simple explanations and practical examples.</p>
<h3>What is the Spread Operator?</h3>
<p>The <strong>spread operator (</strong><code>...</code><strong>)</strong> is used to <strong>expand</strong> elements from an iterable (like an array or object) into individual elements.</p>
<p><strong>Think of it as: <em>“spreading things out”</em></strong></p>
<p><em><strong>Example with Arrays :</strong></em></p>
<pre><code class="language-javascript">const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];

console.log(newNumbers);
// [1, 2, 3, 4, 5]
</code></pre>
<p>Here, the spread operator takes each element from <code>numbers</code> and expands it into the new array.</p>
<p><em><strong>Example with Objects :</strong></em></p>
<pre><code class="language-javascript">const user = { name: "Rahul", age: 22 };
const updatedUser = { ...user, city: "Jodhpur" };

console.log(updatedUser);
// { name: "Rahul", age: 22, city: "Jodhpur" }
</code></pre>
<h3>What is the Rest Operator?</h3>
<p>The <strong>rest operator (</strong><code>...</code><strong>)</strong> does the opposite. It <strong>collects</strong> multiple elements and packs them into a single structure (usually an array).</p>
<p><strong>Think of it as: “gathering things together”</strong></p>
<p><em><strong>Example in Function Parameters :</strong></em></p>
<pre><code class="language-javascript">function sum(...numbers) {
  return numbers.reduce((total, num) =&gt; total + num, 0);
}

console.log(sum(1, 2, 3, 4));
// 10
</code></pre>
<p>Here, all arguments are collected into the <code>numbers</code> array.</p>
<p><em><strong>Example with Destructuring :</strong></em></p>
<pre><code class="language-javascript">const [first, ...rest] = [10, 20, 30, 40];

console.log(first); // 10
console.log(rest);  // [20, 30, 40]
</code></pre>
<h3>Spread vs Rest: Key Differences</h3>
<table>
<thead>
<tr>
<th><em><strong>Feature</strong></em></th>
<th><em><strong>Spread Operator</strong></em></th>
<th><em><strong>Rest Operator</strong></em></th>
</tr>
</thead>
<tbody><tr>
<td>Purpose</td>
<td>Expands elements</td>
<td>Collects elements</td>
</tr>
<tr>
<td>Direction</td>
<td>One → Many</td>
<td>Many → One</td>
</tr>
<tr>
<td>Usage Context</td>
<td>Arrays, Objects, Function calls</td>
<td>Function params, Destructuring</td>
</tr>
<tr>
<td>Behavior</td>
<td>Breaks apart</td>
<td>Packs together</td>
</tr>
</tbody></table>
<h3>Expanding vs Collecting (Core Idea)</h3>
<ul>
<li><p><em><strong>Spread = Expanding values</strong></em></p>
<pre><code class="language-javascript">const arr = [1, 2, 3];
console.log(...arr); // 1 2 3
</code></pre>
</li>
<li><p><em><strong>Rest = Collecting values</strong></em></p>
<pre><code class="language-javascript">function demo(...args) {
  console.log(args);
}
demo(1, 2, 3); // [1, 2, 3]
</code></pre>
</li>
</ul>
<h3>Using Spread with Arrays and Objects</h3>
<p><em><strong>1. Copying Arrays (Shallow Copy)</strong></em></p>
<pre><code class="language-javascript">const original = [1, 2, 3];
const copy = [...original];
</code></pre>
<p><em><strong>2. Merging Arrays</strong></em></p>
<pre><code class="language-javascript">const a = [1, 2];
const b = [3, 4];
const merged = [...a, ...b];
</code></pre>
<p><em><strong>3. Copying Objects</strong></em></p>
<pre><code class="language-javascript">const obj1 = { a: 1 };
const obj2 = { ...obj1 };
</code></pre>
<p><em><strong>4. Merging Objects</strong></em></p>
<pre><code class="language-javascript">const objA = { name: "Rahul" };
const objB = { age: 22 };

const combined = { ...objA, ...objB };
</code></pre>
<h3>Practical Use Cases</h3>
<p><em><strong>1. Cloning State (React Example)</strong></em></p>
<pre><code class="language-javascript">const newState = { ...oldState, isLoggedIn: true };
</code></pre>
<p><em><strong>2. Updating Arrays Without Mutation</strong></em></p>
<pre><code class="language-javascript">const items = ["apple", "banana"];
const newItems = [...items, "orange"];
</code></pre>
<p><em><strong>3. Flexible Function Arguments</strong></em></p>
<pre><code class="language-javascript">function logAll(...args) {
  args.forEach(arg =&gt; console.log(arg));
}
</code></pre>
<p><em><strong>4. Removing Elements Using Destructuring</strong></em></p>
<pre><code class="language-javascript">const [remove, ...remaining] = [1, 2, 3];
</code></pre>
<h3>Visualizing the Concept</h3>
<p><em><strong>Spread (Expanding)</strong></em></p>
<pre><code class="language-javascript">[1, 2, 3]  →  1, 2, 3
</code></pre>
<p><em><strong>Rest (Collecting)</strong></em></p>
<pre><code class="language-javascript">1, 2, 3  →  [1, 2, 3]
</code></pre>
<h3>Common Mistakes</h3>
<ul>
<li><p>Using rest outside valid positions (e.g., not at the end of parameters)</p>
</li>
<li><p>Confusing spread with deep copy (it’s only a shallow copy)</p>
</li>
<li><p>Overwriting properties unintentionally in object spread</p>
</li>
</ul>
<h3>Conclusion</h3>
<p>Even though they share the same syntax (<code>...</code>), the <strong>spread</strong> and <strong>rest</strong> operators serve opposite roles:</p>
<ul>
<li><p><strong>Spread</strong> → expands values</p>
</li>
<li><p><strong>Rest</strong> → collects values</p>
</li>
</ul>
<p>Mastering these operators will make your JavaScript code cleaner, more flexible, and easier to maintain—especially in modern frameworks like React.</p>
<hr />
<p><em>I write articles on</em> <a href="http://blog.prakashtsx.com"><em><strong>blog.prakashtsx.com</strong></em></a> <em>and also post development-related content on:</em></p>
<ul>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>Twitter / X</strong></a></p>
</li>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[JavaScript Modules : Import and Export]]></title><description><![CDATA[So you've been writing JavaScript for a while now. Maybe you started with a single script tag in your HTML, and everything was fine — until it wasn't.
Your index.js became 800 lines long. You had func]]></description><link>https://blog.prakashtsx.me/javascript-modules</link><guid isPermaLink="true">https://blog.prakashtsx.me/javascript-modules</guid><category><![CDATA[import export ]]></category><category><![CDATA[javascript modules]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[@hiteshchoudharylco]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Tue, 21 Apr 2026 08:24:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/778a5f2e-40ac-4d4e-80c9-74d7be9b1f1e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>So you've been writing JavaScript for a while now. Maybe you started with a single script tag in your HTML, and everything was fine — until it wasn't.</p>
<p>Your index.js became 800 lines long. You had functions named <code>doStuff()</code> and <code>handleThing()</code> that you were scared to delete because you didn't know what depended on them. Variables were clashing. You were scrolling forever just to find the function you wrote last week.</p>
<p>Sound familiar?</p>
<p>This is exactly the problem JavaScript modules were built to solve.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/ffb2fb01-1746-441b-803e-3669f1023956.png" alt="" style="display:block;margin:0 auto" />

<h3>Why Modules Are Needed</h3>
<p>Imagine building a house and putting every single thing — furniture, plumbing, wiring, walls — in one giant room. Technically it might work, but it would be a nightmare to maintain, fix, or extend.</p>
<p>That's what happens when all your JavaScript lives in one file.</p>
<p>Here's a real-world scenario. You're building a web app that handles user authentication, fetches data from an API, formats dates, and handles UI interactions. Without modules, your file might look like this:</p>
<p><code>Everything dumped in one file — auth logic, API calls, date formatting, UI code... 1,200 lines later...</code></p>
<p>You can't reuse any of it easily. If your teammate wants to use your <code>formatDate()</code> function in another project, they'd have to copy-paste it manually. If you rename a variable, you might break something three screens away. Testing becomes a guessing game.</p>
<p>Modules fix this by letting you split your code into small, focused files — each doing one job well. Then you connect them together using import and export.</p>
<h3>Exporting — Sharing Code from a Module</h3>
<p>When you want to make something available from a file, you export it.</p>
<p>Think of a file as a shop. By default, everything inside that shop is private — customers can't see it. Exporting is like putting something in the display window so others can use it.</p>
<p>There are two ways to export in JavaScript:</p>
<p><strong>1. Named Exports</strong></p>
<p>You can export multiple things from a single file by name.</p>
<p><em><strong>mathUtils.js</strong></em></p>
<pre><code class="language-javascript"> export function add(a, b) { 
    return a + b; 
}
export function subtract(a, b) {
     return a - b; 
}

export const PI = 3.14159;
</code></pre>
<p>Each of these is exported individually, and each has its own name. You can have as many named exports as you want in a file.</p>
<p><strong>2. Default Export</strong></p>
<p>A default export is the "main thing" a file exports. There can only be one default export per file.</p>
<p><em><strong>greet.js</strong></em></p>
<pre><code class="language-javascript">export default function greet(name) { 
    return Hello, ${name}!; 
}
</code></pre>
<p>Think of it as the star product of the shop — the one thing that represents what this file is primarily about.</p>
<h3>Importing — Using Code from Another Module</h3>
<p><em>Once something is exported, you can import it into another file and use it.</em></p>
<p><strong>Importing Named Exports</strong></p>
<p>When you import named exports, you use curly braces and the exact name used during export.</p>
<p><em><strong>app.js</strong></em></p>
<pre><code class="language-javascript"> import { add, subtract, PI } from './mathUtils.js';

 console.log(add(5, 3)); //Output : 8
 console.log(subtract(10, 4)); // 6 
 console.log(PI); // 3.14159
</code></pre>
<p>The names inside the curly braces must match the exported names exactly. This is important.</p>
<p><strong>Importing Default Exports</strong></p>
<p>When importing a default export, you don't use curly braces, and you can give it any name you like.</p>
<p><em><strong>app.js</strong></em></p>
<pre><code class="language-javascript">import greet from './greet.js';

console.log(greet('Arjun')); // Hello, Arjun!
</code></pre>
<p>Since there's only one default export per file, JavaScript knows exactly what you're asking for.</p>
<p><strong>Importing Everything</strong></p>
<p>If you want to import all named exports from a file at once, you can use the * syntax and give it a namespace:</p>
<pre><code class="language-javascript">import * as MathUtils from './mathUtils.js';

console.log(MathUtils.add(2, 3)); // 5 
console.log(MathUtils.PI); // 3.14159
</code></pre>
<p><strong>Renaming Imports</strong></p>
<p>You can also rename named imports to avoid conflicts:</p>
<pre><code class="language-javascript">import { add as addNumbers } from './mathUtils.js';

console.log(addNumbers(1, 2)); // 3
</code></pre>
<h3>Default vs Named Exports — Which One to Use?</h3>
<p>This is a common question, and the short answer is: it depends on what the file is for.</p>
<p><strong>Use named exports when:</strong></p>
<ul>
<li><p>Your file exports multiple related utilities (like a math or string helper file)</p>
</li>
<li><p>You want callers to be explicit about what they're using</p>
</li>
<li><p>You're building a library or utility module</p>
</li>
</ul>
<p><em><strong>stringUtils.js</strong></em></p>
<pre><code class="language-javascript">export function capitalize(str) { ... } 
export function trim(str) { ... } 
export function slugify(str) { ... }
</code></pre>
<p><strong>Use default exports when:</strong></p>
<ul>
<li><p>Your file represents one main thing — a component, a class, a single function</p>
</li>
<li><p>You want flexibility in naming on the consumer side</p>
</li>
</ul>
<p><em><strong>UserCard.js</strong></em></p>
<pre><code class="language-javascript">export default function UserCard({ name, email }) { ... }
</code></pre>
<p><strong>Can you use both?</strong></p>
<p>Yes! A file can have both named exports and a default export.</p>
<p><em><strong>api.js</strong></em></p>
<pre><code class="language-javascript">export const BASE_URL = 'https://api.example.com'; 
export const TIMEOUT = 5000;

export default async function fetchUser(id) { 
const response = await fetch(\({BASE_URL}/users/\){id}); 
return response.json(); 
}
</code></pre>
<p>Then you import them like this:</p>
<pre><code class="language-javascript">import fetchUser, { BASE_URL, TIMEOUT } from './api.js';
</code></pre>
<h3>A Real-World Example</h3>
<p>Let's put it all together with a mini project structure.</p>
<p>Imagine you're building a simple blog app. Here's how you'd split it into modules:</p>
<h3>File: utils/formatDate.js</h3>
<pre><code class="language-javascript">export function formatDate(dateString) { 
const date = new Date(dateString); 
return date.toLocaleDateString('en-IN', { year: 'numeric', month: 'long', day: 'numeric', }); 
}
</code></pre>
<h3>File: api/posts.js</h3>
<pre><code class="language-javascript">const API_URL = 'https://jsonplaceholder.typicode.com';

export async function fetchPosts() { 
const res = await fetch(${API_URL}/posts); 
return res.json(); 
}

export async function fetchPostById(id) { 
const res = await fetch(\({API_URL}/posts/\){id}); 
return res.json(); 
}
</code></pre>
<h3>File: components/renderPost.js</h3>
<pre><code class="language-javascript">import { formatDate } from '../utils/formatDate.js';

export default function renderPost(post) { 
return &lt;article&gt; 
        &lt;h2&gt;${post.title}&lt;/h2&gt; 
        &lt;p&gt;${post.body}&lt;/p&gt; 
        &lt;small&gt; Posted on: ${formatDate(post.date)}&lt;/small&gt; 
&lt;/article&gt; ; 
}
</code></pre>
<h3>File: app.js (main entry point)</h3>
<pre><code class="language-javascript">import { fetchPosts } from './api/posts.js'; 
import renderPost from './components/renderPost.js';

async function init() { 
const posts = await fetchPosts(); 
const container = document.getElementById('app'); container.innerHTML = posts.map(renderPost).join(''); 
}

init();
</code></pre>
<p>Each file has a single clear job. <code>formatDate.js</code> handles dates. <code>posts.js</code> handles API calls. <code>renderPost.js</code> renders HTML. <code>app.js</code> connects everything.</p>
<p>This is the power of modules.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/75622a27-c5f7-4ece-89e1-e1cc8e444860.png" alt="" style="display:block;margin:0 auto" />

<h3>Benefits of Modular Code</h3>
<p><strong>Let's make it crystal clear why you should care.</strong></p>
<p><strong>1. Maintainability</strong> When a bug appears in date formatting, you go straight to <code>formatDate.js</code>. You don't dig through 900 lines of mixed code. Each file has one job, so you always know where to look.</p>
<p><strong>2. Reusability</strong> Your <code>formatDate.js</code> can be imported in ten different files across your project — or even in a completely different project. Write once, use everywhere.</p>
<p><strong>3. Avoiding Naming Conflicts</strong> In old-school JavaScript without modules, all variables and functions lived in the global scope. Two libraries using a variable called <code>data</code> would clash. With modules, each file has its own scope. There's no pollution, no conflicts.</p>
<p><strong>4. Better Collaboration</strong> In a team, different developers can work on different modules without stepping on each other's code. Merge conflicts become rare because everyone is working in their own file.</p>
<p><strong>5. Easier Testing</strong> Small, focused modules are easy to test in isolation. You can import just <code>formatDate.js</code> and write unit tests for it without bringing in the entire application.</p>
<p><strong>6. Lazy Loading (Performance)</strong> Modern bundlers and browsers can load modules on demand — only when they're needed. This means your app doesn't have to load everything upfront, leading to faster initial load times.</p>
<hr />
<h3>How to Use Modules in the Browser</h3>
<p>If you're running JavaScript directly in the browser (without a bundler like Webpack or Vite), just add <code>type="module"</code> to your script tag:</p>
<p>That's it. The browser will handle the imports and exports natively.</p>
<p>Note: Modules must be served over a server (even a local dev server). They won't work if you just open an HTML file directly in your browser because of CORS restrictions. Use VS Code's Live Server extension or <code>npx serve</code> during development.</p>
<h3>Quick Reference — Cheat Sheet</h3>
<p>NAMED EXPORT: <code>export function myFunction() {} export const myValue = 42;</code></p>
<p>NAMED IMPORT: <code>import { myFunction, myValue } from './myFile.js';</code></p>
<p>DEFAULT EXPORT: <code>export default function myFunction() {}</code></p>
<p>DEFAULT IMPORT: <code>import myFunction from './myFile.js';</code></p>
<p>IMPORT ALL: <code>import * as MyModule from './myFile.js';</code></p>
<p>RENAME ON IMPORT: <code>import { myFunction as renamed } from './myFile.js';</code></p>
<p>MIXED (default + named): <code>import defaultExport, { namedExport } from './myFile.js';</code></p>
<h3>Wrapping Up</h3>
<p>JavaScript modules aren't just a syntax feature — they're a mindset shift. They push you to think about your code in terms of small, independent, reusable pieces rather than one giant tangled mess.</p>
<p>Start small. Next time you write a utility function, put it in its own file and export it. Import it where needed. As your project grows, you'll thank yourself for this habit.</p>
<p>The best codebases aren't the ones with the cleverest tricks — they're the ones where every file does exactly one thing, and does it well.</p>
<p>That's what modules give you.</p>
<hr />
<p><em>I write articles on</em> <a href="http://blog.prakashtsx.com"><em><strong>blog.prakashtsx.com</strong></em></a> <em>and also post development-related content on:</em></p>
<ul>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>Twitter / X</strong></a></p>
</li>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Understanding Objects in JavaScript]]></title><description><![CDATA[If you've been learning JavaScript, you've likely used Arrays to store lists of data. But what happens when you need to describe something more complex — like a person, a car, or a laptop?
An array li]]></description><link>https://blog.prakashtsx.me/js-objects</link><guid isPermaLink="true">https://blog.prakashtsx.me/js-objects</guid><category><![CDATA[javascript objects]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[@hiteshchoudharylco]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Sun, 29 Mar 2026 12:36:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/5b015f9d-eb95-43e0-aabd-f4f939189f44.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you've been learning JavaScript, you've likely used Arrays to store lists of data. But what happens when you need to describe something more complex — like a person, a car, or a laptop?</p>
<p>An array like <code>["MacBook", 16, 2026]</code> doesn't tell us much. What does the <code>16</code> mean? RAM? Screen size? Price in thousands?</p>
<p>That is where <strong>Objects</strong> come to the rescue!</p>
<hr />
<h3>1. Why Objects? The Problem with Arrays</h3>
<p>Before we learn what objects <em>are</em>, let's understand the problem they <em>solve</em>. Imagine you're building a user profile for a website. You might start with an array:</p>
<pre><code class="language-javascript">// What does each value mean?? 
const user = ["Prakash", 20, "Rajasthan", true, "developer"];

// To get the profession, you need to remember it's at index 4
console.log(user[4]); // "developer" — but why 4? Who knows!
</code></pre>
<p>This is a nightmare. Index numbers are <strong>meaningless</strong>. If you add a new field in the middle, every index after it breaks. Your code becomes impossible to read and maintain.</p>
<p>Now here's the same data as an object:</p>
<pre><code class="language-javascript">// Crystal clear — no guessing!
const user = {
  name: "Prakash",
  age: 20,
  state: "Rajasthan",
  isLoggedIn: true,
  profession: "developer"
};

// Now it's obvious what you're getting
console.log(user.profession); // "developer" — perfectly readable!
</code></pre>
<blockquote>
<p>💡 <strong>Rule of Thumb:</strong> Use an <strong>Array</strong> when you have a <em>list</em> of similar items (e.g., a list of product names). Use an <strong>Object</strong> when you're describing a <em>single thing</em> with multiple different attributes.</p>
</blockquote>
<h3>2. What Are Objects? The Key-Value Structure</h3>
<p>In JavaScript, an <strong>object</strong> is a standalone entity that holds related data together. Think of it like a real-world object. A car has a color, a brand, and a top speed. In code, an object allows us to group these related variables into one neat package.</p>
<p>Objects work on a <strong>key-value pair</strong> system:</p>
<ul>
<li><p><strong>Key</strong> → The name/label of the property (e.g., <code>name</code>)</p>
</li>
<li><p><strong>Value</strong> → The data assigned to that label (e.g., <code>"Prakash"</code>)</p>
</li>
</ul>
<p>Think of it like filling out a form:</p>
<table>
<thead>
<tr>
<th>Key</th>
<th>:</th>
<th>Value</th>
</tr>
</thead>
<tbody><tr>
<td><code>name</code></td>
<td>:</td>
<td><code>"Prakash"</code></td>
</tr>
<tr>
<td><code>age</code></td>
<td>:</td>
<td><code>20</code></td>
</tr>
<tr>
<td><code>state</code></td>
<td>:</td>
<td><code>"Rajasthan"</code></td>
</tr>
<tr>
<td><code>isStudent</code></td>
<td>:</td>
<td><code>true</code></td>
</tr>
</tbody></table>
<p>Values can be <strong>any data type</strong> - strings, numbers, booleans, arrays, or even other objects.</p>
<hr />
<h3>3. Creating an Object</h3>
<p>The most common way to create an object is using <strong>Object Literals</strong> the curly braces <code>{}</code>.</p>
<pre><code class="language-javascript">// A real-world example: A Person Object
const person = {
  name: "Prakash",
  age: 20,
  state: "Rajasthan"
};
</code></pre>
<p>Each key-value pair is separated by a <strong>comma</strong>. The key and value are separated by a <strong>colon</strong>.</p>
<p>You might also see the <code>new Object()</code> syntax in older code:</p>
<pre><code class="language-javascript">// The verbose way — avoid this in modern JS
const person = new Object();
person.name = "Prakash";
person.age = 20;
person.state = "Rajasthan";
</code></pre>
<blockquote>
<p><strong>Best Practice:</strong> Always use Object Literals <code>{}</code>. They're shorter, more readable, and the standard in modern JavaScript.</p>
</blockquote>
<h3>4. Accessing Properties — Two Ways</h3>
<p>Once you have an object, you need to <em>read</em> its data. JavaScript gives you two ways to do this.</p>
<p><strong>A. Dot Notation (Most Common)</strong></p>
<p>Use this when you know the property name upfront. Clean and readable.</p>
<pre><code class="language-javascript">const person = {
  name: "Prakash",
  age: 20,
  state: "Rajasthan",
  skills: ["JavaScript", "React", "Node.js"]
};

console.log(person.name);      // "Prakash"
console.log(person.age);       // 20
console.log(person.skills);    // ["JavaScript", "React", "Node.js"]

// You can chain it to access items inside an array!
console.log(person.skills[0]); // "JavaScript"
</code></pre>
<p><strong>B. Bracket Notation</strong></p>
<p>Use this when the key has a <strong>space</strong>, starts with a <strong>number</strong>, or when you're using a <strong>variable</strong> to look up a key dynamically.</p>
<pre><code class="language-javascript">const profile = {
  name: "Prakash",
  age: 20,
  "full name": "Prakash Kumar",  // key with a SPACE
  "2024score": 95                // key starting with a number
};

// CASE 1: Key has a space (dot notation would crash here!)
console.log(profile["full name"]); // "Prakash Kumar"
// profile.full name ← this would be a SYNTAX ERROR ❌

// CASE 2: Dynamic access with a variable
const query = "age";
console.log(profile[query]);       // 20 — reads the variable, not literal "query"

// CASE 3: Looping with bracket notation
const keys = ["name", "age"];
keys.forEach(key =&gt; {
  console.log(profile[key]); // "Prakash", then 20
});
</code></pre>
<blockquote>
<p><strong>Pro Tip:</strong> Bracket notation is incredibly useful when building search features or filters — any time the key is determined at runtime (from user input, for example).</p>
</blockquote>
<hr />
<h3>5. Updating, Adding &amp; Deleting Properties</h3>
<p>Objects are <strong>mutable</strong> - you can freely change them after creation. Even if you declare the object with <code>const</code>, its properties can still be modified.</p>
<p><strong>Updating a Property</strong></p>
<pre><code class="language-javascript">const person = {
  name: "Prakash",
  age: 20,
  state: "Rajasthan"
};

person.age = 21; // Prakash just had a birthday! 🎂
console.log(person.age); // 21
</code></pre>
<p><strong>Adding a New Property</strong></p>
<pre><code class="language-javascript">person.isStudent = true;
person.college = "IIT Jodhpur";

console.log(person);
// { name: "Prakash", age: 21, state: "Rajasthan", isStudent: true, college: "IIT Jodhpur" }
</code></pre>
<p><strong>Deleting a Property</strong></p>
<pre><code class="language-javascript">delete person.state;

console.log(person.state); // undefined
</code></pre>
<p><strong>Checking if a Property Exists</strong></p>
<pre><code class="language-javascript">console.log("isStudent" in person); // true
console.log("state" in person);     // false — we deleted it!
</code></pre>
<blockquote>
<p><strong>Why</strong> <code>const</code> <strong>doesn't freeze objects:</strong> <code>const</code> prevents you from doing <code>person = {}</code> (reassigning the variable). But it doesn't stop you from modifying what's <em>inside</em> the object. Use <code>Object.freeze(person)</code> if you truly want an immutable object.</p>
</blockquote>
<hr />
<h3>6. Array vs Object — What's the Difference?</h3>
<p>This trips up a lot of beginners. Here's the definitive breakdown:</p>
<table>
<thead>
<tr>
<th>Feature</th>
<th>Array <code>[]</code></th>
<th>Object <code>{}</code></th>
</tr>
</thead>
<tbody><tr>
<td><strong>Purpose</strong></td>
<td>An ordered list of similar items</td>
<td>Description of one specific thing</td>
</tr>
<tr>
<td><strong>Access</strong></td>
<td><code>arr[0]</code> — by index number</td>
<td><code>obj.key</code> — by name</td>
</tr>
<tr>
<td><strong>Ordering</strong></td>
<td>Ordered — index 0, 1, 2...</td>
<td>Unordered — key-based</td>
</tr>
<tr>
<td><strong>When to use</strong></td>
<td>Shopping cart items, scores, names</td>
<td>A user, a product, a car</td>
</tr>
<tr>
<td><strong>Loop type</strong></td>
<td><code>for...of</code> or <code>forEach</code></td>
<td><code>for...in</code></td>
</tr>
</tbody></table>
<pre><code class="language-javascript">// Use ARRAY when all items are the same kind of thing
const students = ["Prakash", "Riya", "Arjun", "Meera"];
const scores   = [85, 92, 78, 96];

// Use OBJECT when describing one specific entity
const student = {
  name: "Prakash",
  score: 85,
  grade: "A",
  passed: true
};

// COMBINE BOTH: Array of Objects — an extremely common pattern!
const classroom = [
  { name: "Prakash", score: 85, grade: "A"  },
  { name: "Riya",    score: 92, grade: "A+" },
  { name: "Arjun",   score: 78, grade: "B+" }
];

// Access: first student's name
console.log(classroom[0].name);  // "Prakash"
console.log(classroom[1].grade); // "A+"
</code></pre>
<p><strong>Array of Objects</strong> is one of the most common patterns in real-world JavaScript — you'll see this in every API response, database query result, and React component.</p>
<h3>7. Looping Through an Object</h3>
<p>To walk through every key-value pair in an object, we use the <code>for...in</code> loop.</p>
<pre><code class="language-javascript">const person = {
  name: "Prakash",
  age: 21,
  isStudent: true,
  college: "IIT Jodhpur"
};

// for...in gives you each KEY one at a time
for (let key in person) {
  console.log(key + ": " + person[key]);
}

// Output:
// name: Prakash
// age: 21
// isStudent: true
// college: IIT Jodhpur
</code></pre>
<h3>Object Utility Methods</h3>
<p>JavaScript also provides three handy built-in methods to extract parts of an object as arrays:</p>
<pre><code class="language-javascript">const person = { name: "Prakash", age: 21, college: "IIT Jodhpur" };

// Object.keys() → array of all keys
console.log(Object.keys(person));
// ["name", "age", "college"]

// Object.values() → array of all values
console.log(Object.values(person));
// ["Prakash", 21, "IIT Jodhpur"]

// Object.entries() → array of [key, value] pairs
console.log(Object.entries(person));
// [["name","Prakash"], ["age",21], ["college","IIT Jodhpur"]]

// Practical use: loop with destructuring
for (const [key, value] of Object.entries(person)) {
  console.log(`\({key} → \){value}`);
}
</code></pre>
<h3>8. Nested Objects &amp; Methods</h3>
<p>Objects can contain other objects inside them — this is called <strong>nesting</strong>. Objects can also hold functions as values — these are called <strong>methods</strong>.</p>
<p><strong>Nested Objects</strong></p>
<pre><code class="language-javascript">const student = {
  name: "Prakash",
  age: 21,

  // Nested object: address lives inside student
  address: {
    city: "Jodhpur",
    state: "Rajasthan",
    pincode: "342001"
  },

  // Array inside an object
  skills: ["JavaScript", "React", "Node.js"]
};

// Access nested properties: chain the dots
console.log(student.address.city);   // "Jodhpur"
console.log(student.address.state);  // "Rajasthan"
console.log(student.skills[0]);      // "JavaScript"

// Update a nested property
student.address.city = "Jaipur"; // Prakash moved cities!
</code></pre>
<p><strong>Methods — Functions Inside Objects</strong></p>
<pre><code class="language-javascript">const calculator = {
  brand: "Casio",

  // Functions stored as values are called "methods"
  add: function(a, b) {
    return a + b;
  },

  // Modern shorthand (preferred)
  multiply(a, b) {
    return a * b;
  },

  subtract(a, b) {
    return a - b;
  }
};

console.log(calculator.add(10, 5));      // 15
console.log(calculator.multiply(4, 3));  // 12
console.log(calculator.subtract(9, 4));  // 5
</code></pre>
<hr />
<h3>9. Real-World Example - E-Commerce Product</h3>
<p>Let's put everything together with the kind of data structure you'd actually use when building an e-commerce site.</p>
<pre><code class="language-javascript">const product = {
  id: "MBP-001",
  name: "MacBook Pro 14",
  brand: "Apple",
  price: 199999,       // in rupees ₹
  inStock: true,
  rating: 4.8,
  reviewCount: 2341,

  // Nested object
  specs: {
    chip: "Apple M3 Pro",
    ram: 18,
    storage: 512,
    display: "14.2-inch Liquid Retina XDR",
    battery: "22 hours"
  },

  // Array of strings
  colors: ["Space Black", "Silver"],

  // Array of objects
  reviews: [
    { user: "Riya_Dev",  stars: 5, comment: "Incredible performance!" },
    { user: "TechArjun", stars: 4, comment: "Great but expensive" }
  ],

  // Methods
  getDescription() {
    return `\({this.name} by \){this.brand} — ₹${this.price.toLocaleString()}`;
  },

  isAffordable(budget) {
    return this.price &lt;= budget;
  }
};

// Reading various properties
console.log(product.name);              // "MacBook Pro 14"
console.log(product.specs.ram);         // 18
console.log(product.colors[0]);         // "Space Black"
console.log(product.reviews[0].user);   // "Riya_Dev"
console.log(product.reviews[0].stars);  // 5

// Calling methods
console.log(product.getDescription());
// "MacBook Pro 14 by Apple — ₹1,99,999"

console.log(product.isAffordable(150000)); // false
console.log(product.isAffordable(250000)); // true

// Looping through specs
console.log("=== Tech Specs ===");
for (let spec in product.specs) {
  console.log(`  \({spec}: \){product.specs[spec]}`);
}
// chip: Apple M3 Pro
// ram: 18
// storage: 512
// display: 14.2-inch Liquid Retina XDR
// battery: 22 hours
</code></pre>
<p>This is almost exactly the kind of data you'd receive from a backend API. The ability to navigate nested objects, loop through properties, and call methods is a <strong>daily reality</strong> as a JavaScript developer.</p>
<hr />
<h3>Where You'll Use Objects Every Day</h3>
<ul>
<li><p><strong>User Auth &amp; Profiles</strong> — <code>{ id, name, email, role, avatar }</code></p>
</li>
<li><p><strong>Shopping Cart</strong> — <code>{ productId, qty, price, discount }</code></p>
</li>
<li><p><strong>API Responses</strong> — Every REST API returns JSON, which is just objects</p>
</li>
<li><p><strong>Config &amp; Settings</strong> — <code>{ theme, language, notifications }</code></p>
</li>
<li><p><strong>React State &amp; Props</strong> — Almost always objects under the hood</p>
</li>
</ul>
<h3>Conclusion</h3>
<p>Objects are the <strong>backbone of JavaScript</strong>. They make your data readable, searchable, and organized. By mastering:</p>
<ul>
<li><p>Key-Value pairs</p>
</li>
<li><p>Dot and Bracket notation</p>
</li>
<li><p>Updating, Adding, Deleting</p>
</li>
<li><p>Nested objects and methods</p>
</li>
<li><p>Looping with <code>for...in</code> and <code>Object.entries()</code></p>
</li>
</ul>
<hr />
<p><em>I write articles on</em> <a href="http://blog.prakashtsx.com"><em>blog.prakashtsx.com</em></a> <em>and also post development-related content on:</em></p>
<ul>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#">Twitter / X</a></p>
</li>
<li><p><a href="https://claude.ai/chat/1564e342-3e7b-40fd-ad95-c9d167001fcd#">LinkedIn</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[How Node.js Actually Works]]></title><description><![CDATA[When people start learning Node.js, they often hear statements like:

Node.js is single-threaded

Node.js is non-blocking

Node.js uses an Event Loop


But what actually happens inside Node.js when we]]></description><link>https://blog.prakashtsx.me/node-internal</link><guid isPermaLink="true">https://blog.prakashtsx.me/node-internal</guid><category><![CDATA[Node.js]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Tue, 10 Mar 2026 12:58:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/3b66a6bf-aecd-484d-9f3d-0aed66c03474.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When people start learning <strong>Node.js</strong>, they often hear statements like:</p>
<ul>
<li><p><em>Node.js is single-threaded</em></p>
</li>
<li><p><em>Node.js is non-blocking</em></p>
</li>
<li><p><em>Node.js uses an Event Loop</em></p>
</li>
</ul>
<p>But what actually happens <strong>inside Node.js</strong> when we run a program?</p>
<p>What happens when we execute:</p>
<pre><code class="language-javascript">node index.js
</code></pre>
<p>How does Node.js handle:</p>
<ul>
<li><p>File reading</p>
</li>
<li><p>API calls</p>
</li>
<li><p>Timers</p>
</li>
<li><p>Database queries</p>
</li>
</ul>
<p>while still remaining <strong>fast and scalable</strong>?</p>
<p>To understand this, we must understand <strong>three core components</strong> of Node.js:</p>
<ol>
<li><p><strong>V8 Engine</strong> – executes JavaScript</p>
</li>
<li><p><strong>libuv</strong> – handles asynchronous operations</p>
</li>
<li><p><strong>Event Loop</strong> – coordinates everything</p>
</li>
</ol>
<p>Let’s explore them step by step.</p>
<hr />
<h2>1. What Exactly is Node.js?</h2>
<p>Before diving deeper, we must clear one important confusion.</p>
<p><strong>Node.js is NOT a programming language.</strong></p>
<p>Node.js is a <strong>JavaScript runtime environment</strong>.</p>
<p>This means Node.js provides everything required to <strong>run JavaScript outside the browser</strong>.</p>
<p>A Node.js runtime contains multiple components:</p>
<ul>
<li><p>JavaScript Engine (V8)</p>
</li>
<li><p>Event Loop</p>
</li>
<li><p>libuv threadpool</p>
</li>
<li><p>OS bindings</p>
</li>
<li><p>memory management</p>
</li>
<li><p>networking APIs</p>
</li>
</ul>
<p><strong>In simple terms:</strong></p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/a8a1f73d-8327-4493-87c8-d45dfae08cdb.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>2. Understanding Runtime with a Simple Analogy</h2>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/6d9cc033-b10f-4e2c-83a0-1ce93fb22029.png" alt="" style="display:block;margin:0 auto" />

<p>Similarly:</p>
<pre><code class="language-javascript">Your Code → JavaScript

Player → Node.js Runtime
</code></pre>
<p>Node.js provides the <strong>environment required to run JavaScript</strong>.</p>
<hr />
<h2><strong>3. What is a JavaScript Engine?</strong></h2>
<p>A JavaScript engine is a program that executes JavaScript code. Most engines today are built with similar core components:</p>
<ul>
<li><p><strong>Parser</strong>: Transforms source code into an Abstract Syntax Tree (AST)</p>
</li>
<li><p><strong>Interpreter</strong>: Executes the code line-by-line initially</p>
</li>
<li><p><strong>JIT Compiler</strong>: Compiles hot code paths into optimized machine code</p>
</li>
<li><p><strong>Garbage Collector</strong>: Reclaims memory no longer in use</p>
</li>
</ul>
<h3></h3>
<p>❊ <strong>Key JavaScript Engines</strong></p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/bc3aed28-b04d-49b6-a6e6-6d98e3fc8d09.webp" alt="" style="display:block;margin:0 auto" />

<hr />
<h3>4. The V8 Engine – The Heart of Node.js</h3>
<p><em>V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.</em></p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/b260ae38-d7bb-4b7c-be6a-628c1106b26b.png" alt="" style="display:block;margin:0 auto" />

<p>At the core of Node.js lies the <strong>V8 JavaScript Engine</strong>.</p>
<p>V8 was developed by <strong>Google</strong> and is also used in <strong>Chrome</strong>.</p>
<p>Its job is simple:</p>
<blockquote>
<p>Convert JavaScript code into machine code so the computer can execute it.</p>
</blockquote>
<h2>❊ What V8 Actually Does</h2>
<p>V8 performs several critical tasks:</p>
<ul>
<li><p>Parses JavaScript</p>
</li>
<li><p>Compiles JavaScript into machine code</p>
</li>
<li><p>Executes code</p>
</li>
<li><p>Manages memory</p>
</li>
<li><p>Runs garbage collection</p>
</li>
<li><p>Maintains the call stack</p>
</li>
</ul>
<h3><strong>Workflow</strong></h3>
<ol>
<li><p><strong>Parsing</strong>: JavaScript is parsed into an AST.</p>
</li>
<li><p><strong>Ignition (Interpreter)</strong>: Executes unoptimized bytecode quickly.</p>
</li>
<li><p><strong>Turbofan (JIT Compiler)</strong>: Optimizes frequently executed code into fast machine code.</p>
</li>
</ol>
<h3><strong>Example</strong></h3>
<pre><code class="language-javascript">function square(n) {
  return n * n;
}

console.log(square(5)); // 25
</code></pre>
<ul>
<li><p>Initially run by Ignition</p>
</li>
<li><p>Optimized by Turbofan if called repeatedly</p>
</li>
</ul>
<hr />
<h3>5. Call Stack – Where Code Executes</h3>
<p>Inside the V8 engine there is a <strong>Call Stack</strong>.</p>
<p>The call stack keeps track of <strong>which function is currently executing</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">function greet() {
  sayHello();
}

function sayHello() {
  console.log("Hello");
}

greet();
</code></pre>
<p>Execution order:</p>
<pre><code class="language-plaintext">Call Stack

 console.log 
 sayHello()  
 greet()     
 global()    
</code></pre>
<p>Functions enter the stack when called and leave after execution.</p>
<p>This is why JavaScript is called <strong>single-threaded execution</strong>.</p>
<p>Only <strong>one function executes at a time</strong> in the call stack.</p>
<hr />
<h3>6. The Problem with Synchronous Code</h3>
<p>Imagine Node.js handled everything synchronously.</p>
<p>Example:</p>
<pre><code class="language-javascript">const fs = require("fs");

const data = fs.readFileSync("bigFile.txt");
console.log(data);
</code></pre>
<p>If the file takes <strong>5 seconds</strong> to read, the entire program stops.</p>
<p>Nothing else runs.</p>
<p>This is called <strong>blocking behavior</strong>.</p>
<p>Node.js solves this problem using <strong>asynchronous architecture</strong>.</p>
<p>And that is where <strong>libuv</strong> comes in.</p>
<hr />
<h3>7. libuv – The Hidden Power of Node.js</h3>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/d16608af-aa36-4f50-96f0-ad63696f9bf5.png" alt="" style="display:block;margin:0 auto" />

<p><strong>libuv</strong> is a C library used internally by Node.js.</p>
<p>Its job is to handle <strong>asynchronous operations</strong>.</p>
<p>Examples:</p>
<ul>
<li><p>File system operations</p>
</li>
<li><p>DNS lookups</p>
</li>
<li><p>compression</p>
</li>
<li><p>cryptography</p>
</li>
<li><p>background tasks</p>
</li>
</ul>
<h3>❊ libuv Threadpool</h3>
<p>libuv provides a <strong>threadpool</strong>.</p>
<p>By default:</p>
<pre><code class="language-plaintext">Threadpool Size = 4 threads
</code></pre>
<p>These threads run <strong>heavy operations in the background</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">Read File
Hash Password
Compress Data
Write File
</code></pre>
<p>These tasks are executed <strong>without blocking the main thread</strong>.</p>
<p>You can even change the threadpool size:</p>
<pre><code class="language-javascript">process.env.UV_THREADPOOL_SIZE = 8;
</code></pre>
<hr />
<h3>8. Event Loop – The Coordinator</h3>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/ac3693b4-ecd8-47f1-a471-412ec067417a.png" alt="" style="display:block;margin:0 auto" />

<p>The <strong>Event Loop</strong> is the central coordinator of Node.js.</p>
<p>It constantly checks:</p>
<pre><code class="language-plaintext">Is the call stack empty?
</code></pre>
<p>If the stack is empty, it pulls tasks from queues and executes them.</p>
<h3>❊ Simple Event Loop Flow</h3>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/84808fe0-fa2f-4071-bd93-152c29a0cd7c.png" alt="" style="display:block;margin:0 auto" />

<p>This loop runs <strong>continuously</strong> while the Node.js process is alive.</p>
<h3>❊ Example to Understand Event Loop</h3>
<pre><code class="language-javascript">console.log("Start");

setTimeout(() =&gt; {
  console.log("Timer finished");
}, 0);

console.log("End");
</code></pre>
<p><em>Output:</em></p>
<pre><code class="language-plaintext">Start
End
Timer finished
</code></pre>
<hr />
<h3>❊ Step-by-Step Execution</h3>
<p>Step 1</p>
<pre><code class="language-javascript">console.log("Start")
</code></pre>
<p>Executed immediately.</p>
<p>Step 2</p>
<pre><code class="language-javascript">setTimeout(...)
</code></pre>
<p>Timer is handled by <strong>libuv timers system</strong>.</p>
<p>Callback waits in queue.</p>
<p>Step 3</p>
<pre><code class="language-javascript">console.log("End")
</code></pre>
<p>Runs immediately.</p>
<p>Step 4</p>
<p>Timer finishes → callback enters queue.</p>
<p>Step 5</p>
<p>Event loop pushes callback to stack.</p>
<pre><code class="language-javascript">Timer finished
</code></pre>
<h3>❊ Event Loop Phases (Advanced)</h3>
<p>Node.js Event Loop has multiple phases.</p>
<p>Order:</p>
<pre><code class="language-plaintext">1 Timers
2 Pending Callbacks
3 Idle / Prepare
4 Poll
5 Check
6 Close Callbacks
</code></pre>
<p>Examples:</p>
<pre><code class="language-plaintext">setTimeout → Timers phase
I/O callbacks → Poll phase
setImmediate → Check phase
</code></pre>
<p>This is how Node.js schedules asynchronous work efficiently.</p>
<h3>❊ Complete Node.js Architecture</h3>
<p>Here is the complete flow:</p>
<pre><code class="language-plaintext">Your JavaScript Code
        ↓
V8 Engine executes synchronous code
        ↓
Async operations detected
        ↓
libuv handles background tasks
        ↓
Callbacks added to queues
        ↓
Event Loop picks them
        ↓
Callback executed by V8
</code></pre>
<hr />
<h3>❊ Real-World Analogy (Restaurant System)</h3>
<p>Imagine a restaurant.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/342ddea9-01ae-40d5-8d27-79da034964a5.png" alt="" style="display:block;margin:0 auto" />

<p>Actors:</p>
<pre><code class="language-plaintext">Restaurant → Node.js Runtime
Waiter → Event Loop
Manager → V8 Engine
Chefs → libuv Threadpool
Customers → Users
Food Orders → JavaScript Tasks
</code></pre>
<p>Workflow:</p>
<p>1️⃣ Customer places order  </p>
<p>2️⃣ Waiter forwards order to chefs  </p>
<p>3️⃣ Chefs cook food in kitchen  </p>
<p>4️⃣ Waiter serves when ready</p>
<p>The waiter <strong>never cooks</strong>.</p>
<p>Similarly:</p>
<pre><code class="language-plaintext">Event Loop does NOT execute code
V8 executes the code
</code></pre>
<p>The event loop simply <strong>manages tasks</strong>.</p>
<hr />
<h3>❊ Conclusion</h3>
<p>Whenever you run Node.js, remember this pipeline:</p>
<pre><code class="language-plaintext">JavaScript Code
       ↓
V8 Engine executes code
       ↓
Async tasks sent to libuv
       ↓
Callbacks stored in queues
       ↓
Event Loop pushes callbacks
       ↓
V8 executes them
</code></pre>
<p>This architecture is the reason Node.js can handle <strong>thousands of concurrent connections efficiently</strong>.</p>
<hr />
<h3><strong>❊ Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Understanding Object-Oriented Programming in JavaScript]]></title><description><![CDATA[⤑ Object-Oriented Programming (OOP) is a programming paradigm used to structure code in a way that models real-world entities.
Instead of writing scattered functions and variables, OOP helps us organi]]></description><link>https://blog.prakashtsx.me/js-oops</link><guid isPermaLink="true">https://blog.prakashtsx.me/js-oops</guid><category><![CDATA[OOPS]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Hashnode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Tue, 10 Mar 2026 04:59:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/0c30d2b7-a40e-417e-be9c-00b0e72425ca.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>⤑ Object-Oriented Programming (OOP) is a programming paradigm used to structure code in a way that models real-world entities.</p>
<p>Instead of writing scattered functions and variables, OOP helps us organize code into <strong>objects and classes</strong>.</p>
<p>JavaScript supports OOP concepts, making code more <strong>reusable, modular, and easier to maintain</strong>.</p>
<img src="https://media.geeksforgeeks.org/wp-content/uploads/20250722184505270450/Types-of-OOPS-2.gif" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>❊ What is Object-Oriented Programming?</h2>
<p>⤑ Object-Oriented Programming is a way of writing code where we create <strong>objects that represent real-world entities</strong>.</p>
<p>These objects contain:</p>
<p>• <strong>Properties</strong> → data about the object<br />• <strong>Methods</strong> → actions the object can perform</p>
<h3>❊ Example: Car Object</h3>
<p>Instead of listing properties and methods separately, we can visualize a <strong>Car object</strong> that contains both <strong>data (properties)</strong> and <strong>behavior (methods)</strong>.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/49fd8e14-b4cf-4198-a968-206336e82fd1.png" alt="" style="display:block;margin:0 auto" />

<p>In this example, the <strong>Car object</strong> has:</p>
<p><strong>Properties (data)</strong><br />• brand<br />• color<br />• speed</p>
<p><strong>Methods (behavior)</strong><br />• start()<br />• stop()<br />• accelerate()</p>
<p>Instead of writing separate variables and functions, Object-Oriented Programming groups them together inside a single <strong>object</strong>.</p>
<hr />
<h2>❊ Real World Analogy: Blueprint → Objects</h2>
<p>Think of a <strong>class as a blueprint</strong>.</p>
<p>Example:</p>
<p>Blueprint → Car Design<br />Object → Actual Car</p>
<p>One blueprint can create <strong>many cars</strong>.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/7ff788f3-0256-4a8f-af58-b4eeff4594f9.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>❊ What is a Class in JavaScript?</h2>
<p>⤑ A <strong>class</strong> is a template used to create objects.</p>
<p>It defines the <strong>properties and methods</strong> that objects will have.</p>
<p>Example:</p>
<pre><code class="language-javascript">class Car {
  constructor(brand, color) {
    this.brand = brand;
    this.color = color;
  }

  start() {
    console.log(this.brand + " car started");
  }
}
</code></pre>
<p>Here:</p>
<p><em><strong>Car → class<br />brand, color → properties<br />start() → method</strong></em></p>
<hr />
<h2>❊ Creating Objects from a Class</h2>
<p>⤑ Once we define a class, we can create objects using the <strong>new keyword</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">const car1 = new Car("Toyota", "Red");
const car2 = new Car("BMW", "Black");

car1.start();
car2.start();
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Toyota car started
BMW car started
</code></pre>
<p>Each object has its <strong>own data</strong> but shares the same structure.</p>
<hr />
<h2>❊ Constructor Method</h2>
<p>⤑ The <strong>constructor()</strong> method is a special method used to initialize object properties.</p>
<p>It automatically runs when a new object is created.</p>
<p>Example:</p>
<pre><code class="language-javascript">class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

const p1 = new Person("Rahul", 22);
console.log(p1.name);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Rahul
</code></pre>
<p>The constructor helps us assign values when creating objects.</p>
<hr />
<h2>❊ Methods Inside a Class</h2>
<p>⤑ Methods define the <strong>behavior of objects</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">class Dog {
  constructor(name) {
    this.name = name;
  }

  bark() {
    console.log(this.name + " is barking");
  }
}

const dog1 = new Dog("Rocky");
dog1.bark();
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Rocky is barking
</code></pre>
<p>Methods allow objects to <strong>perform actions</strong>.</p>
<hr />
<h2>❊ Basic Idea of Encapsulation</h2>
<p>⤑ Encapsulation means <strong>bundling data and methods together inside a class</strong>.</p>
<p>It helps protect data and organize code.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/e878a4df-4a43-4ea6-9d86-5803818149ad.png" alt="" style="display:block;margin:0 auto" />

<p>Example:</p>
<pre><code class="language-javascript">class BankAccount {
  constructor(balance) {
    this.balance = balance;
  }

  deposit(amount) {
    this.balance += amount;
  }

  getBalance() {
    return this.balance;
  }
}
</code></pre>
<p>Here:</p>
<p><em><strong>balance → data<br />deposit() → modifies data<br />getBalance() → accesses data</strong></em></p>
<p>All related functionality is inside the class.</p>
<hr />
<h2>❊ Why Use Object-Oriented Programming?</h2>
<p>OOP helps developers:</p>
<p>• Organize code better<br />• Reuse code easily<br />• Reduce duplication<br />• Model real-world systems<br />• Maintain large applications</p>
<p>Many modern frameworks and applications rely on OOP concepts.</p>
<hr />
<h2>❊ Conclusion</h2>
<p>⤑ Object-Oriented Programming is an important concept in JavaScript that helps structure code effectively.</p>
<p>In this blog we learned:</p>
<p>• What Object-Oriented Programming means<br />• Blueprint → Object analogy<br />• What classes are in JavaScript<br />• Creating objects using classes<br />• Constructor methods<br />• Methods inside classes<br />• Basic idea of encapsulation</p>
<p>Understanding OOP will help you write <strong>clean, scalable, and maintainable JavaScript applications</strong>.</p>
<hr />
<h3><strong>❊ Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Control Flow in Programming (JavaScript)]]></title><description><![CDATA[1. Introduction: What is Control Flow in Programming?
Imagine a normal day in your life.
You wake up and think:

If it is morning, I will brush my teeth

If it is Sunday, I will rest

If it is Monday,]]></description><link>https://blog.prakashtsx.me/control-flow</link><guid isPermaLink="true">https://blog.prakashtsx.me/control-flow</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[control flow]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Fri, 06 Mar 2026 18:03:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/79869680-c044-455b-91a8-d60163fc8f99.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>1. Introduction: What is Control Flow in Programming?</h2>
<p>Imagine a normal day in your life.</p>
<p>You wake up and think:</p>
<ul>
<li><p>If it is <strong>morning</strong>, I will <strong>brush my teeth</strong></p>
</li>
<li><p>If it is <strong>Sunday</strong>, I will <strong>rest</strong></p>
</li>
<li><p>If it is <strong>Monday</strong>, I will <strong>go to work</strong></p>
</li>
</ul>
<p>Your brain is constantly making <strong>decisions</strong>.</p>
<p>Programming works the same way.</p>
<p>A computer program also needs to decide:</p>
<ul>
<li><p>Should this code run?</p>
</li>
<li><p>Or should another code run?</p>
</li>
<li><p>Or should it skip something?</p>
</li>
</ul>
<p>This process is called <strong>Control Flow</strong>.</p>
<h3>Definition</h3>
<p><strong>Control Flow determines the order in which instructions are executed in a program.</strong></p>
<p>Without control flow, a program would execute every line from top to bottom with no decision-making.</p>
<h2>❊Example Without Control Flow</h2>
<pre><code class="language-javascript">console.log("Wake up");
console.log("Go to gym");
console.log("Go to office");
console.log("Sleep");
</code></pre>
<p>The program will run every line.</p>
<p>But what if today is <strong>Sunday</strong>?</p>
<p>You might not want to go to the office.</p>
<p>This is where <strong>control flow statements</strong> come into play.</p>
<h2>❊ Types of Control Flow Statements</h2>
<p>The most common ones are:</p>
<ol>
<li><p><strong>if statement</strong></p>
</li>
<li><p><strong>if-else statement</strong></p>
</li>
<li><p><strong>else-if ladder</strong></p>
</li>
<li><p><strong>switch statement</strong></p>
</li>
</ol>
<p>These help programs <strong>make decisions</strong>.</p>
<h2>2. The if Statement</h2>
<p>The <strong>if statement</strong> executes code <strong>only if a condition is true</strong>.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">if (condition) {
    // code runs if condition is true
}
</code></pre>
<h2>❊ Real Life Example</h2>
<p>Imagine a school rule:</p>
<blockquote>
<p>If a student’s marks are greater than 40, they pass.</p>
</blockquote>
<pre><code class="language-javascript">let marks = 50;

if (marks &gt; 40) {
  console.log("You passed the exam");
}
</code></pre>
<h3>Step-by-Step Execution</h3>
<ol>
<li><p>Program reads the value of <code>marks</code></p>
</li>
<li><p>It checks the condition <code>marks &gt; 40</code></p>
</li>
<li><p>If the condition is <strong>true</strong>, the code inside <code>{}</code> runs</p>
</li>
<li><p>If the condition is <strong>false</strong>, the program skips it</p>
</li>
</ol>
<h3>Example Output</h3>
<pre><code class="language-javascript">You passed the exam
</code></pre>
<h3>When the condition is false</h3>
<pre><code class="language-javascript">let marks = 30;

if (marks &gt; 40) {
  console.log("You passed");
}
</code></pre>
<p><em>Output:</em></p>
<pre><code class="language-javascript">(no output)
</code></pre>
<p>Because the condition is <strong>false</strong>.</p>
<h2>3. The if-else Statement</h2>
<p>Sometimes we want the program to do <strong>something else</strong> if the condition is false.</p>
<p>That is where <strong>if-else</strong> is used.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">if (condition) {
   // runs if condition is true
} else {
   // runs if condition is false
}
</code></pre>
<h2>❊ Real Life Example</h2>
<p>At a movie theatre:</p>
<ul>
<li><p>If <strong>age ≥ 18</strong> → Allowed</p>
</li>
<li><p>Else → Not allowed</p>
</li>
</ul>
<h3>Example</h3>
<pre><code class="language-javascript">let age = 16;

if (age &gt;= 18) {
  console.log("You can watch the movie");
} else {
  console.log("You are not allowed");
}
</code></pre>
<h3>❊ Step-by-Step Execution</h3>
<ol>
<li><p>Program checks <code>age &gt;= 18</code></p>
</li>
<li><p>If true → first block runs</p>
</li>
<li><p>If false → <code>else</code> block runs</p>
</li>
</ol>
<h3>Output</h3>
<pre><code class="language-javascript">You are not allowed
</code></pre>
<h2>4. The Else If Ladder</h2>
<p>Sometimes there are <strong>multiple conditions</strong>.</p>
<p>Example: grading system</p>
<table>
<thead>
<tr>
<th>Marks</th>
<th>Grade</th>
</tr>
</thead>
<tbody><tr>
<td>90+</td>
<td>A</td>
</tr>
<tr>
<td>75+</td>
<td>B</td>
</tr>
<tr>
<td>50+</td>
<td>C</td>
</tr>
<tr>
<td>&lt;50</td>
<td>Fail</td>
</tr>
</tbody></table>
<p>This is where <strong>else-if ladder</strong> is useful.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">if (condition1) {

} else if (condition2) {

} else if (condition3) {

} else {

}
</code></pre>
<h3>Example</h3>
<pre><code class="language-javascript">let marks = 82;

if (marks &gt;= 90) {
  console.log("Grade A");
} 
else if (marks &gt;= 75) {
  console.log("Grade B");
}
else if (marks &gt;= 50) {
  console.log("Grade C");
}
else {
  console.log("Fail");
}
</code></pre>
<h3>⤑ Step-by-Step Execution</h3>
<p>Marks = <strong>82</strong></p>
<ol>
<li><p>Check <code>marks &gt;= 90</code> → false</p>
</li>
<li><p>Check <code>marks &gt;= 75</code> → true</p>
</li>
<li><p>Program prints <strong>Grade B</strong></p>
</li>
<li><p>Remaining conditions are skipped</p>
</li>
</ol>
<h3><em>Output</em></h3>
<pre><code class="language-plaintext">Grade B
</code></pre>
<h2>5. The Switch Statement</h2>
<p>The <strong>switch statement</strong> is used when there are <strong>many possible fixed values</strong>.</p>
<p>It is commonly used for:</p>
<ul>
<li><p>menus</p>
</li>
<li><p>days of week</p>
</li>
<li><p>months</p>
</li>
<li><p>options</p>
</li>
</ul>
<h3>⤑ Syntax</h3>
<pre><code class="language-javascript">switch(expression) {

  case value1:
    code
    break;

  case value2:
    code
    break;

  default:
    code
}
</code></pre>
<h3>⤑ <em>Example: Day of Week</em></h3>
<pre><code class="language-javascript">let day = 3;

switch(day) {

  case 1:
    console.log("Monday");
    break;

  case 2:
    console.log("Tuesday");
    break;

  case 3:
    console.log("Wednesday");
    break;

  default:
    console.log("Invalid day");
}
</code></pre>
<h3>⤑ Step-by-Step Execution</h3>
<ol>
<li><p><code>day = 3</code></p>
</li>
<li><p>Switch checks cases</p>
</li>
<li><p><code>case 3</code> matches</p>
</li>
<li><p>Prints <strong>Wednesday</strong></p>
</li>
</ol>
<h2>❊ Understanding break in Switch</h2>
<p><code>break</code> stops execution.</p>
<p>Without break, the program continues executing the next cases.</p>
<h3>⤑ Example Without break</h3>
<pre><code class="language-javascript">let day = 2;

switch(day) {

  case 1:
    console.log("Monday");

  case 2:
    console.log("Tuesday");

  case 3:
    console.log("Wednesday");
}
</code></pre>
<h3><em>Output</em></h3>
<pre><code class="language-plaintext">Tuesday
Wednesday
</code></pre>
<p>This happens because <strong>there is no break</strong>.</p>
<h2>6. When to Use Switch vs If-Else</h2>
<h3>⤑ Use if-else when:</h3>
<ul>
<li>conditions are <strong>ranges</strong></li>
</ul>
<p>Example:</p>
<pre><code class="language-javascript">marks &gt; 90
age &gt;= 18
temperature &gt; 30
</code></pre>
<h3>⤑ Use switch when:</h3>
<ul>
<li>checking <strong>specific values</strong></li>
</ul>
<p>Example:</p>
<pre><code class="language-javascript">day = Monday
role = admin
menu option = 1
</code></pre>
<h3>⤑ Example</h3>
<p>Good for <strong>if-else</strong></p>
<pre><code class="language-javascript">if (marks &gt; 90)
</code></pre>
<p>Good for <strong>switch</strong></p>
<pre><code class="language-javascript">switch(day)
</code></pre>
<h2>❊ Comparison Table</h2>
<table>
<thead>
<tr>
<th>Feature</th>
<th>if-else</th>
<th>switch</th>
</tr>
</thead>
<tbody><tr>
<td>Range conditions</td>
<td>Yes</td>
<td>No</td>
</tr>
<tr>
<td>Exact values</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>Readability with many cases</td>
<td>Hard</td>
<td>Easier</td>
</tr>
<tr>
<td>Common use</td>
<td>logic</td>
<td>menus/options</td>
</tr>
</tbody></table>
<h2>❊ Conclusion</h2>
<p>Control flow is one of the <strong>most fundamental concepts in programming</strong>.</p>
<p>It allows programs to:</p>
<ul>
<li><p>make decisions</p>
</li>
<li><p>run different code paths</p>
</li>
<li><p>handle real-world logic</p>
</li>
</ul>
<p>The most important control flow statements are:</p>
<ul>
<li><p><strong>if</strong></p>
</li>
<li><p><strong>if-else</strong></p>
</li>
<li><p><strong>else-if ladder</strong></p>
</li>
<li><p><strong>switch</strong></p>
</li>
</ul>
<p>Understanding when to use each of them will make your programs <strong>cleaner and easier to understand</strong>.</p>
<hr />
<h3>❊ <strong>Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[JavaScript Promise Methods]]></title><description><![CDATA[Welcome to the world of JavaScript! If you have ever promised a friend you would call them back, you already understand the logic of a Promise.
In programming, things often take time like downloading ]]></description><link>https://blog.prakashtsx.me/promises-methods</link><guid isPermaLink="true">https://blog.prakashtsx.me/promises-methods</guid><category><![CDATA[promises]]></category><category><![CDATA[promise methods]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Sun, 01 Mar 2026 09:43:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/0d8ecbd8-9d58-46ce-bfff-43cbcefa9454.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Welcome to the world of JavaScript! If you have ever promised a friend you would call them back, you already understand the logic of a <strong>Promise</strong>.</p>
<p>In programming, things often take time like downloading a large image or fetching data from a server. Instead of making the whole program wait and freeze, JavaScript uses "Promises" to manage these tasks.</p>
<hr />
<h2><strong>1. What is a Promise?</strong></h2>
<p>⤑ Imagine <strong>Jethalal</strong> wants to impress <strong>Babita Ji</strong> by bringing her a special "Chocolate Gunda" from Ahmedabad.</p>
<ul>
<li><p><strong>The Promise:</strong> Jethalal tells Babita Ji, "I will bring you the sweets by evening."</p>
</li>
<li><p><strong>The Wait:</strong> Babita Ji doesn't know yet if she will get the sweets or if Jethalal will forget. She just has his "Promise."</p>
</li>
</ul>
<p>⤑ In JavaScript, a <strong>Promise</strong> is an object representing the eventual completion (or failure) of an asynchronous operation and its resulting value.</p>
<h2>2. The Three States of a Promise</h2>
<p>⤑ Just like Jethalal’s commitment, every Promise goes through three stages:</p>
<ol>
<li><p><strong>Pending:</strong> Jethalal is currently on his way. The result is unknown.</p>
</li>
<li><p><strong>Fulfilled (Resolved):</strong> Jethalal arrives with the sweets! Babita Ji is happy.</p>
</li>
<li><p><strong>Rejected:</strong> Jethalal gets stuck in a traffic jam or Bagha forgets to order them. Babita Ji is disappointed.</p>
</li>
</ol>
<h2>3. The Basic Methods: Handling One Promise</h2>
<p>⤑ When you have a single promise, you use these three methods to handle the result.</p>
<h3><code>then()</code> – The Success Story</h3>
<p>⤑ If Jethalal successfully brings the sweets, what should Babita Ji do? She handles the success using <code>.then()</code>.</p>
<pre><code class="language-javascript">myPromise.then((message) =&gt; {
  console.log("Success: " + message);
});
</code></pre>
<p>⤑ This code says, "If the promise is fulfilled, take the result and do something with it."</p>
<h3><code>catch()</code> – The Backup Plan</h3>
<p>⤑ If Jethalal fails (the promise is rejected), Babita Ji needs to handle the error. She uses <code>.catch()</code>.</p>
<pre><code class="language-javascript">myPromise.catch((error) =&gt; {
  console.log("Error: " + error);
});
</code></pre>
<p>⤑ This code says, "If something goes wrong, tell me what the error is so I can fix it."</p>
<h3><code>finally()</code> – The Final Word</h3>
<p>⤑ Whether Jethalal brings the sweets or fails, <strong>Bapuji</strong> will definitely give him a lecture. This happens no matter what.</p>
<pre><code class="language-javascript">myPromise.finally(() =&gt; {
  console.log("Process finished. Bapuji starts his lecture.");
});
</code></pre>
<p>⤑ This code runs at the very end, regardless of whether the promise succeeded or failed.</p>
<hr />
<h2>4. Handling Multiple Promises (Static Methods)</h2>
<p>⤑ Sometimes, we have many promises happening at once. This is where things get interesting in Gokuldham!</p>
<h3><code>Promise.all()</code> – The Team Player</h3>
<p>⤑ <strong>Scenario:</strong> Tapu Sena (Tapu, Sonu, Goli) decides to clean the clubhouse.</p>
<ul>
<li><p>If <strong>all</strong> of them finish their work, the party starts.</p>
</li>
<li><p>If even <strong>one</strong> person fails, the whole plan is cancelled.</p>
</li>
</ul>
<pre><code class="language-javascript">Promise.all([promiseTapu, promiseSonu, promiseGoli])
  .then((results) =&gt; console.log("Clubhouse is clean!"))
  .catch((err) =&gt; console.log("Someone failed, party cancelled!"));
</code></pre>
<p>⤑ It waits for <strong>all</strong> to succeed. If one fails, the whole thing fails immediately.</p>
<h3><code>Promise.allSettled()</code> – The Report Card</h3>
<p>⤑ <strong>Scenario:</strong> Bhide gives different tasks to everyone for Ganpati Festival.</p>
<ul>
<li><p>Bhide wants a report of who finished and who failed.</p>
</li>
<li><p>He doesn't stop the festival if one person fails; he just wants to know the status of <strong>everyone</strong>.</p>
</li>
</ul>
<pre><code class="language-javascript">Promise.allSettled([task1, task2, task3])
  .then((results) =&gt; console.log("Here is the status of every task."));
</code></pre>
<p>⤑ <strong>Behavior:</strong> It waits for all promises to finish (either success or failure) and returns an array of results.</p>
<h3><code>Promise.race()</code> – The Fast and Curious</h3>
<p>⤑ <strong>Scenario:</strong> Jethalal, Bhide, and Iyer are all racing to reach the Soda Shop first to talk to Abdul.</p>
<ul>
<li><p>The person who reaches <strong>first</strong> wins.</p>
</li>
<li><p>It doesn't matter if they succeeded or failed; the first one to "cross the finish line" (settle) wins.</p>
</li>
</ul>
<pre><code class="language-javascript">Promise.race([jethaRun, bhideRun, iyerRun])
  .then((winner) =&gt; console.log("The first person reached!"));
</code></pre>
<p>⤑ <strong>Behavior:</strong> It returns the result of the <strong>first</strong> promise that finishes, whether it's a success or an error.</p>
<h3><code>Promise.any()</code> – The First Success</h3>
<p>⤑ <strong>Scenario:</strong> Popatlal is looking for a marriage proposal. He sends his profile to three different matchmakers.</p>
<ul>
<li><p>He only cares about the <strong>first one who says "Yes!"</strong> (success).</p>
</li>
<li><p>If the first one says "No," he waits for the second one.</p>
</li>
<li><p>He only gets sad if <strong>everyone</strong> says "No."</p>
</li>
</ul>
<pre><code class="language-javascript">Promise.any([matchMaker1, matchMaker2, matchMaker3])
  .then((firstYes) =&gt; console.log("Popatlal is getting married!"))
  .catch((err) =&gt; console.log("All rejected. Cancel the umbrella."));
</code></pre>
<p>⤑ <strong>Behavior:</strong> It waits for the <strong>first successful</strong> promise. It only fails if every single promise fails.</p>
<hr />
<h2>❊ Table :</h2>
<table style="min-width:390px"><colgroup><col style="width:365px"></col><col style="min-width:25px"></col></colgroup><tbody><tr><td><p><strong><em>Method</em></strong></p></td><td><p><strong><em>Behavior</em></strong></p></td></tr><tr><td><p><strong>.then()</strong></p></td><td><p>Runs when a promise succeeds.</p></td></tr><tr><td><p><strong>.catch()</strong></p></td><td><p>Runs when a promise fails.</p></td></tr><tr><td><p><strong>.finally()</strong></p></td><td><p>Runs always, regardless of success or failure.</p></td></tr><tr><td><p><strong>Promise.all()</strong></p></td><td><p>Succeeds if <strong>all</strong> succeed; fails if <strong>any</strong> fail.</p></td></tr><tr><td><p><strong>Promise.allSettled()</strong></p></td><td><p>Waits for <strong>all</strong> to finish, no matter the outcome.</p></td></tr><tr><td><p><strong>Promise.race()</strong></p></td><td><p>Returns the <strong>first</strong> promise to finish (win or lose).</p></td></tr><tr><td><p><strong>Promise.any()</strong></p></td><td><p>Returns the <strong>first success</strong>; fails only if <strong>all</strong> fail.</p></td></tr></tbody></table>

<hr />
<h2>❊ Conclusion</h2>
<p>JavaScript Promises are just like the commitments made in Gokuldham Society. They help us manage time, handle failures gracefully, and coordinate between different tasks. Just remember: keep your promises like Jethalal tries to (mostly) and handle your errors like Bhide handles his "Zamana!"</p>
<hr />
<h3><strong>Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Arrow Functions in JavaScript: A Simpler Way to Write Functions]]></title><description><![CDATA[Modern JavaScript introduced arrow functions to make writing functions shorter and cleaner.
If you're learning JS, arrow functions will quickly become your favorite feature.
Let’s understand them step]]></description><link>https://blog.prakashtsx.me/arrow-function</link><guid isPermaLink="true">https://blog.prakashtsx.me/arrow-function</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Thu, 26 Feb 2026 18:53:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/1f90a1dc-e61c-433c-a5af-9d4ed1d578e0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Modern JavaScript introduced <strong>arrow functions</strong> to make writing functions shorter and cleaner.</p>
<p>If you're learning JS, arrow functions will quickly become your favorite feature.</p>
<p>Let’s understand them step by step.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/a04622bf-893d-432f-add6-b1692ba317f5.png" alt="" style="display:block;margin:0 auto" />

<h2>❋ What Are Arrow Functions?</h2>
<p>Arrow functions are a <strong>shorter way to write functions</strong> in JavaScript.</p>
<p>They were introduced in ES6 (2015) to reduce boilerplate code and improve readability.</p>
<p>Instead of writing:</p>
<pre><code class="language-javascript">function greet(name) {
  return "Hello " + name;
}
</code></pre>
<p>We can write:</p>
<pre><code class="language-javascript">const greet = (name) =&gt; {
  return "Hello " + name;
};
</code></pre>
<h2>❋ Basic Arrow Function Syntax</h2>
<p>Here is the general syntax:</p>
<pre><code class="language-javascript">const functionName = (parameters) =&gt; {
  // code
};
</code></pre>
<ul>
<li><p><code>const</code> → storing function in a variable</p>
</li>
<li><p><code>(parameters)</code> → input values</p>
</li>
<li><p><code>=&gt;</code> → arrow symbol</p>
</li>
<li><p><code>{}</code> → function body</p>
</li>
</ul>
<hr />
<h2>❋ Arrow Function with One Parameter</h2>
<p>If there is <strong>only one parameter</strong>, you can remove parentheses.</p>
<p>Normal function:</p>
<pre><code class="language-javascript">function square(num) {
  return num * num;
}
</code></pre>
<p>Arrow function:</p>
<pre><code class="language-javascript">const square = num =&gt; {
  return num * num;
};
</code></pre>
<h2>❋ Arrow Function with Multiple Parameters</h2>
<p>If there are multiple parameters, parentheses are required.</p>
<pre><code class="language-javascript">const add = (a, b) =&gt; {
  return a + b;
};
</code></pre>
<p>Example:</p>
<pre><code class="language-javascript">console.log(add(5, 3)); // 8
</code></pre>
<hr />
<h2>❋ Implicit Return vs Explicit Return</h2>
<p>This is the most powerful feature.</p>
<h3>🔹 Explicit Return (using return keyword)</h3>
<pre><code class="language-javascript">const multiply = (a, b) =&gt; {
  return a * b;
};
</code></pre>
<h3>🔹 Implicit Return (no return keyword)</h3>
<p>If your function has <strong>only one expression</strong>, you can remove <code>{}</code> and <code>return</code>.</p>
<pre><code class="language-javascript">const multiply = (a, b) =&gt; a * b;
</code></pre>
<p>JavaScript automatically returns the value.</p>
<p>That’s called <strong>implicit return</strong>.</p>
<hr />
<h2>❋ Basic Difference: Normal Function vs Arrow Function</h2>
<table>
<thead>
<tr>
<th>Feature</th>
<th>Normal Function</th>
<th>Arrow Function</th>
</tr>
</thead>
<tbody><tr>
<td>Syntax</td>
<td>Longer</td>
<td>Shorter</td>
</tr>
<tr>
<td><code>this</code> behavior</td>
<td>Has its own <code>this</code></td>
<td>Uses parent <code>this</code></td>
</tr>
<tr>
<td>Hoisting</td>
<td>Yes</td>
<td>No</td>
</tr>
<tr>
<td>Beginner friendly</td>
<td>Yes</td>
<td>Yes</td>
</tr>
</tbody></table>
<h2>❋ Why Arrow Functions Matter</h2>
<ul>
<li><p>Less boilerplate</p>
</li>
<li><p>Cleaner callbacks</p>
</li>
<li><p>Perfect for array methods like:</p>
<ul>
<li><p><code>map()</code></p>
</li>
<li><p><code>filter()</code></p>
</li>
<li><p><code>reduce()</code></p>
</li>
</ul>
</li>
<li><p>Modern JavaScript standard</p>
</li>
</ul>
<hr />
<h3><strong>Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Array Methods You Must Know]]></title><description><![CDATA[If you want to become good at JavaScript especially as a web developer arrays are everywhere.

API data comes in arrays

Database results come in arrays

UI lists are rendered from arrays


If you mas]]></description><link>https://blog.prakashtsx.me/array-methods</link><guid isPermaLink="true">https://blog.prakashtsx.me/array-methods</guid><category><![CDATA[array methods]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Wed, 25 Feb 2026 10:40:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/c3eba613-cf12-4fff-bb62-157b90c0cb2c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you want to become good at JavaScript especially as a web developer <strong>arrays</strong> are everywhere.</p>
<ul>
<li><p>API data comes in arrays</p>
</li>
<li><p>Database results come in arrays</p>
</li>
<li><p>UI lists are rendered from arrays</p>
</li>
</ul>
<p>If you master array methods, your code becomes:</p>
<ul>
<li><p>Cleaner</p>
</li>
<li><p>Shorter</p>
</li>
<li><p>More readable</p>
</li>
<li><p>More professional</p>
</li>
</ul>
<p>Let’s understand the most important ones.</p>
<hr />
<h3><code>push()</code></h3>
<p>This method adds new element(s) to the end of the attached JavaScript array, changing the original array, and returns the new length of the array. This method accepts the elements you wish to add at the end of the array as parameters.</p>
<pre><code class="language-javascript">myArray.push(element1, element2, ..., elementX);
</code></pre>
<p>Let’s now create a new array then add some more fruits using the <code>push()</code> method:</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/a4fb1ba0-ed63-4148-aec0-89cd87a55264.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h3><code>pop()</code></h3>
<p>This method removes the last element of the attached JavaScript array, altering the original array, and returns the removed element.</p>
<pre><code class="language-javascript">myArray.pop();
</code></pre>
<p><strong>Note</strong> : You don’t pass anything as a parameter to the <code>pop()</code> method.</p>
<p>Here’s an example showing how to use the <code>pop()</code> method:</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/b76a738e-ce1b-46d7-9dfb-fd8d97a35841.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h3><code>shift()</code></h3>
<p>The <code>shift()</code> method removes the first element of an array. It works similarly to the <code>pop()</code> method, but instead of removing from the end, it removes from the beginning of the attached array. It returns the removed/shifted element.</p>
<pre><code class="language-javascript">myArray.shift();
</code></pre>
<p>Let’s now create a new array of animal names and then learn how to use the <code>shift()</code> method to remove the first name from the array:</p>
<hr />
<h3><code>unshift()</code></h3>
<p>The <code>unshift()</code> method works similarly to the <code>push()</code> method as it is used to add new elements to an array, but to the beginning of an array. This method takes the element(s) you wish to add at the beginning of the array as parameters. It also returns the new length of the array.</p>
<pre><code class="language-javascript">myArray.unshift(element1, element2, ..., elementX);
</code></pre>
<p>Let’s now create a new array of food names and then add more food names to the array using the <code>unshift()</code> method:</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/b3e48eca-6f8a-47e4-823c-51df3dec5331.png" alt="" style="display:block;margin:0 auto" />

<hr />
<p>❋ <strong>Map, reduce, and filter</strong> are all array methods in JavaScript. Each one will iterate over an array and perform a transformation or computation. Each will return a new array based on the result of the function.</p>
<p>Let's Understand these concepts :</p>
<h2>✤ <strong>Map</strong></h2>
<p><code>map()</code> creates a <strong>new array</strong> by transforming each element.</p>
<p>⤑ It does NOT modify the original array.</p>
<h3><strong>Syntax</strong></h3>
<pre><code class="language-javascript">var new_array = arr.map(function callback(element, index, array) {
    // Return value for new_array
}[, thisArg])
</code></pre>
<p>In the callback, only the array <code>element</code> is required. Usually some action is performed on the value and then a new value is returned.</p>
<h2>Traditional For Loop vs map()</h2>
<h3>🔸 Using For Loop</h3>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/a57a4830-7646-4a98-91ee-fba16ed06254.png" alt="" style="display:block;margin:0 auto" />

<h3>🔸 Using map()</h3>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/c6cccebe-62a9-48ea-bddc-3f85903ad78a.png" alt="" style="display:block;margin:0 auto" />

<p>So basically the map function <strong>maps each every value</strong> in the array and transforms it based on a given condition.</p>
<hr />
<h2>✤ Filter()</h2>
<p>The <code>filter()</code> method takes each element in an array and it applies a conditional statement against it. If this conditional returns true, the element gets pushed to the output array. If the condition returns false, the element does not get pushed to the output array.</p>
<h3><strong>Syntax</strong></h3>
<pre><code class="language-javascript">var new_array = arr.filter(function callback(element, index, array) {
    // Return true or false
}[, thisArg])
</code></pre>
<p>The syntax for <code>filter</code> is similar to <code>map</code>, except the callback function should return <code>true</code> to keep the element, or <code>false</code> otherwise. In the callback, only the <code>element</code> is required.</p>
<h3><strong>Examples</strong></h3>
<p>In the following example, odd numbers are "filtered" out, leaving only even numbers.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/23ee1f13-3520-44d6-ad17-0fcd3f56beb7.png" alt="" style="display:block;margin:0 auto" />

<p>In the next example, <code>filter()</code> is used to get all the students whose grades are greater than or equal to 90.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/213f185f-09cc-499e-a943-c01becb1fafd.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>✤ Reduce()</h2>
<p>The <code>reduce()</code> method reduces an array of values down to just one value. To get the output value, it runs a reducer function on each element of the array.</p>
<h3><strong>Syntax</strong></h3>
<pre><code class="language-javascript">arr.reduce(callback[, initialValue])
</code></pre>
<p>The <code>callback</code> argument is a function that will be called once for every item in the array. This function takes four arguments, but often only the first two are used.</p>
<ul>
<li><p><em>accumulator</em> - the returned value of the previous iteration</p>
</li>
<li><p><em>currentValue</em> - the current item in the array</p>
</li>
<li><p><em>index</em> - the index of the current item</p>
</li>
<li><p><em>array</em> - the original array on which reduce was called</p>
</li>
<li><p>The <code>initialValue</code> argument is optional. If provided, it will be used as the initial accumulator value in the first call to the callback function.</p>
</li>
</ul>
<h3><strong>Examples</strong></h3>
<p>The following example adds every number together in an array of numbers.</p>
<pre><code class="language-javascript">const numbers = [1, 2, 3, 4];
const sum = numbers.reduce(function (result, item) {
  return result + item;
}, 0);
console.log(sum); // 10
</code></pre>
<p>In the next example, <code>reduce()</code> is used to transform an array of strings into a single object that shows how many times each string appears in the array. Notice this call to reduce passes an empty object <code>{}</code> as the <code>initialValue</code> parameter. This will be used as the initial value of the accumulator (the first argument) passed to the callback function.</p>
<pre><code class="language-javascript">var pets = ['dog', 'chicken', 'cat', 'dog', 'chicken', 'chicken', 'rabbit'];

var petCounts = pets.reduce(function(obj, pet){
    if (!obj[pet]) {
        obj[pet] = 1;
    } else {
        obj[pet]++;
    }
    return obj;
}, {});

console.log(petCounts); 

/*
Output:
 { 
    dog: 2, 
    chicken: 3, 
    cat: 1, 
    rabbit: 1 
 }
 */
</code></pre>
<h2>✤ forEach()</h2>
<p>The <strong>JavaScript Array forEach() method</strong> is a built-in function that executes a provided function once for each array element. It does not return a new array and does not modify the original array. It's commonly used for iteration and performing actions on each array element.</p>
<h2><strong>Return value</strong></h2>
<p>It does not return a new array. It returns undefined. This method may or may not change the original array provided as it depends upon the functionality of the argument function.</p>
<img src="https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/e8892802-2637-40a3-a3e0-c44b5ef1ad0e.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h3><strong>Want More…?</strong></h3>
<p>I write articles on <a href="http://blog.prakashtsx.com"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[CSS Selectors Explained]]></title><description><![CDATA[Why Do We Need CSS Selectors?
Imagine you have a webpage with:

headings

paragraphs

buttons

cards

links


CSS needs a way to answer one simple question:

Which element should I style?

That’s exactly what CSS selectors do.
Selectors are used to c...]]></description><link>https://blog.prakashtsx.me/css-selectors</link><guid isPermaLink="true">https://blog.prakashtsx.me/css-selectors</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[CSS]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Fri, 30 Jan 2026 14:40:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769783971731/f7b212d6-b873-47e0-88ec-19a419edcc32.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-why-do-we-need-css-selectors">Why Do We Need CSS Selectors?</h2>
<p>Imagine you have a webpage with:</p>
<ul>
<li><p>headings</p>
</li>
<li><p>paragraphs</p>
</li>
<li><p>buttons</p>
</li>
<li><p>cards</p>
</li>
<li><p>links</p>
</li>
</ul>
<p>CSS needs a way to answer one simple question:</p>
<blockquote>
<p><strong>Which element should I style?</strong></p>
</blockquote>
<p>That’s exactly what <strong>CSS selectors</strong> do.</p>
<p><strong>Selectors are used to choose HTML elements</strong> so CSS can apply styles to them.</p>
<p>Without selectors, CSS wouldn’t know <strong>where</strong> to apply styles.</p>
<h2 id="heading-css-selectors-ways-to-choose-elements">CSS Selectors = Ways to Choose Elements</h2>
<p>Think of CSS selectors like <strong>addressing people</strong>.</p>
<ul>
<li><p>“All students” → element selector</p>
</li>
<li><p>“Students wearing red shirts” → class selector</p>
</li>
<li><p>“That one student with roll no. 12” → ID selector</p>
</li>
</ul>
<p>CSS works the same way.</p>
<h2 id="heading-element-selector">Element Selector</h2>
<p>The <strong>element selector</strong> selects HTML elements by their tag name.</p>
<h3 id="heading-example">Example</h3>
<p><strong>HTML</strong></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a paragraph<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is another paragraph<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p><strong>CSS</strong></p>
<pre><code class="lang-css"><span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">color</span>: blue;
}
</code></pre>
<h3 id="heading-result">Result</h3>
<p>All <code>&lt;p&gt;</code> elements become blue.</p>
<p>Use element selectors when:</p>
<ul>
<li>You want to style <strong>all elements of the same type</strong></li>
</ul>
<h2 id="heading-class-selector">Class Selector</h2>
<p>The <strong>class selector</strong> targets elements with a specific class.</p>
<h3 id="heading-syntax">Syntax</h3>
<pre><code class="lang-css"><span class="hljs-selector-class">.class-name</span>
</code></pre>
<h3 id="heading-example-1">Example</h3>
<p><strong>HTML</strong></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"highlight"</span>&gt;</span>Important text<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Normal text<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p><strong>CSS</strong></p>
<pre><code class="lang-css"><span class="hljs-selector-class">.highlight</span> {
  <span class="hljs-attribute">color</span>: red;
}
</code></pre>
<h3 id="heading-result-1">Result</h3>
<p>Only the paragraph with class <code>highlight</code> is red.</p>
<p>Use class selectors when:</p>
<ul>
<li>You want to style <strong>multiple specific elements</strong></li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769783233772/542f8de2-ef4e-4612-87d1-2765662f2db3.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-id-selector">ID Selector</h2>
<p>The <strong>ID selector</strong> targets a single unique element.</p>
<h3 id="heading-syntax-1">Syntax</h3>
<pre><code class="lang-css"><span class="hljs-selector-id">#id-name</span>
</code></pre>
<h3 id="heading-example-2">Example</h3>
<p><strong>HTML</strong></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"main-title"</span>&gt;</span>Welcome<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
</code></pre>
<p><strong>CSS</strong></p>
<pre><code class="lang-css"><span class="hljs-selector-id">#main-title</span> {
  <span class="hljs-attribute">color</span>: green;
}
</code></pre>
<h3 id="heading-result-2">Result</h3>
<p>Only that one heading is styled.</p>
<p>Rules:</p>
<ul>
<li><p>IDs must be <strong>unique</strong></p>
</li>
<li><p>Use IDs sparingly</p>
</li>
</ul>
<h2 id="heading-group-selectors">Group Selectors</h2>
<p>Group selectors let you apply the <strong>same style to multiple selectors</strong>.</p>
<h3 id="heading-example-3">Example</h3>
<p><strong>CSS</strong></p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span>, <span class="hljs-selector-tag">h2</span>, <span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">font-family</span>: Arial;
}
</code></pre>
<h3 id="heading-result-3">Result</h3>
<p><code>h1</code>, <code>h2</code>, and <code>p</code> all use Arial.</p>
<p>Use group selectors to:</p>
<ul>
<li><p>Avoid repeating CSS</p>
</li>
<li><p>Keep styles clean</p>
</li>
</ul>
<h2 id="heading-descendant-selectors">Descendant Selectors</h2>
<p>Descendant selectors target elements <strong>inside other elements</strong>.</p>
<h3 id="heading-syntax-2">Syntax</h3>
<pre><code class="lang-css"><span class="hljs-selector-tag">parent</span> <span class="hljs-selector-tag">child</span>
</code></pre>
<h3 id="heading-example-4">Example</h3>
<p><strong>HTML</strong></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This text is inside card<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This text is outside card<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p><strong>CSS</strong></p>
<pre><code class="lang-css"><span class="hljs-selector-class">.card</span> <span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">color</span>: blue;
}
</code></pre>
<h3 id="heading-result-4">Result</h3>
<p>Only the paragraph inside <code>.card</code> turns blue.</p>
<p>This is very useful for layouts and components.</p>
<h2 id="heading-basic-selector-priority-high-level-only">Basic Selector Priority (High-Level Only)</h2>
<p>Sometimes multiple selectors target the same element.</p>
<p>CSS follows a simple priority rule:</p>
<pre><code class="lang-text">ID selector &gt; Class selector &gt; Element selector
</code></pre>
<h3 id="heading-example-5">Example</h3>
<pre><code class="lang-css"><span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">color</span>: black;
}

<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">color</span>: blue;
}

<span class="hljs-selector-id">#special</span> {
  <span class="hljs-attribute">color</span>: red;
}
</code></pre>
<p>If an element has all three:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"special"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text"</span>&gt;</span>Hello<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p><strong>Red wins</strong> because ID has higher priority.</p>
<p>Don’t worry about memorizing this now.<br />Just remember: <strong>ID is stronger than class, class is stronger than element</strong>.</p>
<p>CSS works like this:</p>
<ol>
<li><p>Read selector</p>
</li>
<li><p>Find matching elements</p>
</li>
<li><p>Apply styles</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769783430228/c7d1a361-483e-4a6d-b908-ff495c5ce600.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-why-css-selectors-are-so-important">Why CSS Selectors Are So Important</h2>
<p>Selectors are the <strong>foundation of CSS</strong>.</p>
<p>If you understand selectors:</p>
<ul>
<li><p>Styling becomes easy</p>
</li>
<li><p>Layouts make sense</p>
</li>
<li><p>Debugging becomes faster</p>
</li>
</ul>
<p>Everything in CSS starts with:</p>
<blockquote>
<p><strong>Which element am I targeting?</strong></p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769783524124/418365a3-d86b-4c58-a3d2-fae1a030dee5.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-want-more"><strong>Want More…?</strong></h3>
<p>I write articles on <a target="_blank" href="https://blog.prakashtsx.me/"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a target="_blank" href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a target="_blank" href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Emmet for HTML]]></title><description><![CDATA[The Problem: Writing HTML Is Slow
Type this by hand:
<div class="container">
  <header class="header">
    <h1>Welcome</h1>
  </header>
  <main class="content">
    <p>This is a paragraph.</p>
  </main>
</div>

That's a lot of typing. Opening tags, c...]]></description><link>https://blog.prakashtsx.me/emmet-for-html</link><guid isPermaLink="true">https://blog.prakashtsx.me/emmet-for-html</guid><category><![CDATA[Hashnode]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[Emmet]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Fri, 30 Jan 2026 14:05:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769781857096/e8542104-029c-4390-8374-9dad72907efb.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-the-problem-writing-html-is-slow"><strong>The Problem: Writing HTML Is Slow</strong></h2>
<p>Type this by hand:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">header</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Welcome<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"content"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a paragraph.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>That's a lot of typing. Opening tags, closing tags, attributes, indentation. Every single character.</p>
<p>Now imagine building a full webpage with dozens or hundreds of elements. It gets tedious fast.</p>
<p><strong>There has to be a better way.</strong></p>
<h2 id="heading-what-is-emmet"><strong>What Is Emmet?</strong></h2>
<p><strong>Emmet</strong> is a shortcut language that lets you write HTML incredibly fast.</p>
<p>Instead of typing full HTML tags, you type a short abbreviation and press Tab. Emmet expands it into complete HTML.</p>
<p><strong>Example:</strong></p>
<p>Type this:</p>
<pre><code class="lang-bash">div.container
</code></pre>
<p>Press <strong>Tab</strong></p>
<p>Get this:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Emmet isn't a separate tool you install. It's built into most modern code editors like VS Code, Sublime Text, and Atom. It just works.</p>
<h2 id="heading-why-beginners-should-use-emmet"><strong>Why Beginners Should Use Emmet</strong></h2>
<p><strong>1. Write HTML 10x faster</strong> Less typing means you focus on structure, not syntax.</p>
<p><strong>2. Fewer mistakes</strong> Emmet generates valid HTML. No forgotten closing tags or typos.</p>
<p><strong>3. Learn HTML structure better</strong> Emmet forces you to think in terms of hierarchy and nesting.</p>
<p><strong>4. Industry standard</strong> Professional developers use Emmet daily. Learning it early gives you good habits.</p>
<p><strong>5. It's optional</strong> You can always type HTML manually. Emmet is there when you want speed.</p>
<h2 id="heading-how-emmet-works"><strong>How Emmet Works</strong></h2>
<p>Emmet watches what you type in your editor. When it recognizes an abbreviation, it offers to expand it.</p>
<p><strong>Step-by-step:</strong></p>
<ol>
<li><p>Type an Emmet abbreviation: <code>p</code></p>
</li>
<li><p>Press <strong>Tab</strong> (or Enter in some editors)</p>
</li>
<li><p>Emmet expands it: <code>&lt;p&gt;&lt;/p&gt;</code></p>
</li>
<li><p>Your cursor is inside the tag, ready to add content</p>
</li>
</ol>
<p><strong>Most editors with Emmet support:</strong></p>
<ul>
<li><p>VS Code (built-in)</p>
</li>
<li><p>Sublime Text (built-in)</p>
</li>
<li><p>Atom (built-in)</p>
</li>
<li><p>WebStorm (built-in)</p>
</li>
</ul>
<p>If you're using VS Code, Emmet works out of the box. Just type and press Tab.</p>
<h2 id="heading-basic-emmet-syntax"><strong>Basic Emmet Syntax</strong></h2>
<h3 id="heading-creating-elements"><strong>Creating Elements</strong></h3>
<p>Type the tag name and press Tab.</p>
<pre><code class="lang-xml">h1    →    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
p     →    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
div   →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
span  →    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
</code></pre>
<h3 id="heading-adding-classes"><strong>Adding Classes</strong></h3>
<p>Use a dot (<code>.</code>) for classes.</p>
<pre><code class="lang-xml">div.container    →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
p.intro          →    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"intro"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p><strong>Multiple classes:</strong></p>
<pre><code class="lang-xml">div.card.shadow    →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card shadow"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-adding-ids"><strong>Adding IDs</strong></h3>
<p>Use a hash (<code>#</code>) for IDs.</p>
<pre><code class="lang-php-template"><span class="xml">div#main      →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"main"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
header#top    →    <span class="hljs-tag">&lt;<span class="hljs-name">header</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"top"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span></span>
</code></pre>
<h3 id="heading-combining-classes-and-ids"><strong>Combining Classes and IDs</strong></h3>
<pre><code class="lang-xml">div#main.container    →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"main"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><strong>Order doesn't matter:</strong></p>
<pre><code class="lang-xml">div.container#main    →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"main"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-implicit-tag-names"><strong>Implicit Tag Names</strong></h3>
<p>If you don't specify a tag, Emmet assumes <code>div</code>.</p>
<pre><code class="lang-xml">.container       →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
#header          →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"header"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
.card.shadow     →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card shadow"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-adding-attributes"><strong>Adding Attributes</strong></h2>
<p>Use square brackets <code>[]</code> for custom attributes.</p>
<pre><code class="lang-xml">a[href="#"]                →    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
img[src="photo.jpg"]       →    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"photo.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>
input[type="text"]         →    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>&gt;</span>
</code></pre>
<p><strong>Multiple attributes:</strong></p>
<pre><code class="lang-xml">img[src="car.jpg" alt="Red car"]    →    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"car.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Red car"</span>&gt;</span>
</code></pre>
<h2 id="heading-creating-nested-elements"><strong>Creating Nested Elements</strong></h2>
<p>Use the <code>&gt;</code> symbol to create child elements.</p>
<pre><code class="lang-xml">div&gt;p    →    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><strong>More complex nesting:</strong></p>
<pre><code class="lang-xml">header&gt;h1
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
</code></pre>
<p><strong>Deeper nesting:</strong></p>
<pre><code class="lang-bash">div&gt;header&gt;h1
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-sibling-elements"><strong>Sibling Elements</strong></h2>
<p>Use the <code>+</code> symbol to create elements at the same level.</p>
<pre><code class="lang-bash">header+main
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
</code></pre>
<p><strong>Multiple siblings:</strong></p>
<pre><code class="lang-bash">header+main+footer
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">footer</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">footer</span>&gt;</span>
</code></pre>
<h2 id="heading-combining-nesting-and-siblings"><strong>Combining Nesting and Siblings</strong></h2>
<pre><code class="lang-bash">div&gt;header+main+footer
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">footer</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">footer</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><strong>More complex:</strong></p>
<pre><code class="lang-bash">div.container&gt;header&gt;h1^main&gt;p^footer
</code></pre>
<p>The <code>^</code> climbs back up one level.</p>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">footer</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">footer</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-repeating-elements-with-multiplication"><strong>Repeating Elements with Multiplication</strong></h2>
<p>Use <code>*</code> to create multiple copies.</p>
<pre><code class="lang-bash">li*3
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
</code></pre>
<p><strong>Real-world example:</strong></p>
<pre><code class="lang-bash">ul&gt;li*5
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
</code></pre>
<p><strong>With classes:</strong></p>
<pre><code class="lang-bash">div.card*3
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-auto-numbering-with"><strong>Auto-Numbering with $</strong></h2>
<p>Use <code>$</code> to add auto-incrementing numbers.</p>
<pre><code class="lang-bash">div.item$*3
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item1"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item2"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item3"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><strong>In IDs:</strong></p>
<pre><code class="lang-bash">section<span class="hljs-comment">#section$*3</span>
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"section1"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"section2"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"section3"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
</code></pre>
<h2 id="heading-adding-text-content"><strong>Adding Text Content</strong></h2>
<p>Use curly braces <code>{}</code> for text.</p>
<pre><code class="lang-bash">h1{Welcome}
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Welcome<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
</code></pre>
<p><strong>With nested elements:</strong></p>
<pre><code class="lang-bash">div&gt;h1{Title}+p{Description}
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Description<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><strong>Numbered text:</strong></p>
<pre><code class="lang-bash">li{Item $}*3
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 2<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 3<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
</code></pre>
<h2 id="heading-generating-html-boilerplate"><strong>Generating HTML Boilerplate</strong></h2>
<p>The most powerful Emmet trick: generate a full HTML template instantly.</p>
<p>Type:</p>
<pre><code class="lang-bash">!
</code></pre>
<p>Press <strong>Tab</strong></p>
<p>Get this:</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><strong>This saves you from typing 10+ lines every time you create a new HTML file.</strong></p>
<p>Alternative syntax (does the same thing):</p>
<pre><code class="lang-bash">html:5
</code></pre>
<h2 id="heading-real-world-examples"><strong>Real-World Examples</strong></h2>
<h3 id="heading-navigation-menu"><strong>Navigation Menu</strong></h3>
<pre><code class="lang-bash">nav&gt;ul&gt;li*4&gt;a
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
</code></pre>
<h3 id="heading-card-grid"><strong>Card Grid</strong></h3>
<pre><code class="lang-bash">div.grid&gt;div.card*3&gt;h2+p
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"grid"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-form"><strong>Form</strong></h3>
<pre><code class="lang-bash">form&gt;input[<span class="hljs-built_in">type</span>=<span class="hljs-string">"text"</span>]+input[<span class="hljs-built_in">type</span>=<span class="hljs-string">"email"</span>]+button{Submit}
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">""</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"email"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<h3 id="heading-table"><strong>Table</strong></h3>
<pre><code class="lang-bash">table&gt;tr*3&gt;td*4
</code></pre>
<p>Expands to:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span>
</code></pre>
<h2 id="heading-emmet-cheat-sheet"><strong>Emmet Cheat Sheet</strong></h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Abbreviation</td><td>Result</td></tr>
</thead>
<tbody>
<tr>
<td><code>div</code></td><td><code>&lt;div&gt;&lt;/div&gt;</code></td></tr>
<tr>
<td><code>.class</code></td><td><code>&lt;div class="class"&gt;&lt;/div&gt;</code></td></tr>
<tr>
<td><code>#id</code></td><td><code>&lt;div id="id"&gt;&lt;/div&gt;</code></td></tr>
<tr>
<td><code>div&gt;p</code></td><td><code>&lt;div&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;</code></td></tr>
<tr>
<td><code>div+p</code></td><td><code>&lt;div&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;</code></td></tr>
<tr>
<td><code>div*3</code></td><td>3 divs</td></tr>
<tr>
<td><code>div.item$*3</code></td><td>divs with item1, item2, item3</td></tr>
<tr>
<td><code>a[href]</code></td><td><code>&lt;a href=""&gt;&lt;/a&gt;</code></td></tr>
<tr>
<td><code>p{text}</code></td><td><code>&lt;p&gt;text&lt;/p&gt;</code></td></tr>
<tr>
<td><code>!</code></td><td>Full HTML template</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-for-cheat-sheet-you-can-visit-here-official-documentationhttpsdocsemmetiocheat-sheet"><mark>For Cheat Sheet You can visit here</mark> : <a target="_blank" href="https://docs.emmet.io/cheat-sheet/">Official Documentation</a></h2>
<h3 id="heading-want-more"><strong>Want More…?</strong></h3>
<p>I write articles on <a target="_blank" href="https://blog.prakashtsx.me/"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a target="_blank" href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a target="_blank" href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Understanding HTML Tags and Elements]]></title><description><![CDATA[What Is HTML and Why Do We Use It?
HTML (HyperText Markup Language) is used to create the structure of a webpage.
When you open a website, the browser first reads HTML code to know:

What text to show

What is a heading

What is a paragraph

Where im...]]></description><link>https://blog.prakashtsx.me/html-tags-and-elements</link><guid isPermaLink="true">https://blog.prakashtsx.me/html-tags-and-elements</guid><category><![CDATA[HTML5]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Hashnode]]></category><dc:creator><![CDATA[Prakash]]></dc:creator><pubDate>Fri, 30 Jan 2026 13:26:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769779445002/66a0fd6f-ac45-4784-914f-ceed4f88b170.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-html-and-why-do-we-use-it">What Is HTML and Why Do We Use It?</h2>
<p><strong>HTML (HyperText Markup Language)</strong> is used to <strong>create the structure of a webpage</strong>.</p>
<p>When you open a website, the browser first reads <strong>HTML code</strong> to know:</p>
<ul>
<li><p>What text to show</p>
</li>
<li><p>What is a heading</p>
</li>
<li><p>What is a paragraph</p>
</li>
<li><p>Where images and links are placed</p>
</li>
</ul>
<p>Think of HTML as the <strong>skeleton</strong> of a webpage.</p>
<h2 id="heading-html-skeleton-of-a-webpage">HTML = Skeleton of a Webpage</h2>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>My Website<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Welcome to my page<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>Without HTML, the browser has <strong>nothing to display</strong>.</p>
<h2 id="heading-what-is-an-html-tag">What Is an HTML Tag?</h2>
<p>An <strong>HTML tag</strong> is written inside angle brackets <code>&lt; &gt;</code>.</p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>This tag tells the browser:</p>
<blockquote>
<p>“This is a paragraph.”</p>
</blockquote>
<p>Some common tags:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>
</code></pre>
<h2 id="heading-opening-tag-closing-tag-and-content">Opening Tag, Closing Tag, and Content</h2>
<p>Most HTML tags have:</p>
<ul>
<li><p>an <strong>opening tag</strong></p>
</li>
<li><p><strong>content</strong></p>
</li>
<li><p>a <strong>closing tag</strong></p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>Breakdown:</p>
<ul>
<li><p><code>&lt;p&gt;</code> → opening tag</p>
</li>
<li><p><code>Hello World</code> → content</p>
</li>
<li><p><code>&lt;/p&gt;</code> → closing tag</p>
</li>
</ul>
<p>Think of it like a <strong>box</strong>:</p>
<pre><code class="lang-text">&lt;p&gt;        ← open
Hello World
&lt;/p&gt;       ← close
</code></pre>
<h2 id="heading-what-is-an-html-element">What Is an HTML Element?</h2>
<p><em>This is very important</em></p>
<p><strong>HTML element = opening tag + content + closing tag</strong></p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is a heading<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
</code></pre>
<ul>
<li><p><code>&lt;h1&gt;</code> → tag</p>
</li>
<li><p><code>&lt;/h1&gt;</code> → tag</p>
</li>
<li><p>Whole line → <strong>HTML element</strong></p>
</li>
</ul>
<p><strong>Tag ≠ Element</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769779076781/0cbf3e4b-24d9-44ed-aabf-b703d6dc6ddb.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-self-closing-void-elements">Self-Closing (Void) Elements</h2>
<p>Some elements <strong>do not have content</strong>.</p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"photo.jpg"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> /&gt;</span>
</code></pre>
<p>These are called <strong>self-closing (void) elements</strong>.</p>
<p><strong>They:</strong></p>
<ul>
<li><p>Don’t wrap text</p>
</li>
<li><p>Don’t need closing tags</p>
</li>
</ul>
<h2 id="heading-block-level-elements">Block-Level Elements</h2>
<p>Block elements:</p>
<ul>
<li><p>Start on a new line</p>
</li>
<li><p>Take full width</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Heading<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a paragraph<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>This is a div<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>They appear like this:</p>
<pre><code class="lang-text">[ Heading          ]
[ Paragraph        ]
[ Div              ]
</code></pre>
<h2 id="heading-inline-elements">Inline Elements</h2>
<p>Inline elements:</p>
<ul>
<li><p>Stay in the same line</p>
</li>
<li><p>Take only required space</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
  This is <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>inline text<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> inside a paragraph.
<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>Inline elements behave like <strong>words in a sentence</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769778863216/fd4ca0e2-dc05-42f7-8130-8819c3761110.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-commonly-used-html-tags-beginner-friendly">Commonly Used HTML Tags (Beginner-Friendly)</h2>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Main heading<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Sub heading<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a paragraph.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Paragraph inside div<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Small inline text<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://blog.prakashtsx.me"</span>&gt;</span>Visit my blog<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"image.png"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"My image"</span> /&gt;</span>
</code></pre>
<p><em>These tags are more than enough to start.</em></p>
<h2 id="heading-inspect-html-in-the-browser-must-try">Inspect HTML in the Browser (Must Try)</h2>
<ol>
<li><p>Open any website</p>
</li>
<li><p>Right-click → <strong>Inspect</strong></p>
</li>
<li><p>Look at the <strong>Elements</strong> tab</p>
</li>
</ol>
<p>You’ll see real HTML like:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Text<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<hr />
<h3 id="heading-want-more"><strong>Want More…?</strong></h3>
<p>I write articles on <a target="_blank" href="https://blog.prakashtsx.me/"><strong>blog.prakashtsx.com</strong></a> and also post development-related content on the following platforms:</p>
<ul>
<li><p><a target="_blank" href="https://x.com/prakashtsx"><strong>Twitter/X</strong></a></p>
</li>
<li><p><a target="_blank" href="https://www.linkedin.com/in/prakashtsx"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>