<?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[JavaScriptWithShubham]]></title><description><![CDATA[JavaScriptWithShubham]]></description><link>https://javascriptwithshubham.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 14:09:24 GMT</lastBuildDate><atom:link href="https://javascriptwithshubham.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Map and Set in JavaScript]]></title><description><![CDATA[What Map Is
Map is a special data structure in JavaScript used for storing key-value pairs. At first glance, it may look similar to objects. Example:
const user = {
  name: "Shubham"
};

But Map provi]]></description><link>https://javascriptwithshubham.hashnode.dev/map-and-set-in-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/map-and-set-in-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 13:18:02 GMT</pubDate><content:encoded><![CDATA[<h3>What <code>Map</code> Is</h3>
<p><code>Map</code> is a special data structure in JavaScript used for storing key-value pairs. At first glance, it may look similar to objects. Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham"
};
</code></pre>
<p>But <code>Map</code> provides more flexibility and better control in certain situations. Example:</p>
<pre><code class="language-js">const map = new Map();

map.set("name", "Shubham");

console.log(map.get("name"));
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Shubham
</code></pre>
<h3>Understanding the Structure of Map</h3>
<p>A <code>Map</code> stores data in:</p>
<pre><code class="language-js">key → value
</code></pre>
<p>format. Example:</p>
<pre><code class="language-js">map.set("age", 21);
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>"age"</code> is the key</p>
</li>
<li><p><code>21</code> is the value</p>
</li>
</ul>
<h3>Common Map Methods</h3>
<p>Some commonly used methods:</p>
<pre><code class="language-js">set()
get()
has()
delete()
clear()
size
</code></pre>
<h3>Example of Multiple Values</h3>
<p>Example:</p>
<pre><code class="language-js">const userMap = new Map();

userMap.set("name", "Shubham");
userMap.set("age", 21);

console.log(userMap);
</code></pre>
<h3>What <code>Set</code> Is</h3>
<p><code>Set</code> is another special JavaScript data structure. Its main purpose is:</p>
<blockquote>
<p>Store unique values only.</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-js">const set = new Set();

set.add(1);
set.add(2);
set.add(2);

console.log(set);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Set(2) {1, 2}
</code></pre>
<p>Notice:</p>
<ul>
<li>duplicate value <code>2</code> is ignored</li>
</ul>
<p>Because sets only allow unique values.</p>
<h3>Understanding the Real Purpose of Set</h3>
<p>Arrays can contain duplicates. Example:</p>
<pre><code class="language-js">[1, 1, 2, 2, 3]
</code></pre>
<p>But sometimes developers only want unique values. That is exactly where <code>Set</code> becomes useful.</p>
<h3>Common Set Methods</h3>
<p>Common methods:</p>
<pre><code class="language-js">add()
has()
delete()
clear()
size
</code></pre>
<p>Example:</p>
<pre><code class="language-js">const fruits = new Set();

fruits.add("apple");
fruits.add("banana");
fruits.add("apple");

console.log(fruits);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Set(2) {'apple', 'banana'}
</code></pre>
<h3>Difference Between <code>Map</code> and <code>Object</code></h3>
<p>Many beginners wonder:</p>
<blockquote>
<p>“If objects already store key-value pairs, why does Map exist?”</p>
</blockquote>
<p>Because <code>Map</code> solves several limitations of objects.</p>
<h3>1. Key Types</h3>
<h4>Objects</h4>
<p>Object keys are mostly strings or symbols.</p>
<p>Example:</p>
<pre><code class="language-js">const obj = {};

obj[1] = "Hello";

console.log(obj);
</code></pre>
<p>Internally:</p>
<ul>
<li><code>1</code> becomes <code>"1"</code></li>
</ul>
<h4>Map</h4>
<p>Maps allow any data type as keys.</p>
<p>Example:</p>
<pre><code class="language-js">const map = new Map();

map.set(1, "Number Key");
map.set(true, "Boolean Key");

console.log(map);
</code></pre>
<p>Even objects and functions can become keys.</p>
<h3>2. Better Iteration</h3>
<p>Maps are easier to iterate.</p>
<p>Example:</p>
<pre><code class="language-js">for (const [key, value] of map) {
  console.log(key, value);
}
</code></pre>
<p>Objects require extra methods like:</p>
<ul>
<li><p><code>Object.keys()</code></p>
</li>
<li><p><code>Object.entries()</code></p>
</li>
</ul>
<h3>3. Size Property</h3>
<p>Objects do not directly provide size. Example:</p>
<pre><code class="language-js">Object.keys(obj).length
</code></pre>
<p>But maps provide:</p>
<pre><code class="language-js">map.size
</code></pre>
<p>Much cleaner.</p>
<h3>4. Performance</h3>
<p>For frequent:</p>
<ul>
<li><p>insertions</p>
</li>
<li><p>deletions</p>
</li>
<li><p>dynamic key management</p>
</li>
</ul>
<p>Maps often perform better than objects.</p>
<h3>Difference Between <code>Set</code> and <code>Array</code></h3>
<p>Both store collections of values. But behavior differs significantly.</p>
<h3>Arrays Allow Duplicates</h3>
<p>Example:</p>
<pre><code class="language-js">const arr = [1, 1, 2, 2];
</code></pre>
<p>Duplicates remain.</p>
<h3>Sets Remove Duplicates</h3>
<p>Example:</p>
<pre><code class="language-js">const set = new Set([1, 1, 2, 2]);

console.log(set);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Set(2) {1, 2}
</code></pre>
<h3>Arrays Maintain Indexed Structure</h3>
<p>Arrays use indexes. Example:</p>
<pre><code class="language-js">arr[0]
arr[1]
</code></pre>
<p>Sets do not use indexes directly.</p>
<h3>Arrays Have More Built-in Methods</h3>
<p>Arrays support methods like:</p>
<ul>
<li><p>map()</p>
</li>
<li><p>filter()</p>
</li>
<li><p>reduce()</p>
</li>
</ul>
<p>Sets are mainly focused on uniqueness.</p>
<h3>Converting Set to Array</h3>
<p>Example:</p>
<pre><code class="language-js">const set = new Set([1, 2, 3]);

const arr = [...set];

console.log(arr);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[1, 2, 3]
</code></pre>
<h3>Removing Duplicates Using Set</h3>
<p>Very common interview trick. Example:</p>
<pre><code class="language-js">const numbers = [1, 1, 2, 3, 3];

const unique = [...new Set(numbers)];

console.log(unique);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[1, 2, 3]
</code></pre>
<h3>When to Use <code>Map</code></h3>
<p>Use <code>Map</code> when:</p>
<ul>
<li><p>dynamic keys are needed</p>
</li>
<li><p>keys are not just strings</p>
</li>
<li><p>frequent insert/delete operations happen</p>
</li>
<li><p>iteration is important</p>
</li>
</ul>
<h3>Real Use Cases of Map</h3>
<p>Examples:</p>
<ul>
<li><p>caching systems</p>
</li>
<li><p>storing metadata</p>
</li>
<li><p>tracking user sessions</p>
</li>
<li><p>object-based keys</p>
</li>
<li><p>fast lookups</p>
</li>
</ul>
<h3>Example with Object Keys</h3>
<p>Example:</p>
<pre><code class="language-js">const user = {
  id: 1
};

const map = new Map();

map.set(user, "User Data");

console.log(map.get(user));
</code></pre>
<p>Objects cannot handle this cleanly.</p>
<p>Maps can.</p>
<h3>When to Use <code>Set</code></h3>
<p>Use <code>Set</code> when:</p>
<ul>
<li><p>unique values are required</p>
</li>
<li><p>duplicates should be removed</p>
</li>
<li><p>fast existence checking is needed</p>
</li>
</ul>
<h3>Real Use Cases of Set</h3>
<p>Examples:</p>
<ul>
<li><p>removing duplicates</p>
</li>
<li><p>tracking unique visitors</p>
</li>
<li><p>storing selected IDs</p>
</li>
<li><p>preventing repeated values</p>
</li>
</ul>
<h3>Fast Lookup with Set</h3>
<p>Example:</p>
<pre><code class="language-js">const ids = new Set([1, 2, 3]);

console.log(ids.has(2));
</code></pre>
<p>Output:</p>
<pre><code class="language-js">true
</code></pre>
<h3>Common Beginner Mistake</h3>
<p>Many beginners treat <code>Map</code> exactly like objects.But maps are different because:</p>
<ul>
<li><p>keys can be any type</p>
</li>
<li><p>order is preserved</p>
</li>
<li><p>iteration is cleaner</p>
</li>
</ul>
<h3>Another Common Mistake</h3>
<p>Beginners sometimes expect:</p>
<pre><code class="language-js">set[0]
</code></pre>
<p>to work. But sets are not index-based collections like arrays.</p>
<h3>Why Modern JavaScript Introduced These Structures</h3>
<p>As applications became larger, developers needed better data structures for:</p>
<ul>
<li><p>performance</p>
</li>
<li><p>flexibility</p>
</li>
<li><p>cleaner APIs</p>
</li>
</ul>
<p><code>Map</code> and <code>Set</code> solve many problems traditional objects and arrays struggle with.</p>
<h3>Final Understanding</h3>
<p><code>Map</code> is a key-value data structure with flexible key support and better iteration features.<br /><code>Set</code> is a collection that stores only unique values.<br />Compared to:</p>
<ul>
<li><p>objects → maps provide more flexibility</p>
</li>
<li><p>arrays → sets provide uniqueness</p>
</li>
</ul>
<p>Understanding when to use:</p>
<ul>
<li><p>objects</p>
</li>
<li><p>arrays</p>
</li>
<li><p>maps</p>
</li>
<li><p>sets</p>
</li>
</ul>
<p>is an important part of writing efficient and scalable modern JavaScript applications.</p>
]]></content:encoded></item><item><title><![CDATA[JS Promises Explained for Beginners ]]></title><description><![CDATA[What Problem Promises Solve
JavaScript is asynchronous. This means some operations take time. Examples:

API calls

database queries

file handling

authentication

timers


Initially, JavaScript hand]]></description><link>https://javascriptwithshubham.hashnode.dev/js-promises-explained-for-beginners</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/js-promises-explained-for-beginners</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 13:14:06 GMT</pubDate><content:encoded><![CDATA[<h3>What Problem Promises Solve</h3>
<p>JavaScript is asynchronous. This means some operations take time. Examples:</p>
<ul>
<li><p>API calls</p>
</li>
<li><p>database queries</p>
</li>
<li><p>file handling</p>
</li>
<li><p>authentication</p>
</li>
<li><p>timers</p>
</li>
</ul>
<p>Initially, JavaScript handled asynchronous operations using callbacks. Example:</p>
<pre><code class="language-js">fetchData(() =&gt; {
  getUser(() =&gt; {
    getPosts(() =&gt; {
      console.log("Done");
    });
  });
});
</code></pre>
<p>As applications became larger, this created a huge problem. Code became:</p>
<ul>
<li><p>deeply nested</p>
</li>
<li><p>difficult to read</p>
</li>
<li><p>difficult to debug</p>
</li>
<li><p>hard to maintain</p>
</li>
</ul>
<p>This problem became known as:</p>
<blockquote>
<p>Callback Hell</p>
</blockquote>
<p>Promises were introduced to solve this problem.</p>
<h3>Real Purpose of Promises</h3>
<p>Promises were created to:</p>
<ul>
<li><p>improve readability</p>
</li>
<li><p>simplify async flow</p>
</li>
<li><p>handle success and failure properly</p>
</li>
<li><p>reduce nested callbacks</p>
</li>
</ul>
<p>Instead of passing callbacks everywhere, promises provide a cleaner structure for asynchronous operations.</p>
<h3>What a Promise Actually Is</h3>
<p>A promise is simply:</p>
<blockquote>
<p>An object representing a future value.</p>
</blockquote>
<p>Think of it like:</p>
<ul>
<li><p>work has started</p>
</li>
<li><p>result is not available yet</p>
</li>
<li><p>eventually it will either succeed or fail</p>
</li>
</ul>
<p>That future result is managed by promises.</p>
<h3>Promise States</h3>
<p>Every promise has a state. There are mainly three states.</p>
<h3>1. Pending</h3>
<p>Initially, every promise starts in:</p>
<pre><code class="language-js">pending
</code></pre>
<p>This means:</p>
<blockquote>
<p>operation is still running.</p>
</blockquote>
<p>Example:</p>
<ul>
<li><p>API request still loading</p>
</li>
<li><p>file still reading</p>
</li>
<li><p>timer still waiting</p>
</li>
</ul>
<h3>2. Fulfilled</h3>
<p>If operation succeeds, promise becomes:</p>
<pre><code class="language-js">fulfilled
</code></pre>
<p>This means:</p>
<blockquote>
<p>task completed successfully.</p>
</blockquote>
<p>And promise returns a value.</p>
<h3>3. Rejected</h3>
<p>If something fails, promise becomes:</p>
<pre><code class="language-js">rejected
</code></pre>
<p>This means:</p>
<blockquote>
<p>operation failed.</p>
</blockquote>
<p>And promise returns an error.</p>
<h3>Visual Understanding of Promise States</h3>
<p>Think of ordering food online.</p>
<h4>Pending</h4>
<p>Food is being prepared.</p>
<h4>Fulfilled</h4>
<p>Food delivered successfully.</p>
<h4>Rejected</h4>
<p>Order failed or canceled.</p>
<p>That is basically how promises behave internally.</p>
<h3>Basic Promise Lifecycle</h3>
<p>A promise moves through states during execution.</p>
<p>Flow:</p>
<pre><code class="language-js">pending → fulfilled
</code></pre>
<p>or</p>
<pre><code class="language-js">pending → rejected
</code></pre>
<p>Once a promise changes state:</p>
<ul>
<li><p>it becomes final</p>
</li>
<li><p>it cannot change again</p>
</li>
</ul>
<h3>Creating a Basic Promise</h3>
<p>Example:</p>
<pre><code class="language-js">const promise = new Promise((resolve, reject) =&gt; {
  const success = true;

  if (success) {
    resolve("Data fetched");
  } else {
    reject("Something went wrong");
  }
});
</code></pre>
<h3>Understanding <code>resolve</code> and <code>reject</code></h3>
<p>Inside promises:</p>
<h4><code>resolve()</code></h4>
<p>Means:</p>
<blockquote>
<p>operation succeeded.</p>
</blockquote>
<h4><code>reject()</code></h4>
<p>Means:</p>
<blockquote>
<p>operation failed.</p>
</blockquote>
<h3>Handling Success and Failure</h3>
<p>Promises provide methods for handling outcomes.</p>
<h3>Using <code>.then()</code></h3>
<p><code>.then()</code> handles successful results.</p>
<p>Example:</p>
<pre><code class="language-js">promise.then((data) =&gt; {
  console.log(data);
});
</code></pre>
<p>If promise resolves:</p>
<ul>
<li><code>.then()</code> executes</li>
</ul>
<h3>Using <code>.catch()</code></h3>
<p><code>.catch()</code> handles errors.</p>
<p>Example:</p>
<pre><code class="language-js">promise.catch((error) =&gt; {
  console.log(error);
});
</code></pre>
<p>If promise rejects:</p>
<ul>
<li><code>.catch()</code> executes</li>
</ul>
<h3>Complete Example</h3>
<p>Example:</p>
<pre><code class="language-js">const promise = new Promise((resolve, reject) =&gt; {
  const success = true;

  if (success) {
    resolve("Data received");
  } else {
    reject("Error occurred");
  }
});

