<?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[DHIRAJ]]></title><description><![CDATA[DHIRAJ]]></description><link>https://blog.dhiraj.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 00:59:15 GMT</lastBuildDate><atom:link href="https://blog.dhiraj.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Async Await in JavaScript Writing Cleaner Asynchronous Code]]></title><description><![CDATA[When we learned about asynchronous JavaScript we saw how callbacks work.
Then promises came to solve callback nesting.
But even promises can feel messy sometimes with .then() chains.
So JavaScript int]]></description><link>https://blog.dhiraj.dev/async-await-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/async-await-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Tue, 24 Mar 2026 16:26:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/3ce29b53-6fe8-4599-b252-5daa14dc50ac.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we learned about asynchronous JavaScript we saw how callbacks work.</p>
<p>Then promises came to solve callback nesting.</p>
<p>But even promises can feel messy sometimes with <code>.then()</code> chains.</p>
<p>So JavaScript introduced async and await to make things cleaner.</p>
<p>In this blog I will share how I understand async await and why it makes async code easier to read and write.</p>
<hr />
<h2>Why async await was introduced</h2>
<p>With promises we usually write code like:</p>
<pre><code class="language-javascript">fetchData()
  .then((res) =&gt; {
    return processData(res)
  })
  .then((data) =&gt; {
    console.log(data)
  })
  .catch((err) =&gt; {
    console.log(err)
  })
</code></pre>
<p>This works fine.</p>
<p>But as things grow it starts looking like a chain.</p>
<p>Harder to read.</p>
<p>So async await was introduced to make this look like normal code.</p>
<hr />
<h2>How async functions work</h2>
<p>When you add <code>async</code> before a function:</p>
<pre><code class="language-javascript">async function getData() {
  return "hello"
}
</code></pre>
<p>This function always returns a promise.</p>
<p>Even if you return a normal value.</p>
<pre><code class="language-js">getData().then(console.log) // hello
</code></pre>
<p>So:</p>
<p>async function → always returns promise</p>
<hr />
<h2>What await does</h2>
<p><code>await</code> is used inside async function.</p>
<p>It pauses the execution until promise resolves.</p>
<p>Example:</p>
<pre><code class="language-javascript">async function getData() {
  const res = await fetch("https://api.freeapi.app/api/v1/public/randomusers?page=1&amp;limit=10")
  console.log(res)
}
</code></pre>
<p>Here:</p>
<p>JavaScript waits for fetch to finish then moves to next line</p>
<p>So it feels like synchronous code.</p>
<hr />
<h2>How it actually feels</h2>
<p>Instead of:</p>
<p>Promise → then → then</p>
<p>You write:</p>
<p>Step 1 wait Step 2</p>
<hr />
<h2>Promise vs async await</h2>
<p>Promise style:</p>
<pre><code class="language-javascript">fetchData()
  .then((res) =&gt; processData(res))
  .then((data) =&gt; console.log(data))
</code></pre>
<p>Async await style:</p>
<pre><code class="language-javascript">async function run() {
  const res = await fetchData()
  const data = await processData(res)
  console.log(data)
}
</code></pre>
<p>Same thing.</p>
<p>But second one looks cleaner.</p>
<hr />
<h2>Async function flow</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/8d389d47-3fdd-4591-bdf0-f2660530d5bf.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Error handling with async await</h2>
<p>With promises:</p>
<pre><code class="language-javascript">fetchData()
  .then((res) =&gt; console.log(res))
  .catch((err) =&gt; console.log(err))
</code></pre>
<p>With async await:</p>
<pre><code class="language-javascript">async function run() {
  try {
    const res = await fetchData()
    console.log(res)
  } catch (err) {
    console.log(err)
  }
}
</code></pre>
<p>Same concept.</p>
<p>Just cleaner.</p>
<hr />
<h2>Why this matters</h2>
<p>Async await makes code:</p>
<ul>
<li><p>easier to read</p>
</li>
<li><p>easier to debug</p>
</li>
<li><p>easier to write</p>
</li>
</ul>
<p>It looks like normal step by step code even though it is async.</p>
<hr />
<h2>Important thing to remember</h2>
<ul>
<li><p>await only works inside async function</p>
</li>
<li><p>async function always returns promise</p>
</li>
</ul>
<hr />
<h2>Simple real example</h2>
<pre><code class="language-javascript">async function getUser() {
  try {
    const res = await fetch("https://api.freeapi.app/api/v1/public/randomusers?page=1&amp;limit=10")
    const data = await res.json()
    console.log(data)
  } catch (err) {
    console.log("Error fetching user")
  }
}
</code></pre>
<hr />
<h2>Mental model</h2>
<p>async await is just:</p>
<p>promise + cleaner syntax</p>
<p>Nothing new under the hood.</p>
<hr />
<p>Async await does not replace promises.</p>
<p>It is built on top of promises.</p>
<p>It just makes async code look simple and readable.</p>
]]></content:encoded></item><item><title><![CDATA[Synchronous vs Asynchronous JavaScript]]></title><description><![CDATA[When we write JavaScript most of the time code runs line by line.
One step after another.
Simple.
But in real apps things do not always happen instantly.
Sometimes you have to wait:

fetching data fro]]></description><link>https://blog.dhiraj.dev/synchronous-vs-asynchronous-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/synchronous-vs-asynchronous-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Tue, 24 Mar 2026 15:23:07 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/9bb4037c-6c0d-4270-83b2-daa113e74379.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we write JavaScript most of the time code runs line by line.</p>
<p>One step after another.</p>
<p>Simple.</p>
<p>But in real apps things do not always happen instantly.</p>
<p>Sometimes you have to wait:</p>
<ul>
<li><p>fetching data from API</p>
</li>
<li><p>loading files</p>
</li>
<li><p>timers</p>
</li>
</ul>
<p>So what happens then?</p>
<p>Does JavaScript just stop and wait?</p>
<p>No.</p>
<p>That is where synchronous and asynchronous behavior comes in.</p>
<p>In this blog I will share how I understand sync vs async in JavaScript.</p>
<hr />
<h2>What synchronous code means</h2>
<p>Synchronous means:</p>
<p>one thing at a time</p>
<p>Code runs step by step.</p>
<p>Example:</p>
<pre><code class="language-javascript">console.log("Start")
console.log("Middle")
console.log("End")
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Start
Middle
End
</code></pre>
<p>each line waits for previous one</p>
<hr />
<h2>How it feels</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/815a1612-5a4a-4503-9b90-2f1c7d46ac50.png" alt="" style="display:block;margin:0 auto" />

<p>Straight line execution</p>
<hr />
<h2>What asynchronous code means</h2>
<p>Asynchronous means:</p>
<p>do not wait move ahead</p>
<p>Example:</p>
<pre><code class="language-javascript">console.log("Start")

setTimeout(() =&gt; {
  console.log("Inside timeout")
}, 2000)

console.log("End")
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Start
End
Inside timeout
</code></pre>
<p>Even though timeout is in middle it runs later</p>
<hr />
<h2>Why JavaScript needs async</h2>
<p>Imagine this:</p>
<p>You call an API that takes 5 seconds.</p>
<p>If JavaScript waits:</p>
<p>everything freezes</p>
<ul>
<li><p>UI stuck</p>
</li>
<li><p>buttons not working</p>
</li>
<li><p>bad experience</p>
</li>
</ul>
<p>So instead JavaScript says:</p>
<p>I will start this task and continue other work</p>
<hr />
<h2>Real life example</h2>
<p>Think like ordering food:</p>
<p>Synchronous:</p>
<p>order → wait → get food → then do next thing</p>
<p>Asynchronous:</p>
<p>order → do other work → get food later</p>
<hr />
<h2>Blocking vs non blocking</h2>
<p>Synchronous:</p>
<p>blocking</p>
<p>It waits.</p>
<p>Asynchronous:</p>
<p>non blocking</p>
<p>It continues.</p>
<hr />
<h2>Problem with blocking code</h2>
<p>Let us look at this:</p>
<pre><code class="language-javascript">console.log("Start")

setTimeout(() =&gt; {
  console.log("Waited 3 seconds")
}, 3000)

console.log("End")
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Start
End
Waited 3 seconds
</code></pre>
<p>Now imagine if JavaScript waited for this like synchronous code:</p>
<pre><code class="language-plaintext">Start
(wait 3 seconds)
Waited 3 seconds
End
</code></pre>
<p>everything would pause</p>
<p>nothing else would run</p>
<p>app would feel stuck</p>
<hr />
<h2>Async flow</h2>
<p>When async task happens:</p>
<p>JavaScript sends it to background</p>
<p>continues execution</p>
<p>later runs the function</p>
<hr />
<h2>Async mental model</h2>
<p>Diagram idea:</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/6b7bedc1-062e-4a8e-848a-a5aa1efc6f96.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Simple async example</h2>
<pre><code class="language-javascript">setTimeout(() =&gt; {
  console.log("Done")
}, 1000)
</code></pre>
<p>runs after delay</p>
<hr />
<h2>Where you see async</h2>
<ul>
<li><p>API calls</p>
</li>
<li><p>database queries</p>
</li>
<li><p>timers</p>
</li>
<li><p>file operations</p>
</li>
</ul>
<hr />
<h2>Why this matters</h2>
<p>Without async:</p>
<p>apps would freeze</p>
<p>With async:</p>
<p>smooth experience</p>
<hr />
<h2>Quick comparison</h2>
<p>Synchronous:</p>
<ul>
<li><p>step by step</p>
</li>
<li><p>blocking</p>
</li>
</ul>
<p>Asynchronous:</p>
<ul>
<li><p>runs in background</p>
</li>
<li><p>non blocking</p>
</li>
</ul>
<hr />
<p>JavaScript is single threaded.</p>
<p>So it cannot do everything at once.</p>
<p>Async is how it handles waiting tasks without stopping everything.</p>
]]></content:encoded></item><item><title><![CDATA[Error Handling in JavaScript: Try, Catch, Finally]]></title><description><![CDATA[When we write JavaScript, we expect things to work.
But in real world apps, things break.
Maybe:

API fails

variable is undefined

wrong data comes


And when something breaks, JavaScript throws an e]]></description><link>https://blog.dhiraj.dev/error-handling-in-javascript-try-catch-finally</link><guid isPermaLink="true">https://blog.dhiraj.dev/error-handling-in-javascript-try-catch-finally</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[error handling]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Tue, 24 Mar 2026 14:47:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/a27da5f2-4ff9-4a47-8b60-5aaff4b08324.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we write JavaScript, we expect things to work.</p>
<p>But in real world apps, things break.</p>
<p>Maybe:</p>
<ul>
<li><p>API fails</p>
</li>
<li><p>variable is undefined</p>
</li>
<li><p>wrong data comes</p>
</li>
</ul>
<p>And when something breaks, JavaScript throws an error and stops execution.</p>
<p>That’s not good for users.</p>
<p>So we need a way to handle errors properly.</p>
<p>In this blog I will share how I understand error handling using try, catch and finally.</p>
<hr />
<h2>What errors are in JavaScript</h2>
<p>Errors are situations where something goes wrong during execution.</p>
<p>Example:</p>
<pre><code class="language-javascript">console.log(x)
</code></pre>
<p>Output:</p>
<pre><code class="language-id=&quot;k1l0m8&quot;">ReferenceError: x is not defined
</code></pre>
<p>Here:</p>
<p>JavaScript stops execution</p>
<p>So if this happens in real app, user experience breaks.</p>
<hr />
<h2>Why error handling matters</h2>
<p>Instead of crashing the app, we can handle the error and continue.</p>
<p>show message, log error, fallback behavior</p>
<p>So error handling is about:</p>
<p>graceful failure</p>
<hr />
<h2>Using try and catch</h2>
<p>Basic idea:</p>
<p>try the code if error happens → catch it</p>
<p>Example:</p>
<pre><code class="language-javascript">try {
  console.log(x)
} catch (error) {
  console.log("Something went wrong")
}
</code></pre>
<p>Now:</p>
<p>instead of crash → we handle it</p>
<hr />
<h2>How it actually flows</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/7740f4ae-99c5-4a14-b41e-7c3228e59aa6.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Accessing the error</h2>
<pre><code class="language-javascript">try {
  console.log(x)
} catch (error) {
  console.log(error.message)
}
</code></pre>
<p><code>error</code> contains details</p>
<p>Useful for debugging.</p>
<hr />
<h2>The finally block</h2>
<p>Finally always runs.</p>
<p>No matter what.</p>
<p>Example:</p>
<pre><code class="language-javascript">try {
  console.log("Try running")
} catch (err) {
  console.log("Error")
} finally {
  console.log("Always runs")
}
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Try running
Always runs
</code></pre>
<p>Even if error happens:</p>
<p>finally still runs</p>
<hr />
<h2>Why finally is useful</h2>
<p>Use it for:</p>
<ul>
<li><p>cleanup</p>
</li>
<li><p>closing resources</p>
</li>
<li><p>final steps</p>
</li>
</ul>
<hr />
<h2>Try → Catch → Finally flow</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/550d3913-6200-466d-a8fd-e8e53b536e74.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Throwing custom errors</h2>
<p>Sometimes we want to create our own error.</p>
<pre><code class="language-javascript">function checkAge(age) {
  if (age &lt; 18) {
    throw new Error("Age must be 18+")
  }

  console.log("Access granted")
}

