<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Anoop Rajoriya</title>
    <description>The latest articles on Forem by Anoop Rajoriya (@anoop-rajoriya).</description>
    <link>https://forem.com/anoop-rajoriya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3269787%2Fc166c1cd-cb7f-4d02-b28c-f5f4a52a8e41.jpg</url>
      <title>Forem: Anoop Rajoriya</title>
      <link>https://forem.com/anoop-rajoriya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anoop-rajoriya"/>
    <language>en</language>
    <item>
      <title>The Magic of this, call(), apply(), and bind() in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Wed, 29 Apr 2026 14:56:20 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/the-magic-of-this-call-apply-and-bind-in-javascript-3d86</link>
      <guid>https://forem.com/anoop-rajoriya/the-magic-of-this-call-apply-and-bind-in-javascript-3d86</guid>
      <description>&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; is the final boss of the javascript developers. It's slippery, contaxtual, and changes its meaning based on where it called. Understanding it along with &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt; is the moment where you stop guessing what your code do and stat knowing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What this means in JavaScript (simple explanation)&lt;/li&gt;
&lt;li&gt;this inside normal functions&lt;/li&gt;
&lt;li&gt;this inside objects&lt;/li&gt;
&lt;li&gt;What call() does&lt;/li&gt;
&lt;li&gt;What apply() does&lt;/li&gt;
&lt;li&gt;What bind() does&lt;/li&gt;
&lt;li&gt;Difference between call, apply, and bind&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What &lt;code&gt;this&lt;/code&gt; means in JavaScript (simple explanation)
&lt;/h2&gt;

&lt;p&gt;Think of &lt;code&gt;this&lt;/code&gt; as dynamic noune like "i" or "me"&lt;/p&gt;

&lt;p&gt;if i say "i am eating" the word "i" refer to me, if you say same sentance then "i" refer to you. The sentace is same but subject is change depending on who is speaking.&lt;/p&gt;

&lt;p&gt;In javascript &lt;code&gt;this&lt;/code&gt; refer to the object which currently executing the code. It provide a way to access the properies of the object without knowing the name of object beforehand.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; inside normal functions
&lt;/h2&gt;

&lt;p&gt;When you use &lt;code&gt;this&lt;/code&gt; inside a standard, standalone function its value depending on whether you using &lt;strong&gt;Strict Mode&lt;/strong&gt; or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Non Strict Mode&lt;/strong&gt;: &lt;code&gt;this&lt;/code&gt; default to the window object inside the browser and global inside the node environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Strict Mode &lt;code&gt;use strict&lt;/code&gt;&lt;/strong&gt;: &lt;code&gt;this&lt;/code&gt; remains &lt;code&gt;undefined&lt;/code&gt; because javascript in strict mode prevents you from accidently modifying global object.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;checkThis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;checkThis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// In a browser: Window object&lt;/span&gt;
&lt;span class="c1"&gt;// In 'use strict': undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; inside object
&lt;/h2&gt;