promise
  .then((data) =&gt; {
    console.log(data);
  })
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<h3>Promise Lifecycle in This Example</h3>
<ol>
<li><p>Promise starts as pending</p>
</li>
<li><p>Condition checks</p>
</li>
<li><p>If successful → resolve()</p>
</li>
<li><p>Promise becomes fulfilled</p>
</li>
<li><p><code>.then()</code> runs</p>
</li>
</ol>
<p>Or:</p>
<ol>
<li><p>reject() runs</p>
</li>
<li><p>Promise becomes rejected</p>
</li>
<li><p><code>.catch()</code> runs</p>
</li>
</ol>
<h3>Promise Chaining Concept</h3>
<p>One major advantage of promises is chaining. Instead of nested callbacks:</p>
<pre><code class="language-js">login(() =&gt; {
  getProfile(() =&gt; {
    getPosts(() =&gt; {
      console.log("Done");
    });
  });
});
</code></pre>
<p>Promises allow sequential flow.</p>
<p>Example:</p>
<pre><code class="language-js">login()
  .then((user) =&gt; {
    return getProfile(user);
  })
  .then((profile) =&gt; {
    return getPosts(profile);
  })
  .then((posts) =&gt; {
    console.log(posts);
  })
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<p>Much cleaner.</p>
<h3>Understanding Promise Chaining</h3>
<p>Each <code>.then()</code> returns another promise. This allows:</p>
<ul>
<li><p>sequential execution</p>
</li>
<li><p>passing results forward</p>
</li>
<li><p>avoiding deep nesting</p>
</li>
</ul>
<h3>Why Chaining Matters</h3>
<p>Without chaining:</p>
<ul>
<li><p>async code becomes messy</p>
</li>
<li><p>nested structures increase</p>
</li>
</ul>
<p>Chaining creates:</p>
<ul>
<li><p>cleaner flow</p>
</li>
<li><p>better readability</p>
</li>
<li><p>easier debugging</p>
</li>
</ul>
<h3>Error Propagation in Chaining</h3>
<p>One powerful thing about promises: Errors automatically travel down the chain. Example:</p>
<pre><code class="language-js">login()
  .then(getProfile)
  .then(getPosts)
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<p>If any step fails:</p>
<ul>
<li><code>.catch()</code> handles everything</li>
</ul>
<p>This is much cleaner than callback-based error handling.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners forget:</p>
<pre><code class="language-js">return
</code></pre>
<p>inside <code>.then()</code>.</p>
<p>Example:</p>
<pre><code class="language-js">.then((data) =&gt; {
  getUser(data);
})
</code></pre>
<p>Without <code>return</code>, chaining breaks.</p>
<p>Correct:</p>
<pre><code class="language-js">.then((data) =&gt; {
  return getUser(data);
})
</code></pre>
<h3>Another Common Confusion</h3>
<p>People think promises make code synchronous. They do not. Promises are still asynchronous. They simply provide:</p>
<ul>
<li><p>better structure</p>
</li>
<li><p>cleaner syntax</p>
</li>
<li><p>easier management</p>
</li>
</ul>
<h3>Promise vs Callback Thinking</h3>
<h4>Callback Style</h4>
<p>“Run this function later.”</p>
<h4>Promise Style</h4>
<p>“This operation will eventually produce a result.”</p>
<p>This mindset shift is important.</p>
<h3>Why Promises Became So Important</h3>
<p>Modern JavaScript depends heavily on promises. Especially:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>databases</p>
</li>
<li><p>fetch requests</p>
</li>
<li><p>authentication</p>
</li>
<li><p>async operations</p>
</li>
</ul>
<p>Even <code>async/await</code> internally works on top of promises.</p>
<h3>Final Understanding</h3>
<p>Promises were introduced to solve the readability and maintenance problems caused by nested callbacks. A promise represents a future result and can exist in three states:</p>
<ul>
<li><p>pending</p>
</li>
<li><p>fulfilled</p>
</li>
<li><p>rejected</p>
</li>
</ul>
<p>Using:</p>
<ul>
<li><p><code>.then()</code></p>
</li>
<li><p><code>.catch()</code></p>
</li>
</ul>
<p>developers can handle success and failure more cleanly. Promise chaining allows sequential asynchronous operations without deep nesting, making JavaScript code easier to read, debug, and maintain.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Promises Explained for Beginners]]></title><description><![CDATA[What Problem Promises Solve
JavaScript is asynchronous. This means some operations take time. Examples:

API calls

database queries

file handling

authentication

timers


Initially, JavaScript hand]]></description><link>https://javascriptwithshubham.hashnode.dev/javascript-promises-explained-for-beginners</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/javascript-promises-explained-for-beginners</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 13:12:43 GMT</pubDate><content:encoded><![CDATA[<h3>What Problem Promises Solve</h3>
<p>JavaScript is asynchronous. This means some operations take time. Examples:</p>
<ul>
<li><p>API calls</p>
</li>
<li><p>database queries</p>
</li>
<li><p>file handling</p>
</li>
<li><p>authentication</p>
</li>
<li><p>timers</p>
</li>
</ul>
<p>Initially, JavaScript handled asynchronous operations using callbacks.</p>
<p>Example:</p>
<pre><code class="language-js">fetchData(() =&gt; {
  getUser(() =&gt; {
    getPosts(() =&gt; {
      console.log("Done");
    });
  });
});
</code></pre>
<p>As applications became larger, this created a huge problem. Code became:</p>
<ul>
<li><p>deeply nested</p>
</li>
<li><p>difficult to read</p>
</li>
<li><p>difficult to debug</p>
</li>
<li><p>hard to maintain</p>
</li>
</ul>
<p>This problem became known as:</p>
<blockquote>
<p>Callback Hell</p>
</blockquote>
<p>Promises were introduced to solve this problem.</p>
<h3>Real Purpose of Promises</h3>
<p>Promises were created to:</p>
<ul>
<li><p>improve readability</p>
</li>
<li><p>simplify async flow</p>
</li>
<li><p>handle success and failure properly</p>
</li>
<li><p>reduce nested callbacks</p>
</li>
</ul>
<p>Instead of passing callbacks everywhere, promises provide a cleaner structure for asynchronous operations.</p>
<h3>What a Promise Actually Is</h3>
<p>A promise is simply:</p>
<blockquote>
<p>An object representing a future value.</p>
</blockquote>
<p>Think of it like:</p>
<ul>
<li><p>work has started</p>
</li>
<li><p>result is not available yet</p>
</li>
<li><p>eventually it will either succeed or fail</p>
</li>
</ul>
<p>That future result is managed by promises.</p>
<h3>Promise States</h3>
<p>Every promise has a state. There are mainly three states.</p>
<h3>1. Pending</h3>
<p>Initially, every promise starts in:</p>
<pre><code class="language-js">pending
</code></pre>
<p>This means:</p>
<blockquote>
<p>operation is still running.</p>
</blockquote>
<p>Example:</p>
<ul>
<li><p>API request still loading</p>
</li>
<li><p>file still reading</p>
</li>
<li><p>timer still waiting</p>
</li>
</ul>
<h3>2. Fulfilled</h3>
<p>If operation succeeds, promise becomes:</p>
<pre><code class="language-js">fulfilled
</code></pre>
<p>This means:</p>
<blockquote>
<p>task completed successfully.</p>
</blockquote>
<p>And promise returns a value.</p>
<h3>3. Rejected</h3>
<p>If something fails, promise becomes:</p>
<pre><code class="language-js">rejected
</code></pre>
<p>This means:</p>
<blockquote>
<p>operation failed.</p>
</blockquote>
<p>And promise returns an error.</p>
<h3>Visual Understanding of Promise States</h3>
<p>Think of ordering food online.</p>
<h4>Pending</h4>
<p>Food is being prepared.</p>
<h4>Fulfilled</h4>
<p>Food delivered successfully.</p>
<h4>Rejected</h4>
<p>Order failed or canceled.</p>
<p>That is basically how promises behave internally.</p>
<h3>Basic Promise Lifecycle</h3>
<p>A promise moves through states during execution.</p>
<p>Flow:</p>
<pre><code class="language-js">pending → fulfilled
</code></pre>
<p>or</p>
<pre><code class="language-js">pending → rejected
</code></pre>
<p>Once a promise changes state:</p>
<ul>
<li><p>it becomes final</p>
</li>
<li><p>it cannot change again</p>
</li>
</ul>
<h3>Creating a Basic Promise</h3>
<p>Example:</p>
<pre><code class="language-js">const promise = new Promise((resolve, reject) =&gt; {
  const success = true;

  if (success) {
    resolve("Data fetched");
  } else {
    reject("Something went wrong");
  }
});
</code></pre>
<h3>Understanding <code>resolve</code> and <code>reject</code></h3>
<p>Inside promises:</p>
<h4><code>resolve()</code></h4>
<p>Means:</p>
<blockquote>
<p>operation succeeded.</p>
</blockquote>
<h4><code>reject()</code></h4>
<p>Means:</p>
<blockquote>
<p>operation failed.</p>
</blockquote>
<h3>Handling Success and Failure</h3>
<p>Promises provide methods for handling outcomes.</p>
<h3>Using <code>.then()</code></h3>
<p><code>.then()</code> handles successful results.</p>
<p>Example:</p>
<pre><code class="language-js">promise.then((data) =&gt; {
  console.log(data);
});
</code></pre>
<p>If promise resolves:</p>
<ul>
<li><code>.then()</code> executes</li>
</ul>
<h3>Using <code>.catch()</code></h3>
<p><code>.catch()</code> handles errors.</p>
<p>Example:</p>
<pre><code class="language-js">promise.catch((error) =&gt; {
  console.log(error);
});
</code></pre>
<p>If promise rejects:</p>
<ul>
<li><code>.catch()</code> executes</li>
</ul>
<h3>Complete Example</h3>
<p>Example:</p>
<pre><code class="language-js">const promise = new Promise((resolve, reject) =&gt; {
  const success = true;

  if (success) {
    resolve("Data received");
  } else {
    reject("Error occurred");
  }
});

promise
  .then((data) =&gt; {
    console.log(data);
  })
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<h3>Promise Lifecycle in This Example</h3>
<ol>
<li><p>Promise starts as pending</p>
</li>
<li><p>Condition checks</p>
</li>
<li><p>If successful → resolve()</p>
</li>
<li><p>Promise becomes fulfilled</p>
</li>
<li><p><code>.then()</code> runs</p>
</li>
</ol>
<p>Or:</p>
<ol>
<li><p>reject() runs</p>
</li>
<li><p>Promise becomes rejected</p>
</li>
<li><p><code>.catch()</code> runs</p>
</li>
</ol>
<h3>Promise Chaining Concept</h3>
<p>One major advantage of promises is chaining. Instead of nested callbacks:</p>
<pre><code class="language-js">login(() =&gt; {
  getProfile(() =&gt; {
    getPosts(() =&gt; {
      console.log("Done");
    });
  });
});
</code></pre>
<p>Promises allow sequential flow.</p>
<p>Example:</p>
<pre><code class="language-js">login()
  .then((user) =&gt; {
    return getProfile(user);
  })
  .then((profile) =&gt; {
    return getPosts(profile);
  })
  .then((posts) =&gt; {
    console.log(posts);
  })
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<p>Much cleaner.</p>
<h3>Understanding Promise Chaining</h3>
<p>Each <code>.then()</code> returns another promise. This allows:</p>
<ul>
<li><p>sequential execution</p>
</li>
<li><p>passing results forward</p>
</li>
<li><p>avoiding deep nesting</p>
</li>
</ul>
<h3>Why Chaining Matters</h3>
<p>Without chaining:</p>
<ul>
<li><p>async code becomes messy</p>
</li>
<li><p>nested structures increase</p>
</li>
</ul>
<p>Chaining creates:</p>
<ul>
<li><p>cleaner flow</p>
</li>
<li><p>better readability</p>
</li>
<li><p>easier debugging</p>
</li>
</ul>
<h3>Error Propagation in Chaining</h3>
<p>One powerful thing about promises: Errors automatically travel down the chain. Example:</p>
<pre><code class="language-js">login()
  .then(getProfile)
  .then(getPosts)
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<p>If any step fails:</p>
<ul>
<li><code>.catch()</code> handles everything</li>
</ul>
<p>This is much cleaner than callback-based error handling.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners forget:</p>
<pre><code class="language-js">return
</code></pre>
<p>inside <code>.then()</code>.</p>
<p>Example:</p>
<pre><code class="language-js">.then((data) =&gt; {
  getUser(data);
})
</code></pre>
<p>Without <code>return</code>, chaining breaks.</p>
<p>Correct:</p>
<pre><code class="language-js">.then((data) =&gt; {
  return getUser(data);
})
</code></pre>
<h3>Another Common Confusion</h3>
<p>People think promises make code synchronous. They do not. Promises are still asynchronous. They simply provide:</p>
<ul>
<li><p>better structure</p>
</li>
<li><p>cleaner syntax</p>
</li>
<li><p>easier management</p>
</li>
</ul>
<h3>Promise vs Callback Thinking</h3>
<h4>Callback Style</h4>
<p>“Run this function later.”</p>
<h4>Promise Style</h4>
<p>“This operation will eventually produce a result.”</p>
<p>This mindset shift is important.</p>
<h3>Why Promises Became So Important</h3>
<p>Modern JavaScript depends heavily on promises. Especially:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>databases</p>
</li>
<li><p>fetch requests</p>
</li>
<li><p>authentication</p>
</li>
<li><p>async operations</p>
</li>
</ul>
<p>Even <code>async/await</code> internally works on top of promises.</p>
<h3>Final Understanding</h3>
<p>Promises were introduced to solve the readability and maintenance problems caused by nested callbacks. A promise represents a future result and can exist in three states:</p>
<ul>
<li><p>pending</p>
</li>
<li><p>fulfilled</p>
</li>
<li><p>rejected</p>
</li>
</ul>
<p>Using:</p>
<ul>
<li><p><code>.then()</code></p>
</li>
<li><p><code>.catch()</code></p>
</li>
</ul>
<p>developers can handle success and failure more cleanly. Promise chaining allows sequential asynchronous operations without deep nesting, making JavaScript code easier to read, debug, and maintain.</p>
]]></content:encoded></item><item><title><![CDATA[Destructuring in JavaScript]]></title><description><![CDATA[What Destructuring Means
In JavaScript, destructuring is a cleaner way to extract values from:

arrays

objects


Instead of accessing values manually again and again, destructuring allows us to unpac]]></description><link>https://javascriptwithshubham.hashnode.dev/destructuring-in-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/destructuring-in-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 13:09:06 GMT</pubDate><content:encoded><![CDATA[<h3>What Destructuring Means</h3>
<p>In JavaScript, destructuring is a cleaner way to extract values from:</p>
<ul>
<li><p>arrays</p>
</li>
<li><p>objects</p>
</li>
</ul>
<p>Instead of accessing values manually again and again, destructuring allows us to unpack values directly into variables. Without destructuring:</p>
<pre><code class="language-js">const user = {
  name: "Shubham",
  age: 21
};

const name = user.name;
const age = user.age;
</code></pre>
<p>With destructuring:</p>
<pre><code class="language-js">const user = {
  name: "Shubham",
  age: 21
};

const { name, age } = user;
</code></pre>
<p>Much cleaner. Much shorter.</p>
<h3>Real Purpose of Destructuring</h3>
<p>The main goal of destructuring is:</p>
<blockquote>
<p>Extract values easily and reduce repetitive code.</p>
</blockquote>
<p>Modern JavaScript applications work heavily with:</p>
<ul>
<li><p>API responses</p>
</li>
<li><p>objects</p>
</li>
<li><p>arrays</p>
</li>
<li><p>function parameters</p>
</li>
</ul>
<p>Destructuring makes handling this data easier.</p>
<h3>Destructuring Arrays</h3>
<p>Array destructuring extracts values based on position.</p>
<p>Example:</p>
<pre><code class="language-js">const numbers = [10, 20, 30];

const [a, b, c] = numbers;

console.log(a);
console.log(b);
console.log(c);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">10
20
30
</code></pre>
<h3>Understanding Array Destructuring</h3>
<p>JavaScript matches values by index.</p>
<p>Example:</p>
<pre><code class="language-js">const [first, second] = [100, 200];
</code></pre>
<p>Here:</p>
<ul>
<li><p>first variable gets first value</p>
</li>
<li><p>second variable gets second value</p>
</li>
</ul>
<h3>Skipping Values in Arrays</h3>
<p>Sometimes we only need specific values. Example:</p>
<pre><code class="language-js">const numbers = [10, 20, 30];

const [first, , third] = numbers;

console.log(first);
console.log(third);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">10
30
</code></pre>
<p>The middle value gets skipped.</p>
<h3>Using Rest Operator with Arrays</h3>
<p>Example:</p>
<pre><code class="language-js">const numbers = [1, 2, 3, 4];

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

console.log(first);
console.log(remaining);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">1
[2, 3, 4]
</code></pre>
<h3>Destructuring Objects</h3>
<p>Object destructuring extracts values based on property names. Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham",
  age: 21
};

const { name, age } = user;

console.log(name);
console.log(age);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Shubham
21
</code></pre>
<h3>Important Difference Between Array and Object Destructuring</h3>
<p>Arrays:</p>
<ul>
<li>work based on position</li>
</ul>
<p>Objects:</p>
<ul>
<li>work based on property names</li>
</ul>
<p>This is very important to remember.</p>
<h3>Renaming Variables During Destructuring</h3>
<p>Sometimes variable names need changing.</p>
<p>Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham"
};

const { name: username } = user;

console.log(username);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Shubham
</code></pre>
<p>Here:</p>
<ul>
<li><p>property is <code>name</code></p>
</li>
<li><p>variable becomes <code>username</code></p>
</li>
</ul>
<h3>Nested Object Destructuring</h3>
<p>Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham",
  address: {
    city: "Delhi"
  }
};

const {
  address: { city }
} = user;

console.log(city);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Delhi
</code></pre>
<h3>Default Values</h3>
<p>Sometimes properties or array values may not exist. Without default values:</p>
<pre><code class="language-js">const user = {
  name: "Shubham"
};

const { age } = user;

console.log(age);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">undefined
</code></pre>
<h3>Using Default Values</h3>
<p>Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham"
};

const { age = 18 } = user;

console.log(age);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">18
</code></pre>
<p>If value does not exist, default value is used.</p>
<h3>Default Values in Arrays</h3>
<p>Example:</p>
<pre><code class="language-js">const numbers = [10];

const [a, b = 20] = numbers;

console.log(a);
console.log(b);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">10
20
</code></pre>
<h3>Destructuring Function Parameters</h3>
<p>Very common in modern JavaScript. Example:</p>
<pre><code class="language-js">function greet({ name, age }) {
  console.log(`Name: ${name}`);
  console.log(`Age: ${age}`);
}

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