try {
  checkAge(16)
} catch (err) {
  console.log(err.message)
}
</code></pre>
<p>Here:</p>
<p>we manually throw error</p>
<hr />
<h2>Why throw is useful</h2>
<ul>
<li><p>validate data</p>
</li>
<li><p>enforce rules</p>
</li>
<li><p>control flow</p>
</li>
</ul>
<hr />
<h2>Real world feel</h2>
<p>Instead of app crashing:</p>
<p>we control what happens when something goes wrong</p>
<hr />
<h2>Common pattern</h2>
<pre><code class="language-javascript">try {
  // risky code
} catch (err) {
  // handle error
} finally {
  // always run
}
</code></pre>
<hr />
<h2>Important thing to remember</h2>
<ul>
<li><p>try only catches runtime errors</p>
</li>
<li><p>syntax errors won’t be caught</p>
</li>
</ul>
<hr />
<p>Errors will happen.</p>
<p>That’s normal.</p>
<p>What matters is how you handle them.</p>
<p>Using try, catch, and finally helps you:</p>
<p>prevent crashes</p>
<p>debug better</p>
<p>build stable apps</p>
]]></content:encoded></item><item><title><![CDATA[The `new` Keyword in JavaScript]]></title><description><![CDATA[In JavaScript, you might have seen new in code like:
const user = new User()

We use it, but many times we don’t really know what it’s doing internally.
It feels like it just creates an object.
But ac]]></description><link>https://blog.dhiraj.dev/the-new-keyword-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/the-new-keyword-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Tue, 24 Mar 2026 14:25:53 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/b0a74cf3-df52-46a0-8d40-47a435d24b29.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In JavaScript, you might have seen <code>new</code> in code like:</p>
<pre><code class="language-javascript">const user = new User()
</code></pre>
<p>We use it, but many times we don’t really know what it’s doing internally.</p>
<p>It feels like it just creates an object.</p>
<p>But actually, a lot of things are happening behind the scenes.</p>
<p>In this blog I will share how I understand the <code>new</code> keyword and what really happens when we use it.</p>
<hr />
<h2>What the <code>new</code> keyword does</h2>
<p>When you use <code>new</code>, JavaScript creates a new object for you.</p>
<p>But not just that.</p>
<p>It also connects things, sets values and returns that object.</p>
<p>So <code>new</code> is doing multiple steps, not just one.</p>
<hr />
<h2>Constructor functions</h2>
<p>To use <code>new</code>, we usually have something called a constructor function.</p>
<p>Example:</p>
<pre><code class="language-javascript">function User(name, age) {
  this.name = name
  this.age = age
}
</code></pre>
<p>Now we use:</p>
<pre><code class="language-javascript">const user1 = new User("Dhiraj", 20)
</code></pre>
<p>Here:</p>
<p><code>User</code> is a constructor --&gt; <code>user1</code> is an instance</p>
<hr />
<h2>How it actually feels</h2>
<p>constructor = blueprint</p>
<p>instance = real object</p>
<hr />
<h2>Object creation step by step</h2>
<p>When we do:</p>
<pre><code class="language-javascript">const user1 = new User("Dhiraj", 20)
</code></pre>
<p>JavaScript does this internally:</p>
<h3>Step 1: create empty object</h3>
<pre><code class="language-js">{}
</code></pre>
<hr />
<h3>Step 2: set <code>this</code> to that object</h3>
<p>Inside function:</p>
<pre><code class="language-plaintext">this → {}
</code></pre>
<hr />
<h3>Step 3: add properties</h3>
<pre><code class="language-javascript">this.name = "Dhiraj"
this.age = 20
</code></pre>
<p>Now object becomes:</p>
<pre><code class="language-plaintext">{ name: "Dhiraj", age: 20 }
</code></pre>
<hr />
<h3>Step 4: link prototype</h3>
<pre><code class="language-javascript">user1.__proto__ = User.prototype
</code></pre>
<p>so it can access shared methods</p>
<hr />
<h3>Step 5: return object</h3>
<p>Final result:</p>
<pre><code class="language-js">user1
</code></pre>
<hr />
<h2>Constructor → instance flow</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/1a7d4c6f-2dd7-4f75-a098-b5d595e5f108.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>How prototype is linked</h2>
<pre><code class="language-javascript">function User(name) {
  this.name = name
}

User.prototype.sayHi = function () {
  console.log("Hi " + this.name)
}

const user1 = new User("Dhiraj")
user1.sayHi()
</code></pre>
<p>Why this works?</p>
<p>because <code>user1</code> is linked to <code>User.prototype</code></p>
<p>So it can access <code>sayHi</code></p>
<hr />
<h2>Prototype visual idea</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/3e83b107-905a-4a8b-8d30-5d444ae37b5f.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Instances created from constructors</h2>
<pre><code class="language-javascript">const u1 = new User("A")
const u2 = new User("B")
</code></pre>
<p>both are different objects</p>
<p>But:</p>
<p>share same prototype</p>
<p>So methods are not duplicated.</p>
<hr />
<h2>Why <code>new</code> is useful</h2>
<p>Without <code>new</code>, you would have to manually:</p>
<ul>
<li><p>create object</p>
</li>
<li><p>assign values</p>
</li>
<li><p>link prototype</p>
</li>
</ul>
<p><code>new</code> does all that automatically.</p>
<hr />
<h2>What happens if we don’t use <code>new</code></h2>
<pre><code class="language-javascript">const user = User("Dhiraj", 20)
</code></pre>
<p>Now:</p>
<p><code>this</code> = global object</p>
<p>So it can break things.</p>
<p>That’s why <code>new</code> is important here.</p>
<hr />
<h2>Simple way to remember</h2>
<p><code>new</code> does:</p>
<p>create object --&gt; bind <code>this</code> --&gt; link prototype --&gt; return object</p>
]]></content:encoded></item><item><title><![CDATA[Spread vs Rest Operators in JavaScript]]></title><description><![CDATA[In JavaScript, you might have seen these three dots ...
Sometimes they behave like magic.
Same syntax, but different use.
Sometimes it spreads values Sometimes it collects values
So what’s actually ha]]></description><link>https://blog.dhiraj.dev/spread-vs-rest-operators-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/spread-vs-rest-operators-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Tue, 24 Mar 2026 13:24:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/70b7ca41-0a6b-4748-a38e-40abdccadf1f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In JavaScript, you might have seen these three dots <code>...</code></p>
<p>Sometimes they behave like magic.</p>
<p>Same syntax, but different use.</p>
<p>Sometimes it spreads values Sometimes it collects values</p>
<p>So what’s actually happening?</p>
<p>In this blog I will share how I understand spread and rest operators and how to use them properly.</p>
<hr />
<h2>What is spread operator</h2>
<p>Spread means:</p>
<p>take something and expand it</p>
<p>Example:</p>
<pre><code class="language-javascript">const arr = [1, 2, 3]

console.log(...arr)
</code></pre>
<p>Output:</p>
<pre><code class="language-id=&quot;d4e5f6&quot;">1 2 3
</code></pre>
<p>array got expanded</p>
<hr />
<h2>Spread with arrays</h2>
<pre><code class="language-javascript">const arr1 = [1, 2]
const arr2 = [3, 4]

const newArr = [...arr1, ...arr2]
</code></pre>
<p>Result:</p>
<pre><code class="language-id=&quot;j1k2l3&quot;">[1, 2, 3, 4]
</code></pre>
<p>combining arrays becomes easy</p>
<hr />
<h2>Spread with objects</h2>
<pre><code class="language-javascript">const user = { name: "Dhiraj" }
const updatedUser = { ...user, age: 20 }
</code></pre>
<p>copies old object + adds new data</p>
<hr />
<h2>Spread idea</h2>
<p><strong>Diagram idea:</strong></p>
<p>[1, 2, 3] → 1, 2, 3</p>
<p>(expanding values)</p>
<hr />
<h2>What is rest operator</h2>
<p>Rest means:</p>
<p>collect multiple values into one</p>
<p>Example:</p>
<pre><code class="language-javascript">function sum(...nums) {
  console.log(nums)
}

sum(1, 2, 3)
</code></pre>
<p>Output:</p>
<pre><code class="language-id=&quot;s1t2u3&quot;">[1, 2, 3]
</code></pre>
<p>values collected into array</p>
<hr />
<h2>Rest in arrays</h2>
<pre><code class="language-javascript">const arr = [1, 2, 3, 4]

const [first, ...rest] = arr
</code></pre>
<p>So:</p>
<ul>
<li><p><code>first = 1</code></p>
</li>
<li><p><code>rest = [2, 3, 4]</code></p>
</li>
</ul>
<hr />
<h2>Rest idea</h2>
<p><strong>Diagram idea:</strong></p>
<p>1, 2, 3, 4 → [1, ...rest]</p>
<p>(rest collects remaining)</p>
<hr />
<h2>Spread vs Rest (main difference)</h2>
<p>Both use <code>...</code> but:</p>
<p>Spread:</p>
<p>expands values</p>
<p>Rest:</p>
<p>collects values</p>
<p>Simple way:</p>
<p>Spread = open</p>
<p>Rest = gather</p>
<hr />
<h2>When to use spread</h2>
<p>Use spread when:</p>
<ul>
<li><p>combining arrays</p>
</li>
<li><p>copying objects</p>
</li>
<li><p>adding values</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-javascript">const arr = [1, 2]
const newArr = [...arr, 3]
</code></pre>
<hr />
<h2>When to use rest</h2>
<p>Use rest when:</p>
<ul>
<li><p>function arguments</p>
</li>
<li><p>extracting remaining values</p>
</li>
<li><p>handling unknown number of inputs</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-javascript">function log(first, ...others) {
  console.log(first)
  console.log(others)
}
</code></pre>
<hr />
<h2>Real world feeling</h2>
<p>Spread:</p>
<p>breaking things apart</p>
<p>Rest:</p>
<p>grouping things together</p>
<hr />
<h2>Small practical example</h2>
<pre><code class="language-javascript">const user = {
  name: "Dhiraj",
  age: 20,
  city: "Kolkata"
}

const { name, ...rest } = user
</code></pre>
<p><code>name</code> separate, rest contains remaining data</p>
<hr />
<p>Spread and rest look same but do opposite things.</p>
<p>Spread expands</p>
<p>Rest collects</p>
]]></content:encoded></item><item><title><![CDATA[Destructuring in JavaScript]]></title><description><![CDATA[When we work with arrays and objects in JavaScript, we often need to take values out of them.
Normally we do something like:
const user = {
  name: "Dhiraj",
  age: 20
}

const name = user.name
const ]]></description><link>https://blog.dhiraj.dev/destructuring-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/destructuring-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Tue, 24 Mar 2026 12:34:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/f55bc015-3d63-419c-8338-6e650392fc15.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we work with arrays and objects in JavaScript, we often need to take values out of them.</p>
<p>Normally we do something like:</p>
<pre><code class="language-javascript">const user = {
  name: "Dhiraj",
  age: 20
}

const name = user.name
const age = user.age
</code></pre>
<p>Works fine.</p>
<p>But it feels repetitive.</p>
<p>So JavaScript gives us a cleaner way to do this.</p>
<p>That is destructuring.</p>
<p>In this blog I will share how I understand destructuring and how it makes code simpler.</p>
<hr />
<h2>What destructuring means</h2>
<p>Destructuring means:</p>
<p>taking values out of an array or object and assigning them to variables</p>
<p>In short:</p>
<p>extract → assign</p>
<hr />
<h2>Destructuring objects</h2>
<p>Instead of this:</p>
<pre><code class="language-javascript">const user = {
  name: "Dhiraj",
  age: 20
}

const name = user.name
const age = user.age
</code></pre>
<p>We can do:</p>
<pre><code class="language-javascript">const { name, age } = user
</code></pre>
<p>That’s it.</p>
<p>less code, same result</p>
<hr />
<h2>How it actually feels</h2>
<p>Object → variables</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/e1486a5b-5ace-4a4c-a8c9-6acdfdbbbc98.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Important thing</h2>
<p>Variable name must match key:</p>
<pre><code class="language-javascript">const { name } = user
</code></pre>
<p>takes <code>user.name</code></p>
<hr />
<h2>Destructuring arrays</h2>
<p>Arrays work a bit differently.</p>
<pre><code class="language-javascript">const arr = [10, 20, 30]