&lt;p&gt;When function is defined inside object as a method, &lt;code&gt;this&lt;/code&gt; point to the object that owns the method. This is where the magic starts to become useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;smartphone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;iPhone 15&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 'this' refers to the smartphone object&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`This is an &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;smartphone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: This is an Apple iPhone 15.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you were extract the function and run it elsewhere, &lt;code&gt;this&lt;/code&gt; would lose its connection to the &lt;code&gt;smartphone&lt;/code&gt;. This losing context problem is exactly why the &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt; where invented.&lt;/p&gt;

&lt;h2&gt;
  
  
  What call() does
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;call()&lt;/code&gt; allow you to borrow a function from one object and use it for another. It invokes the funciton immediatly and allow you to manually specify what the &lt;code&gt;this&lt;/code&gt; should point to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt; functionName.call(thisArg, arg1, arg2, ...)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;setPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shipping&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; costs $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;tax&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;shipping&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;laptop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MacBook&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;phone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pixel 8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;800&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// We call setPrice but tell it to use 'laptop' as 'this'&lt;/span&gt;
&lt;span class="nx"&gt;setPrice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;laptop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// MacBook costs $1320&lt;/span&gt;
&lt;span class="nx"&gt;setPrice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Pixel 8 costs $860&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What apply() does
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;apply()&lt;/code&gt; is the twin siblin to the &lt;code&gt;call()&lt;/code&gt; it does the same thing: invokes the function immedialty with a custom this context, but it handle arguments differently, instead of listing arguments one by one, you pass them as an array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mnemonics:&lt;/strong&gt; &lt;strong&gt;A&lt;/strong&gt;pply == &lt;strong&gt;A&lt;/strong&gt;rray&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sumThree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Total: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Using apply to pass the array as individual arguments&lt;/span&gt;
&lt;span class="nx"&gt;sumThree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Total: 30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What bind() does
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;bind()&lt;/code&gt; is the different it does not invoke funciton immediatly, instead it create a new function that have a specific &lt;code&gt;this&lt;/code&gt; value locken in forever. This is incrediblly useful in react components, event listeners, or anytime you want to preset context for later use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Imagine passing this to a button click handler&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clickHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// clickHandler(); // Error! 'this' is lost.&lt;/span&gt;

&lt;span class="c1"&gt;// We "bind" the context to user and save it as a new function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;boundGreet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;boundGreet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hello, Alex (after 1 second)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Difference between call, apply, and bind
&lt;/h2&gt;

&lt;p&gt;While they all manipulate &lt;code&gt;this&lt;/code&gt;, their usage patterns differ:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;&lt;code&gt;call()&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;apply()&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;bind()&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Execution&lt;/td&gt;
&lt;td&gt;execute immediatly&lt;/td&gt;
&lt;td&gt;also execute immediatly&lt;/td&gt;
&lt;td&gt;return a new function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Arguments&lt;/td&gt;
&lt;td&gt;comma separated&lt;/td&gt;
&lt;td&gt;Array&lt;/td&gt;
&lt;td&gt;Comma separeted (for preset)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Main use case&lt;/td&gt;
&lt;td&gt;method borrowing&lt;/td&gt;
&lt;td&gt;when arguments are already in array&lt;/td&gt;
&lt;td&gt;Event listeners and callbacks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; is JavaScript’s dynamic "subject," referring to the current object context. &lt;code&gt;call()&lt;/code&gt; and &lt;code&gt;apply()&lt;/code&gt; invoke functions immediately—&lt;code&gt;call&lt;/code&gt; takes individual arguments, while &lt;code&gt;apply&lt;/code&gt; uses an array. &lt;code&gt;bind()&lt;/code&gt; is the patient one; it returns a new function with a permanently locked context, perfect for preserving the &lt;code&gt;this&lt;/code&gt; keyword in future event callbacks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>Understanding the this Keyword in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Tue, 28 Apr 2026 12:09:53 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/understanding-the-this-keyword-in-javascript-530e</link>
      <guid>https://forem.com/anoop-rajoriya/understanding-the-this-keyword-in-javascript-530e</guid>
      <description>&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword is one of the confusing concept of javascript, it not behave like it do in other languages like java or c++ in javascript. The value of &lt;code&gt;this&lt;/code&gt; determined not by where function definition is define but how it is called.&lt;/p&gt;

&lt;p&gt;Thinks it like a shapeshifter: it change its identity based on it's surroundings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What this represents&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; in global context&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; inside objects&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; inside functions&lt;/li&gt;
&lt;li&gt;How calling context changes &lt;code&gt;this&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What this represents
&lt;/h2&gt;

&lt;p&gt;At its core &lt;code&gt;this&lt;/code&gt; is a keyword that refers to a object. Which object? The one which currently execution the code.&lt;/p&gt;

&lt;p&gt;You can think &lt;code&gt;this&lt;/code&gt; as pronoun, in the sentance "Sarah is reading a book because she is bored." the word 'she' refer to a 'Sarah'. In javascript the &lt;code&gt;this&lt;/code&gt; keyword act as that 'she' or 'he' allowing you to reuse function across different objects without hardcodding object's name.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; in global context
&lt;/h2&gt;

&lt;p&gt;When you are using &lt;code&gt;this&lt;/code&gt; outside of any function or object, you are in &lt;strong&gt;Global Execution Context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In a browsers &lt;code&gt;this&lt;/code&gt; refer to the &lt;code&gt;window&lt;/code&gt; object and in node.js &lt;code&gt;this&lt;/code&gt; refer to a &lt;code&gt;global&lt;/code&gt; object or empty object if you are using module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// In a browser, this outputs the Window object&lt;/span&gt;
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Red"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenerio &lt;code&gt;this&lt;/code&gt; is essentially the "king of hill" its represent a entire environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; inside objects
&lt;/h2&gt;

&lt;p&gt;When a funciton is defined as method of object, here &lt;code&gt;this&lt;/code&gt; refer to the &lt;strong&gt;object that owns the method&lt;/strong&gt;. This is the most common and intutive use of &lt;code&gt;this&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Leo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hi, I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Hi, I am Leo"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example obove, because &lt;code&gt;greet&lt;/code&gt; was called on the &lt;code&gt;person&lt;/code&gt; object (&lt;code&gt;person.greet()&lt;/code&gt;) so the &lt;code&gt;this&lt;/code&gt; directly point to that &lt;code&gt;person&lt;/code&gt; object.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; inside functions
&lt;/h2&gt;

&lt;p&gt;This is where thing becom spicy, the vlaue of &lt;code&gt;this&lt;/code&gt; is depends on how the funciton is invoked.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simple Function Call&lt;/strong&gt;: in regular function &lt;code&gt;this&lt;/code&gt; point to global object inside function if you are using &lt;strong&gt;Strict Mode&lt;/strong&gt; like &lt;code&gt;use strict&lt;/code&gt; the &lt;code&gt;this&lt;/code&gt; will be &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Arrow Function&lt;/strong&gt;: arrow function are special they do not have thier own &lt;code&gt;this&lt;/code&gt; instead they inherit from surrounding (lexical) scope. This makes them perfect for callbacks but terrible for object methods.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Function Type&lt;/th&gt;
&lt;th&gt;this Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Regular Function&lt;/td&gt;
&lt;td&gt;The object that called it (or global/undefined)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Arrow Function&lt;/td&gt;
&lt;td&gt;Inherited from the parent scope&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  How calling context changes &lt;code&gt;this&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Javascript have explicit way to force &lt;code&gt;this&lt;/code&gt; to be whatever you want, this is called explicit binding. There are three primary methods to do that:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;call()&lt;/code&gt; and &lt;code&gt;bind()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;These methods call function immediatly while manually setting the value of &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;call()&lt;/code&gt; method takes argumetns separatly and &lt;code&gt;apply()&lt;/code&gt; takes arguments as an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;punctuation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, I'm &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;punctuation&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// .call() takes arguments individually&lt;/span&gt;
&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Output: "Hello, I'm Alice!"&lt;/span&gt;

&lt;span class="c1"&gt;// .apply() takes arguments as an array&lt;/span&gt;
&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="c1"&gt;// Output: "Hi, I'm Bob."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;code&gt;bind()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Unlike &lt;code&gt;call()&lt;/code&gt;, &lt;code&gt;bind()&lt;/code&gt; doen't run function immediatly it return a new function with &lt;code&gt;this&lt;/code&gt; value permanantly locked in. This is incrediblly usefull for event listeners.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// This loses context because 'this' becomes the global object or undefined&lt;/span&gt;
&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Hello, undefined"&lt;/span&gt;

&lt;span class="c1"&gt;// bind() creates a new function with 'this' locked to 'user'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;boundGreet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;boundGreet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Hello, Alex"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;code&gt;new&lt;/code&gt; keyword
&lt;/h3&gt;

&lt;p&gt;When you use &lt;code&gt;new&lt;/code&gt; keyword to create a instance from constructor function the js creates a breand new object and sets instance &lt;code&gt;this&lt;/code&gt; point to that object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Robot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myBot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Robot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;T-800&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myBot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "T-800"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; is a dynamic "shapeshifter" representing the execution context. Its value depends on how a function is called: pointing to the global object, an owner object, or a new instance. Regular functions bind at runtime; arrow functions inherit context lexically. Use &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, or &lt;code&gt;bind&lt;/code&gt; to manually control its identity.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>Map and Set in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Tue, 28 Apr 2026 10:07:13 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/map-and-set-in-javascript-4kc</link>
      <guid>https://forem.com/anoop-rajoriya/map-and-set-in-javascript-4kc</guid>
      <description>&lt;p&gt;Javascript comes from a long days where we manage everything with just objects and arrays, in ES6 a &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt; data structures are introduced which provide more specialized ways to handle data collections.&lt;/p&gt;

&lt;p&gt;Here are break down of how they work and why they matters:&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What Map is&lt;/li&gt;
&lt;li&gt;What Set is&lt;/li&gt;
&lt;li&gt;Difference between Map and Object&lt;/li&gt;
&lt;li&gt;Difference between Set and Array&lt;/li&gt;
&lt;li&gt;When to use Map and Set&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Map is
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Map&lt;/strong&gt; is a collection of keyed data items, very similar to object. The most basic differences is map allow keys of any types including functions, objects, and primitives.&lt;/p&gt;

&lt;p&gt;In a Map a data items stored in a &lt;code&gt;[key, value]&lt;/code&gt; pairs, and it remeber the original insertion order of keys.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features of Map
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You can use object as a keys which is impossible in a standard objects (which would stringify it like &lt;code&gt;"[object object]"&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;In map you can get the number of items using &lt;code&gt;.size&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Map are the iterable which means you can directly loop over them without extra step.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Common Methods of Map
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map.set(key, value)&lt;/code&gt;: store the value by key.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map.get(key)&lt;/code&gt;: return the value by key.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map.has(key)&lt;/code&gt;: return &lt;code&gt;true&lt;/code&gt; if the key exists.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map.delete(key)&lt;/code&gt;: remove the element by the key.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;apiCache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Check if we already have the data in our 'Map'&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Returning cached data for:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;apiCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// If not in cache, fetch it&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// Save the result in the Map for next time&lt;/span&gt;
  &lt;span class="nx"&gt;apiCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What Set is
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Set&lt;/strong&gt; is a special type of collection: collection of values (wihout key), where each value may occur only once.&lt;/p&gt;

&lt;p&gt;If you try to add duplicates values to a set, it simply ignore your request. This makes it ultimate tool for ensure uniqueness in your data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features of Set
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It automatically filter out the duplicates values.&lt;/li&gt;
&lt;li&gt;just like a map it also maintain insertion order.&lt;/li&gt;
&lt;li&gt;checking a specific values exist in a set is significantly faster then searching in array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Common Methods of Set
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;set.add(value)&lt;/code&gt;: add a value and returns the set itself.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;set.delete(value)&lt;/code&gt;: remove the value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;set.has(value)&lt;/code&gt;: return &lt;code&gt;true&lt;/code&gt; if value exist.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;set.clear()&lt;/code&gt;: removes everything from set.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// A list of tags entered by a user with duplicates&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rawTags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webdev&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webdev&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Convert to a Set to remove duplicates instantly&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;uniqueTags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawTags&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Convert back to an array to use it elsewhere&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cleanTagList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;uniqueTags&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;// Result: ['javascript', 'webdev', 'react']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Difference between Map and Object
&lt;/h2&gt;

&lt;p&gt;While they looks similar but they server different masters.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Key Types&lt;/strong&gt;: in object you can only use strings or symbols types but map allow to use any data types like function, object, and primitives.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Order&lt;/strong&gt;: object not strictly gurantee is they maintaing insertion order but map are strictly maintain insertion order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Size&lt;/strong&gt;: getting size of object need manual code but map has a builtin property &lt;code&gt;.size&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: optimized for small static records, but map provide better performance for frequent addtion and removals.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Iteration&lt;/strong&gt;: map required to use &lt;code&gt;Object.keys()&lt;/code&gt; or &lt;code&gt;for...in&lt;/code&gt; loops but map are directly iterable with &lt;code&gt;for...of&lt;/code&gt; loop.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Difference between Set and Array
&lt;/h2&gt;

&lt;p&gt;If you are wondering why we should not use array for everything, here is how they stack up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Duplicats&lt;/strong&gt;: array allowd to add duplicates value but set ignore you duplicate values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Access&lt;/strong&gt;: can use index in array but in set you don't have access, for it you need to use iterators or &lt;code&gt;.has()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Searching&lt;/strong&gt;: in array you can use &lt;code&gt;includes()&lt;/code&gt; or &lt;code&gt;indexOf()&lt;/code&gt; methods but it has O(n) time complexity which is slower for large dataset, but with &lt;code&gt;has()&lt;/code&gt; in set is much faster it provide O(1) constant time complexity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;: array used to store ordered list where duplicates are allowed but sets used for collection where you need unique items.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to use Map and Set
&lt;/h2&gt;

&lt;p&gt;Knowing the syntax is one thing, but knowing when to reach for them is what makes you an expert.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Map When
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The key are not strings&lt;/strong&gt;: if you need to associate data with DOM element or functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You need to preserve order&lt;/strong&gt;: when the sequance of entries matter for you ui.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Frequent updates&lt;/strong&gt;: maps are more performent than object when you are constantly adding or removing key valus pairs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use Set When
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You need a unique list&lt;/strong&gt;: for examples when you need to maintain a user ID's or tags on a blogs posts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;High performanc searching&lt;/strong&gt;: if you have a massive list and you need to constancly check "is this item in here" the set will crush an array in term of speed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data de-duplication&lt;/strong&gt;: the easiest way to remove duplicates from array is use set &lt;code&gt;const uniqu = = [... new Set(myArray)];&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;JavaScript Maps are ordered key-value collections allowing any key type, offering more flexibility than Objects. Sets store unique values with high-performance existence checks, unlike Arrays. Use Map for complex keys or ordered data, and reach for Set to eliminate duplicates and ensure efficient membership testing in large datasets.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>JavaScript Promises Explained for Beginners</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Mon, 27 Apr 2026 06:52:47 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/javascript-promises-explained-for-beginners-1big</link>
      <guid>https://forem.com/anoop-rajoriya/javascript-promises-explained-for-beginners-1big</guid>
      <description>&lt;p&gt;Think Javascript Promises as promise you makes in real life. Like you promise your friend to buy a coffee, they don't have coffee now but they have guarantee you will apear with a fresh coffee or a come back with excuse to why cafe was closed.&lt;/p&gt;

&lt;p&gt;In code promise help use to manage tasks which takes time like fetching data from server or leading heavy image wihout freezing the entire code while we waiting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What Problem Promises Solve&lt;/li&gt;
&lt;li&gt;Promise States (Pending, Fulfilled, Rejected)&lt;/li&gt;
&lt;li&gt;Basic Promise Lifecycle&lt;/li&gt;
&lt;li&gt;Handling Success and Failure&lt;/li&gt;
&lt;li&gt;Promise Chaining Concept&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Problem Promises Solve
&lt;/h2&gt;

&lt;p&gt;Before promises javascript used callbacks to perform tasks. If you need to do three taks depends on each other like logic user, fetch user profile, fetch user posts then you would end up nesting function inside funciton inside function.&lt;/p&gt;

&lt;p&gt;This creates infamous &lt;strong&gt;callback hell&lt;/strong&gt; also known as &lt;strong&gt;pyramid of doom&lt;/strong&gt;. The code become nearly impossible to read, incredibly fregile and nightmare to debugg because error handling had to reapeted at every level.&lt;/p&gt;

&lt;p&gt;Promise solve this by &lt;strong&gt;flattening&lt;/strong&gt; instead of nesting. Which makes code more linear and readable way that looks much more like standard top to bottom code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promise States (Pending, Fulfilled, Rejected)
&lt;/h2&gt;

&lt;p&gt;At any given movment the promise exist in one of the three state. Think it like ordering pizza.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pending:&lt;/strong&gt; its a initial process, you had ordered a pizza but it has not arrive yet, the process still in progress.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fulfilled (resolved):&lt;/strong&gt; success, the pizza arived. The process completed succefully, you had the result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rejected:&lt;/strong&gt; the bad news state, may be the shope ran out pepperoni or the delivery driver got lost. The operation failed and you have the reason (the error).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once a promise either be resolved or rejected it considred settled, it can not change its state again. you can't un-eat the pizza or have it fail after it already succeeded.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Promise Lifecycle
&lt;/h2&gt;

&lt;p&gt;The lifecycle starts with its creations and ends with its consuptions.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Creation:&lt;/strong&gt; you create a promise using &lt;code&gt;new Promise()&lt;/code&gt; constructor, it takes an callback called executor, which runs immeditaly. This function have 2 tools &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execution:&lt;/strong&gt; inside that executor you performd you heavy lifting like api calls etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resolution:&lt;/strong&gt; if the everything goes correctly you call &lt;code&gt;reslve(value)&lt;/code&gt; and if someting goes wrong you call &lt;code&gt;reject(error)&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;success&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The operation worked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Something went wrong.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Handling Success and Failure
&lt;/h2&gt;

&lt;p&gt;Once a promise created you need a way to say "okay, now what", here the consumer comes in, you use a specific methods to listen for the promise settle.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.then(data)&lt;/code&gt;: it runs when the promise &lt;strong&gt;fulfilled&lt;/strong&gt;, it recieves data you paased into &lt;code&gt;resolve(data)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.catch()&lt;/code&gt;: it runs when the promise &lt;strong&gt;rejected&lt;/strong&gt;, it catchs the error so your whole program does not crash.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.finally()&lt;/code&gt;: This runs no matter what happens (sucess or failure), it's great for cleanup tasks like removing loading spinner.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;myPromise&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "The operation worked!"&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Something went wrong."&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;All done!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Promise Chaining Concept
&lt;/h2&gt;

&lt;p&gt;This is the superpower of promises, When you return a value or a another promise from &lt;code&gt;then()&lt;/code&gt;. It automatically wrap that value in new promise. This allow you to link multiple asynchronous steps together in a vertical chain.&lt;/p&gt;

&lt;p&gt;instead of nesting, you simply keep adding &lt;code&gt;.then()&lt;/code&gt; blocks, if any step in chain will fail it skip all remaining &lt;code&gt;.then()&lt;/code&gt; blocks and jump to the nearest &lt;code&gt;.catch()&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fetchPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Returns a new promise&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;// Handles the result of fetchPosts&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;    &lt;span class="c1"&gt;// Catches errors from ANY of the above steps&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using chaining you code stays clean, and you logic stay linear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Promises manage asynchronous tasks, replacing messy "callback hell" with clean, readable code. They exist in three states: &lt;strong&gt;pending&lt;/strong&gt;, &lt;strong&gt;fulfilled&lt;/strong&gt;, or &lt;strong&gt;rejected&lt;/strong&gt;. Once settled, they use &lt;code&gt;.then()&lt;/code&gt; for success and &lt;code&gt;.catch()&lt;/code&gt; for errors, allowing developers to "chain" multiple operations together linearly for better control and error handling.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>Understanding Object-Oriented Programming in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Mon, 27 Apr 2026 05:24:20 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/understanding-object-oriented-programming-in-javascript-2lke</link>
      <guid>https://forem.com/anoop-rajoriya/understanding-object-oriented-programming-in-javascript-2lke</guid>
      <description>&lt;p&gt;OOPs is the programming style which organize code into &lt;strong&gt;Objects&lt;/strong&gt; rather than lists of instructions. It helps developers to model complex systems by grouping related data and behaviours together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What Object-Oriented Programming (OOP) means&lt;/li&gt;
&lt;li&gt;Real-world analogy (blueprint → objects)&lt;/li&gt;
&lt;li&gt;What is a class in JavaScript&lt;/li&gt;
&lt;li&gt;Creating objects using classes&lt;/li&gt;
&lt;li&gt;Constructor method&lt;/li&gt;
&lt;li&gt;Methods inside a class&lt;/li&gt;
&lt;li&gt;Basic idea of encapsulation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Object-Oriented Programming (OOP) means
&lt;/h2&gt;

&lt;p&gt;A object oriented programming is a paradigm (a way of thinking) where you view progarm as collection of objects that interact with each other.&lt;/p&gt;

&lt;p&gt;Instead of writing a script with lots of functions, you organize them as specific data (properties) and actions (methods) into a single unit called &lt;strong&gt;Object&lt;/strong&gt;. It make you program to easy to reuse, organize, and scale as project get bigger.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-world analogy (blueprint → objects)
&lt;/h2&gt;

&lt;p&gt;The easiest way to understand OOPs is to think about architect and house:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Blueprint (class)
&lt;/h3&gt;

&lt;p&gt;Architect design a detailed plan for a house, its not a house it self. It's just a detailed set of instructions of house which define overall structure of house like how walls constructs, how many window there are, where the doors go. &lt;/p&gt;

&lt;h3&gt;
  
  
  2. The House (object)
&lt;/h3&gt;

&lt;p&gt;Using this plan builder can buid ten different houses, each house is a real physical thing made from that plan. While they all follow the same blueprint, one house might be printed blue or another is red.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a class in JavaScript
&lt;/h2&gt;

&lt;p&gt;In javascript, class is used to create a blueprint before class was introduced in ES6 &lt;strong&gt;Prototypes&lt;/strong&gt; are used. Prototypes were a bit more complex, class provides a cleaner, easy readable way to define how object should looks and behave.&lt;/p&gt;

&lt;p&gt;In a class you define two things &lt;strong&gt;Properties &amp;amp; Methods&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Propeties:&lt;/strong&gt; what the object is like color, name, size etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Methods:&lt;/strong&gt; what the object does like run, stop, burk etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating objects using classes
&lt;/h2&gt;

&lt;p&gt;To create a new object from a class we use &lt;code&gt;new&lt;/code&gt; keyword. This process called &lt;strong&gt;instantiation&lt;/strong&gt;,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Laptop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Class definition goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Creating an object (instance) of the Laptop class&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myMacbook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Laptop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myThinkpad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Laptop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this &lt;code&gt;myMackbook&lt;/code&gt; and &lt;code&gt;myThinkpad&lt;/code&gt; are the two distinct object created from same &lt;code&gt;Laptop&lt;/code&gt; class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Constructor method
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;constructor&lt;/code&gt; is the special method which run authomatically the moment you create new object. It job is to set-up or initialize object's properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 'this' refers to the object being created&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Toyota&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: Toyota&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword is crucial, it tell javascript to assign a values to the unique object you are currently creating.&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods inside a class
&lt;/h2&gt;

&lt;p&gt;Methods are the function belongs to class, its used to perform tasks that object can do. Unlike standard function we don't use &lt;code&gt;function&lt;/code&gt; keyword, we declare methods without it in class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Robot&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// This is a method&lt;/span&gt;
  &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wallE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Robot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;WALL-E&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;wallE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: Hello, my name is WALL-E!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Basic idea of encapsulation
&lt;/h2&gt;

&lt;p&gt;The encapsulation is the practise of bundling data and mehods into a single unit (a class) and &lt;strong&gt;restricting access&lt;/strong&gt; to some of the other objects components.&lt;/p&gt;

&lt;p&gt;Think it like a capsul you dont need to see the chamical powder inside to take madicine, you just need to interact with outer shell.&lt;/p&gt;

&lt;p&gt;In programming encapsulation: protect data from external code to accientally changing the internal variabals. Hide complexity: you only interact with simple method &lt;code&gt;startEngine()&lt;/code&gt; wihtout needing to know complex logic happening inside.&lt;/p&gt;

&lt;p&gt;In OOPs, its a good practise to use &lt;code&gt;#&lt;/code&gt; or &lt;code&gt;_&lt;/code&gt; symbols before the any private variable to make it private, meaning it cannot be directly accessed or changed from outside the class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symmary
&lt;/h2&gt;

&lt;p&gt;Object-Oriented Programming (OOP) organizes code into reusable &lt;strong&gt;objects&lt;/strong&gt; containing data and behaviors. &lt;strong&gt;Classes&lt;/strong&gt; act as blueprints, while objects are the physical instances. &lt;strong&gt;Constructors&lt;/strong&gt; initialize properties, and &lt;strong&gt;methods&lt;/strong&gt; define actions. &lt;strong&gt;Encapsulation&lt;/strong&gt; protects internal data, hiding complexity and improving code structure, making systems easier to manage, scale, and maintain effectively over time.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>Synchronous vs Asynchronous JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Sun, 26 Apr 2026 11:08:13 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/synchronous-vs-asynchronous-javascript-4oh</link>
      <guid>https://forem.com/anoop-rajoriya/synchronous-vs-asynchronous-javascript-4oh</guid>
      <description>&lt;p&gt;To understand javascript you have to understand its &lt;strong&gt;single threaded&lt;/strong&gt; nature. Imagin a chef in tiny kitchen with only single burner, that burner is the &lt;strong&gt;javascript engine&lt;/strong&gt; and how the chef manage burner heat which determins either restaurant runs smoothly or ends up in a shouting match.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What synchronous code means&lt;/li&gt;
&lt;li&gt;What Asynchronous Code Means&lt;/li&gt;
&lt;li&gt;Why JavaScript Needs Asynchronous Behavior&lt;/li&gt;
&lt;li&gt;Examples: Timers and API Calls&lt;/li&gt;
&lt;li&gt;Problems with Blocking Code&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What synchronous code means
&lt;/h2&gt;

&lt;p&gt;Synchronous or sync is the default behaviour of the javascript, its a top to bottom, line by line execution of the code, in that each operaiton must be finish before next one starts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Call Stack:&lt;/strong&gt; a stack data structured by the js we called it call stack, it used to keep track what happening. When operation execution start it pushed into the call stack and after finishing it pop out from it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sequential Nature:&lt;/strong&gt; if line 2 takes 10 seconds to finish the line 3 not run until those 10 seconds are up.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Asynchronous Code Means
&lt;/h2&gt;

&lt;p&gt;Asynchronous or async code allow javascript to intiat a task and move to the next one without waiting for completion of first task. When long running task completed it notify js engine to handle the result.&lt;/p&gt;

&lt;p&gt;This achieves with the help of &lt;strong&gt;Event Loop&lt;/strong&gt; which cordinate between callstack, web apis, and the callback queues.&lt;/p&gt;

&lt;p&gt;Non-blocking code which doesn't stop the execution of the script. Javscript hands off the heavy lifting task like file operations, network calling or times to the environment we called it &lt;strong&gt;delegation&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why JavaScript Needs Asynchronous Behavior
&lt;/h2&gt;

&lt;p&gt;If javascript were purly synchronous it will be useless to the moderen browsers in web development.&lt;/p&gt;

&lt;p&gt;Since javascript has only one main thread. Any heavy task would freeze the entire browser or programm execution known as &lt;strong&gt;Single Threaded Promblem&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Asynchronous code allow us to perform any heavy lifting task in the background without intruppting user's experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples: Timers and API Calls
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Timer &lt;code&gt;setTimeout()&lt;/code&gt;&lt;/strong&gt;, even if you set timer for 0 milliseconds it becomes asynchronous code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Inside Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="c1"&gt;// Start&lt;/span&gt;
&lt;span class="c1"&gt;// End&lt;/span&gt;
&lt;span class="c1"&gt;// Inside Timeout&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;setTimeout()&lt;/code&gt; handed callback to the browser timer api and finishing remaing code after event loop push back on to the call stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Api call &lt;code&gt;fetch()&lt;/code&gt;&lt;/strong&gt;, if you make a data request to the server you don't now how much time response take to come back the the client is 10 milliseconds or 10 seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fetching data...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Data received:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Doing other things...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The network request handled by the network api which push the result into call stack when it finished, not block the other execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problems with Blocking Code
&lt;/h2&gt;

&lt;p&gt;Blocking code occure when syncronous code takes too long time which hold the main thread prevent to executre other tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Frozen UI&lt;/strong&gt;, the browser rendering engine usually run on the main thread, if the thread block the browser can not repaint the screen. To user side it looks applicaiton chashed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lagginess&lt;/strong&gt;, even minor blocking can cause "junk" - stammering animation make ui cheaper or broken.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stack Overflow&lt;/strong&gt;, excessive synchronous recursion can fill the call stack and exceed its limits which crashing script entirely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Comparison Table:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Synchronous&lt;/th&gt;
&lt;th&gt;Asynchronous&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Execution&lt;/td&gt;
&lt;td&gt;Line-by-line (Sequential)&lt;/td&gt;
&lt;td&gt;Concurrent-ish (Non-blocking)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wait time&lt;/td&gt;
&lt;td&gt;Blocks until finished&lt;/td&gt;
&lt;td&gt;Moves to next task immediately&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Usage&lt;/td&gt;
&lt;td&gt;"Simple logic, math, variable setup"&lt;/td&gt;
&lt;td&gt;"Data fetching, Timers, File I/O"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Complexity&lt;/td&gt;
&lt;td&gt;Easy to read and debug&lt;/td&gt;
&lt;td&gt;Requires Promises or Async/Await&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Synchronous JavaScript executes code sequentially, blocking the thread until tasks finish. Asynchronous code prevents UI freezes by offloading long-running operations—like API calls or timers—to the browser environment. Managed by the Event Loop, this non-blocking approach ensures applications stay responsive while handling background tasks, crucial for modern web performance.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>Async/Await in JavaScript: Writing Cleaner Asynchronous Code</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Sun, 26 Apr 2026 09:17:27 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/asyncawait-in-javascript-writing-cleaner-asynchronous-code-3e8a</link>
      <guid>https://forem.com/anoop-rajoriya/asyncawait-in-javascript-writing-cleaner-asynchronous-code-3e8a</guid>
      <description>&lt;p&gt;If you ever lost youself forest of &lt;code&gt;.then()&lt;/code&gt; or trapped in the &lt;strong&gt;callback hell&lt;/strong&gt; then &lt;code&gt;async/await&lt;/code&gt; is about to become you'r best friend. It introduced in ES2017, it's ofthen known as &lt;strong&gt;syntatic sugar&lt;/strong&gt; because it does not change how javascript work under the hood, its only make it easier for us humans to read.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Why async/await was introduced&lt;/li&gt;
&lt;li&gt;How Async Functions Work&lt;/li&gt;
&lt;li&gt;The await Keyword Concept&lt;/li&gt;
&lt;li&gt;Error Handling with Async Code&lt;/li&gt;
&lt;li&gt;Comparison: Promises vs. Async/Await&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why async/await was introduced
&lt;/h2&gt;

&lt;p&gt;Before the &lt;code&gt;asyn/await&lt;/code&gt;, we handle asynchronous operation by callbacks and then promises were introduced. While promises were a massive upgrade but it have some issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Chaining complexisty:&lt;/strong&gt; long chaining of &lt;code&gt;.then()&lt;/code&gt; become difficult to follow, especially when passing data between them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verbosity:&lt;/strong&gt; you had write a lot of boiler plate just to write a simple data fetch.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging:&lt;/strong&gt; error handling with &lt;code&gt;.catch()&lt;/code&gt; was something tricky and stack traces inside promises chain were not alway helpful.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;async/await&lt;/code&gt; was created to make asynchronous code look and behave like a synchronous code, reduceing cognitive load and making logic flow linearly.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Async Functions Work
&lt;/h2&gt;

&lt;p&gt;To use this featuer, you start with &lt;code&gt;async&lt;/code&gt; keyword by placing it before the function declaration, arrow function and methods.&lt;/p&gt;

&lt;p&gt;The most important thing is &lt;code&gt;async&lt;/code&gt; function always return a &lt;code&gt;pormise&lt;/code&gt;. Even you return a simple string or number it authomatically wrap it by resolved promise and if you throw any error then it return a rejected promise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// This is equivalent to:&lt;/span&gt;
&lt;span class="c1"&gt;// return Promise.resolve("Hello, World!");&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "Hello, World!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The &lt;code&gt;await&lt;/code&gt; Keyword Concept
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; keyword is the magical ingrediant of the &lt;code&gt;async&lt;/code&gt; function. It tell the javascript to paused the specific awaited code inside async function untile this promise settles.&lt;/p&gt;

&lt;p&gt;While the function freez but you rest of applicaiton dose not freez, js engine free go to do other things because pause is the non-blocking to main thread.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;await&lt;/code&gt; is only used inside the &lt;code&gt;async&lt;/code&gt; function or at top level of a module in modern environments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fetching...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Execution pauses here until the fetch promise resolves&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Data received:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Error Handling with Async Code
&lt;/h2&gt;

&lt;p&gt;The one of the biggest wins is it allow us to use standard &lt;code&gt;try...catch&lt;/code&gt; blocks. This is the same pattern used in synchronous code, making our error handling logic consistance across entire codebase.&lt;/p&gt;

&lt;p&gt;instead of attaching a &lt;code&gt;.catch()&lt;/code&gt; block you wrape you awated calls inside the &lt;code&gt;try&lt;/code&gt; block and handle error in &lt;code&gt;catch&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Network response was not ok&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Oops! Something went wrong:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Comparison: Promises vs. Async/Await
&lt;/h2&gt;

&lt;p&gt;To see the clear difference lets look at the same task handled in both ways:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Using Promises:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getRecipe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/ingredients&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ingredients&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/api/cook/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;ingredients&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dish&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dish&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Using Async/Await:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getRecipe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/ingredients&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ingredients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/api/cook/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;ingredients&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dish&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Promise (&lt;code&gt;.then&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;Async/Await&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Readability&lt;/td&gt;
&lt;td&gt;become nested or overly long&lt;/td&gt;
&lt;td&gt;more lenear, looks like synchronous code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Error Handling&lt;/td&gt;
&lt;td&gt;Uses &lt;code&gt;.catch&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Uses &lt;code&gt;try...catch&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Conditionals&lt;/td&gt;
&lt;td&gt;hard to handle logic inside chains&lt;/td&gt;
&lt;td&gt;stanard &lt;code&gt;if...else&lt;/code&gt; work naturally&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Debugging&lt;/td&gt;
&lt;td&gt;harder to set breakpoints in chains&lt;/td&gt;
&lt;td&gt;easy to set breakpoints on each line&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Async/await makes asynchronous JavaScript look synchronous, replacing messy Promise chains. The &lt;code&gt;async&lt;/code&gt; keyword ensures functions return Promises, while &lt;code&gt;await&lt;/code&gt; pauses execution non-blockingly until tasks finish. This enables standard &lt;code&gt;try...catch&lt;/code&gt; error handling, making code cleaner, easier to debug, and more readable than traditional &lt;code&gt;.then()&lt;/code&gt; syntax.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>Error Handling in JavaScript: Try, Catch, Finally</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Sat, 25 Apr 2026 10:43:02 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/error-handling-in-javascript-try-catch-finally-42j7</link>
      <guid>https://forem.com/anoop-rajoriya/error-handling-in-javascript-try-catch-finally-42j7</guid>
      <description>&lt;p&gt;Handling errors is the difference between professional application and one who left user staring at the frozen screen. In javascript things will go wrong - apis will fails, user will provide creative inputs, and variables will be &lt;code&gt;undfined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is the in depth guide to managing the chaos using &lt;code&gt;try&lt;/code&gt;, &lt;code&gt;catch&lt;/code&gt;, &lt;code&gt;finally&lt;/code&gt;, and custom errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What errors are in JavaScript&lt;/li&gt;
&lt;li&gt;Using try and catch Blocks&lt;/li&gt;
&lt;li&gt;The finally Block&lt;/li&gt;
&lt;li&gt;Throwing Custom Errors&lt;/li&gt;
&lt;li&gt;Why Error Handling Matters&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What errors are in JavaScript
&lt;/h2&gt;

&lt;p&gt;Error is the object in javascript, when things goes wrong the JS &lt;code&gt;throws&lt;/code&gt; this object. It contains name (a type or error), message (a human readable description), and a stack tracing (A GPS of code: provide location of error from it generate).&lt;/p&gt;

&lt;p&gt;If error did not chaught the js stops the entire exection of programm. There are some common built in errors types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ReferenceError:&lt;/strong&gt; using variables that has not been declared.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TypeError:&lt;/strong&gt; performing operation on wrong data types.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SyntaxError:&lt;/strong&gt; writing code that js engine can't parse (try/catch can't catch syntax error in same script block because the code will not even run).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RangeError:&lt;/strong&gt; using a number outside the allowable range.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using try and catch Blocks
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;try...catch&lt;/code&gt; statements are you safety net. You wrap the code wich might be fails in the &lt;code&gt;try&lt;/code&gt; block and defining how you want to handle that failure in &lt;code&gt;catch&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Code that might cause an error&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid JSON String&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This line will never run.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Code to handle the error&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Oops! Something went wrong:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Message:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// The error description&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Name:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// The type of error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;catch&lt;/code&gt; block only execute if the code in &lt;code&gt;try&lt;/code&gt; will fails if the code run successfully the &lt;code&gt;catch&lt;/code&gt; block skipped entirely.&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;finally&lt;/code&gt; Block
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;finally&lt;/code&gt; block is the "loyal companion" of error handling. It execture not matter what wheter an error was throwen or not, or even you return early form &lt;code&gt;try&lt;/code&gt; or &lt;code&gt;catch&lt;/code&gt; blocks.&lt;/p&gt;

&lt;p&gt;It primary purpose is cleanup, you placing code which close database connection, stop loading spinners or releasing file handles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;fetchDataFromAPI&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error fetching data:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cleanup complete. Loader hidden.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Throwing Custom Errors
&lt;/h2&gt;

&lt;p&gt;Some times code is technically valid but logically wrong like user entering negative age, you can use &lt;code&gt;throw&lt;/code&gt; keyword to manually trigger errors.&lt;/p&gt;

&lt;p&gt;While you can throw any things like &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt; but it is a good practise to throw &lt;code&gt;Error Object&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;checkAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Age cannot be negative!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Creating a custom error&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access granted&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;checkAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Age cannot be negative!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For advanced use cases you can &lt;code&gt;extend&lt;/code&gt; Error to create a customized error types like &lt;code&gt;ValidationError&lt;/code&gt; or &lt;code&gt;DatabaseError&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Error Handling Matters
&lt;/h2&gt;

&lt;p&gt;Why not console let to show the error? becuase the error handling is vital for several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Graceful Degradation:&lt;/strong&gt; instead of showing white screen, show the user a friendly "Sorry, our servers are napping" message.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System Stability:&lt;/strong&gt; a single api fails can  should not crash the entire user interface.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier Debugging:&lt;/strong&gt; well-placed &lt;code&gt;try...catch&lt;/code&gt; blocks can log the data about state of application when it failed, which make it eaier to fix.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; by catching errors you can prevent the browers form leaking sensitive stack traces or server-side file paths to attackers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Block&lt;/th&gt;
&lt;th&gt;Execution Rule&lt;/th&gt;
&lt;th&gt;Typical Use Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;try&lt;/td&gt;
&lt;td&gt;Always runs first.&lt;/td&gt;
&lt;td&gt;Code that might fail (API calls, JSON parsing).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;catch&lt;/td&gt;
&lt;td&gt;Runs only if try fails.&lt;/td&gt;
&lt;td&gt;Error logging, showing user alerts, retrying logic.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;finally&lt;/td&gt;
&lt;td&gt;Always runs last.&lt;/td&gt;
&lt;td&gt;Closing connections, hiding loaders, resetting state.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;throw&lt;/td&gt;
&lt;td&gt;Manual trigger.&lt;/td&gt;
&lt;td&gt;Enforcing business logic or custom validation.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>Understanding Objects in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Sat, 25 Apr 2026 08:42:23 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/understanding-objects-in-javascript-4ca5</link>
      <guid>https://forem.com/anoop-rajoriya/understanding-objects-in-javascript-4ca5</guid>
      <description>&lt;p&gt;Objects are the backbone of the javascript eco-system. Understanding it like a understanding language DNA's. Here are the deep dive into how they works and why they are the MVP or your codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What objects are and why they are needed&lt;/li&gt;
&lt;li&gt;Creating objects&lt;/li&gt;
&lt;li&gt;Accessing properties&lt;/li&gt;
&lt;li&gt;Updating object properties&lt;/li&gt;
&lt;li&gt;Adding and deleting properties&lt;/li&gt;
&lt;li&gt;Looping through object keys&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What objects are and why they are needed
&lt;/h2&gt;

&lt;p&gt;In programming we ofter need to represent complex entitis. A simple variables like string or number can only hold one piece of data, however real world item have a multiple characterstics.&lt;/p&gt;

&lt;p&gt;A objects are a collection or properties &amp;amp; methods, they store values in key-values pairs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You might thinking is really we need object there is why:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;without object keeping track of related data is messy, imagin you have to store a user profile data, keeping each data in single variable like &lt;code&gt;username&lt;/code&gt;, &lt;code&gt;userEmail&lt;/code&gt;, and &lt;code&gt;userAge&lt;/code&gt;. If there are 100 user then you have to manage 300 disconnected variable. Object allow you to group that data into one single package like &lt;code&gt;const user = {username: "siv", email: "siv2testing.com", age: 21}&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating objects
&lt;/h2&gt;

&lt;p&gt;The most common way to create object is useing &lt;strong&gt;Object Literals&lt;/strong&gt; syntax (the curly braces)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// A Smartphone Example&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;smartphone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;iPhone 15&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;is5G&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;batteryLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// Objects can even contain functions (methods)&lt;/span&gt;
  &lt;span class="na"&gt;ring&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Beep! Beep!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing properties
&lt;/h2&gt;

&lt;p&gt;One the object created you need a way to get the data back out. There are two primary ways to do this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Dot Notation:&lt;/strong&gt; this is the most common and readable way, you use it when you now the name of the property beforehand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;smartphone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Apple"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Bracket Notation:&lt;/strong&gt; this is more powerful way you will be using it, if you key names have spaces, special characters or using variables to access keys.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;keyToLookup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;model&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;smartphone&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;model&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;    &lt;span class="c1"&gt;// "iPhone 15"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;smartphone&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;keyToLookup&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// "iPhone 15" (Dynamic access)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Updating object properties
&lt;/h2&gt;

&lt;p&gt;Objects in js are mutables means you can change the values inside the object even if you declare it with &lt;code&gt;const&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;make&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tesla&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Parked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Changing the value&lt;/span&gt;
&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Driving&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;make&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tesla Motors&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Driving"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding and deleting properties
&lt;/h2&gt;

&lt;p&gt;Objects are flexibles mean they can grow or shrink during the execution of you programm.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Properties
&lt;/h3&gt;

&lt;p&gt;You adding a property by simply assigning a vlaue to a key that dosn't exist yet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;employee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sarah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Developer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Property added&lt;/span&gt;
&lt;span class="nx"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;salary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;90000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Property added&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deleting Properties
&lt;/h3&gt;

&lt;p&gt;You using a &lt;code&gt;delete&lt;/code&gt; operator to remvoe the key-value paires entirely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Looping through object keys
&lt;/h2&gt;

&lt;p&gt;Unlike array, you can not use standard &lt;code&gt;for&lt;/code&gt; loop to iterate over objects because they don't have indexes, instead we use &lt;code&gt;for...in&lt;/code&gt; loop or &lt;code&gt;Object&lt;/code&gt; built-in methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;for...in&lt;/code&gt; Loop
&lt;/h3&gt;

&lt;p&gt;This loop iterate over every enumarable property of the objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;math&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;science&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;history&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;subject&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Output: &lt;/span&gt;
&lt;span class="c1"&gt;// math: 90&lt;/span&gt;
&lt;span class="c1"&gt;// science: 85&lt;/span&gt;
&lt;span class="c1"&gt;// history: 88&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using &lt;code&gt;Object.keys()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If you need a array of keys to use moderen array methods like &lt;code&gt;map()&lt;/code&gt;, or &lt;code&gt;filter()&lt;/code&gt;. Use this to create a array contains all keys of the intended object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// ["math", "science", "history"]&lt;/span&gt;

&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;JavaScript objects group related data using &lt;strong&gt;key-value pairs&lt;/strong&gt;, making complex data manageable. You create them via literals &lt;code&gt;{}&lt;/code&gt;, access properties using &lt;strong&gt;dot&lt;/strong&gt; or &lt;strong&gt;bracket&lt;/strong&gt; notation, and freely add, update, or delete entries. To iterate through data, use &lt;code&gt;for...in&lt;/code&gt; loops or &lt;code&gt;Object.keys()&lt;/code&gt; to handle keys dynamically and efficiently.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>Array Methods You Must Know</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Fri, 24 Apr 2026 10:56:44 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/array-methods-you-must-know-4kh3</link>
      <guid>https://forem.com/anoop-rajoriya/array-methods-you-must-know-4kh3</guid>
      <description>&lt;p&gt;Arrays are the bread and butter of the javascript. Whether you are building shopping cart or social media feed you are spending lots of time poking and prodding lists of data.&lt;/p&gt;

&lt;p&gt;here are the breakdown of the essentials arrays methods with real world examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Adding &amp;amp; Removing from the End&lt;/li&gt;
&lt;li&gt;Adding &amp;amp; Removing from the Front&lt;/li&gt;
&lt;li&gt;Transformation &amp;amp; Selection&lt;/li&gt;
&lt;li&gt;The "Heavier" Methods&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Adding &amp;amp; Removing from the End
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;push()&lt;/code&gt; and &lt;code&gt;pop()&lt;/code&gt; methods are used to adding and removing elements from the end of the arrays, it's like a interact with pringles can.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;push()&lt;/code&gt;: this method take a items which you want to add in array end, it returns a length of the array after the adding item.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;milk&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bread&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eggs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ["milk", "bread", "eggs"], 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pop()&lt;/code&gt;: remove the last elment form an array and return it. It does not accept any arguments.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ["milk", "bread"], "eggs"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding &amp;amp; Removing from the Front
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;shift()&lt;/code&gt; and &lt;code&gt;unshift()&lt;/code&gt; methods are used to add or remove items form the front of the array. You can think it like a waiting line at a coffee shop.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;shift()&lt;/code&gt;: it remove the front element and return the current length after removing element. It does not accept any argument.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rahul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dipali&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jani&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ["dipali", "jani"], 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;unshift()&lt;/code&gt;: it add one or more element form beginning of array, elements added in order passed to it. It return new length of the array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;VIP Guest&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Senior cityson&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// ["VIP Guest", "Senior cityson", "dipali", "jani"], 4&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Transformation &amp;amp; Selection
&lt;/h2&gt;

&lt;p&gt;These methods are not changes yor original arrays instead it returns a brand new array based on yor interection.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map()&lt;/code&gt;: creates a new array by performaing an action on every item in the original array. Action is provided as callback, processing item and it's index are passed to callback.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;priceInInr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;priceInUsd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;priceInInr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// print prcessing price index&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.011&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;priceInUsd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ['0.11', '1.10', '11.00']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;filter()&lt;/code&gt;: mostly used to filter items based on certain condition. Accept callback in that item and index passed as arguments. It return a new array containing elements passed a specific condition.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dune&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;genre&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sci-Fi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Emma&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;genre&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Romance&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sciFiOnly&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;genre&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sci-Fi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sciFiOnly&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//[{ title: 'Dune', genre: 'Sci-Fi' }]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The "Heavier" Methods
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;reduce()&lt;/code&gt;: it takes two arguments a callback (reducer) and a accumulator. It runs reducer on each element, accumulator and current items are paased as an argument to reducer. Returned value form reducer is passed as an accumulator in next iteration. If accumulator not passed explicitly it assigned accumulator by it self: first value in array become accumulator and next one is current item.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bill&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;bill&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;foreach()&lt;/code&gt;: used to traver over an array, it takes two argumetns a callback and thisArg. In callback it passed these three things: &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;current value:&lt;/strong&gt; a items currently being processed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;index:&lt;/strong&gt; a index place of current item in original array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;array:&lt;/strong&gt; a original array &lt;code&gt;foreach&lt;/code&gt; called on.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The thisArg is the value you want to use it as this when executing the callback, its a optional argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Laptop&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;inStock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mouse&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;inStock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Keyboard&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;inStock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;totalBill&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inStock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;totalBill&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Alert: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is currently unavailable.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Your total is: $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;totalBill&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;push&lt;/code&gt;/&lt;code&gt;pop&lt;/code&gt; and &lt;code&gt;unshift&lt;/code&gt;/&lt;code&gt;shift&lt;/code&gt; add or remove items from array ends and fronts. &lt;code&gt;map&lt;/code&gt; transforms elements into new lists, while &lt;code&gt;filter&lt;/code&gt; extracts specific data. &lt;code&gt;reduce&lt;/code&gt; condenses arrays into single values, and &lt;code&gt;forEach&lt;/code&gt; runs logic for every item. These methods are essential for clean, efficient JavaScript data handling.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>String Polyfills and Common Interview Methods in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Thu, 23 Apr 2026 18:15:29 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/string-polyfills-and-common-interview-methods-in-javascript-176i</link>
      <guid>https://forem.com/anoop-rajoriya/string-polyfills-and-common-interview-methods-in-javascript-176i</guid>
      <description>&lt;p&gt;Strings are more than just a sequance of characters. It's a backbone concept of the data manipulation. Whether you are cleaning user input to solving leetcode problems, understaing how they work under the hood gives you super power.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What are String Methods?&lt;/li&gt;
&lt;li&gt;Why Developers Write Polyfills?&lt;/li&gt;
&lt;li&gt;Implementing Simple String Utilities (Polyfills)&lt;/li&gt;
&lt;li&gt;Common Interview String Problems&lt;/li&gt;
&lt;li&gt;Importance of Understanding Built-in Behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What are String Methods?
&lt;/h2&gt;

&lt;p&gt;In javascript strings are the primitive data types but js wraped it by &lt;code&gt;String&lt;/code&gt; object temporarly allow you to access some built-in methods on string. These methods are lived within &lt;code&gt;String.prototype&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;When you using those methods remember than strings are immutable so these never change it, they instead return a new string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are some string methods categorized:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Searching:&lt;/strong&gt; &lt;code&gt;indexOf()&lt;/code&gt;, &lt;code&gt;includes()&lt;/code&gt;, &lt;code&gt;startsWith()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transforming:&lt;/strong&gt; &lt;code&gt;toUpperCase()&lt;/code&gt;, &lt;code&gt;trim()&lt;/code&gt;, &lt;code&gt;replace()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extraction:&lt;/strong&gt; &lt;code&gt;slice()&lt;/code&gt;, &lt;code&gt;substring()&lt;/code&gt;, &lt;code&gt;split()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Developers Write Polyfills
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;polyfills&lt;/strong&gt; are the piece of code (usually on the brower) which provides a specific morder functionalities to the older systems thos did not have native support of these functionalities.&lt;/p&gt;

&lt;p&gt;If the browser already have the method we use it (it's faster), if it does not have this native support then we fill it out that hole with own custom implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are some common reasons for polyfills:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lagacy Support:&lt;/strong&gt; supporting older environments like &lt;code&gt;IE11&lt;/code&gt; that lack of &lt;code&gt;ES6+&lt;/code&gt; features like &lt;code&gt;.includes()&lt;/code&gt; and &lt;code&gt;.padStarts()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Interview Performance:&lt;/strong&gt; some interviewers ask you to write a polyfills to test your understanding of concepts, prototypes, &lt;code&gt;this&lt;/code&gt; binding, and some edge cases handling.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementing Simple String Utilities (Polyfills)
&lt;/h2&gt;

&lt;p&gt;Lets looking how can create polyfills manually for &lt;code&gt;String.prototype.includes()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myIncludes&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myIncludes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchingStr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="c1"&gt;// this is the string on that method called&lt;/span&gt;
        &lt;span class="c1"&gt;// it search str from or after the position&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchingStr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;myIncludes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**Another example for &lt;code&gt;repeat&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myRepeat&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myRepeat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;count must be non-negative&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common Interview String Problems
&lt;/h2&gt;

&lt;p&gt;Interviewers love strings because it test you understandin abilites of loops and logic without using other complex data structres.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are some common problems interviewers ask:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Palidrome Check:&lt;/strong&gt; comparing string to its reverse version or use of concepts two pointer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anagram Check:&lt;/strong&gt; sort both strings and compares, or use frequency counter (Hash map).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;First Non-repeating Char:&lt;/strong&gt; use objects or map to store char coutns and loop again to find the first 1.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reverse Word:&lt;/strong&gt; &lt;code&gt;str.split(" ").reverse().join(" ")&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Longest Substring (No-Repeat):&lt;/strong&gt; use of sliding window technique with set.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Importance of Understanding Built-in Behavior
&lt;/h2&gt;

&lt;p&gt;Its easy to revers a string in javascript with the help of &lt;code&gt;.split("").reverse().join("")&lt;/code&gt; but the experts know the why and how.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Performance (Time Complexity):&lt;/strong&gt; many people don't understaning these &lt;code&gt;.split()&lt;/code&gt; and &lt;code&gt;.join()&lt;/code&gt; takes a O(n) time. Chaining them on short string may be fine but with it will become slow on larger data set.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Management:&lt;/strong&gt; since you now strings are immutable, every time you concatenate it in a loop like &lt;code&gt;str+= "0"&lt;/code&gt;, you are creating whole new string in memory which is a O(n^2) behaviour.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The 'Prototype' Chain:&lt;/strong&gt; understanding these methods are living on &lt;code&gt;String.Prototype&lt;/code&gt; property is the key of master inheritance in js. If you are modifing prototype you are affacting the every string in you application (which is why you should only do it for polyfills).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;JavaScript string methods provide immutable data manipulation. Polyfills ensure legacy browser compatibility by manually adding missing functionality to String.prototype. Understanding these tools is vital for solving interview problems like anagrams, optimizing performance, and mastering the prototype chain. It bridges the gap between basic coding and professional software engineering practices.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>The new Keyword in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Thu, 23 Apr 2026 05:06:28 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/the-new-keyword-in-javascript-17f9</link>
      <guid>https://forem.com/anoop-rajoriya/the-new-keyword-in-javascript-17f9</guid>
      <description>&lt;p&gt;To understand the &lt;code&gt;new&lt;/code&gt; keyword in deep you have to imagine it like a factory supervioser which takes a generic function and force it to behave like a object-manufacturing plant.&lt;/p&gt;

&lt;p&gt;Here are the deep dive to understand how these concepts intertwine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is constructor function&lt;/li&gt;
&lt;li&gt;What is new Keyword&lt;/li&gt;
&lt;li&gt;Waht is Object Creation Process&lt;/li&gt;
&lt;li&gt;How &lt;code&gt;new&lt;/code&gt; Links Prototypes&lt;/li&gt;
&lt;li&gt;Instances Created from Constructors&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The constructure function: Blueprint
&lt;/h2&gt;

&lt;p&gt;A constructor funciton is like a any normal function. There is nothing syntatically special about it unitle you invoked it with &lt;code&gt;new&lt;/code&gt; keyword. By convention we named it with capital letter to tell other developers "Hey brother this is constructor funciton never ever call it without new, Ok".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Plane&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;brand&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Plane&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fly&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is taking off!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you calling &lt;code&gt;Plane()&lt;/code&gt; without &lt;code&gt;new&lt;/code&gt; keyword. &lt;code&gt;this&lt;/code&gt; is pointing to global object (window) or undefined in strict mode and you will end up like a error or global variable which you didn't need.&lt;/p&gt;

&lt;h2&gt;
  
  
  The new Keyword: The Factory Supervisor
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword is the operator which trigger the "Object Creation" process, it's a four steps internal ritual. Without it the funciton executed normally line-by-line and return undefined or retured valus, but with it the js engine shift into "Instentiation Mode".&lt;/p&gt;

&lt;h2&gt;
  
  
  The Object Creation Process (Step-by-Step)
&lt;/h2&gt;

&lt;p&gt;When you call function like it, the js engine perform bellow four steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myPlane&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Plane&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Boeing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="mi"&gt;747&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Creation:&lt;/strong&gt; it creates a brand new object &lt;code&gt;{}&lt;/code&gt; in memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Linking:&lt;/strong&gt; it takes this created object and sets its internal &lt;code&gt;[[Prototype]]&lt;/code&gt; (the hidden &lt;code&gt;__prototype__&lt;/code&gt; property) to match the &lt;code&gt;Plane.prototype&lt;/code&gt; object. This create a "inheritance" link between them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Binding:&lt;/strong&gt; it calls the &lt;code&gt;Plane&lt;/code&gt; function, but it hands function newly created empty object to use it as &lt;code&gt;this&lt;/code&gt;. Every function say &lt;code&gt;this.brand&lt;/code&gt; it is actually, writing data onto that new object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Returning:&lt;/strong&gt; if the function returns an non-primitives like &lt;code&gt;arrya&lt;/code&gt; or &lt;code&gt;objects&lt;/code&gt; (&lt;code&gt;return {a:1}&lt;/code&gt;). Javascript throws away work it just did and return that object instead. But if the non-primitives or nothing at all returns like (&lt;code&gt;return "Done"&lt;/code&gt;) then javascript returned that newly creaetd object form step 1.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How &lt;code&gt;new&lt;/code&gt; Links Prototypes: The "Secret Tunnel"
&lt;/h2&gt;

&lt;p&gt;This is the most misunderstood part of the &lt;code&gt;new&lt;/code&gt; keyword. It established a delegation chain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;Constructor&lt;/strong&gt; has a &lt;code&gt;.prototype&lt;/code&gt; property where you put all you methods or values you want to share it with all its instances.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Instances&lt;/strong&gt; have a &lt;code&gt;[[Prototype]]&lt;/code&gt; link which points back to that same object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of the &lt;code&gt;new&lt;/code&gt; keyword the second steps is implemented, when you call &lt;code&gt;myPlane.fly()&lt;/code&gt; it finds into the &lt;code&gt;myPlane&lt;/code&gt; object, it doesn't find &lt;code&gt;fly()&lt;/code&gt; there, then it follow its "secret tunnel" (the prototype link) to &lt;code&gt;Plane.prototype&lt;/code&gt; and it finds &lt;code&gt;fly()&lt;/code&gt; there, executes it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Instances Created from Constructors
&lt;/h2&gt;

&lt;p&gt;The final result of these process is &lt;strong&gt;Instace&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Instace&lt;/strong&gt; is a unique object belongs to a type. You can create multiple instances form one type like &lt;code&gt;myPlane&lt;/code&gt; or &lt;code&gt;otherPlane&lt;/code&gt;, both are separate objects in memory of &lt;code&gt;Plane&lt;/code&gt; type (changin the brand of one will not change other), they are siblings because they share the same prototype.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword is what transform javascript form simple functions into a system capable of Object-Orianted Programming (OOP). it automate a boilerplate of creating an objects, linking its lineage, and populating its data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The new keyword automates object creation. It creates an empty object, links its internal prototype to the constructor's prototype, and binds this to the new instance. After the constructor populates the object, it returns the instance (unless an object is explicitly returned), enabling shared methods and powerful prototype inheritance patterns.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode</category>
    </item>
  </channel>
</rss>