greet(user);
</code></pre>
<p>This is heavily used in:</p>
<ul>
<li><p>React</p>
</li>
<li><p>Node.js</p>
</li>
<li><p>APIs</p>
</li>
</ul>
<h3>Benefits of Destructuring</h3>
<p>Destructuring became extremely popular because it simplifies code significantly.</p>
<h3>Cleaner Syntax</h3>
<p>Instead of repetitive property access:</p>
<pre><code class="language-js">user.name
user.age
user.city
</code></pre>
<p>you can extract everything once.</p>
<h3>Better Readability</h3>
<p>Code becomes easier to understand because required values become immediately visible.</p>
<p>Example:</p>
<pre><code class="language-js">const { name, age } = user;
</code></pre>
<h3>Less Repetitive Code</h3>
<p>Without destructuring:</p>
<pre><code class="language-js">const name = user.name;
const age = user.age;
</code></pre>
<p>With destructuring:</p>
<pre><code class="language-js">const { name, age } = user;
</code></pre>
<p>Much shorter.</p>
<h3>Easier Handling of API Responses</h3>
<p>APIs often return large objects.</p>
<p>Example:</p>
<pre><code class="language-js">const response = {
  data: {
    user: {
      name: "Shubham"
    }
  }
};
</code></pre>
<p>Destructuring helps access nested data more cleanly.</p>
<h3>Common in Modern Frameworks</h3>
<p>Destructuring is used everywhere in:</p>
<ul>
<li><p>React props</p>
</li>
<li><p>Express request handling</p>
</li>
<li><p>API responses</p>
</li>
<li><p>hooks</p>
</li>
<li><p>state management</p>
</li>
</ul>
<h3>Common Beginner Mistake</h3>
<p>Many beginners confuse:</p>
<ul>
<li><p>array destructuring</p>
</li>
<li><p>object destructuring</p>
</li>
</ul>
<p>Remember:</p>
<h4>Arrays</h4>
<p>Use:</p>
<pre><code class="language-js">[]
</code></pre>
<p>because order matters.</p>
<h4>Objects</h4>
<p>Use:</p>
<pre><code class="language-js">{}
</code></pre>
<p>because property names matter.</p>
<h3>Another Common Mistake</h3>
<p>Beginners often try:</p>
<pre><code class="language-js">const { first } = [1, 2];
</code></pre>
<p>This does not work properly because arrays use positional destructuring, not property names.</p>
<p>Correct:</p>
<pre><code class="language-js">const [first] = [1, 2];
</code></pre>
<h3>Real Importance of Destructuring</h3>
<p>Destructuring is not just shorter syntax.</p>
<p>It improves:</p>
<ul>
<li><p>readability</p>
</li>
<li><p>maintainability</p>
</li>
<li><p>cleaner function parameters</p>
</li>
<li><p>handling of complex data structures</p>
</li>
</ul>
<p>And this becomes extremely important in large applications.</p>
<h3>Final Understanding</h3>
<p>Destructuring is a modern JavaScript feature used to extract values from arrays and objects easily. Array destructuring works based on position, while object destructuring works based on property names.</p>
<p>Features like:</p>
<ul>
<li><p>default values</p>
</li>
<li><p>nested destructuring</p>
</li>
<li><p>parameter destructuring</p>
</li>
</ul>
<p>make JavaScript code cleaner and more readable. And because modern applications work heavily with objects and arrays, destructuring has become one of the most commonly used features in modern JavaScript development.</p>
]]></content:encoded></item><item><title><![CDATA[Synchronous vs Asynchronous JavaScript]]></title><description><![CDATA[What Synchronous Code Means
Synchronous code means:

JavaScript executes one line at a time in sequence.

It waits for the current task to finish before moving to the next one. Example:
console.log("F]]></description><link>https://javascriptwithshubham.hashnode.dev/synchronous-vs-asynchronous-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/synchronous-vs-asynchronous-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 13:05:47 GMT</pubDate><content:encoded><![CDATA[<h3>What Synchronous Code Means</h3>
<p>Synchronous code means:</p>
<blockquote>
<p>JavaScript executes one line at a time in sequence.</p>
</blockquote>
<p>It waits for the current task to finish before moving to the next one. Example:</p>
<pre><code class="language-js">console.log("First");

console.log("Second");

console.log("Third");
</code></pre>
<p>Output:</p>
<pre><code class="language-js">First
Second
Third
</code></pre>
<p>JavaScript executes line by line in exact order. This is synchronous behavior.</p>
<h3>Understanding Synchronous Execution</h3>
<p>Think of synchronous code like standing in a queue. One person completes their work first. Only then the next person gets a turn. JavaScript behaves similarly in synchronous execution. It does not jump ahead. It waits.</p>
<h3>Why Synchronous Code Feels Simple</h3>
<p>Synchronous code is easy to:</p>
<ul>
<li><p>read</p>
</li>
<li><p>predict</p>
</li>
<li><p>debug</p>
</li>
</ul>
<p>Because execution flow is straightforward.<br />Line 1 runs first.<br />Then line 2.<br />Then line 3.<br />No surprises.</p>
<h3>Problem with Synchronous Behavior</h3>
<p>The issue starts when some operations take time. Example:</p>
<ul>
<li><p>API requests</p>
</li>
<li><p>database queries</p>
</li>
<li><p>file reading</p>
</li>
<li><p>timers</p>
</li>
<li><p>authentication</p>
</li>
</ul>
<p>If JavaScript waits for every slow operation, the entire application becomes blocked. And this creates performance problems.</p>
<h3>What Asynchronous Code Means</h3>
<p>Asynchronous code means:</p>
<blockquote>
<p>JavaScript can start a task and continue doing other work without waiting for it to finish.</p>
</blockquote>
<p>Instead of blocking execution, JavaScript handles slow tasks in the background. When the task completes, JavaScript processes the result later.</p>
<h3>Simple Async Example</h3>
<p>Example:</p>
<pre><code class="language-js">console.log("Start");

setTimeout(() =&gt; {
  console.log("Timer completed");
}, 2000);

console.log("End");
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Start
End
Timer completed
</code></pre>
<p>Even though timer appears in the middle, JavaScript does not wait for 2 seconds. It continues execution. This is asynchronous behavior.</p>
<h3>Understanding What Happened</h3>
<p>JavaScript saw:</p>
<pre><code class="language-js">setTimeout()
</code></pre>
<p>and said:</p>
<blockquote>
<p>“This task takes time.<br />I’ll continue running other code first.”</p>
</blockquote>
<p>So:</p>
<ul>
<li><p>timer starts</p>
</li>
<li><p>execution continues</p>
</li>
<li><p>callback runs later</p>
</li>
</ul>
<h3>Why JavaScript Needs Asynchronous Behavior</h3>
<p>JavaScript originally runs on a single thread. Meaning:</p>
<ul>
<li>one task executes at a time</li>
</ul>
<p>Now imagine if JavaScript blocked execution during:</p>
<ul>
<li><p>API calls</p>
</li>
<li><p>file handling</p>
</li>
<li><p>database requests</p>
</li>
</ul>
<p>The entire application would freeze while waiting. Users would experience:</p>
<ul>
<li><p>lag</p>
</li>
<li><p>freezing</p>
</li>
<li><p>slow responses</p>
</li>
</ul>
<p>Asynchronous behavior solves this problem.</p>
<h3>Real-World Thinking Process</h3>
<p>Imagine ordering food at a restaurant. You place your order. Now imagine standing at the counter for 30 minutes waiting for food before anyone else can order. That would be terrible. Instead:</p>
<ul>
<li><p>your order starts processing</p>
</li>
<li><p>you move aside</p>
</li>
<li><p>others continue ordering</p>
</li>
</ul>
<p>That is asynchronous behavior.</p>
<h3>Examples Like API Calls or Timers</h3>
<p>Some operations naturally take time. JavaScript handles these asynchronously.</p>
<h3>API Calls</h3>
<p>Example:</p>
<pre><code class="language-js">fetch("https://api.example.com/data")
  .then((response) =&gt; response.json())
  .then((data) =&gt; {
    console.log(data);
  });
</code></pre>
<p>Fetching data from servers takes time because:</p>
<ul>
<li><p>internet communication happens</p>
</li>
<li><p>server processes request</p>
</li>
<li><p>response travels back</p>
</li>
</ul>
<p>JavaScript cannot freeze entire application while waiting.</p>
<h3>Timers</h3>
<p>Example:</p>
<pre><code class="language-js">setTimeout(() =&gt; {
  console.log("Executed later");
}, 3000);
</code></pre>
<p>JavaScript schedules the task and continues execution.</p>
<h3>File Reading</h3>
<p>In Node.js:</p>
<pre><code class="language-js">fs.readFile("data.txt", "utf8", (err, data) =&gt; {
  console.log(data);
});
</code></pre>
<p>Reading files may take time. Async behavior prevents blocking the server.</p>
<h3>Database Operations</h3>
<p>Example:</p>
<pre><code class="language-js">db.users.find()
</code></pre>
<p>Database queries are also asynchronous because fetching data takes time.</p>
<h3>Problems That Occur with Blocking Code</h3>
<p>Blocking code means:</p>
<blockquote>
<p>JavaScript cannot move forward until the current task finishes.</p>
</blockquote>
<p>This creates major problems in real applications.</p>
<h3>Example of Blocking Behavior</h3>
<p>Example:</p>
<pre><code class="language-js">function longTask() {
  const start = Date.now();

  while (Date.now() - start &lt; 5000) {}
}

console.log("Start");

longTask();

console.log("End");
</code></pre>
<p>Output after 5 seconds:</p>
<pre><code class="language-js">Start
End
</code></pre>
<p>Here:</p>
<ul>
<li><p>JavaScript becomes stuck inside loop</p>
</li>
<li><p>nothing else can execute</p>
</li>
</ul>
<p>The application freezes temporarily.</p>
<h3>Why Blocking is Dangerous</h3>
<p>In real applications, blocking behavior causes:</p>
<ul>
<li><p>frozen UI</p>
</li>
<li><p>slow websites</p>
</li>
<li><p>delayed responses</p>
</li>
<li><p>poor user experience</p>
</li>
</ul>
<p>Especially in backend servers, blocking can affect thousands of users.</p>
<h3>Node.js and Non-Blocking Behavior</h3>
<p>One major reason Node.js became popular is:</p>
<blockquote>
<p>Non-blocking asynchronous architecture.</p>
</blockquote>
<p>Instead of waiting for one user request to finish, Node.js continues handling other users simultaneously.</p>
<p>This improves:</p>
<ul>
<li><p>scalability</p>
</li>
<li><p>performance</p>
</li>
<li><p>responsiveness</p>
</li>
</ul>
<h3>Understanding JavaScript's Nature</h3>
<p>JavaScript itself is single-threaded. But environments like:</p>
<ul>
<li><p>browsers</p>
</li>
<li><p>Node.js</p>
</li>
</ul>
<p>provide asynchronous features through:</p>
<ul>
<li><p>Web APIs</p>
</li>
<li><p>Event Loop</p>
</li>
<li><p>Callback Queue</p>
</li>
</ul>
<p>This allows JavaScript to behave asynchronously even though it executes code on a single thread.</p>
<h3>Common Beginner Confusion</h3>
<p>Many beginners think async code runs at the exact same time as synchronous code.</p>
<p>Not exactly.</p>
<p>JavaScript still executes one thing at a time.</p>
<p>The difference is:</p>
<ul>
<li><p>slow tasks are delegated temporarily</p>
</li>
<li><p>execution continues elsewhere</p>
</li>
<li><p>completed tasks return later</p>
</li>
</ul>
<h3>Another Common Confusion</h3>
<p>People think:</p>
<pre><code class="language-js">setTimeout(fn, 0)
</code></pre>
<p>runs instantly.</p>
<p>Wrong.</p>
<p>Even with 0 delay:</p>
<ul>
<li><p>callback still enters callback queue</p>
</li>
<li><p>synchronous code finishes first</p>
</li>
</ul>
<h3>Why Async Programming Became Essential</h3>
<p>Modern applications constantly depend on:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>databases</p>
</li>
<li><p>authentication systems</p>
</li>
<li><p>external services</p>
</li>
<li><p>file handling</p>
</li>
</ul>
<p>Without asynchronous programming:</p>
<ul>
<li><p>applications would feel extremely slow</p>
</li>
<li><p>servers would become inefficient</p>
</li>
<li><p>user experience would suffer badly</p>
</li>
</ul>
<h3>Final Understanding</h3>
<p>Synchronous code executes line by line and waits for each task to complete before moving ahead. Asynchronous code allows JavaScript to handle slow operations without blocking execution. JavaScript needs asynchronous behavior because operations like:</p>
<ul>
<li><p>API calls</p>
</li>
<li><p>timers</p>
</li>
<li><p>database queries</p>
</li>
<li><p>file handling</p>
</li>
</ul>
<p>take time. Without async programming, applications would freeze and become inefficient. And understanding the difference between synchronous and asynchronous behavior is one of the most important foundations of modern JavaScript development.</p>
]]></content:encoded></item><item><title><![CDATA[Error Handling in JavaScript: Try, Catch, Finally]]></title><description><![CDATA[What Errors Are in JavaScript
Errors are situations where JavaScript cannot execute code properly. Example:
console.log(userName);

Output:
ReferenceError: userName is not defined

Because the variabl]]></description><link>https://javascriptwithshubham.hashnode.dev/error-handling-in-javascript-try-catch-finally</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/error-handling-in-javascript-try-catch-finally</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 13:01:19 GMT</pubDate><content:encoded><![CDATA[<h3>What Errors Are in JavaScript</h3>
<p>Errors are situations where JavaScript cannot execute code properly. Example:</p>
<pre><code class="language-js">console.log(userName);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">ReferenceError: userName is not defined
</code></pre>
<p>Because the variable does not exist. Errors are a normal part of programming. Every developer faces:</p>
<ul>
<li><p>syntax errors</p>
</li>
<li><p>logic errors</p>
</li>
<li><p>runtime errors</p>
</li>
<li><p>API errors</p>
</li>
<li><p>network errors</p>
</li>
</ul>
<p>The important thing is not avoiding all errors. The important thing is:</p>
<blockquote>
<p>Handling errors properly.</p>
</blockquote>
<h3>Why Errors Happen</h3>
<p>Errors can happen for many reasons. Examples:</p>
<ul>
<li><p>wrong variable names</p>
</li>
<li><p>invalid user input</p>
</li>
<li><p>failed API requests</p>
</li>
<li><p>database failures</p>
</li>
<li><p>undefined values</p>
</li>
<li><p>incorrect logic</p>
</li>
</ul>
<p>Real applications constantly deal with unexpected situations. So JavaScript provides mechanisms to handle them safely.</p>
<h3>Understanding Runtime Errors</h3>
<p>Some errors happen during execution.</p>
<p>Example:</p>
<pre><code class="language-js">const user = undefined;

console.log(user.name);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">TypeError
</code></pre>
<p>Because:</p>
<ul>
<li><p>JavaScript tries accessing <code>name</code></p>
</li>
<li><p>but <code>user</code> is undefined</p>
</li>
</ul>
<p>Without proper handling, the application may crash.</p>
<h3>Using <code>try</code> and <code>catch</code> Blocks</h3>
<p>JavaScript provides:</p>
<pre><code class="language-js">try...catch
</code></pre>
<p>for handling errors safely.</p>
<p>Basic syntax:</p>
<pre><code class="language-js">try {
  // risky code
} catch (error) {
  // handle error
}
</code></pre>
<h3>How <code>try</code> Works</h3>
<p>Inside <code>try</code> block:</p>
<ul>
<li>JavaScript attempts executing code</li>
</ul>
<p>If no error happens:</p>
<ul>
<li>code runs normally</li>
</ul>
<p>If an error occurs:</p>
<ul>
<li>execution jumps to <code>catch</code></li>
</ul>
<h3>Example of <code>try...catch</code></h3>
<p>Example:</p>
<pre><code class="language-js">try {
  console.log(userName);
} catch (error) {
  console.log("Something went wrong");
}
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Something went wrong
</code></pre>
<p>Instead of crashing the application.</p>
<h3>Understanding the <code>error</code> Object</h3>
<p>Inside <code>catch</code>:</p>
<pre><code class="language-js">catch (error)
</code></pre>
<p>JavaScript provides an error object.</p>
<p>Example:</p>
<pre><code class="language-js">try {
  console.log(userName);
} catch (error) {
  console.log(error);
}
</code></pre>
<p>Output:</p>
<pre><code class="language-js">ReferenceError: userName is not defined
</code></pre>
<p>This object contains useful debugging information.</p>
<h3>Common Error Properties</h3>
<p>Example:</p>
<pre><code class="language-js">console.log(error.name);
console.log(error.message);
</code></pre>
<p>Possible output:</p>
<pre><code class="language-js">ReferenceError
userName is not defined
</code></pre>
<p>This helps developers identify problems faster.</p>
<h3>The <code>finally</code> Block</h3>
<p>JavaScript also provides:</p>
<pre><code class="language-js">finally
</code></pre>
<p>Syntax:</p>
<pre><code class="language-js">try {

} catch (error) {

} finally {

}
</code></pre>
<p>The <code>finally</code> block always runs.</p>
<p>Whether:</p>
<ul>
<li><p>error happens</p>
</li>
<li><p>or no error happens</p>
</li>
</ul>
<h3>Example of <code>finally</code></h3>
<p>Example:</p>
<pre><code class="language-js">try {
  console.log("Running code");
} catch (error) {
  console.log(error);
} finally {
  console.log("This always runs");
}
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Running code
This always runs
</code></pre>
<h3>Why <code>finally</code> is Useful</h3>
<p><code>finally</code> is commonly used for:</p>
<ul>
<li><p>closing database connections</p>
</li>
<li><p>stopping loaders</p>
</li>
<li><p>cleaning resources</p>
</li>
<li><p>removing temporary files</p>
</li>
</ul>
<p>Basically:</p>
<blockquote>
<p>cleanup tasks.</p>
</blockquote>
<h3>Throwing Custom Errors</h3>
<p>JavaScript also allows developers to create their own errors. Using:</p>
<pre><code class="language-js">throw
</code></pre>
<h3>Simple Custom Error Example</h3>
<p>Example:</p>
<pre><code class="language-js">function checkAge(age) {
  if (age &lt; 18) {
    throw new Error("Age must be 18 or above");
  }

  console.log("Access granted");
}

checkAge(15);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Error: Age must be 18 or above
</code></pre>
<h3>Understanding <code>throw</code></h3>
<p><code>throw</code> manually creates an error. This allows developers to:</p>
<ul>
<li><p>validate data</p>
</li>
<li><p>stop invalid operations</p>
</li>
<li><p>enforce rules</p>
</li>
</ul>
<h3>Real Use Case of Custom Errors</h3>
<p>Example:</p>
<ul>
<li><p>invalid login</p>
</li>
<li><p>weak password</p>
</li>
<li><p>payment failure</p>
</li>
<li><p>unauthorized access</p>
</li>
</ul>
<p>Instead of silently failing, developers can throw meaningful errors.</p>
<h3>Example with <code>try...catch</code></h3>
<p>Example:</p>
<pre><code class="language-js">function divide(a, b) {
  if (b === 0) {
    throw new Error("Cannot divide by zero");
  }

  return a / b;
}

try {
  console.log(divide(10, 0));
} catch (error) {
  console.log(error.message);
}
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Cannot divide by zero
</code></pre>
<h3>Why Error Handling Matters</h3>
<p>Without proper error handling:</p>
<ul>
<li><p>applications crash</p>
</li>
<li><p>users get blank screens</p>
</li>
<li><p>debugging becomes difficult</p>
</li>
<li><p>unexpected behavior increases</p>
</li>
</ul>
<p>Error handling improves:</p>
<ul>
<li><p>application stability</p>
</li>
<li><p>debugging</p>
</li>
<li><p>user experience</p>
</li>
<li><p>security</p>
</li>
</ul>
<h3>Real World Importance</h3>
<p>Imagine:</p>
<ul>
<li><p>payment systems crashing silently</p>
</li>
<li><p>banking apps failing without messages</p>
</li>
<li><p>login systems breaking without explanation</p>
</li>
</ul>
<p>That would create terrible user experience. Good applications handle failures gracefully.</p>
<h3>Error Handling in APIs</h3>
<p>Example:</p>
<pre><code class="language-js">try {
  const response = await fetch(url);
} catch (error) {
  console.log("API request failed");
}
</code></pre>
<p>Because:</p>
<ul>
<li><p>internet may fail</p>
</li>
<li><p>server may go down</p>
</li>
<li><p>timeout may occur</p>
</li>
</ul>
<p>Real applications must expect failures.</p>
<h3>Common Types of JavaScript Errors</h3>
<p>Some common error types:<br />SyntaxError Invalid JavaScript syntax.</p>
<h4>ReferenceError</h4>
<p>Using variables that do not exist.</p>
<h4>TypeError</h4>
<p>Using values incorrectly.</p>
<p>Example:</p>
<pre><code class="language-js">undefined.name
</code></pre>
<h4>RangeError</h4>
<p>Value goes outside allowed range.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners write:</p>
<pre><code class="language-js">try {
  riskyCode();
}
</code></pre>
<p>without proper <code>catch</code>.</p>
<p>But:</p>
<ul>
<li><p>errors should be handled meaningfully</p>
</li>
<li><p>not ignored silently</p>
</li>
</ul>
<p>Good error messages make debugging easier.</p>
<h3>Another Common Mistake</h3>
<p>Some beginners overuse <code>try...catch</code>. Not every line needs it.</p>
<p>Usually:</p>
<ul>
<li><p>risky operations</p>
</li>
<li><p>async operations</p>
</li>
<li><p>external data handling</p>
</li>
</ul>
<p>are the most common places for error handling.</p>
<h3>Understanding the Bigger Picture</h3>
<p>Error handling is not just about preventing crashes. It is about:</p>
<ul>
<li><p>creating reliable applications</p>
</li>
<li><p>improving debugging</p>
</li>
<li><p>handling failures gracefully</p>
</li>
<li><p>protecting user experience</p>
</li>
</ul>
<p>Professional applications are built assuming:</p>
<blockquote>
<p>things can fail anytime.</p>
</blockquote>
<h3>Final Understanding</h3>
<p>Errors are situations where JavaScript cannot execute code properly.</p>
<p>Using:</p>
<ul>
<li><p><code>try</code></p>
</li>
<li><p><code>catch</code></p>
</li>
<li><p><code>finally</code></p>
</li>
<li><p><code>throw</code></p>
</li>
</ul>
<p>developers can safely handle failures and control application behavior. <code>try...catch</code> prevents applications from crashing unexpectedly, while custom errors help enforce rules and validations. And proper error handling is one of the biggest differences between beginner-level code and production-ready applications.</p>
]]></content:encoded></item><item><title><![CDATA[Async/Await in JavaScript: Writing Cleaner Asynchronous Code]]></title><description><![CDATA[Why async/await Was Introduced
JavaScript originally handled asynchronous operations using callbacks. Example:
fetchData(() => {
  getUser(() => {
    getPosts(() => {
      console.log("Done");
    }]]></description><link>https://javascriptwithshubham.hashnode.dev/async-await-in-javascript-writing-cleaner-asynchronous-code</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/async-await-in-javascript-writing-cleaner-asynchronous-code</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 12:54:20 GMT</pubDate><content:encoded><![CDATA[<h3>Why <code>async/await</code> Was Introduced</h3>
<p>JavaScript originally handled asynchronous operations using callbacks. Example:</p>
<pre><code class="language-js">fetchData(() =&gt; {
  getUser(() =&gt; {
    getPosts(() =&gt; {
      console.log("Done");
    });
  });
});
</code></pre>
<p>As applications became larger, this structure became difficult to:</p>
<ul>
<li><p>read</p>
</li>
<li><p>debug</p>
</li>
<li><p>maintain</p>
</li>
</ul>
<p>This problem became known as:</p>
<blockquote>
<p>Callback Hell</p>
</blockquote>
<p>Then promises were introduced. Promises improved async code significantly.</p>
<p>Example:</p>
<pre><code class="language-js">fetchData()
  .then((data) =&gt; {
    return getUser(data);
  })
  .then((user) =&gt; {
    return getPosts(user);
  })
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<p>This was much cleaner than nested callbacks. But large promise chains still became difficult to manage. That’s why JavaScript introduced:</p>
<pre><code class="language-js">async/await
</code></pre>
<p>to make asynchronous code look more like normal synchronous code.</p>
<h3>Real Purpose of <code>async/await</code></h3>
<p>The goal was simple:</p>
<blockquote>
<p>Make asynchronous code easier for humans to read.</p>
</blockquote>
<p>Instead of deeply chained <code>.then()</code> blocks, developers could now write code that feels natural and sequential.</p>
<h3>How Async Functions Work</h3>
<p>An async function is created using:</p>
<pre><code class="language-js">async
</code></pre>
<p>keyword.</p>
<p>Example:</p>
<pre><code class="language-js">async function test() {
  return "Hello";
}
</code></pre>
<h3>Important Thing About Async Functions</h3>
<p>An async function always returns a promise. Even if you return a normal value. Example:</p>
<pre><code class="language-js">async function test() {
  return "Hello";
}

console.log(test());
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Promise { "Hello" }
</code></pre>
<p>JavaScript automatically wraps the return value inside a promise.</p>
<h3>Async Function Example</h3>
<p>Example:</p>
<pre><code class="language-js">async function fetchData() {
  return "Data received";
}

fetchData().then((data) =&gt; {
  console.log(data);
});
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Data received
</code></pre>
<h3>Understanding the <code>await</code> Keyword</h3>
<p><code>await</code> is used inside async functions. Its purpose is:</p>
<blockquote>
<p>Pause execution until a promise resolves.</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-js">function getData() {
  return new Promise((resolve) =&gt; {
    setTimeout(() =&gt; {
      resolve("Data received");
    }, 2000);
  });
}

async function fetchData() {
  const result = await getData();

  console.log(result);
}

fetchData();
</code></pre>
<p>Output after 2 seconds:</p>
<pre><code class="language-js">Data received
</code></pre>
<h3>What <code>await</code> Actually Does</h3>
<p>Without <code>await</code>, JavaScript continues immediately. But with:</p>
<pre><code class="language-js">await promise
</code></pre>
<p>JavaScript waits for the promise to complete before moving ahead. This creates code that feels synchronous while still being asynchronous internally.</p>
<h3>Thinking Process Behind <code>await</code></h3>
<p>Instead of:</p>
<pre><code class="language-js">promise.then((data) =&gt; {
  console.log(data);
});
</code></pre>
<p>you can now write:</p>
<pre><code class="language-js">const data = await promise;
</code></pre>
<p>Much cleaner. Much easier to read.</p>
<h3>Sequential Async Flow</h3>
<p>Example:</p>
<pre><code class="language-js">async function process() {
  const user = await getUser();

  const posts = await getPosts(user);

  console.log(posts);
}
</code></pre>
<p>This looks almost like normal synchronous code. But internally promises are still being used.</p>
<h3>Error Handling with Async Code</h3>
<p>One of the biggest advantages of <code>async/await</code> is cleaner error handling. With promises:</p>
<pre><code class="language-js">fetchData()
  .then((data) =&gt; {
    console.log(data);
  })
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<p>With async/await:</p>
<pre><code class="language-js">async function fetchData() {
  try {
    const data = await getData();

    console.log(data);
  } catch (error) {
    console.log(error);
  }
}
</code></pre>
<h3>Why <code>try...catch</code> Feels Better</h3>
<p><code>try...catch</code> makes async code easier to understand because:</p>
<ul>
<li><p>success logic stays together</p>
</li>
<li><p>error logic stays together</p>
</li>
<li><p>nested <code>.catch()</code> chains reduce</p>
</li>
</ul>
<p>This feels much more natural for developers.</p>
<h3>What Happens if Promise Rejects</h3>
<p>If awaited promise fails:</p>
<pre><code class="language-js">await somePromise
</code></pre>
<p>JavaScript throws an error.</p>
<p>And that error gets caught inside:</p>
<pre><code class="language-js">catch
</code></pre>
<p>block.</p>
<h3>Comparison with Promises</h3>
<p>Both promises and async/await solve asynchronous programming. But syntax and readability differ.</p>
<h3>Promise Style</h3>
<p>Example:</p>
<pre><code class="language-js">getUser()
  .then((user) =&gt; {
    return getPosts(user);
  })
  .then((posts) =&gt; {
    console.log(posts);
  })
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<h3>Async/Await Style</h3>
<p>Example:</p>
<pre><code class="language-js">async function fetchPosts() {
  try {
    const user = await getUser();

    const posts = await getPosts(user);

    console.log(posts);
  } catch (error) {
    console.log(error);
  }
}
</code></pre>
<h3>Why Developers Prefer Async/Await</h3>
<p>Because it:</p>
<ul>
<li><p>looks cleaner</p>
</li>
<li><p>feels synchronous</p>
</li>
<li><p>improves readability</p>
</li>
<li><p>simplifies debugging</p>
</li>
<li><p>makes error handling easier</p>
</li>
</ul>
<p>Especially in large applications.</p>
<h3>Important Understanding</h3>
<p><code>async/await</code> does NOT replace promises. It is built on top of promises.<br />Internally:</p>
<ul>
<li><p>async functions return promises</p>
</li>
<li><p>await waits for promises</p>
</li>
</ul>
<p>So promises still exist underneath.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners try using <code>await</code> outside async functions. Example:</p>
<pre><code class="language-js">const data = await getData();
</code></pre>
<p>This causes an error. Because <code>await</code> only works:</p>
<ul>
<li><p>inside async functions</p>
</li>
<li><p>or top-level modules in modern environments</p>
</li>
</ul>
<h3>Another Common Confusion</h3>
<p>People think <code>await</code> blocks the entire JavaScript engine. It does not. It only pauses execution inside that async function. Other JavaScript code can still continue running.</p>
<h3>Real Benefit of Async/Await</h3>
<p>The biggest achievement of async/await is not performance. It is:</p>
<blockquote>
<p>readability and maintainability.</p>
</blockquote>
<p>Developers can now write asynchronous code in a way that feels much closer to normal programming logic.</p>
<h3>Final Understanding</h3>
<p><code>async/await</code> was introduced to simplify asynchronous programming in JavaScript.<br />Async functions automatically return promises, and <code>await</code> pauses execution until promises resolve.<br />Compared to promise chains, async/await provides:</p>
<ul>
<li><p>cleaner syntax</p>
</li>
<li><p>easier debugging</p>
</li>
<li><p>better readability</p>
</li>
<li><p>simpler error handling</p>
</li>
</ul>
<p>And even though the syntax looks synchronous, promises are still working internally behind the scenes.</p>
]]></content:encoded></item><item><title><![CDATA[Spread vs Rest Operators in JavaScript]]></title><description><![CDATA[What Spread Operator Does
The spread operator is one of the most useful modern JavaScript features. It is written using three dots:
...

The main purpose of spread is:

Expanding values from arrays, o]]></description><link>https://javascriptwithshubham.hashnode.dev/spread-vs-rest-operators-in-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/spread-vs-rest-operators-in-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 12:50:27 GMT</pubDate><content:encoded><![CDATA[<h3>What Spread Operator Does</h3>
<p>The spread operator is one of the most useful modern JavaScript features. It is written using three dots:</p>
<pre><code class="language-js">...
</code></pre>
<p>The main purpose of spread is:</p>
<blockquote>
<p>Expanding values from arrays, objects, or iterables.</p>
</blockquote>
<p>Think of spread like this:</p>
<blockquote>
<p>“Open everything and spread the values out.”</p>
</blockquote>
<h3>Understanding Spread with Arrays</h3>
<p>Example:</p>
<pre><code class="language-js">const numbers = [1, 2, 3];

console.log(...numbers);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">1 2 3
</code></pre>
<p>Instead of printing the whole array structure, spread extracts individual values.</p>
<h3>Why Spread Became Important</h3>
<p>Before spread operator, tasks like:</p>
<ul>
<li><p>copying arrays</p>
</li>
<li><p>merging arrays</p>
</li>
<li><p>cloning objects</p>
</li>
</ul>
<p>were more complicated. Spread made these operations cleaner and easier to read.</p>
<h3>Copying Arrays with Spread</h3>
<p>Example:</p>
<pre><code class="language-js">const arr1 = [1, 2, 3];

const arr2 = [...arr1];

console.log(arr2);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[1, 2, 3]
</code></pre>
<p>This creates a new copied array.</p>
<h3>Merging Arrays</h3>
<p>Example:</p>
<pre><code class="language-js">const arr1 = [1, 2];
const arr2 = [3, 4];

const result = [...arr1, ...arr2];

console.log(result);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[1, 2, 3, 4]
</code></pre>
<p>This is much cleaner compared to older methods.</p>
<h3>Using Spread with Objects</h3>
<p>Spread also works with objects.</p>
<p>Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham",
  age: 21
};

const copiedUser = {
  ...user
};

console.log(copiedUser);
</code></pre>
<h3>Merging Objects</h3>
<p>Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham"
};