const [a, b, c] = arr
</code></pre>
<p>So:</p>
<ul>
<li><p><code>a = 10</code></p>
</li>
<li><p><code>b = 20</code></p>
</li>
<li><p><code>c = 30</code></p>
</li>
</ul>
<p>Here it depends on position.</p>
<hr />
<h2>Array mapping idea</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/c53971a4-9d4b-4442-818b-2f1a70e3d7e8.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Skipping values</h2>
<pre><code class="language-javascript">const arr = [10, 20, 30]

const [a, , c] = arr
</code></pre>
<p>skips middle value</p>
<hr />
<h2>Default values</h2>
<p>Sometimes value may not exist.</p>
<pre><code class="language-javascript">const user = {
  name: "Dhiraj"
}

const { name, age = 18 } = user
</code></pre>
<p>if <code>age</code> not present → use default</p>
<hr />
<h2>Before vs After</h2>
<p>Before:</p>
<pre><code class="language-javascript">const title = post.title
const author = post.author
const date = post.date
</code></pre>
<p>After:</p>
<pre><code class="language-javascript">const { title, author, date } = post
</code></pre>
<p>cleaner, less repetition</p>
<hr />
<h2>Benefits of destructuring</h2>
<ul>
<li><p>reduces repetitive code</p>
</li>
<li><p>makes code cleaner</p>
</li>
<li><p>easier to read</p>
</li>
<li><p>faster to write</p>
</li>
</ul>
<hr />
<h2>Small real example</h2>
<pre><code class="language-javascript">function greet(user) {
  const { name } = user
  console.log("Hi " + name)
}
</code></pre>
<p>Even shorter:</p>
<pre><code class="language-javascript">function greet({ name }) {
  console.log("Hi " + name)
}
</code></pre>
<hr />
<h2>Where you will see it a lot</h2>
<ul>
<li><p>React props</p>
</li>
<li><p>API responses</p>
</li>
<li><p>function parameters</p>
</li>
<li><p>arrays from functions</p>
</li>
</ul>
<hr />
<p>Destructuring is just a cleaner way to take values out of arrays and objects.</p>
<p>Once you start using it, going back feels weird.</p>
<p>It doesn’t add new power, just makes things simpler.</p>
]]></content:encoded></item><item><title><![CDATA[String Polyfills and Common Interview Methods in JavaScript]]></title><description><![CDATA[In JavaScript we use string methods all the time.
includes(), slice(), toUpperCase() we just use them and move on.
But in interviews or real problem solving, you might get asked:
how does this work in]]></description><link>https://blog.dhiraj.dev/string-polyfills-and-common-interview-methods-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/string-polyfills-and-common-interview-methods-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[polyfills]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Tue, 24 Mar 2026 12:17:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/8ca4b1dd-4f0a-4866-8893-f836e2ba9e20.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In JavaScript we use string methods all the time.</p>
<p><code>includes()</code>, <code>slice()</code>, <code>toUpperCase()</code> we just use them and move on.</p>
<p>But in interviews or real problem solving, you might get asked:</p>
<p>how does this work internally ?</p>
<p>can you implement it yourself ?</p>
<p>That’s where polyfills come in.</p>
<p>In this blog I’ll share how I understand string methods, how they actually work behind the scenes and how to approach them in interviews.</p>
<hr />
<h2>What string methods are</h2>
<p>String methods are just functions attached to strings.</p>
<pre><code class="language-javascript">"hello".toUpperCase()
</code></pre>
<p>Looks simple.</p>
<p>But internally it’s just:</p>
<p>loop over characters --&gt; change something --&gt; return new string</p>
<p>Nothing magical.</p>
<hr />
<h2>How to think about it</h2>
<p>Every string method is basically:</p>
<p>input string --&gt; process --&gt; output string</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/8b1eb99f-4c17-41c8-9ac2-3c740690fe34.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Why developers write polyfills</h2>
<p>Polyfill = writing your own version of built in method.</p>
<p>Why?</p>
<ul>
<li><p>to understand how it works</p>
</li>
<li><p>interview questions</p>
</li>
<li><p>improve logic building</p>
</li>
</ul>
<p>So it’s less about code, more about thinking.</p>
<hr />
<h2>Important: where methods actually live</h2>
<p>All string methods come from:</p>
<pre><code class="language-javascript">String.prototype
</code></pre>
<p>So when you do:</p>
<pre><code class="language-javascript">"hello".includes("he")
</code></pre>
<p>it’s actually using <code>String.prototype.includes</code></p>
<p>So real polyfills are written like:</p>
<p>adding your own method to prototype</p>
<hr />
<h2>Polyfill example (includes)</h2>
<pre><code class="language-javascript">String.prototype.myIncludes = function (word) {
  const str = this

  for (let i = 0; i &lt;= str.length - word.length; i++) {
    let match = true

    for (let j = 0; j &lt; word.length; j++) {
      if (str[i + j] !== word[j]) {
        match = false
        break
      }
    }

    if (match) return true
  }

  return false
}
</code></pre>
<p>Usage:</p>
<pre><code class="language-javascript">"hello".myIncludes("he") // true
</code></pre>
<p>What’s happening:</p>
<p>we slide over string match character by character</p>
<hr />
<h2>Polyfill example (startsWith)</h2>
<pre><code class="language-javascript">String.prototype.myStartsWith = function (word) {
  const str = this

  for (let i = 0; i &lt; word.length; i++) {
    if (str[i] !== word[i]) {
      return false
    }
  }

  return true
}
</code></pre>
<hr />
<h2>Why <code>this</code> is important here</h2>
<p>Inside prototype:</p>
<p><code>this</code> = the string calling the method</p>
<pre><code class="language-javascript">"hello".myIncludes("he")
</code></pre>
<p><code>this</code> = <code>"hello"</code></p>
<hr />
<h2>Implementing simple string utilities</h2>
<p>Most string utilities are just loops.</p>
<h3>Reverse string</h3>
<pre><code class="language-javascript">function reverse(str) {
  let res = ""

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

  return res
}
</code></pre>
<hr />
<h3>Check palindrome</h3>
<pre><code class="language-javascript">function isPalindrome(str) {
  return str === reverse(str)
}
</code></pre>
<hr />
<h3>Count characters</h3>
<pre><code class="language-javascript">function countChars(str) {
  const map = {}

  for (let char of str) {
    map[char] = (map[char] || 0) + 1
  }

  return map
}
</code></pre>
<hr />
<h2>Common interview mindset</h2>
<p>When interviewer asks:</p>
<p>“implement includes / slice / reverse”</p>
<p>They are checking:</p>
<ul>
<li><p>can you break problem</p>
</li>
<li><p>can you use loops</p>
</li>
<li><p>can you think step by step</p>
</li>
</ul>
<p>Not memorization.</p>
<hr />
<h2>Polyfill behavior</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/650b8e92-240b-4583-a2fe-b49c4b421d92.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Importance of understanding built in behavior</h2>
<p>If you only use methods:</p>
<p>you know usage</p>
<p>If you understand internals:</p>
<p>you know control</p>
<p>That helps in:</p>
<ul>
<li><p>interviews</p>
</li>
<li><p>debugging</p>
</li>
<li><p>writing custom logic</p>
</li>
</ul>
<hr />
<h2>One important note</h2>
<p>In real projects:</p>
<p>don’t modify <code>String.prototype</code></p>
<p>But for:</p>
<p>learning, interviews</p>
<p>this is exactly what you should do.</p>
<hr />
<p>String methods are simple once you break them.</p>
<p>They are just loops + conditions.</p>
<p>Polyfills help you see that clearly.</p>
]]></content:encoded></item><item><title><![CDATA[Map and Set in JavaScript]]></title><description><![CDATA[In JavaScript, we usually use objects and arrays for most things.
Objects for key value data, Arrays for lists
But sometimes these are not enough.
Like:

you want keys that are not just strings

you w]]></description><link>https://blog.dhiraj.dev/map-and-set-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/map-and-set-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Mon, 23 Mar 2026 15:41:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/2709bb04-c028-43bd-9374-0a792fdcf97c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In JavaScript, we usually use objects and arrays for most things.</p>
<p>Objects for key value data, Arrays for lists</p>
<p>But sometimes these are not enough.</p>
<p>Like:</p>
<ul>
<li><p>you want keys that are not just strings</p>
</li>
<li><p>you want to store unique values only</p>
</li>
<li><p>you want better control over data</p>
</li>
</ul>
<p>That’s where Map and Set come into the picture.</p>
<p>In this blog I will share how I understand Map and Set and when I actually use them.</p>
<hr />
<h2>Problem with objects and arrays</h2>
<p>Before jumping into Map and Set, let’s see the problem.</p>
<h3>Objects problem</h3>
<pre><code class="language-javascript">const obj = {}

obj[1] = "one"
obj["1"] = "string one"

console.log(obj)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">{ '1': 'string one' }
</code></pre>
<p>Here:</p>
<p>number <code>1</code> and string <code>"1"</code> become same key</p>
<p>So object forces keys to be strings.</p>
<hr />
<h3>Arrays problem</h3>
<pre><code class="language-javascript">const arr = [1, 2, 2, 3]
</code></pre>
<p>Here:</p>
<p>duplicates are allowed</p>
<p>Sometimes we don’t want that.</p>
<hr />
<h2>What is Map</h2>
<p>Map is like an object but better.</p>
<p>It stores data in key value pairs, but:</p>
<p>keys can be anything (number, string, object, function)</p>
<p>Example:</p>
<pre><code class="language-javascript">const map = new Map()

map.set(1, "one")
map.set("1", "string one")

console.log(map)
</code></pre>
<pre><code class="language-plaintext">Map(1) { 1 =&gt; 'one' }
Map(2) { 1 =&gt; 'one', '1' =&gt; 'string one' }
</code></pre>
<p>Here both keys are different.</p>
<p>So:</p>
<p>Map does not convert keys to string</p>
<hr />
<h2>How Map feels</h2>
<p>Think like:</p>
<p>Map = advanced key value storage</p>
<hr />
<h2>Getting values from Map</h2>
<pre><code class="language-js">map.get(1)       // "one"
map.get("1")     // "string one"
</code></pre>
<hr />
<h2>Map key value visual</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/a306a392-2c55-4ba9-9e91-3e424a1bff40.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>What is Set</h2>
<p>Set is used to store only unique values.</p>
<p>That’s its main job.</p>
<p>Example:</p>
<pre><code class="language-javascript">const set = new Set([1, 2, 2, 3])

console.log(set)
</code></pre>
<p>Output:</p>
<pre><code class="language-id=&quot;y7d2hb&quot;">{1, 2, 3}
</code></pre>
<p>duplicates automatically removed</p>
<hr />
<h2>How Set feels</h2>
<p>Set = unique collection</p>
<p>No duplicates allowed</p>
<hr />
<h2>Adding values in Set</h2>
<pre><code class="language-javascript">const set = new Set()

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

console.log(set)
</code></pre>
<p>Still:</p>
<pre><code class="language-id=&quot;p0v9xz&quot;">{1, 2}
</code></pre>
<hr />
<h2>Set visual</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/3e685d50-41fe-4e1d-9151-d60704414901.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Map vs Object</h2>
<p>Both store key value, but:</p>
<p>Object:</p>
<ul>
<li><p>keys become string</p>
</li>
<li><p>limited flexibility</p>
</li>
</ul>
<p>Map:</p>
<ul>
<li><p>keys can be anything</p>
</li>
<li><p>keeps original type</p>
</li>
<li><p>better for dynamic data</p>
</li>
</ul>
<p>Simple line:</p>
<p>Object = basic</p>
<p>Map = flexible</p>
<hr />
<h2>Set vs Array</h2>
<p>Array:</p>
<ul>
<li><p>allows duplicates</p>
</li>
<li><p>used for ordered list</p>
</li>
</ul>
<p>Set:</p>
<ul>
<li><p>only unique values</p>
</li>
<li><p>removes duplicates automatically</p>
</li>
</ul>
<p>Simple line:</p>
<p>Array = list</p>
<p>Set = unique list</p>
<hr />
<h2>When to use Map</h2>
<p>Use Map when:</p>
<ul>
<li><p>keys are not just strings</p>
</li>
<li><p>you need key value structure</p>
</li>
<li><p>keys can be objects or numbers</p>
</li>
<li><p>you want more control than object</p>
</li>
</ul>
<p>Example use case:</p>
<pre><code class="language-javascript">const user1 = { name: "A" }
const user2 = { name: "B" }

const map = new Map()

map.set(user1, "data1")
map.set(user2, "data2")
</code></pre>
<hr />
<h2>When to use Set</h2>
<p>Use Set when:</p>
<ul>
<li><p>you want unique values</p>
</li>
<li><p>removing duplicates</p>
</li>
<li><p>checking if value already exists</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-javascript">const nums = [1, 2, 2, 3]

const unique = new Set(nums)

console.log([...unique])
</code></pre>
<hr />
<p>Map and Set are not used everywhere, but when needed, they make things much cleaner.</p>
<p>Map solves key limitations of objects</p>
<p>Set solves duplicate problems of arrays</p>
]]></content:encoded></item><item><title><![CDATA[Understanding the `this` Keyword in JavaScript]]></title><description><![CDATA[When we start writing JavaScript, everything feels simple. Variables, functions, objects all good.
Then suddenly we see this.
And things start getting confusing.
Sometimes it works, sometimes it doesn]]></description><link>https://blog.dhiraj.dev/understanding-the-this-keyword-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/understanding-the-this-keyword-in-javascript</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[this keyword]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Mon, 23 Mar 2026 15:05:54 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/7da2d15f-c8ec-4f2c-bbf6-b35c40bd8110.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we start writing JavaScript, everything feels simple. Variables, functions, objects all good.</p>
<p>Then suddenly we see <code>this</code>.</p>
<p>And things start getting confusing.</p>
<p>Sometimes it works, sometimes it doesn’t. Same code, different output.</p>
<p>So what actually is <code>this</code>?</p>
<p>In this blog I will share how I understand <code>this</code> and why it behaves differently in different places.</p>
<hr />
<h2>What <code>this</code> actually represents</h2>
<p>The easiest way to think about <code>this</code> is:</p>
<p><code>this</code> means <strong>who is calling this function</strong></p>
<p>Not where the function is written Not where it is defined</p>
<p>But <strong>who is calling it</strong></p>
<p>That’s the whole game.</p>
<hr />
<h2><code>this</code> in global context</h2>
<p>If you use <code>this</code> outside any function:</p>
<pre><code class="language-javascript">console.log(this)
</code></pre>
<p>In browser, this points to:</p>
<p><code>window</code></p>
<p>So here:</p>
<pre><code class="language-javascript">this === window // true
</code></pre>
<p>So global level → <code>this</code> = global object</p>
<hr />
<h2><code>this</code> inside an object</h2>
<p>Now the interesting part.</p>
<pre><code class="language-javascript">const user = {
  name: "Dhiraj",
  greet() {
    console.log(this.name)
  }
}

user.greet()
</code></pre>
<p>Output:</p>
<pre><code class="language-id=&quot;m3yazk&quot;">Dhiraj
</code></pre>
<p>Why?</p>
<p>Because:</p>
<p><code>user</code> is calling <code>greet()</code></p>
<p>So:</p>
<p><code>this</code> = <code>user</code></p>
<p>Simple.</p>
<hr />
<h2><code>this</code> inside a normal function</h2>
<p>Now this is where confusion starts.</p>
<pre><code class="language-javascript">function show() {
  console.log(this)
}

show()
</code></pre>
<p>Here:</p>
<p>No object is calling this function</p>
<p>So in browser:</p>
<p><code>this</code> = <code>window</code></p>
<p>Because it falls back to global object.</p>
<hr />
<h2>Same function, different caller</h2>
<p>Now see this carefully:</p>
<pre><code class="language-javascript">const obj1 = {
  name: "A",
  show() {
    console.log(this.name)
  }
}

const obj2 = {
  name: "B",
  show: obj1.show
}

obj1.show() // ?
obj2.show() // ?
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">A
B
</code></pre>
<p>Same function.</p>
<p>Different output.</p>
<p>Why?</p>
<p>Because caller changed</p>
<p>So:</p>
<ul>
<li><p><code>obj1.show()</code> → <code>this</code> = obj1</p>
</li>
<li><p><code>obj2.show()</code> → <code>this</code> = obj2</p>
</li>
</ul>
<p>This proves:</p>
<p><code>this</code> depends on caller, not function</p>
<hr />
<h2>Calling context changes everything</h2>
<p>Let’s break it more.</p>
<pre><code class="language-javascript">const user = {
  name: "Dhiraj",
  greet() {
    console.log(this.name)
  }
}

const fn = user.greet
fn()
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">undefined
</code></pre>
<p>Why?</p>
<p>Because now:</p>
<p>function is called directly no object is calling it</p>
<p>So:</p>
<p><code>this</code> = window window.name is undefined</p>
<p>That’s why output is undefined</p>
<hr />
<h2>Simple way to remember</h2>
<p>Just ask:</p>
<p>Who is calling this function?</p>
<p>That’s your <code>this</code>.</p>
<hr />
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/9c334dc5-ee8c-4eed-9c99-5ecad52e2834.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>One more simple example</h2>
<pre><code class="language-javascript">const car = {
  brand: "BMW",
  show() {
    console.log(this.brand)
  }
}

car.show()
</code></pre>
<p>car is calling → <code>this</code> = car</p>
<hr />
<h2>Where people get confused</h2>
<p>Main confusion happens when:</p>
<ul>
<li><p>function is detached from object</p>
</li>
<li><p>function is passed somewhere</p>
</li>
<li><p>function is called without object</p>
</li>
</ul>
<p>Because then:</p>
<p><code>this</code> changes</p>
<hr />
<p><code>this</code> is not complicated.</p>
<p>We just overthink it.</p>
<p>Just remember one line:</p>
<p><code>this</code> = who is calling the function</p>
]]></content:encoded></item><item><title><![CDATA[Callbacks in JavaScript: Why They Exist]]></title><description><![CDATA[When we write JavaScript, we usually call functions and get results immediately. Everything feels simple you call something, it runs and gives back output.
But in real world apps, things are not alway]]></description><link>https://blog.dhiraj.dev/callbacks-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/callbacks-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Mon, 23 Mar 2026 13:32:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/c5035f38-b12c-4f67-8427-e7b59dc4dd6d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we write JavaScript, we usually call functions and get results immediately. Everything feels simple you call something, it runs and gives back output.</p>
<p>But in real world apps, things are not always instant.</p>
<p>Sometimes you are fetching data from an API, reading a file or waiting for some operation to finish. These things take time. JavaScript doesn’t want to stop everything and wait for that.</p>
<p>So how does it handle this?</p>
<p>That’s where callbacks come into the picture.</p>
<p>In this blog I will share how I understand callbacks, why they exist and where they are actually used.</p>
<hr />
<h2>Functions as values</h2>
<p>Before callbacks, we need to understand one simple thing.</p>
<p>In JavaScript, functions are just values.</p>
<p>That means you can:</p>
<ul>
<li><p>store them in variables</p>
</li>
<li><p>pass them to other functions</p>
</li>
<li><p>return them from functions</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-javascript">function sayHello() {
  console.log("Hello")
}

function greet(fn) {
  fn()
}

greet(sayHello)
</code></pre>
<p>Here we are passing a function inside another function.</p>
<p>So this is the base of callbacks.</p>
<hr />
<h2>What is a callback function</h2>
<p>A callback is just a function that you pass into another function so it can be called later.</p>
<p>That’s it.</p>
<p>Example:</p>
<pre><code class="language-javascript">function greet(name, callback) {
  console.log("Hi " + name)
  callback()
}

function sayBye() {
  console.log("Bye")
}

greet("Dhiraj", sayBye)
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>sayBye</code> is passed as a callback</p>
</li>
<li><p><code>greet</code> decides when to call it</p>
</li>
</ul>
<p>So callback means:</p>
<p>“I’ll give you this function, you call it when needed”</p>
<hr />
<h2>Why callbacks exist</h2>
<p>Now the important part.</p>
<p>Why do we even need callbacks?</p>
<p>Because some tasks take time.</p>
<p>Example:</p>
<ul>
<li><p>fetching data from server</p>
</li>
<li><p>reading files</p>
</li>
<li><p>timers</p>
</li>
<li><p>database queries</p>
</li>
</ul>
<p>JavaScript is single threaded, so it cannot block everything and wait.</p>
<p>Instead, it says:</p>
<p>“Start this task and when it finishes, call this function”</p>
<p>That function is the callback.</p>
<hr />
<h2>Simple async example</h2>
<pre><code class="language-javascript">setTimeout(() =&gt; {
  console.log("This runs later")
}, 2000)

console.log("This runs first")
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">This runs first
This runs later
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>setTimeout</code> takes a function</p>
</li>
<li><p>runs it after 2 seconds</p>
</li>
</ul>
<p>That function is a callback.</p>
<p>So flow becomes:</p>
<p>You give a function → JS runs it later</p>
<hr />
<h2>Callback in real use</h2>
<p>Let’s say you are fetching data:</p>
<pre><code class="language-javascript">function getData(callback) {
  setTimeout(() =&gt; {
    console.log("Data received")
    callback()
  }, 2000)
}

getData(() =&gt; {
  console.log("Process data")
})
</code></pre>
<p>Here:</p>
<ul>
<li><p>first data comes</p>
</li>
<li><p>then callback runs</p>
</li>
</ul>
<p>So instead of waiting, we say:</p>
<p>“When data is ready, then do this”</p>
<hr />
<h2>How it actually flows</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/39b4fea7-fdf1-4918-b59c-de40f674e2cc.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Passing functions as arguments</h2>
<p>This is the main thing behind callbacks.</p>
<pre><code class="language-javascript">function doSomething(fn) {
  console.log("Doing something")
  fn()
}

doSomething(() =&gt; {
  console.log("Callback called")
})
</code></pre>
<p>So:</p>
<ul>
<li><p>function goes inside another function</p>
</li>
<li><p>gets executed when needed</p>
</li>
</ul>
<hr />
<h2>Where you actually see callbacks</h2>
<p>You already use callbacks without noticing:</p>
<ul>
<li><p><code>setTimeout</code></p>
</li>
<li><p>event listeners</p>
</li>
<li><p>API calls (older style)</p>
</li>
<li><p>array methods like <code>map</code>, <code>filter</code></p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-javascript">[1, 2, 3].map((num) =&gt; num * 2)
</code></pre>
<p>That function inside <code>map</code> is also a callback.</p>
<hr />
<h2>The problem with callbacks</h2>
<p>Callbacks solve async problem, but they create another problem.</p>
<p>When things get complex, callbacks start going inside callbacks.</p>
<p>Example:</p>
<pre><code class="language-javascript">setTimeout(() =&gt; {
  console.log("Step 1")

  setTimeout(() =&gt; {
    console.log("Step 2")

    setTimeout(() =&gt; {
      console.log("Step 3")
    }, 1000)

  }, 1000)

}, 1000)
</code></pre>
<p>Now it starts going right → right → right</p>
<p>This is called:</p>
<p>callback nesting or callback hell</p>
<p>Hard to read Hard to debug Hard to maintain</p>
<hr />
<h2>Why this becomes a problem</h2>
<p>Because:</p>
<ul>
<li><p>code becomes deeply nested</p>
</li>
<li><p>flow is hard to understand</p>
</li>
<li><p>error handling becomes messy</p>
</li>
</ul>
<p>So while callbacks solve async, they introduce complexity.</p>
<p>That’s why later:</p>
<p>Promises came, async/await came</p>
<p>(but that’s for another blog <a href="https://blog.dhiraj.dev/understanding-javascript-promises-with-shaktimaan-vs-dr-jackal">here</a> you can read)</p>
<hr />
<h2>Nested callback flow</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/310f82df-0ccf-4df9-a94b-953c3a22e609.png" alt="" style="display:block;margin:0 auto" />

<hr />
<p>Callbacks are simple.</p>
<p>Just functions passed into other functions.</p>
<p>But they exist for a very important reason to handle things that don’t happen instantly</p>
]]></content:encoded></item><item><title><![CDATA[Array Flatten in JavaScript]]></title><description><![CDATA[When working with arrays, sometimes we don’t get a simple flat array.
Instead, we get nested arrays.
That means arrays inside arrays.
Example:
const arr = [1, [2, 3], [4, [5, 6]]]

Now this is not a s]]></description><link>https://blog.dhiraj.dev/array-flatten-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/array-flatten-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[array]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Mon, 23 Mar 2026 12:04:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/2dde382c-a732-488f-b39a-34ed58613b6c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When working with arrays, sometimes we don’t get a simple flat array.</p>
<p>Instead, we get <strong>nested arrays</strong>.</p>
<p>That means arrays inside arrays.</p>
<p>Example:</p>
<pre><code class="language-javascript">const arr = [1, [2, 3], [4, [5, 6]]]
</code></pre>
<p>Now this is not a simple array anymore.</p>
<p>It has multiple levels.</p>
<p>So if we want all values in a single list, we need to <strong>flatten</strong> it.</p>
<hr />
<h1>What are Nested Arrays</h1>
<p>A nested array is just an array that contains another array inside it.</p>
<p>Example:</p>
<pre><code class="language-javascript">const arr = [1, [2, 3], 4]
</code></pre>
<p>Structure looks like:</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/e4efa052-0b3e-4658-805a-b7b74593d701.png" alt="" style="display:block;margin:0 auto" />