const details = {
  city: "Delhi"
};

const finalUser = {
  ...user,
  ...details
};

console.log(finalUser);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">{
  name: "Shubham",
  city: "Delhi"
}
</code></pre>
<h3>Real Meaning of Spread</h3>
<p>Spread takes grouped data and expands it into individual values.</p>
<p>Example:</p>
<pre><code class="language-js">[1, 2, 3]
</code></pre>
<p>becomes:</p>
<pre><code class="language-js">1, 2, 3
</code></pre>
<p>This is the core idea behind spread operator.</p>
<h3>What Rest Operator Does</h3>
<p>The rest operator also uses:</p>
<pre><code class="language-js">...
</code></pre>
<p>But its behavior is completely different. Instead of expanding values, rest does the opposite.</p>
<p>Rest means:</p>
<blockquote>
<p>Collect remaining values into one structure.</p>
</blockquote>
<p>Think of it like:</p>
<blockquote>
<p>“Take everything left and pack it together.”</p>
</blockquote>
<h3>Rest Operator Example</h3>
<p>Example:</p>
<pre><code class="language-js">function sum(...numbers) {
  console.log(numbers);
}

sum(1, 2, 3, 4);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[1, 2, 3, 4]
</code></pre>
<p>Here:</p>
<ul>
<li><p>all arguments are collected into an array</p>
</li>
<li><p><code>numbers</code> becomes an array</p>
</li>
</ul>
<h3>Why Rest Operator is Useful</h3>
<p>Sometimes we do not know:</p>
<ul>
<li><p>how many arguments will come</p>
</li>
<li><p>how much data needs handling</p>
</li>
</ul>
<p>Rest operator solves this problem.</p>
<h3>Rest with Array Destructuring</h3>
<p>Example:</p>
<pre><code class="language-js">const numbers = [1, 2, 3, 4];

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

console.log(first);
console.log(remaining);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">1
[2, 3, 4]
</code></pre>
<p>Here:</p>
<ul>
<li><p>first value gets separated</p>
</li>
<li><p>remaining values get collected</p>
</li>
</ul>
<h3>Rest with Objects</h3>
<p>Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham",
  age: 21,
  city: "Delhi"
};

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

console.log(others);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">{
  age: 21,
  city: "Delhi"
}
</code></pre>
<h3>Differences Between Spread and Rest</h3>
<p>Both use:</p>
<pre><code class="language-js">...
</code></pre>
<p>But the purpose changes depending on usage.</p>
<h3>Spread Operator</h3>
<p>Spread:</p>
<blockquote>
<p>Expands values.</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-js">const arr = [1, 2, 3];