<p>More complex example:</p>
<pre><code class="language-javascript">[1, [2, [3, [4]]]]
</code></pre>
<p>Here we have multiple levels.</p>
<hr />
<h1>Why Flattening is Useful</h1>
<p>In real programs, nested arrays can make things harder.</p>
<p>For example:</p>
<ul>
<li><p>looping becomes complex</p>
</li>
<li><p>accessing values is difficult</p>
</li>
<li><p>data processing becomes messy</p>
</li>
</ul>
<p>Sometimes we just want:</p>
<pre><code class="language-javascript">[1, 2, 3, 4, 5, 6]
</code></pre>
<p>instead of:</p>
<pre><code class="language-javascript">[1, [2, 3], [4, [5, 6]]]
</code></pre>
<p>So flattening makes the data easier to work with.</p>
<hr />
<h1>Concept of Flattening</h1>
<p>Flattening means:</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/d3a45fed-cf7e-45d3-9889-ffd383e71a97.png" alt="" style="display:block;margin:0 auto" />

<p>We remove all nesting and bring everything to a single level.</p>
<hr />
<h2>Flatten Transformation</h2>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/b89535af-842c-43e1-b467-de7873532e95.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>Approach 1: Using flat()</h1>
<p>JavaScript already gives a method called <code>flat()</code>.</p>
<pre><code class="language-javascript">const arr = [1, [2, 3], [4, [5, 6]]]

const flatArr = arr.flat(2)

console.log(flatArr)
</code></pre>
<p>Output:</p>
<pre><code class="language-id=&quot;0g6e8u&quot;">[1, 2, 3, 4, 5, 6]
</code></pre>
<p>Here <code>2</code> means how deep we want to flatten.</p>
<hr />
<h1>Approach 2: Using Infinity</h1>
<p>If we don’t know the depth, we can use:</p>
<pre><code class="language-javascript">const flatArr = arr.flat(Infinity)
</code></pre>
<p>This will flatten everything.</p>
<hr />
<h1>Approach 3: Using Recursion</h1>
<p>Sometimes in interviews, they expect you to write your own logic.</p>
<p>So let’s think step by step.</p>
<p>We go through each element:</p>
<ul>
<li><p>if it is a number → push it</p>
</li>
<li><p>if it is an array → go inside it</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-javascript">function flatten(arr) {
  let result = []

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

  return result
}

const arr = [1, [2, 3], [4, [5, 6]]]

console.log(flatten(arr))
</code></pre>
<p>Output:</p>
<pre><code class="language-id=&quot;z9h2kd&quot;">[1, 2, 3, 4, 5, 6]
</code></pre>
<hr />
<h1>How to Think About It</h1>
<p>The idea is simple:</p>
<pre><code class="language-plaintext">If value is array --&gt; go deeper  
If value is normal --&gt; collect it
</code></pre>
<p>This thinking works for any depth.</p>
<hr />
<p>Nested arrays are common in real world data.</p>
<p>Flattening helps simplify the structure and makes it easier to work with.</p>
<p>JavaScript provides <code>flat()</code> for quick use, but knowing how to solve it manually helps in interviews and deeper understanding.</p>
]]></content:encoded></item><item><title><![CDATA[Template Literals in JavaScript]]></title><description><![CDATA[When working with strings in JavaScript, many times we need to combine text with variables.
For example showing a user’s name, building messages or printing values.
Before template literals, we used s]]></description><link>https://blog.dhiraj.dev/template-literals-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/template-literals-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Mon, 23 Mar 2026 08:49:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/180aaaf1-a814-4585-be38-8a3933d2865b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When working with strings in JavaScript, many times we need to combine text with variables.</p>
<p>For example showing a user’s name, building messages or printing values.</p>
<p>Before template literals, we used <strong>string concatenation</strong>.</p>
<p>But that can get messy very quickly.</p>
<p>Let’s see why.</p>
<hr />
<h1>Problem with Traditional String Concatenation</h1>
<p>Suppose we want to print a message like this:</p>
<pre><code class="language-javascript">const name = "Bruce"
const age = 35

const message = "My name is " + name + " and I am " + age + " years old"

console.log(message)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">My name is Bruce and I am 35 years old
</code></pre>
<p>This works, but notice:</p>
<ul>
<li><p>lots of <code>+</code></p>
</li>
<li><p>harder to read</p>
</li>
<li><p>messy when string becomes longer</p>
</li>
</ul>
<p>Now imagine doing this in bigger strings. It becomes difficult to manage.</p>
<hr />
<h1>Template Literal Syntax</h1>
<p>To solve this problem, JavaScript introduced <strong>template literals</strong>.</p>
<p>Instead of using quotes (<code>"</code> or <code>'</code>), we use <strong>backticks</strong> (<code>`</code>).</p>
<p>Example:</p>
<pre><code class="language-javascript">const name = "Bruce"
const age = 35

const message = `My name is \({name} and I am \){age} years old`

console.log(message)
</code></pre>
<p>Much cleaner and easier to read.</p>
<hr />
<h1>Embedding Variables</h1>
<p>Inside template literals, we can directly insert variables using:</p>
<pre><code class="language-plaintext">${variable}
</code></pre>
<p>Example:</p>
<pre><code class="language-javascript">const hero = "Batman"

console.log(`I am ${hero}`)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">I am Batman
</code></pre>
<p>No need for <code>+</code> anymore.</p>
<hr />
<h1>Multi line Strings</h1>
<p>Another big advantage is <strong>multi line strings</strong>.</p>
<p>With normal strings:</p>
<pre><code class="language-javascript">const text = "Hello\nWorld"
</code></pre>
<p>With template literals:</p>
<pre><code class="language-javascript">const text = `Hello
World`

console.log(text)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Hello
World
</code></pre>
<p>No need for <code>\n</code>, it works naturally.</p>
<hr />
<h1>Before vs After</h1>
<p>Before (concatenation):</p>
<pre><code class="language-javascript">const name = "Clark"

const msg = "Hello " + name + ", welcome to Metropolis"
</code></pre>
<p>After (template literal):</p>
<pre><code class="language-javascript">const name = "Clark"

const msg = `Hello ${name}, welcome to Metropolis`
</code></pre>
<p>Cleaner and easier to understand.</p>
<hr />
<h2>Visual Idea</h2>
<p>Diagram idea: Before vs After</p>
<pre><code class="language-plaintext">"Hello " + name + "!"   →   `Hello ${name}!`
</code></pre>
<hr />
<h1>Use Cases in Modern JavaScript</h1>
<p>Template literals are used everywhere in modern JavaScript.</p>
<p>For example:</p>
<p><strong>Dynamic messages</strong></p>
<pre><code class="language-javascript">const score = 95

console.log(`Your score is ${score}`)
</code></pre>
<p><strong>Building HTML (frontend)</strong></p>
<pre><code class="language-javascript">const name = "Diana"

const html = `&lt;h1&gt;Hello ${name}&lt;/h1&gt;`
</code></pre>
<p><strong>Logging values</strong></p>
<pre><code class="language-javascript">const item = "Laptop"
const price = 50000

console.log(`Item: \({item}, Price: \){price}`)
</code></pre>
<hr />
<h1>Why Template Literals Are Better</h1>
<ul>
<li><p>More readable</p>
</li>
<li><p>Less messy</p>
</li>
<li><p>Easy to write multi line strings</p>
</li>
<li><p>No need for <code>+</code> everywhere</p>
</li>
</ul>
<hr />
<p>Template literals make working with strings much simpler.</p>
<p>Instead of joining strings manually, we can directly embed values inside them.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Modules: Import and Export Explained]]></title><description><![CDATA[When we start writing small programs, everything usually goes inside a single file.
That works fine in the beginning.
But as the project grows, we start adding more functions, more logic and more feat]]></description><link>https://blog.dhiraj.dev/javascript-modules-import-and-export-explained</link><guid isPermaLink="true">https://blog.dhiraj.dev/javascript-modules-import-and-export-explained</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[coding]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Mon, 23 Mar 2026 08:42:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/41f30204-b6a7-4a2d-bd3b-9f5119ad8531.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we start writing small programs, everything usually goes inside a single file.</p>
<p>That works fine in the beginning.</p>
<p>But as the project grows, we start adding more functions, more logic and more features.</p>
<p>Slowly, one file becomes very big and difficult to manage.</p>
<pre><code class="language-javascript">function add(a, b) { return a + b }
function multiply(a, b) { return a * b }
function divide(a, b) { return a / b }
// many more functions...
</code></pre>
<p>Now everything is mixed in one place.</p>
<p>It becomes harder to:</p>
<ul>
<li><p>find code</p>
</li>
<li><p>reuse code</p>
</li>
<li><p>maintain code</p>
</li>
</ul>
<p>This is where <strong>modules</strong> come in.</p>
<hr />
<h1>Why Modules Are Needed</h1>
<p>Modules help us <strong>split code into multiple files</strong>.</p>
<p>Each file can handle a specific responsibility.</p>
<p>For example:</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/f67ceb51-fb5f-4322-be19-1975779db511.png" alt="" style="display:block;margin:0 auto" />

<p>Instead of writing everything in one file, we organize code into smaller pieces.</p>
<p>This makes the code easier to read and manage.</p>
<hr />
<h1>Exporting Code</h1>
<p>To use code from one file in another, we need to <strong>export</strong> it.</p>
<p>Example: <code>math.js</code></p>
<pre><code class="language-javascript">export function add(a, b) {
  return a + b
}

export function multiply(a, b) {
  return a * b
}
</code></pre>
<p>Here we are exporting two functions.</p>
<p>Now these functions can be used in other files.</p>
<hr />
<h1>Importing Code</h1>
<p>To use exported functions, we <strong>import</strong> them.</p>
<p>Example: <code>app.js</code></p>
<pre><code class="language-javascript">import { add, multiply } from "./math.js"

console.log(add(2, 3))
console.log(multiply(4, 5))
</code></pre>
<p>Now <code>app.js</code> can use functions defined in <code>math.js</code>.</p>
<hr />
<h1>Default vs Named Exports</h1>
<p>There are two ways to export things.</p>
<h2>Named Export</h2>
<p>This is what we saw earlier.</p>
<pre><code class="language-javascript">export function add(a, b) {
  return a + b
}
</code></pre>
<p>Importing:</p>
<pre><code class="language-javascript">import { add } from "./math.js"
</code></pre>
<p>We must use the <strong>same name</strong> while importing.</p>
<hr />
<h2>Default Export</h2>
<p>A file can also have one <strong>default export</strong>.</p>
<pre><code class="language-javascript">export default function subtract(a, b) {
  return a - b
}
</code></pre>
<p>Importing:</p>
<pre><code class="language-javascript">import subtract from "./math.js"
</code></pre>
<p>Here we can use <strong>any name</strong> while importing.</p>
<hr />
<h1>Module Flow</h1>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/8b3336fc-95e3-4fb8-83cd-a462491d71c4.png" alt="" style="display:block;margin:0 auto" />

<p>Data flows from one file to another using import/export.</p>
<hr />
<h1>Benefits of Modular Code</h1>
<p>Using modules improves code in many ways.</p>
<p><strong>Better organization</strong> Code is split into smaller files.</p>
<p><strong>Reusability</strong> Functions can be reused in different files.</p>
<p><strong>Maintainability</strong> Changes in one module don’t affect everything.</p>
<p><strong>Readability</strong> Each file has a clear purpose.</p>
<hr />
<h1>Small Example</h1>
<p><code>math.js</code></p>
<pre><code class="language-javascript">export function square(num) {
  return num * num
}
</code></pre>
<p><code>app.js</code></p>
<pre><code class="language-javascript">import { square } from "./math.js"

console.log(square(5))
</code></pre>
<p>Output</p>
<pre><code class="language-id=&quot;6phu3y&quot;">25
</code></pre>
<hr />
<p>As applications grow, keeping everything in one file becomes difficult.</p>
<p>Modules help us break code into smaller, manageable pieces.</p>
<p>Using <strong>export and import</strong>, we can share code between files and build better structured applications.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Object-Oriented Programming in JavaScript]]></title><description><![CDATA[As programs grow bigger, managing data and logic can become messy. We may have many variables and functions that belong together but are scattered across the code.
To organize code better, programming]]></description><link>https://blog.dhiraj.dev/understanding-object-oriented-programming-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/understanding-object-oriented-programming-in-javascript</guid><category><![CDATA[oop]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[software development]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Sun, 15 Mar 2026 08:44:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/e5af172e-462f-4ef0-97bb-29d66d8905c7.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As programs grow bigger, managing data and logic can become messy. We may have many variables and functions that belong together but are scattered across the code.</p>
<p>To organize code better, programming languages provide a concept called <strong>Object-Oriented Programming</strong>, often called <strong>OOP</strong>.</p>
<p>The idea behind OOP is simple: instead of thinking only about functions, we think about <strong>objects that contain both data and behavior</strong>.</p>
<p>JavaScript also supports this programming style.</p>
<p>Let’s understand it using an example from <strong>Formula 1</strong>.</p>
<hr />
<h1>A Real-World Analogy</h1>
<p>Think about how an <strong>F1 team designs a car</strong>.</p>
<p>Before building the car, engineers create a <strong>blueprint</strong>.</p>
<p>The blueprint defines things like:</p>
<ul>
<li><p>team</p>
</li>
<li><p>driver</p>
</li>
<li><p>engine</p>
</li>
<li><p>top speed</p>
</li>
</ul>
<p>But the blueprint itself is not a car.</p>
<p>From that blueprint, the team can build multiple cars.</p>
<p>Example:</p>
<pre><code class="language-text">Blueprint → Race Cars
</code></pre>
<p>Ferrari Car<br />Mercedes Car<br />Red Bull Car</p>
<p>Each car follows the same design but has different drivers or setups.</p>
<p>In programming:</p>
<ul>
<li><p><strong>Blueprint = Class</strong></p>
</li>
<li><p><strong>Car created from blueprint = Object</strong></p>
</li>
</ul>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/7f51765e-6ee1-4cea-9e7a-58a3eb223d6d.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>What is a Class in JavaScript</h1>
<p>In JavaScript, a <strong>class</strong> is a template used to create objects.</p>
<p>It defines what properties and methods objects should have.</p>
<p>Example:</p>
<pre><code class="language-javascript">class F1Car {

}
</code></pre>
<p>This class defines the structure for creating F1 car objects.</p>
<hr />
<h1>Constructor Method</h1>
<p>Usually we want each car to have specific data.</p>
<p>For example:</p>
<ul>
<li><p>team</p>
</li>
<li><p>driver</p>
</li>
</ul>
<p>Inside a class we initialize this data using a <strong>constructor</strong>.</p>
<pre><code class="language-javascript">class F1Car {

  constructor(team, driver) {
    this.team = team
    this.driver = driver
  }

}
</code></pre>
<p>The constructor runs automatically when a new object is created.</p>
<hr />
<h1>Creating Objects</h1>
<p>Now we can create different cars from the same blueprint.</p>
<pre><code class="language-javascript">const ferrari = new F1Car("Ferrari", "Charles Leclerc")
const mercedes = new F1Car("Mercedes", "George Russell")
const redBull = new F1Car("Red Bull", "Max Verstappen")
</code></pre>
<p>Each object stores its own data.</p>
<p>Example:</p>
<pre><code class="language-plaintext">ferrari → Ferrari, Charles Leclerc
mercedes → Mercedes, George Russell
redBull → Red Bull, Max Verstappen
</code></pre>
<hr />
<h1>Methods Inside a Class</h1>
<p>Classes can also contain <strong>methods</strong>.</p>
<p>Methods are simply functions that belong to the class.</p>
<p>Example:</p>
<pre><code class="language-javascript">class F1Car {

  constructor(team, driver) {
    this.team = team
    this.driver = driver
  }

  carInfo() {
    console.log(`\({this.driver} drives for \){this.team}`)
  }

}
</code></pre>
<p>Now every object can use this method.</p>
<pre><code class="language-javascript">const ferrari = new F1Car("Ferrari", "Charles Leclerc")
const mclaren = new F1Car("McLaren", "Lando Norris")

ferrari.carInfo()
mclaren.carInfo()
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Charles Leclerc drives for Ferrari
Lando Norris drives for McLaren
</code></pre>
<hr />
<h1>Basic Idea of Encapsulation</h1>
<p>One important concept in OOP is <strong>encapsulation</strong>.</p>
<p>Encapsulation means keeping <strong>data and behavior together</strong> inside a single structure.</p>
<p>In our example:</p>
<p>Data</p>
<pre><code class="language-plaintext">team
driver
</code></pre>
<p>Behavior</p>
<pre><code class="language-plaintext">carInfo()
</code></pre>
<p>Both live inside the <strong>F1Car class</strong>, which keeps the code organized.</p>
<hr />
<h1>Why OOP is Useful</h1>
<p>Object-Oriented Programming helps with:</p>
<p><strong>Code organization</strong><br />Related data and logic stay together.</p>
<p><strong>Code reusability</strong><br />One class can create many objects.</p>
<p><strong>Better structure</strong><br />Programs become easier to maintain.</p>
<hr />
<h1>Example Summary</h1>
<pre><code class="language-javascript">class F1Car {

  constructor(team, driver) {
    this.team = team
    this.driver = driver
  }

  carInfo() {
    console.log(`\({this.driver} drives for \){this.team}`)
  }

}

const ferrari = new F1Car("Ferrari", "Charles Leclerc")
const mercedes = new F1Car("Mercedes", "Kimi Antonelli")

ferrari.carInfo()
mercedes.carInfo()
</code></pre>
<hr />
<p>Object-Oriented Programming helps structure code in a way that models real-world systems.</p>
<p>By using <strong>classes and objects</strong>, we can create reusable blueprints and generate multiple objects from them.</p>
<p>In JavaScript, classes make it easier to build structured applications just like creating multiple F1 cars from a single design blueprint.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Objects in JavaScript]]></title><description><![CDATA[When we write programs, we often need to store related pieces of information together.
For example imagine we want to store details about a person like their name, age and city.
We could store them in]]></description><link>https://blog.dhiraj.dev/understanding-objects-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/understanding-objects-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Objects]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[software development]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Sat, 14 Mar 2026 19:57:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/61f43c83-601f-46ab-b17f-4168e3d99bf3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we write programs, we often need to store related pieces of information together.</p>
<p>For example imagine we want to store details about a person like their name, age and city.</p>
<p>We could store them in separate variables, but that quickly becomes messy.</p>
<pre><code class="language-javascript">const name = "Bruce Wayne"
const age = 35
const city = "Gotham"
</code></pre>
<p>These values belong together, but they are scattered.</p>
<p>To organize related data in a better way, JavaScript provides <strong>objects</strong>.</p>
<p>An object lets us store multiple values inside a single structure.</p>
<p>You can think of an object as a collection of <strong>key value pairs</strong>.</p>
<hr />
<h1>What an Object Looks Like</h1>
<p>Here is a simple example.</p>
<pre><code class="language-javascript">const hero = {
  name: "Bruce Wayne",
  heroName: "Batman",
  city: "Gotham"
}
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>name</code>, <code>heroName</code>, and <code>city</code> are <strong>keys</strong></p>
</li>
<li><p><code>"Bruce Wayne"</code>, <code>"Batman"</code>, <code>"Gotham"</code> are <strong>values</strong></p>
</li>
</ul>
<p>So the object stores information like this:</p>
<pre><code class="language-plaintext">name → Bruce Wayne
heroName → Batman
city → Gotham
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/11c243f0-1c99-4988-88e5-32636aef2a72.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>Creating Objects</h1>
<p>Objects are created using <strong>curly braces</strong> <code>{}</code>.</p>
<p>Example:</p>
<pre><code class="language-javascript">const hero = {
  name: "Clark Kent",
  heroName: "Superman",
  city: "Metropolis"
}
</code></pre>
<p>Each property is written as:</p>
<pre><code class="language-plaintext">key: value
</code></pre>
<hr />
<h1>Accessing Object Properties</h1>
<p>There are two ways to access values inside an object.</p>
<h2>Dot Notation</h2>
<p>This is the most common way.</p>
<pre><code class="language-javascript">console.log(hero.name)
console.log(hero.heroName)
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Clark Kent
Superman
</code></pre>
<hr />
<h2>Bracket Notation</h2>
<p>Another way is using square brackets.</p>
<pre><code class="language-javascript">console.log(hero["city"])
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Metropolis
</code></pre>
<p>Both ways work the same, but bracket notation can be useful when property names are dynamic.</p>
<hr />
<h1>Updating Object Properties</h1>
<p>We can update values inside an object easily.</p>
<pre><code class="language-javascript">hero.city = "Smallville"

console.log(hero.city)
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Smallville
</code></pre>
<p>The value simply changes.</p>
<hr />
<h1>Adding New Properties</h1>
<p>Objects are flexible, so we can add new properties anytime.</p>
<pre><code class="language-javascript">hero.power = "Flight"

console.log(hero)
</code></pre>
<p>Now the object contains a new property.</p>
<hr />
<h1>Deleting Properties</h1>
<p>If we want to remove a property, we can use the <code>delete</code> keyword.</p>
<pre><code class="language-javascript">delete hero.city

console.log(hero)
</code></pre>
<p>The <code>city</code> property will be removed from the object.</p>
<hr />
<h1>Looping Through Object Keys</h1>
<p>Sometimes we want to go through all properties inside an object.</p>
<p>JavaScript provides a simple loop for this.</p>
<pre><code class="language-javascript">for (const key in hero) {
  console.log(key, hero[key])
}
</code></pre>
<p>Example output</p>
<pre><code class="language-plaintext">name Clark Kent
heroName Superman
power Flight
</code></pre>
<p>The loop goes through every key and prints the value.</p>
<hr />
<h1>Array vs Object</h1>
<p>Sometimes beginners confuse arrays and objects.</p>
<p>Arrays store values using <strong>index numbers</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">const heroes = ["Batman", "Superman", "Wonder Woman"]
</code></pre>
<p>Accessing array values:</p>
<pre><code class="language-javascript">heroes[0]
heroes[1]
</code></pre>
<p>Objects store values using <strong>keys instead of indexes</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">const hero = {
  name: "Diana Prince",
  heroName: "Wonder Woman"
}
</code></pre>
<p>Accessing object values:</p>
<pre><code class="language-javascript">hero.name
hero.heroName
</code></pre>
<p><strong>Array vs Object</strong></p>
<pre><code class="language-plaintext">Array
["Batman", "Superman", "Wonder Woman"]
   0          1           2

Object
name → Diana Prince
heroName → Wonder Woman
</code></pre>
<hr />
<h1>Example: Student Object</h1>
<p>Let’s create an object representing a student.</p>
<pre><code class="language-javascript">const student = {
  name: "Barry Allen",
  age: 24,
  course: "Forensic Science"
}
</code></pre>
<p>Update a property.</p>
<pre><code class="language-javascript">student.age = 25
</code></pre>
<p>Now print all keys and values.</p>
<pre><code class="language-javascript">for (const key in student) {
  console.log(key, student[key])
}
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">name Barry Allen
age 25
course Forensic Science
</code></pre>
<hr />
<p>Objects are one of the most important data structures in JavaScript.</p>
<p>They allow us to group related information together using key value pairs.</p>
<p>With objects we can store data, update values, add new properties, delete properties and loop through them easily.</p>
]]></content:encoded></item><item><title><![CDATA[Arrow Functions in JavaScript: A Simpler Way to Write Functions]]></title><description><![CDATA[When writing JavaScript functions, sometimes the syntax can feel a little long. Especially when the function is small and only does a simple task.
For example calculating a number, returning a value o]]></description><link>https://blog.dhiraj.dev/arrow-functions-in-javascript</link><guid isPermaLink="true">https://blog.dhiraj.dev/arrow-functions-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[#arrowfunction]]></category><category><![CDATA[ Arrow Functions In JavaScript]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Sat, 14 Mar 2026 18:39:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/c9da5258-9e3c-45be-8efe-d9dfbb19428c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When writing JavaScript functions, sometimes the syntax can feel a little long. Especially when the function is small and only does a simple task.</p>
<p>For example calculating a number, returning a value or transforming data.</p>
<p>To make function writing simpler and cleaner, JavaScript introduced <strong>arrow functions</strong> in ES6.</p>
<p>Arrow functions provide a shorter syntax for writing functions and are widely used in modern JavaScript.</p>
<p>Let’s understand how they work.</p>
<hr />
<h1>Normal Function Example</h1>
<p>First let’s start with a normal function.</p>
<p>Suppose we want a function that calculates the square of a number.</p>
<pre><code class="language-javascript">function square(num) {
  return num * num
}

console.log(square(4))
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">16
</code></pre>
<p>This works perfectly fine.</p>
<p>But for very small functions, this syntax can feel a bit verbose.</p>
<p>Arrow functions provide a simpler way.</p>
<hr />
<h1>Arrow Function Syntax</h1>
<p>The same function written using an arrow function looks like this.</p>
<pre><code class="language-javascript">const square = (num) =&gt; {
  return num * num
}

console.log(square(4))
</code></pre>
<p>The arrow <code>=&gt;</code> replaces the <code>function</code> keyword.</p>
<p>So instead of writing <code>function</code>, we write an <strong>arrow</strong>.</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/4193bfcb-d23a-4155-9039-8fc0aebb80f9.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>Arrow Function with One Parameter</h1>
<p>If the function has <strong>only one parameter</strong>, we can even remove the parentheses.</p>
<pre><code class="language-javascript">const square = num =&gt; {
  return num * num
}