console.log(...arr);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">1 2 3
</code></pre>
<h3>Rest Operator</h3>
<p>Rest:</p>
<blockquote>
<p>Collects values.</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-js">function test(...args) {
  console.log(args);
}
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[all values]
</code></pre>
<h3>Simple Way to Remember</h3>
<h4>Spread</h4>
<blockquote>
<p>Break things apart.</p>
</blockquote>
<h4>Rest</h4>
<blockquote>
<p>Collect things together.</p>
</blockquote>
<h3>Using Spread with Arrays and Objects</h3>
<p>Spread is heavily used in modern JavaScript frameworks like React.</p>
<p>Because immutable updates are very important.</p>
<h3>Updating Arrays</h3>
<p>Example:</p>
<pre><code class="language-js">const numbers = [1, 2, 3];

const updated = [...numbers, 4];

console.log(updated);
</code></pre>
<p>Instead of modifying original array directly.</p>
<h3>Updating Objects</h3>
<p>Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham"
};

const updatedUser = {
  ...user,
  age: 21
};
</code></pre>
<p>This creates a new updated object.</p>
<h3>Overriding Values</h3>
<p>Example:</p>
<pre><code class="language-js">const user = {
  name: "Shubham",
  age: 20
};

const updatedUser = {
  ...user,
  age: 21
};

console.log(updatedUser);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">{
  name: "Shubham",
  age: 21
}
</code></pre>
<p>Later values overwrite earlier ones.</p>
<h3>Practical Use Cases</h3>
<p>Spread and rest operators are used almost everywhere in modern JavaScript.</p>
<h3>Function Arguments</h3>
<p>Example:</p>
<pre><code class="language-js">const numbers = [1, 2, 3];

function add(a, b, c) {
  return a + b + c;
}

console.log(add(...numbers));
</code></pre>
<p>Spread converts array into separate arguments.</p>
<h3>Copying Data Safely</h3>
<p>Example:</p>
<pre><code class="language-js">const arr1 = [1, 2];

const arr2 = [...arr1];
</code></pre>
<p>This avoids accidental mutation.</p>
<h3>React State Updates</h3>
<p>Very common in React:</p>
<pre><code class="language-js">setUser({
  ...user,
  age: 21
});
</code></pre>
<p>Because React prefers immutable updates.</p>
<h3>Collecting Dynamic Arguments</h3>
<p>Example:</p>
<pre><code class="language-js">function logger(...messages) {
  console.log(messages);
}
</code></pre>
<p>Useful when number of arguments is unknown.</p>
<h3>Common Beginner Confusion</h3>
<p>Many beginners think spread and rest are different symbols.</p>
<p>But both use the exact same syntax:</p>
<pre><code class="language-js">...
</code></pre>
<p>Difference depends on context.</p>
<h3>Easy Trick to Identify</h3>
<p>If values are being:</p>
<ul>
<li><p>expanded → spread</p>
</li>
<li><p>collected → rest</p>
</li>
</ul>
<p>That’s the easiest way to identify them.</p>
<h3>Final Understanding</h3>
<p>The spread operator expands arrays, objects, or iterables into individual values. The rest operator collects multiple values into a single structure. Even though both use:</p>
<pre><code class="language-js">...
</code></pre>
<p>their behavior depends entirely on context. Spread is mainly used for:</p>
<ul>
<li><p>copying</p>
</li>
<li><p>merging</p>
</li>
<li><p>expanding data</p>
</li>
</ul>
<p>Rest is mainly used for:</p>
<ul>
<li><p>collecting arguments</p>
</li>
<li><p>destructuring remaining values</p>
</li>
</ul>
<p>And understanding these operators properly is extremely important for writing modern JavaScript and React applications.</p>
]]></content:encoded></item><item><title><![CDATA[String Polyfills and Common Interview Methods in JavaScript]]></title><description><![CDATA[What String Methods Are
Strings are one of the most commonly used data types in JavaScript.Example:
const name = "JavaScript";

But strings are not just plain text. JavaScript provides many built-in m]]></description><link>https://javascriptwithshubham.hashnode.dev/string-polyfills-and-common-interview-methods-in-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/string-polyfills-and-common-interview-methods-in-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 12:47:07 GMT</pubDate><content:encoded><![CDATA[<h3>What String Methods Are</h3>
<p>Strings are one of the most commonly used data types in JavaScript.<br />Example:</p>
<pre><code class="language-js">const name = "JavaScript";
</code></pre>
<p>But strings are not just plain text. JavaScript provides many built-in methods to work with strings easily. Example:</p>
<pre><code class="language-js">const text = "hello";

console.log(text.toUpperCase());
</code></pre>
<p>Output:</p>
<pre><code class="language-js">HELLO
</code></pre>
<p>Here:</p>
<ul>
<li><code>toUpperCase()</code> is a string method</li>
</ul>
<p>String methods are basically:</p>
<blockquote>
<p>built-in functions that help manipulate strings.</p>
</blockquote>
<hr />
<h3>Common String Methods</h3>
<p>JavaScript provides many useful string methods. Examples:</p>
<pre><code class="language-js">length
toUpperCase()
toLowerCase()
slice()
substring()
includes()
replace()
trim()
split()
</code></pre>
<p>These methods help developers:</p>
<ul>
<li><p>modify text</p>
</li>
<li><p>search values</p>
</li>
<li><p>format data</p>
</li>
<li><p>validate input</p>
</li>
<li><p>process user information</p>
</li>
</ul>
<h3>Why String Methods Matter So Much</h3>
<p>Almost every real application works with strings. For example:</p>
<ul>
<li><p>usernames</p>
</li>
<li><p>passwords</p>
</li>
<li><p>emails</p>
</li>
<li><p>search queries</p>
</li>
<li><p>API responses</p>
</li>
<li><p>form validation</p>
</li>
</ul>
<p>Without string methods, handling text data would become extremely difficult.</p>
<h3>Why Developers Write Polyfills</h3>
<p>Modern JavaScript methods were not always available in older browsers. For example:</p>
<ul>
<li><p>older browsers may not support <code>includes()</code></p>
</li>
<li><p>some methods behave differently in different environments</p>
</li>
</ul>
<p>This created compatibility problems. To solve this, developers started creating polyfills. A polyfill is basically:</p>
<blockquote>
<p>A custom implementation of a built-in feature.</p>
</blockquote>
<p>It acts as a fallback if the environment does not support that feature.</p>
<h3>Real Thinking Behind Polyfills</h3>
<p>Suppose a browser does not support:</p>
<pre><code class="language-js">includes()
</code></pre>
<p>Instead of breaking the application, developers manually recreate the same functionality. That custom implementation is called a polyfill.</p>
<h3>Simple Polyfill Example</h3>
<p>Suppose we want to create our own version of <code>includes()</code>. Example:</p>
<pre><code class="language-js">String.prototype.myIncludes = function (word) {
  return this.indexOf(word) !== -1;
};

const text = "JavaScript";

console.log(text.myIncludes("Script"));
</code></pre>
<p>Output:</p>
<pre><code class="language-js">true
</code></pre>
<h3>Understanding What Happened</h3>
<pre><code class="language-js">String.prototype
</code></pre>
<p>means:</p>
<blockquote>
<p>adding a custom method to all strings.</p>
</blockquote>
<p>So now every string gets access to:</p>
<pre><code class="language-js">myIncludes()
</code></pre>
<p>Just like built-in methods.</p>
<h3>Implementing Simple String Utilities</h3>
<p>Many interview questions ask developers to create utility functions manually.</p>
<p>Because interviewers want to test:</p>
<ul>
<li><p>logic building</p>
</li>
<li><p>loops</p>
</li>
<li><p>understanding of strings</p>
</li>
<li><p>edge case handling</p>
</li>
</ul>
<h3>Example — Reverse a String</h3>
<p>Example:</p>
<pre><code class="language-js">function reverseString(str) {
  return str.split("").reverse().join("");
}

console.log(reverseString("hello"));
</code></pre>
<p>Output:</p>
<pre><code class="language-js">olleh
</code></pre>
<h3>Manual Reverse Without Built-in Reverse</h3>
<p>Interviewers may ask:</p>
<blockquote>
<p>“Do it without <code>reverse()</code>.”</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-js">function reverseString(str) {
  let result = "";

  for (let i = str.length - 1; i &gt;= 0; i--) {
    result += str[i];
  }

  return result;
}

console.log(reverseString("hello"));
</code></pre>
<h3>Example — Count Characters</h3>
<p>Example:</p>
<pre><code class="language-js">function countCharacters(str) {
  return str.length;
}

console.log(countCharacters("JavaScript"));
</code></pre>
<h3>Example — Check Palindrome</h3>
<p>A palindrome reads the same forward and backward.</p>
<p>Example:</p>
<pre><code class="language-js">madam
racecar
</code></pre>
<p>Implementation:</p>
<pre><code class="language-js">function isPalindrome(str) {
  const reversed = str.split("").reverse().join("");

  return str === reversed;
}

console.log(isPalindrome("madam"));
</code></pre>
<p>Output:</p>
<pre><code class="language-js">true
</code></pre>
<h3>Common Interview String Problems</h3>
<p>String questions are extremely common in JavaScript interviews.</p>
<p>Because strings test:</p>
<ul>
<li><p>loops</p>
</li>
<li><p>conditions</p>
</li>
<li><p>problem solving</p>
</li>
<li><p>algorithms</p>
</li>
<li><p>logical thinking</p>
</li>
</ul>
<h3>Frequently Asked String Questions</h3>
<p>Some very common problems:</p>
<h4>Reverse a string</h4>
<p>Example:</p>
<pre><code class="language-js">"hello" → "olleh"
</code></pre>
<h4>Check palindrome</h4>
<p>Example:</p>
<pre><code class="language-js">"madam" → true
</code></pre>
<h4>Count vowels</h4>
<p>Example:</p>
<pre><code class="language-js">"javascript" → 3
</code></pre>
<h4>Find duplicate characters</h4>
<p>Example:</p>
<pre><code class="language-js">"programming"
</code></pre>
<h4>Capitalize first letter</h4>
<p>Example:</p>
<pre><code class="language-js">"hello world" → "Hello World"
</code></pre>
<h4>Find longest word</h4>
<p>Example:</p>
<pre><code class="language-js">"I love JavaScript"
</code></pre>
<h3>Why Interviewers Love String Questions</h3>
<p>Because string problems reveal:</p>
<ul>
<li><p>how you think</p>
</li>
<li><p>how you break problems</p>
</li>
<li><p>how comfortable you are with loops and conditions</p>
</li>
</ul>
<p>Even simple-looking string problems can test deep logical understanding.</p>
<h3>Importance of Understanding Built-in Behavior</h3>
<p>Most beginners only memorize methods.</p>
<p>Example:</p>
<pre><code class="language-js">split()
slice()
substring()
</code></pre>
<p>But real understanding comes from knowing:</p>
<ul>
<li><p>how they work internally</p>
</li>
<li><p>their edge cases</p>
</li>
<li><p>their differences</p>
</li>
</ul>
<h3>Example — <code>slice()</code> vs <code>substring()</code></h3>
<p>Both extract parts of strings. But behavior differs. Example:</p>
<pre><code class="language-js">const text = "JavaScript";

console.log(text.slice(-4));
</code></pre>
<p>Output:</p>
<pre><code class="language-js">ript
</code></pre>
<p>But:</p>
<pre><code class="language-js">console.log(text.substring(-4));
</code></pre>
<p>Output becomes different because:</p>
<ul>
<li><code>substring()</code> does not support negative indexes properly</li>
</ul>
<p>Understanding these small differences matters a lot.</p>
<h3>Why Internal Understanding Matters</h3>
<p>When you understand built-in behavior:</p>
<ul>
<li><p>debugging becomes easier</p>
</li>
<li><p>interview performance improves</p>
</li>
<li><p>you write better logic</p>
</li>
<li><p>edge cases become manageable</p>
</li>
</ul>
<p>And this is where developers move from:</p>
<ul>
<li><p>memorizing syntax to</p>
</li>
<li><p>actually understanding JavaScript deeply</p>
</li>
</ul>
<h3>Common Beginner Mistake</h3>
<p>Many beginners depend too much on built-in methods without understanding logic.</p>
<p>Example:</p>
<pre><code class="language-js">str.reverse()
</code></pre>
<p>But strings do not even have a direct <code>reverse()</code> method.</p>
<p>So interviewers often ask:</p>
<blockquote>
<p>“Implement it manually.”</p>
</blockquote>
<p>Because real understanding matters more than memorization.</p>
<h3>Final Understanding</h3>
<p>String methods are built-in functions that help manipulate and process text data.<br />Developers create polyfills when built-in features are unavailable or unsupported in certain environments.<br />Implementing custom string utilities helps strengthen:</p>
<ul>
<li><p>problem solving</p>
</li>
<li><p>loops</p>
</li>
<li><p>conditions</p>
</li>
<li><p>algorithmic thinking</p>
</li>
</ul>
<p>And understanding how built-in methods actually behave is one of the most important skills for becoming strong in JavaScript and technical interviews.</p>
]]></content:encoded></item><item><title><![CDATA[The new Keyword in JavaScript]]></title><description><![CDATA[What the new Keyword Does
In JavaScript, objects can be created in multiple ways. One common way is:
const user = {
  name: "Shubham"
};

But when applications grow, manually creating objects again an]]></description><link>https://javascriptwithshubham.hashnode.dev/the-new-keyword-in-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/the-new-keyword-in-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 12:43:20 GMT</pubDate><content:encoded><![CDATA[<h3>What the <code>new</code> Keyword Does</h3>
<p>In JavaScript, objects can be created in multiple ways. One common way is:</p>
<pre><code class="language-js">const user = {
  name: "Shubham"
};
</code></pre>
<p>But when applications grow, manually creating objects again and again becomes repetitive. Imagine creating 100 users. Writing:</p>
<pre><code class="language-js">const user1 = { ... }
const user2 = { ... }
const user3 = { ... }
</code></pre>
<p>is not scalable.</p>
<p>That’s where constructor functions and the <code>new</code> keyword come into the picture. The <code>new</code> keyword helps create multiple objects from a blueprint.</p>
<h3>Understanding the Real Purpose of <code>new</code></h3>
<p>Think about a factory. A factory does not build products manually every time from scratch. Instead:</p>
<ul>
<li><p>one design exists</p>
</li>
<li><p>multiple products are created from it</p>
</li>
</ul>
<p>Constructor functions work the same way. And the <code>new</code> keyword tells JavaScript:</p>
<blockquote>
<p>“Create a brand new object using this blueprint.”</p>
</blockquote>
<h3>Constructor Functions</h3>
<p>A constructor function is a normal function used for creating objects. Example:</p>
<pre><code class="language-js">function User(name, age) {
  this.name = name;
  this.age = age;
}
</code></pre>
<p>This function itself does not create objects automatically. The real magic happens when we use <code>new</code>.</p>
<p>Example:</p>
<pre><code class="language-js">const user1 = new User("Shubham", 21);

console.log(user1);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">{
  name: "Shubham",
  age: 21
}
</code></pre>
<h3>Why Constructor Functions Start with Capital Letters</h3>
<p>By convention, constructor functions usually start with capital letters. Example:</p>
<pre><code class="language-js">function User() {}
function Person() {}
function Product() {}
</code></pre>
<p>This helps developers understand:</p>
<blockquote>
<p>“This function is meant to be used with <code>new</code>.”</p>
</blockquote>
<h3>What Happens Internally When <code>new</code> Runs</h3>
<p>Most beginners use <code>new</code> without understanding what JavaScript is actually doing internally. But internally, several steps happen automatically.<br />Example:</p>
<pre><code class="language-js">const user1 = new User("Shubham", 21);
</code></pre>
<p>JavaScript performs multiple operations behind the scenes.</p>
<h3>Step 1 — A New Empty Object is Created</h3>
<p>JavaScript first creates an empty object. Something like:</p>
<pre><code class="language-js">{}
</code></pre>
<h3>Step 2 — <code>this</code> Points to That Object</h3>
<p>Inside the constructor function:</p>
<pre><code class="language-js">this.name = name;
</code></pre>
<p>JavaScript makes <code>this</code> refer to the newly created object.</p>
<p>So internally:</p>
<pre><code class="language-js">this = {}
</code></pre>
<h3>Step 3 — Properties Get Added</h3>
<p>Now values are attached to the object. Example:</p>
<pre><code class="language-js">this.name = "Shubham";
this.age = 21;
</code></pre>
<p>Object becomes:</p>
<pre><code class="language-js">{
  name: "Shubham",
  age: 21
}
</code></pre>
<h3>Step 4 — Prototype Linking Happens</h3>
<p>JavaScript automatically links the new object to the constructor’s prototype. This is extremely important. Because now the object can access shared methods through prototype inheritance.</p>
<h3>Step 5 — The Object Gets Returned</h3>
<p>Finally, JavaScript returns the newly created object automatically. So:</p>
<pre><code class="language-js">const user1 = new User("Shubham", 21);
</code></pre>
<p>stores the returned object inside <code>user1</code>.</p>
<h3>Object Creation Process Manually</h3>
<p>The <code>new</code> keyword basically automates this entire process. Something similar to:</p>
<pre><code class="language-js">const obj = {};

obj.name = "Shubham";
obj.age = 21;

return obj;
</code></pre>
<p>But with prototype linking included.</p>
<h3>How <code>new</code> Links Prototypes</h3>
<p>Every constructor function has a special property called:</p>
<pre><code class="language-js">prototype
</code></pre>
<p>Example:</p>
<pre><code class="language-js">function User(name) {
  this.name = name;
}
</code></pre>
<p>JavaScript automatically creates:</p>
<pre><code class="language-js">User.prototype
</code></pre>
<p>Now when objects are created using <code>new</code>, they get linked to this prototype.</p>
<h3>Adding Shared Methods</h3>
<p>Example:</p>
<pre><code class="language-js">function User(name) {
  this.name = name;
}