console.log(square(5))
</code></pre>
<p>This is very common in modern JavaScript.</p>
<hr />
<h1>Arrow Function with Multiple Parameters</h1>
<p>If the function has multiple parameters, we keep the parentheses.</p>
<p>Example:</p>
<pre><code class="language-javascript">const add = (a, b) =&gt; {
  return a + b
}

console.log(add(3, 7))
</code></pre>
<p>Here both parameters go inside the parentheses.</p>
<hr />
<h1>Implicit Return vs Explicit Return</h1>
<p>Arrow functions have a useful feature called <strong>implicit return</strong>.</p>
<p>If the function body contains only one expression, JavaScript automatically returns the result.</p>
<p>Normal arrow function:</p>
<pre><code class="language-javascript">const square = (num) =&gt; {
  return num * num
}
</code></pre>
<p>Implicit return version:</p>
<pre><code class="language-javascript">const square = num =&gt; num * num
</code></pre>
<p>Here we removed:</p>
<ul>
<li><p>curly braces</p>
</li>
<li><p><code>return</code> keyword</p>
</li>
</ul>
<p>JavaScript automatically returns the result.</p>
<p>This makes arrow functions very concise.</p>
<hr />
<h1>Even or Odd Example</h1>
<p>Here is a small example that checks if a number is even.</p>
<pre><code class="language-javascript">const isEven = num =&gt; num % 2 === 0

console.log(isEven(6))
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">true
</code></pre>
<hr />
<h1>Arrow Functions with Arrays</h1>
<p>Arrow functions are often used with array methods like <code>map()</code>, <code>filter()</code>, and <code>reduce()</code>.</p>
<p>Example using <code>map()</code>.</p>
<pre><code class="language-javascript">const numbers = [1, 2, 3, 4]

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

console.log(squares)
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">[1, 4, 9, 16]
</code></pre>
<p>Here the arrow function makes the code much shorter and easier to read.</p>
<hr />
<h1>Basic Difference from Normal Functions</h1>
<p>Arrow functions and normal functions both create functions, but they are used slightly differently.</p>
<p>Normal functions are commonly used for defining main logic.</p>
<p>Arrow functions are often used for:</p>
<ul>
<li><p>short utility functions</p>
</li>
<li><p>callbacks</p>
</li>
<li><p>array transformations</p>
</li>
</ul>
<p>One important difference is how they handle <code>this</code>, but that is a deeper topic and we don’t need to go into it right now.</p>
<hr />
<h1>Syntax Breakdown</h1>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/569476b2-69c9-4911-ad5e-108ea237b09a.png" alt="" style="display:block;margin:0 auto" />

<hr />
<p>Arrow functions provide a shorter and cleaner way to write functions in JavaScript.</p>
<p>They reduce boilerplate code and make small functions easier to read.</p>
<p>Because of this, arrow functions are widely used in modern JavaScript, especially when working with arrays, callbacks and functional patterns.</p>
<p>Normal functions are still important, but arrow functions make many everyday tasks much simpler.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Node.js Internals]]></title><description><![CDATA[When we use Node.js we can do many powerful things like making network requests, reading files, working with the operating system or running servers.
But if you think about it for a second, JavaScript]]></description><link>https://blog.dhiraj.dev/understanding-node-js-internals</link><guid isPermaLink="true">https://blog.dhiraj.dev/understanding-node-js-internals</guid><category><![CDATA[Node.js]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[v8 engine]]></category><category><![CDATA[libuv]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Sat, 14 Mar 2026 12:07:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/6fa3975f-2c28-4f7d-b615-3c3c8e458a4c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we use Node.js we can do many powerful things like making network requests, reading files, working with the operating system or running servers.</p>
<p>But if you think about it for a second, JavaScript originally runs inside the browser. Browsers provide many APIs that JavaScript can use.</p>
<p>So you might wonder:</p>
<p><strong>Does JavaScript itself have all these powers?</strong></p>
<p>Not really.</p>
<p>Inside Node.js there are multiple libraries and components working together that provide these capabilities.</p>
<p>Node.js mainly relies on three important parts:</p>
<ul>
<li><p><strong>V8</strong></p>
</li>
<li><p><strong>JS Bindings (C++ layer)</strong></p>
</li>
<li><p><strong>libuv</strong></p>
</li>
</ul>
<p>These pieces work together to allow JavaScript to interact with the system.</p>
<hr />
<h1>V8 (JavaScript Engine)</h1>
<p>Node.js runs on <strong>V8</strong>, which is a JavaScript engine developed by Google.</p>
<p>V8 is responsible for executing JavaScript code.</p>
<p>It does several important things:</p>
<ul>
<li><p>Parses JavaScript</p>
</li>
<li><p>Compiles JavaScript to machine code (JIT compilation)</p>
</li>
<li><p>Manages memory (heap and garbage collection)</p>
</li>
<li><p>Maintains the call stack</p>
</li>
</ul>
<p>So when you run a Node.js program like this:</p>
<pre><code class="language-javascript">console.log("Hello Node")
</code></pre>
<p>V8 is the component that reads and executes this JavaScript code.</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/0b2fa40b-bc0a-435c-9df6-a5e9524a65d8.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>The Limitation of V8</h1>
<p>Even though V8 is very powerful, it cannot perform system operations by itself.</p>
<p>For example V8 cannot directly:</p>
<ul>
<li><p>read files</p>
</li>
<li><p>open network sockets</p>
</li>
<li><p>perform DNS lookups</p>
</li>
<li><p>interact with the operating system</p>
</li>
</ul>
<p>So something else is needed to handle these tasks.</p>
<p>This is where <strong>Node.js bindings</strong> and <strong>libuv</strong> come in.</p>
<hr />
<h1>JS Bindings (C++ Layer)</h1>
<p>Node.js is written mainly in <strong>C++</strong> and it exposes many system capabilities to JavaScript through a layer called <strong>JS bindings</strong>.</p>
<p>This layer works like a bridge between JavaScript and native system code.</p>
<p>When you write JavaScript like this:</p>
<pre><code class="language-javascript">const fs = require("fs")

fs.readFile("data.txt", () =&gt; {
  console.log("file read")
})
</code></pre>
<p>The <code>fs</code> module is not pure JavaScript.</p>
<p>Behind the scenes it connects to <strong>C++ code</strong> inside Node.js.</p>
<p>So JS bindings do two main things:</p>
<ul>
<li><p>expose system functionality as JavaScript APIs</p>
</li>
<li><p>convert JavaScript calls into C/C++ calls</p>
</li>
</ul>
<p>You can think of it like a translator between JavaScript and the lower level system code.</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/8e5abae0-add1-46cb-815f-48730d3fe278.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>libuv (Async I/O Engine)</h1>
<p>Now we reach one of the most important parts of Node.js: <strong>libuv</strong>.</p>
<p>libuv is a large <strong>C++ library</strong> that handles many of Node.js asynchronous operations.</p>
<p>JavaScript itself is <strong>single threaded</strong>, meaning it executes one thing at a time.</p>
<p>But Node.js still manages to handle thousands of operations concurrently. This is possible because of libuv.</p>
<p>libuv provides:</p>
<ul>
<li><p>Event loop</p>
</li>
<li><p>Non-blocking I/O</p>
</li>
<li><p>Thread pool</p>
</li>
<li><p>File system operations</p>
</li>
<li><p>Networking</p>
</li>
<li><p>DNS operations</p>
</li>
<li><p>Child processes</p>
</li>
</ul>
<p>For example when you run something like:</p>
<pre><code class="language-javascript">const fs = require("fs")

fs.readFile("file.txt", () =&gt; {
  console.log("done")
})
</code></pre>
<p>The file reading work is handled by <strong>libuv</strong>, not V8.</p>
<p>libuv processes the operation asynchronously and notifies Node.js when the task is completed.</p>
<p>This is why Node.js can remain fast and handle many requests at the same time.</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/7f4220ff-3856-472d-b1aa-a2b26176532c.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>Full Node.js Architecture</h1>
<p>So when you run a Node.js application, these components work together like this:</p>
<ol>
<li><p><strong>V8 executes JavaScript</strong></p>
</li>
<li><p><strong>JS bindings connect JavaScript to native APIs</strong></p>
</li>
<li><p><strong>libuv performs asynchronous I/O operations</strong></p>
</li>
</ol>
<p>In simple terms:</p>
<p>JavaScript code runs in <strong>V8</strong>, Node’s <strong>C++ bindings</strong> translate JavaScript calls into native operations and <strong>libuv</strong> interacts with the operating system to perform tasks like networking and file handling.</p>
<p><strong>Diagram idea:</strong></p>
<pre><code class="language-plaintext">JavaScript Application
        ↓
       V8
(JavaScript Engine)
        ↓
   Node C++ Bindings
        ↓
      libuv
(Event Loop, Thread Pool)
        ↓
   Operating System
</code></pre>
<hr />
<p>Node.js looks simple from the outside because we write JavaScript.</p>
<p>But internally there are multiple layers working together to make everything possible.</p>
<ul>
<li><p><strong>V8</strong> executes JavaScript</p>
</li>
<li><p><strong>JS bindings</strong> connect JavaScript to native code</p>
</li>
<li><p><strong>libuv</strong> handles asynchronous operations and the event loop</p>
</li>
</ul>
<p>Understanding these components gives a clearer picture of how Node.js works under the hood.</p>
]]></content:encoded></item><item><title><![CDATA[Function Declaration vs Function Expression: What’s the Difference?]]></title><description><![CDATA[When we write programs, we often repeat the same logic again and again.
For example imagine we need to add two numbers in different parts of a program.
Instead of writing the same code multiple times,]]></description><link>https://blog.dhiraj.dev/function-declaration-vs-function-expression</link><guid isPermaLink="true">https://blog.dhiraj.dev/function-declaration-vs-function-expression</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Chaiaurcode]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Fri, 13 Mar 2026 19:27:41 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/48c921be-d882-46be-a271-d57e4a8a020e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we write programs, we often repeat the same logic again and again.</p>
<p>For example imagine we need to add two numbers in different parts of a program.</p>
<p>Instead of writing the same code multiple times, we can create a function and reuse it whenever we need it.</p>
<p>Functions are reusable blocks of code that perform a specific task.</p>
<p>Example:</p>
<pre><code class="language-javascript">function add(a, b) {
  return a + b
}

console.log(add(2, 3))
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">5
</code></pre>
<p>Here the function performs the addition and we can call it anytime we need.</p>
<p>We can create functions in many ways in JavaScript, but in this blog we will focus on these two ways:</p>
<ul>
<li><p>Function Declaration</p>
</li>
<li><p>Function Expression</p>
</li>
</ul>
<p>Let’s understand both.</p>
<hr />
<h1>Function Declaration</h1>
<p>A function declaration defines a function using the <code>function</code> keyword followed by the function name.</p>
<p>Example:</p>
<pre><code class="language-javascript">function multiply(a, b) {
  return a * b
}

console.log(multiply(4, 5))
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">20
</code></pre>
<p>Here we created a function named <strong>multiply</strong>.</p>
<p>The function is declared using the <code>function</code> keyword and can be called normally in the program.</p>
<hr />
<h1>Function Expression</h1>
<p>A function expression is slightly different.</p>
<p>Here the function is stored inside a variable.</p>
<p>Example:</p>
<pre><code class="language-javascript">const multiply = function(a, b) {
  return a * b
}

console.log(multiply(4, 5))
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">20
</code></pre>
<p>The function still performs the same task.</p>
<p>The difference is that instead of declaring it directly, we assign the function to a variable.</p>
<p>So the variable now holds the function.</p>
<hr />
<h1>Side by Side Comparison</h1>
<p>Function Declaration:</p>
<pre><code class="language-javascript">function add(a, b) {
  return a + b
}
</code></pre>
<p>Function Expression:</p>
<pre><code class="language-javascript">const add = function(a, b) {
  return a + b
}
</code></pre>
<p>Both create functions and both work in almost the same way when we call them.</p>
<p>But there is one important difference between them.</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/4ddd5d29-6f09-4a3d-ad1f-83f0117518e1.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>Hoisting</h1>
<p>In JavaScript, before the code runs, the engine prepares some things in memory. This behavior is commonly referred to as <strong>hoisting</strong>.</p>
<p>At a very high level, this means some declarations become available earlier than where they appear in the code.</p>
<p>Function declarations follow this behavior.</p>
<p>Example:</p>
<pre><code class="language-javascript">console.log(add(2,3))

function add(a, b) {
  return a + b
}
</code></pre>
<p>Even though the function appears later in the code, the program still runs.</p>
<p>Now look at a function expression:</p>
<pre><code class="language-javascript">console.log(add(2,3))