User.prototype.greet = function () {
  console.log(`Hello ${this.name}`);
};
</code></pre>
<p>Now:</p>
<pre><code class="language-js">const user1 = new User("Shubham");

user1.greet();
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Hello Shubham
</code></pre>
<p>Even though <code>greet()</code> does not exist directly inside <code>user1</code>.</p>
<h3>Why Prototype Linking Matters</h3>
<p>Without prototypes, every object would create separate copies of methods.<br />Example:</p>
<pre><code class="language-js">function User(name) {
  this.name = name;

  this.greet = function () {
    console.log("Hello");
  };
}
</code></pre>
<p>Now every object gets its own separate <code>greet()</code> function. This wastes memory. Prototypes solve this by sharing methods between instances.</p>
<h3>Instances Created from Constructors</h3>
<p>Objects created using constructor functions are called instances. Example:</p>
<pre><code class="language-js">const user1 = new User("Shubham");
const user2 = new User("Rahul");
</code></pre>
<p>Both are instances of <code>User</code>.</p>
<h3>Understanding Instances</h3>
<p>Think about:</p>
<ul>
<li><p>constructor function → blueprint</p>
</li>
<li><p>instances → actual objects created from blueprint</p>
</li>
</ul>
<p>Like:</p>
<ul>
<li><p>class design of a car</p>
</li>
<li><p>actual manufactured cars</p>
</li>
</ul>
<h3>Checking Instances</h3>
<p>JavaScript provides <code>instanceof</code> Example:</p>
<pre><code class="language-js">console.log(user1 instanceof User);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">true
</code></pre>
<p>Because <code>user1</code> was created using <code>User</code>.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners forget the <code>new</code> keyword. Example:</p>
<pre><code class="language-js">const user1 = User("Shubham");
</code></pre>
<p>Now strange behavior happens because:</p>
<ul>
<li><p>no new object gets created</p>
</li>
<li><p><code>this</code> may point to global object or become undefined in strict mode</p>
</li>
</ul>
<p>Always use <code>new</code> with constructor functions.</p>
<h3>Constructor Functions vs Classes</h3>
<p>Modern JavaScript introduced classes.</p>
<p>Example:</p>
<pre><code class="language-js">class User {
  constructor(name) {
    this.name = name;
  }
}
</code></pre>
<p>But internally:</p>
<ul>
<li><p>classes still use prototypes</p>
</li>
<li><p>prototype inheritance still exists</p>
</li>
<li><p>constructor behavior is still happening</p>
</li>
</ul>
<p>Classes are mainly cleaner syntax over the same prototype-based system.</p>
<h3>Final Understanding</h3>
<p>The <code>new</code> keyword automates object creation in JavaScript.</p>
<p>When <code>new</code> is used:</p>
<ol>
<li><p>a new object is created</p>
</li>
<li><p><code>this</code> points to that object</p>
</li>
<li><p>properties are added</p>
</li>
<li><p>prototype linking happens</p>
</li>
<li><p>object gets returned</p>
</li>
</ol>
<p>Constructor functions act like blueprints, and objects created from them are called instances.</p>
<p>And understanding how <code>new</code> works internally is one of the most important steps toward mastering JavaScript object-oriented programming.</p>
]]></content:encoded></item><item><title><![CDATA[Callbacks in JavaScript: Why They Exist]]></title><description><![CDATA[What a Callback Function Is
One of the most important things to understand in JavaScript is that:

Functions can be treated like normal values.

This means:

functions can be stored in variables

func]]></description><link>https://javascriptwithshubham.hashnode.dev/callbacks-in-javascript-why-they-exist</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/callbacks-in-javascript-why-they-exist</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 12:39:36 GMT</pubDate><content:encoded><![CDATA[<h3>What a Callback Function Is</h3>
<p>One of the most important things to understand in JavaScript is that:</p>
<blockquote>
<p>Functions can be treated like normal values.</p>
</blockquote>
<p>This means:</p>
<ul>
<li><p>functions can be stored in variables</p>
</li>
<li><p>functions can be passed into other functions</p>
</li>
<li><p>functions can be returned from functions</p>
</li>
</ul>
<p>And this is exactly where callback functions come from. A callback function is simply:</p>
<blockquote>
<p>A function passed as an argument into another function.</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-js">function greet(name) {
  console.log(`Hello ${name}`);
}

function processUser(callback) {
  const username = "Shubham";

  callback(username);
}

processUser(greet);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Hello Shubham
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>greet</code> is passed into another function</p>
</li>
<li><p>later it gets executed</p>
</li>
</ul>
<p>That passed function is called a callback function.</p>
<h3>Understanding the Real Idea Behind Callbacks</h3>
<p>Think about ordering food online. You place an order and say:</p>
<blockquote>
<p>“When food arrives, call me.”</p>
</blockquote>
<p>That “call me later” behavior is basically the callback concept. Instead of waiting continuously, JavaScript says:</p>
<blockquote>
<p>“Give me a function.<br />I’ll execute it when the task finishes.”</p>
</blockquote>
<h3>Passing Functions as Arguments</h3>
<p>Since functions are treated like values in JavaScript, they can be passed as arguments.</p>
<p>Example:</p>
<pre><code class="language-js">function add(a, b) {
  return a + b;
}

function calculate(x, y, operation) {
  return operation(x, y);
}

const result = calculate(5, 3, add);

console.log(result);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">8
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>add</code> function is passed as an argument</p>
</li>
<li><p><code>calculate()</code> executes it later</p>
</li>
</ul>
<p>This is callback behavior.</p>
<h3>Why Parentheses Matter</h3>
<p>Many beginners make this mistake:</p>
<pre><code class="language-js">calculate(5, 3, add());
</code></pre>
<p>Wrong.</p>
<p>Because:</p>
<ul>
<li><p><code>add()</code> executes immediately</p>
</li>
<li><p>its return value gets passed</p>
</li>
</ul>
<p>Correct:</p>
<pre><code class="language-js">calculate(5, 3, add);
</code></pre>
<p>Because we are passing the function itself, not executing it instantly.</p>
<h3>Why Callbacks Are Used in Asynchronous Programming</h3>
<p>Some operations take time.</p>
<p>Like:</p>
<ul>
<li><p>fetching data from APIs</p>
</li>
<li><p>reading files</p>
</li>
<li><p>database queries</p>
</li>
<li><p>authentication</p>
</li>
<li><p>timers</p>
</li>
</ul>
<p>JavaScript does not want to block the entire application while waiting.</p>
<p>So instead of waiting, it says:</p>
<blockquote>
<p>“Start the task and give me a callback.<br />I’ll run it once the work is completed.”</p>
</blockquote>
<p>This is why callbacks became extremely important in asynchronous programming.</p>
<h3>Callback Example with <code>setTimeout</code></h3>
<p>Example:</p>
<pre><code class="language-js">setTimeout(() =&gt; {
  console.log("Task completed");
}, 2000);
</code></pre>
<p>Output after 2 seconds:</p>
<pre><code class="language-js">Task completed
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>setTimeout()</code> starts a timer</p>
</li>
<li><p>callback function runs later</p>
</li>
</ul>
<p>The function inside <code>setTimeout()</code> is a callback.</p>
<h3>Callback Usage in Common Scenarios</h3>
<p>Callbacks are used almost everywhere in JavaScript. Especially in:</p>
<ul>
<li><p>asynchronous programming</p>
</li>
<li><p>event handling</p>
</li>
<li><p>timers</p>
</li>
<li><p>API calls</p>
</li>
<li><p>array methods</p>
</li>
</ul>
<h3>Event Handling</h3>
<p>Example:</p>
<pre><code class="language-js">button.addEventListener("click", () =&gt; {
  console.log("Button clicked");
});
</code></pre>
<p>Here:</p>
<ul>
<li><p>JavaScript waits for click event</p>
</li>
<li><p>callback runs after click happens</p>
</li>
</ul>
<h3>Array Methods</h3>
<p>Callbacks are heavily used in array methods.</p>
<p>Example:</p>
<pre><code class="language-js">const numbers = [1, 2, 3];

numbers.forEach((num) =&gt; {
  console.log(num);
});
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>forEach()</code> receives a callback</p>
</li>
<li><p>callback executes for every array element</p>
</li>
</ul>
<h3>API Simulation</h3>
<p>Example:</p>
<pre><code class="language-js">function fetchData(callback) {
  setTimeout(() =&gt; {
    callback("Data received");
  }, 2000);
}