const add = function(a, b) {
  return a + b
}
</code></pre>
<p>This will cause an error.</p>
<p>That happens because the variable that holds the function is not ready yet at that point.</p>
<p>So the simple idea is:</p>
<p>Function Declaration → can be called earlier in the code<br />Function Expression → cannot be called before it is defined</p>
<p>No need to go deep into how JavaScript internally handles this. Just remembering this behavior is enough for now.</p>
<hr />
<h1>When to Use Each Type</h1>
<p>Both styles are used a lot in JavaScript.</p>
<p>Function declarations are commonly used when the function represents a main piece of logic in the program.</p>
<p>Function expressions are useful when functions need to be stored in variables, passed around or used inside other functions.</p>
<p>You will see function expressions very often when working with callbacks and modern JavaScript patterns.</p>
<hr />
<h1>Example</h1>
<p>Function declaration that multiplies two numbers:</p>
<pre><code class="language-javascript">function multiply(a, b) {
  return a * b
}

console.log(multiply(3, 4))
</code></pre>
<p>The same logic using a function expression:</p>
<pre><code class="language-javascript">const multiplyExp = function(a, b) {
  return a * b
}

console.log(multiplyExp(3, 4))
</code></pre>
<p>If you try calling these before their definitions, you will notice something interesting.</p>
<p>Function declarations still work, but function expressions will throw an error.</p>
<p>That small behavior difference is what usually separates these two ways of creating functions.</p>
]]></content:encoded></item><item><title><![CDATA[Control Flow in JavaScript: If, Else and Switch Explained]]></title><description><![CDATA[When we write programs, not every line of code should run every time.
Sometimes the program needs to make decisions.
Think about a game.

If a player has enough health, they continue playing.

If the ]]></description><link>https://blog.dhiraj.dev/control-flow-in-javascript-if-else-and-switch-explained</link><guid isPermaLink="true">https://blog.dhiraj.dev/control-flow-in-javascript-if-else-and-switch-explained</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[javascript framework]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Fri, 13 Mar 2026 08:12:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/950b71b6-14c2-461b-89f9-316cd7fdfd04.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we write programs, not every line of code should run every time.</p>
<p>Sometimes the program needs to <strong>make decisions</strong>.</p>
<p>Think about a game.</p>
<ul>
<li><p>If a player has enough health, they continue playing.</p>
</li>
<li><p>If the health reaches zero, the game is over.</p>
</li>
<li><p>If the player collects a special item, they unlock a new ability.</p>
</li>
</ul>
<p>The game constantly checks conditions and decides what should happen next.</p>
<p>This decision making is called <strong>control flow</strong>.</p>
<p>Control flow controls <strong>which part of the program runs based on conditions</strong>.</p>
<p>In JavaScript we mainly use:</p>
<ul>
<li><p><code>if</code></p>
</li>
<li><p><code>if-else</code></p>
</li>
<li><p><code>else if</code></p>
</li>
<li><p><code>switch</code></p>
</li>
</ul>
<p>Let’s understand them using simple game examples.</p>
<hr />
<h1>The <code>if</code> Statement</h1>
<p>The <code>if</code> statement runs code <strong>only if a condition is true</strong>.</p>
<p>Imagine a player trying to enter a special level that requires at least <strong>100 coins</strong>.</p>
<pre><code class="language-javascript">let coins = 120

if (coins &gt;= 100) {
  console.log("Level unlocked")
}
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Level unlocked
</code></pre>
<p>If the player has enough coins, the level unlocks.</p>
<p>If not, nothing happens.</p>
<hr />
<h1>The <code>if-else</code> Statement</h1>
<p>Sometimes we want <strong>two possible outcomes</strong>.</p>
<p>Example: checking player health.</p>
<pre><code class="language-javascript">let health = 0

if (health &gt; 0) {
  console.log("Player is still alive")
} else {
  console.log("Game Over")
}
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Game Over
</code></pre>
<p>Here the game checks the condition.</p>
<p>If true → player continues<br />If false → game ends</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/edc148d7-3a9e-4157-88c3-12eda525c259.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>The <code>else if</code> Ladder</h1>
<p>Games often have <strong>multiple conditions</strong>.</p>
<p>Example: assigning player rank based on score.</p>
<pre><code class="language-javascript">let score = 850

if (score &gt;= 1000) {
  console.log("Legend Rank")
}
else if (score &gt;= 800) {
  console.log("Diamond Rank")
}
else if (score &gt;= 500) {
  console.log("Gold Rank")
}
else {
  console.log("Bronze Rank")
}
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Diamond Rank
</code></pre>
<p>The program checks conditions <strong>from top to bottom</strong>.</p>
<p>As soon as one condition is true, the rest are skipped.</p>
<hr />
<h1>The <code>switch</code> Statement</h1>
<p>Sometimes we check <strong>specific values instead of ranges</strong>.</p>
<p>Example: choosing a character class in a game.</p>
<pre><code class="language-javascript">let character = "mage"

switch (character) {
  case "warrior":
    console.log("Strong attack and high defense")
    break

  case "mage":
    console.log("Powerful magic abilities")
    break

  case "archer":
    console.log("Long range attacks")
    break

  default:
    console.log("Unknown character")
}
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Powerful magic abilities
</code></pre>
<p>The <code>break</code> statement stops execution once a match is found.</p>
<p>Without <code>break</code>, the next cases would also run.</p>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/a35dd186-80bc-4016-8102-93f8bc89a9af.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>When to Use <code>switch</code> vs <code>if-else</code></h1>
<p>Both structures help us make decisions.</p>
<p>Use <strong>if-else</strong> when checking conditions or ranges.</p>
<p>Example:</p>
<pre><code class="language-plaintext">score &gt;= 800
health &gt; 0
coins &gt;= 100
</code></pre>
<p>Use <strong>switch</strong> when checking <strong>specific values</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">character type
weapon type
game difficulty
</code></pre>
<hr />
<h1>Examples</h1>
<p>Check if a player's score is positive, negative or zero.</p>
<pre><code class="language-javascript">let score = -10

if (score &gt; 0) {
  console.log("Positive score")
}
else if (score &lt; 0) {
  console.log("Negative score")
}
else {
  console.log("Score is zero")
}
</code></pre>
<p>Now choose a game difficulty using <code>switch</code>.</p>
<pre><code class="language-javascript">let difficulty = "hard"

switch (difficulty) {
  case "easy":
    console.log("Enemies are weaker")
    break

  case "medium":
    console.log("Balanced difficulty")
    break

  case "hard":
    console.log("Enemies are stronger")
    break

  default:
    console.log("Unknown difficulty")
}
</code></pre>
<p>In the first example we used <strong>if-else</strong> because we are checking conditions.</p>
<p>In the second example we used <strong>switch</strong> because we are checking specific values.</p>
<hr />
<p>Control flow is one of the most important concepts in programming.</p>
<p>It allows programs to <strong>react to different situations</strong>.</p>
]]></content:encoded></item><item><title><![CDATA[Array Methods You Must Know]]></title><description><![CDATA[Arrays help us store multiple values in a single variable. But storing data is only part of the story. Most of the time we also need to process that data.
For example:

change values

remove unwanted ]]></description><link>https://blog.dhiraj.dev/javascript-array-methods</link><guid isPermaLink="true">https://blog.dhiraj.dev/javascript-array-methods</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Chaiaurcode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[array]]></category><category><![CDATA[array methods]]></category><category><![CDATA[software development]]></category><dc:creator><![CDATA[Dhiraj]]></dc:creator><pubDate>Wed, 11 Mar 2026 19:31:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/7ba24649-f4e8-44d6-ad58-1543950a7f0e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Arrays help us store multiple values in a single variable. But storing data is only part of the story. Most of the time we also need to <strong>process that data</strong>.</p>
<p>For example:</p>
<ul>
<li><p>change values</p>
</li>
<li><p>remove unwanted values</p>
</li>
<li><p>calculate totals</p>
</li>
</ul>
<p>JavaScript gives us built in methods to do these tasks easily.</p>
<p>Some of the most useful array methods are:</p>
<ul>
<li><p><code>map()</code></p>
</li>
<li><p><code>filter()</code></p>
</li>
<li><p><code>reduce()</code></p>
</li>
</ul>
<p>Let’s understand them one by one.</p>
<hr />
<h1>map()</h1>
<p><code>map()</code> is used when we want to <strong>transform every value in an array</strong>.</p>
<p>It goes through each element and returns a <strong>new array</strong> with the updated values.</p>
<p>Example array:</p>
<pre><code class="language-javascript">const numbers = [2, 4, 6, 8]
</code></pre>
<p>Suppose we want to <strong>double each number</strong>.</p>
<h3>Using a traditional loop</h3>
<pre><code class="language-javascript">const numbers = [2, 4, 6, 8]

const doubled = []

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

console.log(doubled)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">[4, 8, 12, 16]
</code></pre>
<p>Now let’s do the same thing using <code>map()</code>.</p>
<h3>Using map()</h3>
<pre><code class="language-javascript">const numbers = [2, 4, 6, 8]

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

console.log(doubled)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">[4, 8, 12, 16]
</code></pre>
<p>Original array stays the same:</p>
<pre><code class="language-js">console.log(numbers)
</code></pre>
<pre><code class="language-plaintext">[2, 4, 6, 8]
</code></pre>
<p><code>map()</code> creates a <strong>new array</strong> instead of modifying the original one.</p>
<hr />
<h1>filter()</h1>
<p>Sometimes we only want <strong>some values from an array</strong>.</p>
<p>That is where <code>filter()</code> helps.</p>
<p>It returns a <strong>new array containing only the values that match a condition</strong>.</p>
<p>Example array:</p>
<pre><code class="language-javascript">const numbers = [5, 12, 8, 20, 3]
</code></pre>
<p>Suppose we want <strong>numbers greater than 10</strong>.</p>
<h3>Using a loop</h3>
<pre><code class="language-javascript">const numbers = [5, 12, 8, 20, 3]

const result = []

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

console.log(result)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">[12, 20]
</code></pre>
<p>Now using <code>filter()</code>.</p>
<h3>Using filter()</h3>
<pre><code class="language-javascript">const numbers = [5, 12, 8, 20, 3]

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

console.log(result)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">[12, 20]
</code></pre>
<p>Again, the original array remains unchanged.</p>
<hr />
<h1>reduce()</h1>
<p><code>reduce()</code> is used when we want to <strong>combine all values into a single result</strong>.</p>
<p>Common examples:</p>
<ul>
<li><p>calculating totals</p>
</li>
<li><p>counting items</p>
</li>
<li><p>combining values</p>
</li>
</ul>
<p>Example array:</p>
<pre><code class="language-javascript">const numbers = [10, 20, 30, 40]
</code></pre>
<p>Suppose we want the <strong>total sum</strong>.</p>
<h3>Using a loop</h3>
<pre><code class="language-javascript">const numbers = [10, 20, 30, 40]

let total = 0

for (let i = 0; i &lt; numbers.length; i++) {
  total += numbers[i]
}

console.log(total)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">100
</code></pre>
<p>Now using <code>reduce()</code>.</p>
<h3>Using reduce()</h3>
<pre><code class="language-javascript">const numbers = [10, 20, 30, 40]

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

console.log(total)
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">100
</code></pre>
<p>How it works:</p>
<ol>
<li><p>Start with <code>sum = 0</code></p>
</li>
<li><p>Add each number</p>
</li>
<li><p>Return the final value</p>
</li>
</ol>
<img src="https://cdn.hashnode.com/uploads/covers/69513de35d3cf3dcde6a6e95/47803709-7f8d-4573-9214-af04e4a443a9.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h1>Try These Examples Yourself</h1>
<p>Create an array of numbers.</p>
<pre><code class="language-javascript">const numbers = [5, 10, 15, 20]
</code></pre>
<p>Double every number using <code>map()</code>.</p>
<pre><code class="language-javascript">const doubled = numbers.map(function(num) {
  return num * 2
})

console.log(doubled)
</code></pre>
<p>Get numbers greater than 10 using <code>filter()</code>.</p>
<pre><code class="language-javascript">const greater = numbers.filter(function(num) {
  return num &gt; 10
})

console.log(greater)
</code></pre>
<p>Calculate the total sum using <code>reduce()</code>.</p>
<pre><code class="language-javascript">const total = numbers.reduce(function(sum, num) {
  return sum + num
}, 0)

console.log(total)
</code></pre>
<p>Try running these examples in your browser console.</p>
<hr />
<p>Array methods make working with data much easier.</p>
<p>Instead of writing long loops, we can use built in methods like:</p>
<ul>
<li><p><code>map()</code> to transform values</p>
</li>
<li><p><code>filter()</code> to select values</p>
</li>
<li><p><code>reduce()</code> to combine values</p>
</li>
</ul>
]]></content:encoded></item></channel></rss>