fetchData((data) =&gt; {
  console.log(data);
});
</code></pre>
<p>This simulates async API behavior.</p>
<h3>Real Power of Callbacks</h3>
<p>Callbacks allow JavaScript to become:</p>
<ul>
<li><p>event-driven</p>
</li>
<li><p>asynchronous</p>
</li>
<li><p>non-blocking</p>
</li>
</ul>
<p>Without callbacks, handling delayed operations would become much harder.</p>
<h3>Basic Problem of Callback Nesting</h3>
<p>Callbacks solved many problems initially. But large applications introduced another issue. Nested callbacks.<br />Example:</p>
<pre><code class="language-js">loginUser(() =&gt; {
  getProfile(() =&gt; {
    getPosts(() =&gt; {
      getComments(() =&gt; {
        console.log("All tasks completed");
      });
    });
  });
});
</code></pre>
<p>Now code keeps moving toward the right side.</p>
<p>This becomes difficult to:</p>
<ul>
<li><p>read</p>
</li>
<li><p>debug</p>
</li>
<li><p>maintain</p>
</li>
</ul>
<h3>What is Callback Hell?</h3>
<p>Deeply nested callbacks are commonly called:</p>
<blockquote>
<p>Callback Hell</p>
</blockquote>
<p>Because:</p>
<ul>
<li><p>code structure becomes ugly</p>
</li>
<li><p>understanding flow becomes harder</p>
</li>
<li><p>error handling becomes messy</p>
</li>
</ul>
<p>Developers started struggling with large async applications because of this pattern.</p>
<h3>Real Problem Was Not Callbacks</h3>
<p>Callbacks themselves were not bad. The actual problem was:</p>
<ul>
<li><p>excessive nesting</p>
</li>
<li><p>unreadable code structure</p>
</li>
<li><p>poor maintainability</p>
</li>
</ul>
<p>That’s why JavaScript later introduced:</p>
<ul>
<li><p>Promises</p>
</li>
<li><p>async/await</p>
</li>
</ul>
<p>to improve asynchronous code handling.</p>
<h3>Common Beginner Confusion</h3>
<p>Many beginners think callbacks are only related to async programming. That is not true. Callbacks can be:</p>
<ul>
<li><p>synchronous</p>
</li>
<li><p>asynchronous</p>
</li>
</ul>
<p>Example of synchronous callback:</p>
<pre><code class="language-js">numbers.forEach((num) =&gt; {
  console.log(num);
});
</code></pre>
<p>Example of asynchronous callback:</p>
<pre><code class="language-js">setTimeout(() =&gt; {
  console.log("Hello");
}, 1000);
</code></pre>
<p>Both are callbacks.</p>
<h3>Final Understanding</h3>
<p>A callback function is simply:</p>
<blockquote>
<p>a function passed into another function.</p>
</blockquote>
<p>Callbacks became extremely important because JavaScript needed a way to handle delayed operations without blocking execution.</p>
<p>They are widely used in:</p>
<ul>
<li><p>timers</p>
</li>
<li><p>event handling</p>
</li>
<li><p>APIs</p>
</li>
<li><p>asynchronous programming</p>
</li>
<li><p>array methods</p>
</li>
</ul>
<p>But deeply nested callbacks created readability and maintenance problems, leading to concepts like:</p>
<ul>
<li><p>promises</p>
</li>
<li><p>async/await</p>
</li>
</ul>
<p>which were built to make asynchronous JavaScript cleaner and easier to manage.</p>
]]></content:encoded></item><item><title><![CDATA[Template Literals in JavaScript]]></title><description><![CDATA[Problems with Traditional String Concatenation
Before template literals existed, JavaScript developers mostly created strings using concatenation.
Example:
const name = "Shubham";

const message = "He]]></description><link>https://javascriptwithshubham.hashnode.dev/template-literals-in-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/template-literals-in-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 12:36:56 GMT</pubDate><content:encoded><![CDATA[<h3>Problems with Traditional String Concatenation</h3>
<p>Before template literals existed, JavaScript developers mostly created strings using concatenation.</p>
<p>Example:</p>
<pre><code class="language-js">const name = "Shubham";

const message = "Hello " + name + ", welcome to JavaScript";

console.log(message);
</code></pre>
<p>This works fine for small cases. But as strings become larger, concatenation starts becoming messy.<br />Example:</p>
<pre><code class="language-js">const product = "Laptop";
const price = 50000;

const text =
  "The product name is " +
  product +
  " and its price is " +
  price +
  " rupees.";
</code></pre>
<p>Now imagine:</p>
<ul>
<li><p>bigger paragraphs</p>
</li>
<li><p>HTML templates</p>
</li>
<li><p>dynamic API responses</p>
</li>
<li><p>React components</p>
</li>
</ul>
<p>The code quickly becomes difficult to read.</p>
<h3>Real Problem with Concatenation</h3>
<p>The biggest issue was readability. Developers constantly had to manage:</p>
<ul>
<li><p>quotation marks</p>
</li>
<li><p>spaces</p>
</li>
<li><p><code>+</code> operators</p>
</li>
<li><p>line breaks</p>
</li>
</ul>
<p>And one missing <code>+</code> could break the entire string.</p>
<p>Example:</p>
<pre><code class="language-js">const text = "Hello " + name + " welcome";
</code></pre>
<p>Small mistakes become common in large strings. That’s why JavaScript introduced template literals.</p>
<h3>What are Template Literals?</h3>
<p>Template literals are a modern way to work with strings in JavaScript.</p>
<p>Instead of normal quotes:</p>
<pre><code class="language-js">" "
' '
</code></pre>
<p>template literals use backticks:</p>
<pre><code class="language-js">` `
</code></pre>
<p>Example:</p>
<pre><code class="language-js">const name = "Shubham";

const message = `Hello ${name}`;

console.log(message);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Hello Shubham
</code></pre>
<p>This looks much cleaner compared to string concatenation.</p>
<h3>Template Literal Syntax</h3>
<p>Basic syntax:</p>
<pre><code class="language-js">`Your text here`
</code></pre>
<p>Using variables:</p>
<pre><code class="language-js">`${variableName}`
</code></pre>
<p>Example:</p>
<pre><code class="language-js">const language = "JavaScript";

console.log(`I am learning ${language}`);
</code></pre>
<h3>Understanding <code>${}</code></h3>
<p>Inside template literals:</p>
<pre><code class="language-js">${}
</code></pre>
<p>is used to insert dynamic values. This is called interpolation. Instead of manually joining strings with <code>+</code>, JavaScript automatically inserts the value.</p>
<p>Example:</p>
<pre><code class="language-js">const age = 21;

console.log(`My age is ${age}`);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">My age is 21
</code></pre>
<h3>Embedding Variables in Strings</h3>
<p>One of the biggest advantages of template literals is embedding variables directly inside strings.</p>
<p>Example:</p>
<pre><code class="language-js">const name = "Shubham";
const city = "Delhi";

const intro = `My name is \({name} and I live in \){city}`;

console.log(intro);
</code></pre>
<p>This feels much more natural compared to concatenation.</p>
<h3>Embedding Expressions</h3>
<p>Template literals can also evaluate expressions.</p>
<p>Example:</p>
<pre><code class="language-js">const a = 10;
const b = 20;

console.log(`Sum is ${a + b}`);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Sum is 30
</code></pre>
<p>You are not limited to variables only. JavaScript expressions can also run inside <code>${}</code>.</p>
<h3>Multi-Line Strings</h3>
<p>Before template literals, writing multi-line strings was painful.</p>
<p>Example:</p>
<pre><code class="language-js">const text =
  "Hello\n" +
  "Welcome to JavaScript\n" +
  "Learning template literals";
</code></pre>
<p>This becomes ugly very quickly.</p>
<h3>Multi-Line Strings with Template Literals</h3>
<p>Template literals solve this naturally.</p>
<p>Example:</p>
<pre><code class="language-js">const text = `
Hello
Welcome to JavaScript
Learning template literals
`;

console.log(text);
</code></pre>
<p>This preserves line breaks automatically. No need for:</p>
<ul>
<li><p><code>\n</code></p>
</li>
<li><p>concatenation</p>
</li>
<li><p>extra operators</p>
</li>
</ul>
<h3>Why Multi-Line Strings Matter</h3>
<p>This becomes extremely useful when working with:</p>
<ul>
<li><p>HTML templates</p>
</li>
<li><p>emails</p>
</li>
<li><p>API responses</p>
</li>
<li><p>large formatted text</p>
</li>
<li><p>JSX-like structures</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-js">const html = `
&lt;div&gt;
  &lt;h1&gt;Welcome&lt;/h1&gt;
  &lt;p&gt;This is JavaScript&lt;/p&gt;
&lt;/div&gt;
`;
</code></pre>
<p>Without template literals, this would become very messy.</p>
<h3>Use Cases in Modern JavaScript</h3>
<p>Template literals are heavily used in modern JavaScript development. Especially in:</p>
<ul>
<li><p>React</p>
</li>
<li><p>Node.js</p>
</li>
<li><p>API handling</p>
</li>
<li><p>dynamic HTML generation</p>
</li>
<li><p>logging</p>
</li>
<li><p>email templates</p>
</li>
</ul>
<h3>Dynamic Messages</h3>
<p>Example:</p>
<pre><code class="language-js">const username = "Shubham";

console.log(`Welcome back, ${username}`);
</code></pre>
<h3>Building HTML Dynamically</h3>
<p>Example:</p>
<pre><code class="language-js">const title = "JavaScript";

const html = `
&lt;div&gt;
  &lt;h1&gt;${title}&lt;/h1&gt;
&lt;/div&gt;
`;
</code></pre>
<p>This is very common in frontend development.</p>
<h3>API Responses</h3>
<p>Example:</p>
<pre><code class="language-js">const user = "Rahul";

const response = {
  message: `User ${user} logged in successfully`
};
</code></pre>
<h3>Cleaner Logging</h3>
<p>Example:</p>
<pre><code class="language-js">const score = 95;

console.log(`Your score is ${score}`);
</code></pre>
<p>Cleaner than:</p>
<pre><code class="language-js">console.log("Your score is " + score);
</code></pre>
<h3>Real Reason Developers Love Template Literals</h3>
<p>Template literals make code:</p>
<ul>
<li><p>cleaner</p>
</li>
<li><p>easier to read</p>
</li>
<li><p>easier to maintain</p>
</li>
<li><p>less error-prone</p>
</li>
</ul>
<p>And readability becomes extremely important in large applications.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners confuse:</p>
<ul>
<li><p>single quotes</p>
</li>
<li><p>double quotes</p>
</li>
<li><p>backticks</p>
</li>
</ul>
<p>Remember:</p>
<pre><code class="language-js">` `
</code></pre>
<p>These are backticks.</p>
<p>Not normal quotation marks.</p>
<p>Template literals only work with backticks.</p>
<h3>Final Understanding</h3>
<p>Traditional string concatenation worked, but it became messy for large dynamic strings.</p>
<p>Template literals solved this by providing:</p>
<ul>
<li><p>cleaner syntax</p>
</li>
<li><p>variable interpolation</p>
</li>
<li><p>expression support</p>
</li>
<li><p>multi-line strings</p>
</li>
</ul>
<p>Using backticks and <code>${}</code> makes modern JavaScript code more readable and maintainable.</p>
<p>And because of this, template literals are now used almost everywhere in modern JavaScript development.</p>
]]></content:encoded></item><item><title><![CDATA[Array Flatten in JavaScript]]></title><description><![CDATA[What Nested Arrays Are
A normal array looks like this:
const arr = [1, 2, 3, 4];

But sometimes arrays contain other arrays inside them.Example:
const arr = [1, 2, [3, 4], [5, 6]];

This is called a n]]></description><link>https://javascriptwithshubham.hashnode.dev/array-flatten-in-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/array-flatten-in-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 12:33:04 GMT</pubDate><content:encoded><![CDATA[<h3>What Nested Arrays Are</h3>
<p>A normal array looks like this:</p>
<pre><code class="language-js">const arr = [1, 2, 3, 4];
</code></pre>
<p>But sometimes arrays contain other arrays inside them.<br />Example:</p>
<pre><code class="language-js">const arr = [1, 2, [3, 4], [5, 6]];
</code></pre>
<p>This is called a nested array. Because arrays are nested inside another array. And nesting can go even deeper.<br />Example:</p>
<pre><code class="language-js">const arr = [1, [2, [3, [4, 5]]]];
</code></pre>
<p>Now this becomes a deeply nested array.</p>
<h3>Why Nested Arrays Exist</h3>
<p>Nested arrays are actually very common in real applications.</p>
<p>For example:</p>
<ul>
<li><p>grouped data</p>
</li>
<li><p>categories and subcategories</p>
</li>
<li><p>comments and replies</p>
</li>
<li><p>matrix data</p>
</li>
<li><p>API responses</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-js">const students = [
  ["Shubham", "Rahul"],
  ["Aman", "Karan"]
];
</code></pre>
<p>Or:</p>
<pre><code class="language-js">const comments = [
  "First Comment",
  ["Reply 1", ["Reply 2"]]
];
</code></pre>
<p>So nested arrays are not weird. They naturally appear in real-world data structures.</p>
<h3>Why Flattening Arrays is Useful</h3>
<p>Sometimes nested arrays become difficult to work with.<br />Suppose you want:</p>
<ul>
<li><p>total number of elements</p>
</li>
<li><p>searching values</p>
</li>
<li><p>filtering data</p>
</li>
<li><p>looping properly</p>
</li>
<li><p>sending clean API responses</p>
</li>
</ul>
<p>Nested structures make these operations harder.</p>
<p>Example:</p>
<pre><code class="language-js">const arr = [1, [2, [3, 4]]];
</code></pre>
<p>If you want:</p>
<pre><code class="language-js">[1, 2, 3, 4]
</code></pre>
<p>You need flattening. Flattening means:</p>
<blockquote>
<p>Converting nested arrays into a single normal array.</p>
</blockquote>
<hr />
<h3>Understanding the Concept of Flattening</h3>
<p>Flattening removes nested levels.</p>
<p>Example:</p>
<pre><code class="language-js">const arr = [1, [2, 3], [4, 5]];
</code></pre>
<p>Flattened version:</p>
<pre><code class="language-js">[1, 2, 3, 4, 5]
</code></pre>
<p>Another example:</p>
<pre><code class="language-js">const arr = [1, [2, [3, [4]]]];
</code></pre>
<p>Flattened:</p>
<pre><code class="language-js">[1, 2, 3, 4]
</code></pre>
<p>So the main goal is simple:</p>
<blockquote>
<p>Remove nesting and create one single array.</p>
</blockquote>
<hr />
<h3>Different Approaches to Flatten Arrays</h3>
<p>There are multiple ways to flatten arrays in JavaScript. And interviewers often ask multiple approaches to check your understanding.</p>
<h3>Approach 1 — Using <code>flat()</code></h3>
<p>JavaScript already provides an inbuilt method.</p>
<p>Example:</p>
<pre><code class="language-js">const arr = [1, 2, [3, 4]];

const result = arr.flat();

console.log(result);
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[1, 2, 3, 4]
</code></pre>
<h3>Understanding Depth in <code>flat()</code></h3>
<p>By default:</p>
<pre><code class="language-js">flat()
</code></pre>
<p>only removes one level.</p>
<p>Example:</p>
<pre><code class="language-js">const arr = [1, [2, [3]]];

console.log(arr.flat());
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[1, 2, [3]]
</code></pre>
<p>Because only one level got flattened.</p>
<h3>Flattening Deeply Nested Arrays</h3>
<p>You can specify depth.</p>
<p>Example:</p>
<pre><code class="language-js">const arr = [1, [2, [3, [4]]]];

console.log(arr.flat(3));
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[1, 2, 3, 4]
</code></pre>
<h3>Using <code>Infinity</code></h3>
<p>If nesting level is unknown:</p>
<pre><code class="language-js">const arr = [1, [2, [3, [4]]]];

console.log(arr.flat(Infinity));
</code></pre>
<p>This completely flattens the array.</p>
<h3>Approach 2 — Using Recursion</h3>
<p>Interviews often expect recursive solutions. Because flattening deeply nested arrays is a very good recursion problem.</p>
<p>Example:</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;
}

const arr = [1, [2, [3, [4]]]];

console.log(flattenArray(arr));
</code></pre>
<p>Output:</p>
<pre><code class="language-js">[1, 2, 3, 4]
</code></pre>
<h3>Thinking Process Behind Recursion</h3>
<p>The logic is:</p>
<ul>
<li><p>if item is normal → push into result</p>
</li>
<li><p>if item is array → flatten again</p>
</li>
</ul>
<p>So the function keeps calling itself until all nesting disappears. This is classic recursion.</p>
<h3>Approach 3 — Using <code>reduce()</code></h3>
<p>Another popular interview solution.</p>
<p>Example:</p>
<pre><code class="language-js">function flattenArray(arr) {
  return arr.reduce((acc, curr) =&gt; {
    if (Array.isArray(curr)) {
      return acc.concat(flattenArray(curr));
    }

    return acc.concat(curr);
  }, []);
}

const arr = [1, [2, [3, 4]]];

console.log(flattenArray(arr));
</code></pre>
<h3>Why Interviewers Ask Flatten Array Questions</h3>
<p>Because this single problem checks many concepts together.</p>
<p>Like:</p>
<ul>
<li><p>arrays</p>
</li>
<li><p>recursion</p>
</li>
<li><p>iteration</p>
</li>
<li><p>condition checking</p>
</li>
<li><p>problem solving</p>
</li>
<li><p>thinking ability</p>
</li>
</ul>
<p>That’s why flatten array questions are extremely common in interviews.</p>
<h3>Common Interview Scenarios</h3>
<p>You may see questions like:</p>
<h4>Flatten an array without using <code>flat()</code></h4>
<p>This checks your logical thinking.</p>
<h4>Flatten only specific depth</h4>
<p>Example:</p>
<pre><code class="language-js">flat(2)
</code></pre>
<p>Interviewers may ask you to manually implement this.</p>
<h4>Convert deeply nested arrays into single arrays</h4>
<p>Classic recursion question.</p>
<h4>Flatten arrays with mixed data</h4>
<p>Example:</p>
<pre><code class="language-js">[1, "Hello", [true, [null]]]
</code></pre>
<p>This checks whether your solution handles all data types properly.</p>
<h3>Most Important Understanding</h3>
<p>Flattening is not about memorizing methods. It is about understanding:</p>
<blockquote>
<p>How to traverse nested structures.</p>
</blockquote>
<p>And once you understand flattening arrays, concepts like:</p>
<ul>
<li><p>tree traversal</p>
</li>
<li><p>nested objects</p>
</li>
<li><p>recursive data structures</p>
</li>
</ul>
<p>start becoming easier.</p>
<h3>Final Understanding</h3>
<p>Nested arrays are arrays inside arrays. Flattening means:</p>
<blockquote>
<p>converting all nested levels into one single array.</p>
</blockquote>
<p>JavaScript provides:</p>
<ul>
<li><p><code>flat()</code></p>
</li>
<li><p>recursion</p>
</li>
<li><p><code>reduce()</code></p>
</li>
</ul>
<p>to solve this problem. But the real learning is understanding:</p>
<ul>
<li><p>recursion</p>
</li>
<li><p>nested traversal</p>
</li>
<li><p>breaking complex structures into smaller problems</p>
</li>
</ul>
<p>And that is exactly why flatten array questions are so popular in JavaScript interviews.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Modules: Import and Export Explained]]></title><description><![CDATA[Why Modules Are Needed
When beginners start learning JavaScript, most code looks something like this:
function add(a, b) {
  return a + b;
}

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

function mult]]></description><link>https://javascriptwithshubham.hashnode.dev/javascript-modules-import-and-export-explained</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/javascript-modules-import-and-export-explained</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 09:57:56 GMT</pubDate><content:encoded><![CDATA[<h3>Why Modules Are Needed</h3>
<p>When beginners start learning JavaScript, most code looks something like this:</p>
<pre><code class="language-js">function add(a, b) {
  return a + b;
}

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

function multiply(a, b) {
  return a * b;
}
</code></pre>
<p>Initially this feels fine. But imagine building:</p>
<ul>
<li><p>a backend server</p>
</li>
<li><p>an e-commerce app</p>
</li>
<li><p>a social media platform</p>
</li>
<li><p>a large React application</p>
</li>
</ul>
<p>Now everything inside one file becomes a disaster.<br />You start facing problems like:</p>
<ul>
<li><p>huge files</p>
</li>
<li><p>difficult debugging</p>
</li>
<li><p>duplicate code</p>
</li>
<li><p>messy structure</p>
</li>
<li><p>hard maintenance</p>
</li>
</ul>
<p>That’s why modules exist.<br />Modules help divide code into smaller reusable parts. Instead of writing everything in one file, we separate logic into different files based on responsibility.</p>
<h3>Real Thinking Process Behind Modules</h3>
<p>Think about a real company. One person does not handle:</p>
<ul>
<li><p>marketing</p>
</li>
<li><p>finance</p>
</li>
<li><p>development</p>
</li>
<li><p>design</p>
</li>
<li><p>management</p>
</li>
</ul>
<p>Everyone has separate responsibilities. Modules work the same way. One file handles:</p>
<ul>
<li><p>authentication</p>
</li>
<li><p>database logic</p>
</li>
<li><p>utility functions</p>
</li>
<li><p>API routes</p>
</li>
<li><p>calculations</p>
</li>
</ul>
<p>This makes applications cleaner and easier to scale.</p>
<h3>What is a Module?</h3>
<p>A module is simply:</p>
<blockquote>
<p>A separate file containing reusable code.</p>
</blockquote>
<p>That code can be:</p>
<ul>
<li><p>functions</p>
</li>
<li><p>variables</p>
</li>
<li><p>arrays</p>
</li>
<li><p>objects</p>
</li>
<li><p>classes</p>
</li>
</ul>
<p>And other files can use that code whenever needed.</p>
<h3>Exporting Functions or Values</h3>
<p>If we want to use something outside a file, we need to export it.</p>
<p>Example:</p>
<pre><code class="language-js">// math.js

function add(a, b) {
  return a + b;
}

module.exports = add;
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>add</code> function is being exported</p>
</li>
<li><p>other files can now use it</p>
</li>
</ul>
<p>We are basically saying:</p>
<blockquote>
<p>“This function can be accessed outside this file.”</p>
</blockquote>
<h3>Exporting Multiple Values</h3>
<p>We can also export multiple things.</p>
<p>Example:</p>
<pre><code class="language-js">// math.js

function add(a, b) {
  return a + b;
}

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

module.exports = {
  add,
  subtract
};
</code></pre>
<p>Now both functions become available outside the file.</p>
<h3>Importing Modules</h3>
<p>Once something is exported, another file can import it.</p>
<p>Example:</p>
<pre><code class="language-js">// app.js

const math = require("./math");

console.log(math.add(2, 3));
</code></pre>
<p>Output:</p>
<pre><code class="language-js">5
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>require()</code> imports the module</p>
</li>
<li><p>Node.js loads the exported values from <code>math.js</code></p>
</li>
</ul>
<hr />
<h3>Importing Specific Functions</h3>
<p>Instead of accessing with dot notation every time, we can directly extract functions.</p>
<p>Example:</p>
<pre><code class="language-js">const { add, subtract } = require("./math");

console.log(add(5, 2));
</code></pre>
<p>This is cleaner and commonly used.</p>
<h3>Default vs Named Exports</h3>
<p>Modern JavaScript also supports ES Modules.</p>
<p>This uses:</p>
<ul>
<li><p><code>export</code></p>
</li>
<li><p><code>import</code></p>
</li>
</ul>
<p>instead of:</p>
<ul>
<li><p><code>module.exports</code></p>
</li>
<li><p><code>require</code></p>
</li>
</ul>
<h3>Default Export</h3>
<p>A file can have one default export.</p>
<p>Example:</p>
<pre><code class="language-js">// math.js

export default function add(a, b) {
  return a + b;
}
</code></pre>
<p>Importing:</p>
<pre><code class="language-js">import add from "./math.js";
</code></pre>
<p>Here:</p>
<ul>
<li><p>no curly braces are used</p>
</li>
<li><p>the imported name can be changed</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-js">import sum from "./math.js";
</code></pre>
<p>Still works. Because default export has no fixed name requirement during import.</p>
<h3>Named Export</h3>
<p>Named exports export multiple things using their exact names.</p>
<p>Example:</p>
<pre><code class="language-js">// math.js

export function add(a, b) {
  return a + b;
}

export function subtract(a, b) {
  return a - b;
}
</code></pre>
<p>Importing:</p>
<pre><code class="language-js">import { add, subtract } from "./math.js";
</code></pre>
<p>Here:</p>
<ul>
<li><p>curly braces are required</p>
</li>
<li><p>names must match exactly</p>
</li>
</ul>
<h3>Difference Between Default and Named Exports</h3>
<h4>Default Export</h4>
<ul>
<li><p>only one default export per file</p>
</li>
<li><p>imported without curly braces</p>
</li>
<li><p>import name can be changed</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-js">export default add;
</code></pre>
<h4>Named Export</h4>
<ul>
<li><p>multiple named exports allowed</p>
</li>
<li><p>imported using curly braces</p>
</li>
<li><p>names must match</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-js">export { add, subtract };
</code></pre>
<h3>Benefits of Modular Code</h3>
<p>Modules completely change how applications are structured.</p>
<h3>Better Code Organization</h3>
<p>Instead of one giant file, logic is separated into meaningful files.</p>
<p>Example:</p>
<ul>
<li><p>auth.js</p>
</li>
<li><p>database.js</p>
</li>
<li><p>payment.js</p>
</li>
<li><p>userController.js</p>
</li>
</ul>
<p>This makes the project easier to understand.</p>
<h3>Reusability</h3>
<p>Once a module is created, it can be reused in multiple places.</p>
<p>Example:</p>
<ul>
<li><p>validation functions</p>
</li>
<li><p>utility functions</p>
</li>
<li><p>database connections</p>
</li>
</ul>
<p>No need to rewrite code again and again.</p>
<h3>Easier Debugging</h3>
<p>If something breaks:</p>
<ul>
<li><p>you can quickly identify the responsible file</p>
</li>
<li><p>fixing bugs becomes easier</p>
</li>
</ul>
<p>Instead of searching inside thousands of lines.</p>
<h3>Better Team Collaboration</h3>
<p>In real companies:</p>
<ul>
<li><p>one developer works on authentication</p>
</li>
<li><p>another works on payments</p>
</li>
<li><p>another works on APIs</p>
</li>
</ul>
<p>Modules make this possible. Everyone can work independently.</p>
<h3>Easier Maintenance</h3>
<p>Large applications constantly change.</p>
<p>Modular code makes updates easier because:</p>
<ul>
<li><p>changes stay isolated</p>
</li>
<li><p>fewer side effects happen</p>
</li>
<li><p>code becomes more scalable</p>
</li>
</ul>
<h3>Final Understanding</h3>
<p>Modules exist because applications grow.<br />Without modules:</p>
<ul>
<li><p>code becomes messy</p>
</li>
<li><p>maintenance becomes difficult</p>
</li>
<li><p>debugging becomes painful</p>
</li>
</ul>
<p>Modules solve this by splitting applications into smaller reusable pieces.<br />The main flow is simple:</p>
<ol>
<li><p>Create reusable code</p>
</li>
<li><p>Export it</p>
</li>
<li><p>Import it wherever needed</p>
</li>
</ol>
<p>And this modular thinking is one of the biggest foundations of scalable JavaScript and Node.js applications.</p>
]]></content:encoded></item><item><title><![CDATA[Async Code in Node.js: Callbacks and Promises]]></title><description><![CDATA[Why Async Code Exists in Node.js
Imagine if Node.js handled everything one by one in a blocking way.For example:

reading a file

fetching data from database

calling an API

uploading an image


All ]]></description><link>https://javascriptwithshubham.hashnode.dev/async-code-in-node-js-callbacks-and-promises</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/async-code-in-node-js-callbacks-and-promises</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 09:55:12 GMT</pubDate><content:encoded><![CDATA[<h3>Why Async Code Exists in Node.js</h3>
<p>Imagine if Node.js handled everything one by one in a blocking way.<br />For example:</p>
<ul>
<li><p>reading a file</p>
</li>
<li><p>fetching data from database</p>
</li>
<li><p>calling an API</p>
</li>
<li><p>uploading an image</p>
</li>
</ul>
<p>All these operations take time.<br />Now if JavaScript waits for every task to finish before moving to the next line, the entire application becomes slow.<br />That is why asynchronous code exists.</p>
<p>Instead of waiting, Node.js says:</p>
<blockquote>
<p>“Start this task and continue doing other work.<br />When the task finishes, I’ll handle the result.”</p>
</blockquote>
<p>This is what makes Node.js fast and scalable.</p>
<h3>Understanding the Real Problem</h3>
<p>Suppose a database takes 5 seconds to return data.</p>
<p>If Node.js waits those 5 seconds for every user request, then thousands of users would get stuck waiting.<br />Instead, Node.js handles tasks asynchronously so other users can still use the application while one task is processing.<br />This is one of the biggest reasons Node.js became popular for backend development.</p>
<h3>Callback-Based Async Execution</h3>
<p>Initially, JavaScript handled async operations using callbacks.<br />A callback is simply:</p>
<blockquote>
<p>A function passed inside another function that runs later.</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-js">function fetchData(callback) {
  setTimeout(() =&gt; {
    callback("Data received");
  }, 2000);
}

fetchData((data) =&gt; {
  console.log(data);
});
</code></pre>
<p>Output after 2 seconds:</p>
<pre><code class="language-js">Data received
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>fetchData()</code> starts the async task</p>
</li>
<li><p><code>setTimeout()</code> waits 2 seconds</p>
</li>
<li><p>after completion, callback function runs</p>
</li>
</ul>
<p>This is asynchronous behavior.</p>
<h3>Thinking Process Behind Callbacks</h3>
<p>Instead of doing this:</p>
<pre><code class="language-js">const data = fetchData();
</code></pre>
<p>JavaScript says:</p>
<blockquote>
<p>“This operation takes time, so give me a function.<br />I’ll call that function once work is finished.”</p>
</blockquote>
<p>That function is called a callback.</p>
<h3>Problems with Nested Callbacks</h3>
<p>Callbacks worked fine initially.<br />But when applications became larger, developers started facing a huge problem.<br />Nested callbacks.</p>
<p>Example:</p>
<pre><code class="language-js">loginUser(user, () =&gt; {
  getProfile(profile, () =&gt; {
    getPosts(posts, () =&gt; {
      getComments(comments, () =&gt; {
        console.log("All data fetched");
      });
    });
  });
});
</code></pre>
<p>This structure starts moving more and more to the right side.<br />People called this:</p>
<blockquote>
<p>Callback Hell</p>
</blockquote>
<p>Because:</p>
<ul>
<li><p>code becomes hard to read</p>
</li>
<li><p>debugging becomes difficult</p>
</li>
<li><p>error handling becomes messy</p>
</li>
<li><p>maintaining code becomes painful</p>
</li>
</ul>
<hr />
<h3>The Real Issue with Callback Hell</h3>
<p>The problem was not callbacks themselves.<br />The real issue was:</p>
<ul>
<li><p>too much nesting</p>
</li>
<li><p>difficult flow control</p>
</li>
<li><p>unreadable structure</p>
</li>
</ul>
<p>As applications scaled, developers needed a cleaner way to handle asynchronous code.<br />That’s where promises came into the picture.</p>
<h3>Promise-Based Async Handling</h3>
<p>A Promise is an object that represents:</p>
<blockquote>
<p>“A value that may arrive in the future.”</p>
</blockquote>
<p>Instead of passing callbacks everywhere, promises allow cleaner chaining.</p>
<p>Example:</p>
<pre><code class="language-js">const promise = new Promise((resolve, reject) =&gt; {
  const success = true;

  if (success) {
    resolve("Data fetched successfully");
  } else {
    reject("Something went wrong");
  }
});

promise
  .then((data) =&gt; {
    console.log(data);
  })
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<h3>Understanding <code>resolve</code> and <code>reject</code></h3>
<p>Promises mainly have two outcomes:</p>
<ul>
<li><p><code>resolve()</code> → task completed successfully</p>
</li>
<li><p><code>reject()</code> → task failed</p>
</li>
</ul>
<p>And then:</p>
<ul>
<li><p><code>.then()</code> handles success</p>
</li>
<li><p><code>.catch()</code> handles errors</p>
</li>
</ul>
<p>This makes async flow much cleaner compared to deeply nested callbacks.</p>
<h3>Benefits of Promises</h3>
<p>Promises solved many problems of callback-based code. Some major benefits:</p>
<h4>Better Readability</h4>
<p>Code becomes cleaner and easier to understand.</p>
<h4>Better Error Handling</h4>
<p>Instead of handling errors at every level, promises allow centralized error handling using <code>.catch()</code>.</p>
<h4>Easier Chaining</h4>
<p>You can perform multiple async operations in sequence without excessive nesting.</p>
<p>Example:</p>
<pre><code class="language-js">fetchUser()
  .then(getProfile)
  .then(getPosts)
  .then(getComments)
  .catch((error) =&gt; {
    console.log(error);
  });
</code></pre>
<p>This looks much cleaner than nested callbacks.</p>
<h4>Better Maintainability</h4>
<p>As projects grow, promise-based code becomes easier to manage and debug.</p>
<h3>Final Understanding</h3>
<p>Asynchronous programming exists because operations like:</p>
<ul>
<li><p>database queries</p>
</li>
<li><p>API calls</p>
</li>
<li><p>file handling</p>
</li>
<li><p>authentication</p>
</li>
</ul>
<p>take time.<br />Node.js avoids blocking the application by handling these tasks asynchronously. Callbacks were the first major solution for async execution, but nested callbacks created readability and maintenance issues.<br />Promises improved async handling by making code:</p>
<ul>
<li><p>cleaner</p>
</li>
<li><p>more readable</p>
</li>
<li><p>easier to debug</p>
</li>
<li><p>easier to scale</p>
</li>
</ul>
<p>And later, JavaScript became even cleaner with <code>async/await</code>, which is built on top of promises.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding the this Keyword in JavaScript]]></title><description><![CDATA[Most beginners learn JavaScript like this:

variables

functions

arrays

objects


And then suddenly… this And now everything starts becoming weird.Because sometimes this means:

window

object

unde]]></description><link>https://javascriptwithshubham.hashnode.dev/understanding-the-this-keyword-in-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/understanding-the-this-keyword-in-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 09:51:39 GMT</pubDate><content:encoded><![CDATA[<p>Most beginners learn JavaScript like this:</p>
<ul>
<li><p>variables</p>
</li>
<li><p>functions</p>
</li>
<li><p>arrays</p>
</li>
<li><p>objects</p>
</li>
</ul>
<p>And then suddenly… <code>this</code> And now everything starts becoming weird.Because sometimes <code>this</code> means:</p>
<ul>
<li><p>window</p>
</li>
<li><p>object</p>
</li>
<li><p>undefined</p>
</li>
<li><p>some other function</p>
</li>
<li><p>something completely different</p>
</li>
</ul>
<p>And the biggest problem?People try to <em>memorize</em> <code>this</code>.<br />But the reality is:</p>
<blockquote>
<p><code>this</code> is not about where the function is written.<br />It is about HOW the function is CALLED.</p>
</blockquote>
<p>Once you understand this line…<br />90% confusion disappears.</p>
<h3>What <code>this</code> Actually Represents</h3>
<p><code>this</code> basically means:</p>
<blockquote>
<p>“Who is calling me right now?”</p>
</blockquote>
<p>That’s it.</p>
<p>JavaScript checks the calling context and decides the value of <code>this</code>.<br />So instead of asking:<br />“Where is the function created?”<br />Ask:<br />“Who called the function?”</p>
<h3><code>this</code> in Global Context</h3>
<p>Now let’s start from the easiest place.</p>
<pre><code class="language-js">console.log(this);
</code></pre>
<p>In browser:</p>
<pre><code class="language-js">Window
</code></pre>
<p>Because in global scope, the browser says:</p>
<blockquote>
<p>“You are inside the window object.”</p>
</blockquote>
<p>So here:</p>
<pre><code class="language-js">this === window // true
</code></pre>
<h3>Why Does Browser Return Window?</h3>
<p>Because browser keeps many things inside the global <code>window</code> object.</p>
<p>Things like:</p>
<ul>
<li><p>alert</p>
</li>
<li><p>console</p>
</li>
<li><p>setTimeout</p>
</li>
</ul>
<p>All belong to window.</p>
<p>So globally:</p>
<pre><code class="language-js">var name = "Shubham";

console.log(window.name);
</code></pre>
<p>Same value.</p>
<h3><code>this</code> Inside Objects</h3>
<p>Now comes the interesting part.</p>
<pre><code class="language-js">const user = {
  name: "Shubham",
  greet() {
    console.log(this.name);
  }
};

user.greet();
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Shubham
</code></pre>
<p>Why?<br />Because <code>user</code> called <code>greet()</code>.</p>
<p>So here:</p>
<pre><code class="language-js">this = user
</code></pre>
<h3>Important Thinking Process</h3>
<p>Don’t think:<br />“Inside object means this becomes object.”<br />That is incomplete thinking.</p>
<p>Correct thinking:<br />“Who called the method?”<br />Because THIS is the real rule.</p>
<h3>See This Carefully</h3>
<pre><code class="language-js">const user = {
  name: "Shubham",
  greet() {
    console.log(this.name);
  }
};

const x = user.greet;

x();
</code></pre>
<p>Now output becomes:</p>
<pre><code class="language-js">undefined
</code></pre>
<p>Why?</p>
<p>Because now <code>x()</code> is calling the function.<br />NOT <code>user</code>.</p>
<p>So the connection with object is lost.<br />This is where beginners realize:</p>
<blockquote>
<p>Ohhh… calling context changes everything.</p>
</blockquote>
<p>Exactly.</p>
<h3><code>this</code> Inside Normal Functions</h3>
<p>Now look at this:</p>
<pre><code class="language-js">function test() {
  console.log(this);
}

test();
</code></pre>
<p>In browser normal mode:</p>
<pre><code class="language-js">window
</code></pre>
<p>But in strict mode:</p>
<pre><code class="language-js">undefined
</code></pre>
<h3>Why Undefined in Strict Mode?</h3>
<p>Because JavaScript tries to make behavior safer.</p>
<p>Instead of automatically attaching to window, strict mode says:</p>
<blockquote>
<p>“No caller? Then undefined.”</p>
</blockquote>
<h3>The Biggest Rule of <code>this</code></h3>
<p>This single line can save you from confusion forever:</p>
<blockquote>
<p><code>this</code> depends on HOW a function is called.</p>
</blockquote>
<p>NOT where it is written.</p>
<h3>How Calling Context Changes <code>this</code></h3>
<p>Now let’s see real magic.<br />Example 1</p>
<pre><code class="language-js">const person1 = {
  name: "Shubham",
  sayName() {
    console.log(this.name);
  }
};

const person2 = {
  name: "Rahul"
};

person2.sayName = person1.sayName;

person2.sayName();
</code></pre>
<p>Output:</p>
<pre><code class="language-js">Rahul
</code></pre>
<p>Why?<br />Because now <code>person2</code> is calling the function.<br />So:</p>
<pre><code class="language-js">this = person2
</code></pre>
<h3>Same Function, Different <code>this</code></h3>
<p>This is the most powerful thing.<br />One function can behave differently depending on who calls it.<br />That’s why understanding calling context is more important than memorizing syntax.</p>
<h3>Real Beginner Mistake</h3>
<p>Most people think:</p>
<pre><code class="language-js">this
</code></pre>
<p>is permanently attached to something.<br />Nope.<br />It changes dynamically.<br />And this is why JavaScript interviews LOVE asking questions about <code>this</code>.</p>
<h3>Final Understanding</h3>
<p>If you remember only one thing from this blog, remember this:</p>
<blockquote>
<p>JavaScript decides <code>this</code> during function execution time.</p>
</blockquote>
<p>Not during writing time.<br />So always ask:<br />“Who called the function?”<br />And suddenly <code>this</code> becomes much easier.</p>
]]></content:encoded></item><item><title><![CDATA[Array Methods You Must Know]]></title><description><![CDATA[Topics to Cover

push() and pop()

shift() and unshift()

map()

filter()

reduce() (basic explanation only)

forEach()


upcoming...]]></description><link>https://javascriptwithshubham.hashnode.dev/array-methods-you-must-know</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/array-methods-you-must-know</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 15 Mar 2026 09:17:14 GMT</pubDate><content:encoded><![CDATA[<h3><strong>Topics to Cover</strong></h3>
<ol>
<li><p>push() and pop()</p>
</li>
<li><p>shift() and unshift()</p>
</li>
<li><p>map()</p>
</li>
<li><p>filter()</p>
</li>
<li><p>reduce() (basic explanation only)</p>
</li>
<li><p>forEach()</p>
</li>
</ol>
<p>upcoming...</p>
]]></content:encoded></item><item><title><![CDATA[Arrow Functions in JavaScript: A Simpler Way to Write Function]]></title><description><![CDATA[Topics to Cover

What arrow functions are

Basic arrow function syntax

Arrow functions with one parameter

Arrow functions with multiple parameters

Implicit return vs explicit return

Basic differen]]></description><link>https://javascriptwithshubham.hashnode.dev/arrow-functions-in-javascript-a-simpler-way-to-write-function</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/arrow-functions-in-javascript-a-simpler-way-to-write-function</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 15 Mar 2026 09:16:22 GMT</pubDate><content:encoded><![CDATA[<h3><strong>Topics to Cover</strong></h3>
<ol>
<li><p>What arrow functions are</p>
</li>
<li><p>Basic arrow function syntax</p>
</li>
<li><p>Arrow functions with one parameter</p>
</li>
<li><p>Arrow functions with multiple parameters</p>
</li>
<li><p>Implicit return vs explicit return</p>
</li>
<li><p>Basic difference between arrow function and normal function<br />Upcoming...</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[JavaScript Operators: The Basics You Need to Know]]></title><description><![CDATA[Topics to Cover

What operators are

Arithmetic operators (+, -, *, /, %)

Comparison operators (==, ===, !=, >, <)

Logical operators (&&, ||, !)

Assignment operators (=, +=, -=)


upcoming...]]></description><link>https://javascriptwithshubham.hashnode.dev/javascript-operators-the-basics-you-need-to-know</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/javascript-operators-the-basics-you-need-to-know</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 15 Mar 2026 09:15:24 GMT</pubDate><content:encoded><![CDATA[<p><strong>Topics to Cover</strong></p>
<ol>
<li><p>What operators are</p>
</li>
<li><p>Arithmetic operators (+, -, *, /, %)</p>
</li>
<li><p>Comparison operators (==, ===, !=, &gt;, &lt;)</p>
</li>
<li><p>Logical operators (&amp;&amp;, ||, !)</p>
</li>
<li><p>Assignment operators (=, +=, -=)</p>
</li>
</ol>
<p>upcoming...</p>
]]></content:encoded></item><item><title><![CDATA[The Magic of this, call(), apply(), and bind() in JavaScript]]></title><description><![CDATA[Topics to Cover

What this means in JavaScript (simple explanation)

this inside normal functions

this inside objects

What call() does

What apply() does

What bind() does

Difference between call, ]]></description><link>https://javascriptwithshubham.hashnode.dev/the-magic-of-this-call-apply-and-bind-in-javascript</link><guid isPermaLink="true">https://javascriptwithshubham.hashnode.dev/the-magic-of-this-call-apply-and-bind-in-javascript</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 15 Mar 2026 09:14:32 GMT</pubDate><content:encoded><![CDATA[<h2><strong>Topics to Cover</strong></h2>
<ol>
<li><p>What <code>this</code> means in JavaScript (simple explanation)</p>
</li>
<li><p><code>this</code> inside normal functions</p>
</li>
<li><p><code>this</code> inside objects</p>
</li>
<li><p>What <code>call()</code> does</p>
</li>
<li><p>What <code>apply()</code> does</p>
</li>
<li><p>What <code>bind()</code> does</p>
</li>
<li><p>Difference between call, apply, and bind</p>
</li>
</ol>
<p>Upcoming...</p>
]]></content:encoded></item></channel></rss>