<?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: Imran shaikh</title>
    <description>The latest articles on Forem by Imran shaikh (@imranmind).</description>
    <link>https://forem.com/imranmind</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%2F68851%2F2c95a0f0-9c48-44e6-9666-3b7342b9769b.jpg</url>
      <title>Forem: Imran shaikh</title>
      <link>https://forem.com/imranmind</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/imranmind"/>
    <language>en</language>
    <item>
      <title>Ace Your JS Interview: How to Implement a Polyfill for the some() Method</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Sun, 01 Feb 2026 11:37:55 +0000</pubDate>
      <link>https://forem.com/imranmind/ace-your-js-interview-how-to-implement-a-polyfill-for-the-some-method-4hcm</link>
      <guid>https://forem.com/imranmind/ace-your-js-interview-how-to-implement-a-polyfill-for-the-some-method-4hcm</guid>
      <description>&lt;p&gt;One of the most common questions in senior JavaScript interviews is asking a candidate to write a polyfill. It’s not enough to just know how to use built-in array methods; you need to understand how they work under the hood.&lt;br&gt;
In my latest YouTube tutorial, I broke down exactly how to build a custom implementation of the some() function. If you are preparing for a coding interview or just want to strengthen your logic building, this guide is for you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=7t9sft0vAmQ" rel="noopener noreferrer"&gt;Youtube Link&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What is the some() Method?
&lt;/h2&gt;

&lt;p&gt;Before we build it, we must understand what it does. As explained in the video, the some() function checks an array to see if at least one element satisfies a specific condition.&lt;br&gt;
It works on a simple boolean logic:&lt;br&gt;
• If one or more elements meet the condition, it returns true.&lt;br&gt;
• If no elements meet the condition, it returns false.&lt;br&gt;
For example, if we have an array of mixed negative and positive numbers [-2, 0, 3, -1, 5, -4], and we want to check if there is any positive number, some() will iterate through the list. As soon as it finds 3 (which is greater than 0), it stops looking and returns true.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: Setting up the Prototype
&lt;/h2&gt;

&lt;p&gt;To make our custom function available to all arrays—just like the native .some() method—we need to attach it to the Array.prototype.&lt;br&gt;
In the video, we define our custom method (let’s call it mySome) on the prototype so that any array object can call it directly.&lt;br&gt;
&lt;code&gt;Array.prototype.mySome = function(callback) {&lt;br&gt;
  // Logic goes here&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
By using Array.prototype, we ensure that the function is accessible to any array instance, allowing us to use the this keyword to access the data.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Handling Errors
&lt;/h2&gt;

&lt;p&gt;A good polyfill is robust. Before processing the logic, we must ensure the user has actually passed a function as an argument.&lt;br&gt;
If the typeof the callback is not a function, we should throw an error immediately: "Callback should be a function". This prevents the code from crashing unexpectedly later on.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: The Logic (The this Keyword)
&lt;/h2&gt;

&lt;p&gt;Inside our prototype function, the array we are operating on is accessible via the this keyword. For clarity, we can assign this to a variable, though we can also access it directly.&lt;br&gt;
We then need to loop through the array. The standard some() method accepts a callback that takes three arguments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Item: The current element.&lt;/li&gt;
&lt;li&gt;Index: The current index.&lt;/li&gt;
&lt;li&gt;Self: The array itself.
Here is the core logic we implemented in the tutorial:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.mySome = function (callback, thisArg) {
    if (typeof callback !== "function") {
        throw new TypeError("Callback must be a function");
    }
    console.log(this);
    console.log(thisArg);
    for (let i = 0; i &amp;lt; this.length; i++) {
        // 🔥 Skip sparse (empty) indexes
        if (i in this) {
            if (callback.call(thisArg, this[i], i, this)) {
                return true;
            }
        }
    }
    return false;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;As demonstrated in the video, the moment the logic finds a "truthy" value (like the number 3 in our positive number check), it returns true and exits the loop. If it iterates through the whole array without success, it defaults to false.&lt;br&gt;
Testing the Polyfill&lt;br&gt;
Does it work? In the video, we test this against a real scenario: checking for values greater than zero.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [-2, 0, 3, -1, 5, -4];

const result = numbers.mySome((element) =&amp;gt; {
    return element &amp;gt; 0;
});
console.log(result); // Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the logic encountered 3 and 5, which are positive, our polyfill correctly identified that the condition was met.&lt;br&gt;
Watch the Full Implementation&lt;br&gt;
Writing polyfills is a fantastic way to master JavaScript fundamentals. If you want to see this code being written line-by-line, along with a detailed explanation of how the call stack and callbacks work in real-time, check out my full video tutorial below.&lt;br&gt;
I cover the edge cases and debugging steps that I couldn't fit into this article!&lt;/p&gt;

&lt;p&gt;If you found this guide helpful, please follow me on dev.to and subscribe to my &lt;a href="https://www.youtube.com/@imranshaikh0404" rel="noopener noreferrer"&gt;YouTube channel&lt;/a&gt; for more JavaScript interview prep!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>interview</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>JavaScript Interview Trick: typeof vs instanceof</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Tue, 29 Apr 2025 16:38:12 +0000</pubDate>
      <link>https://forem.com/imranmind/javascript-interview-trick-typeof-vs-instanceof-3jlb</link>
      <guid>https://forem.com/imranmind/javascript-interview-trick-typeof-vs-instanceof-3jlb</guid>
      <description>&lt;p&gt;Title: JavaScript Interview Trick: Understanding typeof vs instanceof&lt;/p&gt;

&lt;p&gt;As we dive into the world of JavaScript, we encounter different methods to determine the type of a variable or object. Two of the most common methods are the 'typeof' and 'instanceof' operators. In this blog post, we will explore these operators, their use cases, edge cases, and when it's best to use each one.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;typeof Use Cases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The 'typeof' operator in JavaScript is used to return a string indicating the type of the operand. An operand is the object or variable you're testing. It’s a unary operator, meaning it takes only one operand. Here are some examples:&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;typeof&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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="c1"&gt;// Returns "string"&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;typeof&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns "number"&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;typeof&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns "boolean"&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;typeof&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns "undefined"&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;typeof&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="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Returns "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="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns "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="k"&gt;typeof&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="c1"&gt;// Returns "function"&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;typeof&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Returns "object"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;instanceof Use Cases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On the other hand, 'instanceof' operator in JavaScript is used to check the current object and returns true if an object is an instance of a particular class or constructor, and false otherwise. It's a binary operator, meaning it requires two operands: an object and a constructor. Here are some examples:&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;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns true&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;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns true&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="s1"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns false&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;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Edge Cases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While 'typeof' and 'instanceof' operators are straightforward in most cases, there are some edge cases that might trip you up. For instance,&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;typeof&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns "object"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a long-standing bug in JS, but one we're stuck with for now. Null is not really an object; it has its own type.&lt;/p&gt;

&lt;p&gt;Another edge case is when dealing with arrays. When you use the 'typeof' operator with an array, it will return "object", which is not entirely accurate.&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;typeof&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Returns "object"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's where 'instanceof' comes in handy:&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="mi"&gt;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When to use what?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Understanding when to use 'typeof' vs 'instanceof' can be a bit tricky. Here are some rules of thumb:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use 'typeof' when you want to know the type of a primitive value.&lt;/li&gt;
&lt;li&gt;Use 'instanceof' when you want to know if an object is an instance of a certain class.&lt;/li&gt;
&lt;li&gt;'instanceof' is typically better for custom objects where you want to check if an object was instantiated from a specific class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember, JavaScript is a dynamically typed language, which means that the same variable can be used to hold different data types. Therefore, understanding 'typeof' and 'instanceof' is crucial in your journey as a JavaScript developer. &lt;/p&gt;

&lt;p&gt;By understanding the subtleties of these operators, you'll be better prepared to write and debug JavaScript code, and you'll be more equipped to handle any curveballs thrown your way during a JavaScript interview. Happy coding!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tech</category>
      <category>blog</category>
    </item>
    <item>
      <title>Deep Dive into JavaScript Event Loop</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Fri, 25 Apr 2025 06:46:06 +0000</pubDate>
      <link>https://forem.com/imranmind/deep-dive-into-javascript-event-loop-20je</link>
      <guid>https://forem.com/imranmind/deep-dive-into-javascript-event-loop-20je</guid>
      <description>&lt;p&gt;JavaScript is a single-threaded, non-blocking, asynchronous, concurrent language. Its concurrency model is heavily reliant on an entity known as the Event Loop. This blog post will provide a detailed exploration of the JavaScript Event Loop and how it handles asynchronous operations. We'll discuss the call stack, web APIs, the callback queue, the microtask queue, and async/await &amp;amp; promises.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Call Stack&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The call stack is a mechanism that JavaScript uses to track function execution. It's a LIFO (Last In, First Out) data structure - the last function that gets pushed into the stack is the first one to be popped out when its execution is complete. When a script calls a function, JavaScript pushes that function onto the call stack and starts executing it. Once it's done, JavaScript pops it off the stack and resumes execution where it left off in the code. If the stack takes up more space than it had assigned to it, it results in a "stack overflow."&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Web APIs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Web APIs are not provided by the JavaScript language itself, but by the browser environment. They are essentially a set of functions and objects that JavaScript can interact with, allowing it to perform operations like fetch data, manipulate the DOM, handle user events, etc. These operations are asynchronous, meaning they can occur outside the regular top-to-bottom flow of the JavaScript execution.&lt;/p&gt;

&lt;p&gt;When a function with an asynchronous task (like a setTimeout or an AJAX call) is called, it gets pushed to the call stack, but then it's offloaded to the Web API. The Web API handles the task, allowing the call stack to continue executing other tasks without being blocked.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Callback Queue&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once the Web API has completed the asynchronous task, it doesn't return the result to the call stack directly. Instead, it pushes the callback function (a function to be executed after the task is complete) to a Callback Queue. The Callback Queue is a FIFO (First In, First Out) data structure, meaning that tasks are dequeued in the order they arrived.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Microtask Queue&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next to the callback queue, there's another queue called the Microtask Queue. This queue is used for "promise" tasks. Promises in JavaScript are objects that represent the eventual completion or failure of an asynchronous operation. &lt;/p&gt;

&lt;p&gt;The difference between the Callback Queue and the Microtask Queue is that tasks in the Microtask Queue have higher priority. After each callback is processed, the Event Loop checks if there’s a function in the Microtask Queue. If there is, it runs that function before moving on to the next callback.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Async/Await &amp;amp; Promises&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Promises and async/await are modern ways of dealing with asynchronous operations in JavaScript.&lt;/p&gt;

&lt;p&gt;A Promise is an object that may produce a single value sometime in the future. It can be in one of three states: fulfilled, rejected, or pending. Promises are added to the Microtask Queue once they're resolved or rejected.&lt;/p&gt;

&lt;p&gt;Async/await is a syntactic sugar built on top of Promises. It makes asynchronous code look and behave like synchronous code. The keyword "async" is used to declare an asynchronous function, while "await" is used to pause async function execution until a Promise is fulfilled or rejected.&lt;/p&gt;

&lt;p&gt;In conclusion, the JavaScript Event Loop is a complex but efficient system for handling synchronous and asynchronous operations. By understanding the Call Stack, Web APIs, the Callback Queue, the Microtask Queue, and async/await &amp;amp; Promises, you can better understand and use JavaScript's concurrency model.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tech</category>
      <category>blog</category>
    </item>
    <item>
      <title>Understanding JavaScript Hoisting with Examples</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Thu, 24 Apr 2025 01:29:16 +0000</pubDate>
      <link>https://forem.com/imranmind/understanding-javascript-hoisting-with-examples-21fg</link>
      <guid>https://forem.com/imranmind/understanding-javascript-hoisting-with-examples-21fg</guid>
      <description>&lt;p&gt;Title: Understanding JavaScript Hoisting with Examples&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is Hoisting?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the world of JavaScript, hoisting is a fundamental concept that every developer must understand. To put it simply, hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their enclosing scope during the compile phase, before the code has been executed.&lt;/p&gt;

&lt;p&gt;Think of hoisting like a flag hoisted at the top of a pole. No matter where the flag is initially placed, it's eventually hoisted to the top. Similarly, regardless of where you declare functions or variables in your code, they are hoisted to the top of their scope.&lt;/p&gt;

&lt;p&gt;Let's consider this piece of 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="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&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 output will be 'undefined' instead of a ReferenceError. This is because of the hoisting mechanism. When JavaScript is compiled, all variable declarations (not initializations) are hoisted to the top. The code is interpreted as follows:&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;var&lt;/span&gt; &lt;span class="nx"&gt;myName&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;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//undefined&lt;/span&gt;
&lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&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;ol&gt;
&lt;li&gt;Function vs Variable Hoisting&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Function and variable hoisting behaviors differ slightly in JavaScript. &lt;/p&gt;

&lt;p&gt;For functions, both the declaration and the body are hoisted. Thus, you can call a function before declaring it in your 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="nf"&gt;hoistedFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//Outputs: "Hello, I have been hoisted!"&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hoistedFunction&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="s1"&gt;Hello, I have been hoisted!&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;p&gt;In contrast, for variables, only declarations are hoisted, not initializations. So, if you try to use a variable before it's declared and initialized, you'll get 'undefined'.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;let &amp;amp; const behavior&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The hoisting mechanism works a bit differently with 'let' and 'const' compared to 'var'. While 'var' declarations are hoisted and initialized with 'undefined', 'let' and 'const' declarations are hoisted but not initialized.&lt;/p&gt;

&lt;p&gt;If you try to access a 'let' or 'const' variable before declaration, you'll get a ReferenceError because JavaScript doesn't initialize them with 'undefined' like it does with 'var'.&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;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//ReferenceError: myName is not defined&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&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;ol&gt;
&lt;li&gt;Interview Pitfalls&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Understanding hoisting is crucial not only for writing efficient code but also for acing technical interviews. &lt;/p&gt;

&lt;p&gt;Many interviewers ask questions related to hoisting to test a candidate's knowledge of JavaScript's inner workings. For instance, you might be asked why a particular piece of code returns 'undefined' instead of a ReferenceError, or why a function call before its declaration doesn't result in an error.&lt;/p&gt;

&lt;p&gt;Remember, JavaScript does not hoist initializations, only declarations. This is an easy detail to overlook, but it's crucial for understanding why your code behaves the way it does.&lt;/p&gt;

&lt;p&gt;In conclusion, hoisting is a unique and important feature of JavaScript that plays a crucial role in how the code is interpreted and executed. By understanding hoisting, you can write cleaner, more predictable code and avoid common bugs and pitfalls.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tech</category>
      <category>blog</category>
    </item>
    <item>
      <title>Explain AI Agents in a Simple Manner</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Wed, 23 Apr 2025 02:30:22 +0000</pubDate>
      <link>https://forem.com/imranmind/explain-ai-agents-in-a-simple-manner-51hk</link>
      <guid>https://forem.com/imranmind/explain-ai-agents-in-a-simple-manner-51hk</guid>
      <description>&lt;h1&gt;
  
  
  Explaining AI Agents in a Simple Manner
&lt;/h1&gt;

&lt;p&gt;As we move further into the 21st century, artificial intelligence (AI) continues to shape our world in ways we could not have imagined. One of the key components of AI is the concept of an agent - an element that perceives its environment and takes actions that maximize its chance of achieving its goals. In this blog, we will take a deep dive into AI agents: what they are, how they work, and why they matter. &lt;/p&gt;

&lt;h2&gt;
  
  
  What are AI Agents?
&lt;/h2&gt;

&lt;p&gt;In the simplest terms, an AI agent is an autonomous entity that observes through sensors and acts upon an environment using actuators. It directs its activity towards achieving goals, from simple tasks like setting a timer to complex activities like playing chess. These agents are designed to handle tasks in a flexible and intelligent manner, responding to changes in the environment or different user requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-life examples of agents
&lt;/h2&gt;

&lt;p&gt;AI agents are all around us, often in ways we take for granted. Your phone's virtual assistant (like Siri or Google Assistant) is an AI agent, using your voice commands (sensors) to perform tasks (actuators). Self-driving cars are another example, using complex sensors like cameras and radars to navigate the environment and reach a destination.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of AI agents
&lt;/h2&gt;

&lt;p&gt;There are several types of AI agents, each with its unique characteristics:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simple Reflex Agents:&lt;/strong&gt; These agents act only on the current percept, ignoring the rest of the percept history. They choose actions based on the current situation, not considering the consequences of their actions. An example is a thermostat controlling temperature.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Model-Based Reflex Agents:&lt;/strong&gt; These agents consider the history of the world and keep track of the part of the world not in view. They have a model of the world, which they use to handle partially observable situations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Goal-Based Agents:&lt;/strong&gt; These agents take future actions into account and are flexible to changing goals. They take steps to achieve a specific objective, like a GPS navigating to a destination.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Utility-Based Agents:&lt;/strong&gt; These agents not only aim for a specific goal but also try to maximize the overall expected benefit. They choose actions based on a utility function, like a stock trading bot trying to maximize profit.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How they make decisions
&lt;/h2&gt;

&lt;p&gt;AI agents use a variety of techniques to make decisions. These include search and planning, where they determine the sequence of actions that lead to the desired goal, and machine learning, where they improve performance based on experience. More advanced agents also use knowledge and reasoning to make decisions based on logic and inference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases in real-world applications
&lt;/h2&gt;

&lt;p&gt;AI agents have a multitude of real-world applications. In healthcare, they can assist doctors in diagnosing diseases and suggesting treatments. In finance, they can predict stock market trends and advise on investments. In entertainment, they can power video game characters and virtual reality experiences. And in everyday life, they can manage smart homes, recommend products online, and even help you find your way around town.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why developers should care about them
&lt;/h2&gt;

&lt;p&gt;AI agents represent a significant opportunity for developers. They are a key part of many emerging technologies, from autonomous vehicles to virtual reality. Understanding how they work and how to develop them can open up a world of possibilities. Furthermore, as AI becomes more integrated into our lives, the demand for developers who can create intelligent agents will only increase. By learning about AI agents, developers can stay ahead of the curve and be prepared for the future of technology.&lt;/p&gt;

&lt;p&gt;In conclusion, AI agents are a fascinating and important aspect of artificial intelligence. They are not just the future; they are the present. So, whether you're a developer, a tech enthusiast, or just curious, there's never been a better time to learn about AI agents.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>agents</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Mastering JavaScript Closures in Simple Terms</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Wed, 23 Apr 2025 02:24:32 +0000</pubDate>
      <link>https://forem.com/imranmind/mastering-javascript-closures-in-simple-terms-1l6b</link>
      <guid>https://forem.com/imranmind/mastering-javascript-closures-in-simple-terms-1l6b</guid>
      <description>&lt;p&gt;Title: Mastering JavaScript Closures in Simple Terms&lt;/p&gt;

&lt;p&gt;Introduction&lt;/p&gt;

&lt;p&gt;JavaScript, one of the most popular scripting languages in today's web development, fascinates developers with its power, flexibility, and versatility. But to master this language, one has to understand its fundamental concepts thoroughly. One important concept that often confuses new JavaScript developers is closures. So, let's demystify this concept and help you become a JavaScript maestro.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is a Closure?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In JavaScript, a closure is a function that has access to its own scope, the outer function's scope, and the global scope. It is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). Simply put, a closure gives you access to an outer function’s scope from an inner function. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lexical Environment&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To understand closures, it's crucial to comprehend what the lexical environment is. In JavaScript, a lexical environment is a data structure that holds variable and function declarations. It's used by the JavaScript engine to look up variables and functions, and it consists of two main components: the environment record and a reference to the outer environment. &lt;/p&gt;

&lt;p&gt;When a function is invoked, a new lexical environment is created to hold the local variables of that function. If that function contains nested functions, those functions hold a reference to their parent function’s environment, as well as the global environment. This is the basis of how closures work.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Practical Examples&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's take a look at a practical example to understand closures better.&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;outerFunction&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;outerVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am outside!&lt;/span&gt;&lt;span class="dl"&gt;'&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;innerFunction&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;outerVariable&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="nx"&gt;innerFunction&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;myNewFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nf"&gt;myNewFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs: 'I am outside!'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;myNewFunction&lt;/code&gt; is a closure that consists of the &lt;code&gt;innerFunction&lt;/code&gt; and the scope in which &lt;code&gt;innerFunction&lt;/code&gt; was declared. When we invoke &lt;code&gt;myNewFunction&lt;/code&gt;, it can still access &lt;code&gt;outerVariable&lt;/code&gt; from its parent scope, even though &lt;code&gt;outerFunction&lt;/code&gt; has finished executing. This is the power of closures - they remember the environment in which they were created.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Interview Tricky Questions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Closures are a common topic in JavaScript interviews. Here are a few tricky questions you might encounter:&lt;/p&gt;

&lt;p&gt;a) What will be the output of the following 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="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&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;1&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="mi"&gt;5&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="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="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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&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="mi"&gt;1000&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;Many would expect this code to log the numbers 1 through 5, each after one second. However, it will actually log the number 6 five times, each after one second. This is because &lt;code&gt;var&lt;/code&gt; does not have block scope, and the callbacks capture the same single global scope.&lt;/p&gt;

&lt;p&gt;b) How would you make this code log the numbers 1 through 5?&lt;/p&gt;

&lt;p&gt;The answer is to use a closure. By creating an IIFE (Immediately Invoked Function Expression) inside the loop, we can create a new scope for each iteration, capturing the current value of &lt;code&gt;i&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;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&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;1&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="mi"&gt;5&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="p"&gt;{&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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&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="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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&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="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;i&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;In this version, each IIFE creates a new scope, capturing the current value of &lt;code&gt;i&lt;/code&gt;. The &lt;code&gt;setTimeout&lt;/code&gt; callback then references this scope, logging the expected values.&lt;/p&gt;

&lt;p&gt;Wrapping Up&lt;/p&gt;

&lt;p&gt;Understanding closures is a crucial step on the path to mastering JavaScript. They offer powerful capabilities for managing scope and preserving state over time. Keep practicing and experimenting with closures, and you'll find them becoming second nature in no time.&lt;/p&gt;

&lt;p&gt;Remember, as with all things JavaScript, it is practice and exploration that makes perfect. Happy coding!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tech</category>
      <category>blog</category>
    </item>
    <item>
      <title>How to Build a REST API with Node.js</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Mon, 21 Apr 2025 08:27:59 +0000</pubDate>
      <link>https://forem.com/imranmind/how-to-build-a-rest-api-with-nodejs-3f5l</link>
      <guid>https://forem.com/imranmind/how-to-build-a-rest-api-with-nodejs-3f5l</guid>
      <description>&lt;p&gt;Title: Building a REST API with Node.js: A Comprehensive Guide&lt;/p&gt;

&lt;p&gt;Introduction &lt;/p&gt;

&lt;p&gt;Node.js is one of the most popular technologies used in modern web development today. It is an open-source, cross-platform JavaScript runtime environment that allows developers to build server-side and networking applications. In this blog post, we will walk you through the steps to build a REST API using Node.js. We will cover setting up Express, defining routes, and using middleware.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting Up Express&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Express.js is a fast, unopinionated, minimalist web framework for Node.js. It is the de facto standard server framework for Node.js.&lt;/p&gt;

&lt;p&gt;To start with, you need to have Node.js and npm (Node Package Manager) installed on your machine. You can download it from the official website: &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;https://nodejs.org/en/download/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you have Node.js installed, create a new folder for your project, navigate into it, and initiate a new Node.js project using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm init -y&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now, install Express using npm:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install express --save&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This command will install Express in your project and save it in your dependencies.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Defining Routes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After setting up Express, we need to set up our routes. Routes are the endpoints that define how an API responds to client requests. In an Express app, we define routes using methods of an Express application instance.&lt;/p&gt;

&lt;p&gt;Create a new file named 'app.js' in your project root and require the express module:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const express = require('express');&lt;br&gt;
const app = express();&lt;br&gt;
const port = 3000;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now, let's define a basic route:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.get('/', (req, res) =&amp;gt; {&lt;br&gt;
  res.send('Hello World!')&lt;br&gt;
})&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This code creates a route handler for HTTP GET requests to the application’s home page. The callback function takes a request and a response object as arguments, and simply responds by sending a text message.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using Middleware&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. Middleware functions can perform the following tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Execute any code.&lt;/li&gt;
&lt;li&gt;Make changes to the request and the response objects.&lt;/li&gt;
&lt;li&gt;End the request-response cycle.&lt;/li&gt;
&lt;li&gt;Call the next middleware function in the stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To use a middleware, we can use the app.use() function. Here's an example of how to use a middleware function at the application level:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.use(function (req, res, next) {&lt;br&gt;
  console.log('Time:', Date.now())&lt;br&gt;
  next()&lt;br&gt;
})&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This middleware function simply prints the current time and then passes control to the next middleware function in the stack.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;Building a REST API with Node.js and Express.js is a straightforward process. This blog post only covers the basics, but there’s a lot more you can do, such as adding a database, validating user input, handling errors, and more. With a good understanding of Express and Node.js, you can build highly scalable and efficient web applications. The key is to understand how routes and middleware work, as they form the core of any Express application. Happy coding!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/OYkmIIKfWq4?si=xLtWbtVJTF131vqL" rel="noopener noreferrer"&gt;https://youtu.be/OYkmIIKfWq4?si=xLtWbtVJTF131vqL&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To master Login/Signup with MERN stack, please have a look this video&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tech</category>
      <category>blog</category>
      <category>webdev</category>
    </item>
    <item>
      <title>10 JavaScript Tricks Every Developer Should Know</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Sun, 20 Apr 2025 18:00:01 +0000</pubDate>
      <link>https://forem.com/imranmind/10-javascript-tricks-every-developer-should-know-cdk</link>
      <guid>https://forem.com/imranmind/10-javascript-tricks-every-developer-should-know-cdk</guid>
      <description>&lt;p&gt;Title: 10 JavaScript Tricks Every Developer Should Know&lt;/p&gt;

&lt;p&gt;Hello to all the code enthusiasts out there! Today, we're going to delve into the world of JavaScript, a cornerstone language of the web, and explore some tricks that can elevate your coding skills to a new level. Whether you're a novice developer or a seasoned programmer, these JavaScript tricks are sure to add a new dimension to your coding repertoire. So let's get started!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Destructuring:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Destructuring in JavaScript is a handy way to extract multiple properties from an object or array in a single statement. It can significantly simplify your code and make it more readable. For example:&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="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="o"&gt;=&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="mi"&gt;2&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: 1&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;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same can be done with 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;let&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;age&lt;/span&gt;&lt;span class="p"&gt;}&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;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;22&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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: John&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;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: 22&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Optional Chaining:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Optional chaining is a recent addition to JavaScript (ES2020), which allows you to access deeply nested object properties without having to check if each reference in the chain is valid. It helps in writing cleaner and safer code. Here's how it works:&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt; &lt;span class="c1"&gt;// an empty 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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;street&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: undefined, instead of throwing an error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Spread/Rest Operators:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The spread operator (…) in JavaScript is used to spread the elements of an array or object. The rest operator, also (…), is used to collect the rest of the elements into an array. Both these operators contribute to shorter and cleaner 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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&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;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="c1"&gt;// outputs: [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the rest operator:&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="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;first&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: 1&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;rest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: [2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Short-circuit Evaluation:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Short-circuit evaluation in JavaScript involves logical operators (&amp;amp;&amp;amp; and ||) to create shorter code. It can be used to set default values, validate objects before using them, and more:&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&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;name&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;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;serverPort&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&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;Template Literals:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Template literals provide an easy way to interpolate variables and expressions into strings. They are enclosed by backtick (&lt;code&gt;&lt;/code&gt;) characters instead of quotes:&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&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="s2"&gt;`Hello, &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="c1"&gt;// outputs: Hello, John!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Arrow Functions:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Arrow functions offer a more concise syntax for writing function expressions. They are ideal for non-method functions and they do not have their own 'this', arguments, super, or new.target:&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;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// outputs: Hello World&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Nullish Coalescing Operator:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;default 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;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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: default name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Ternary Operator:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The ternary operator is a quicker way to write simple if-else statements. The syntax is: condition ? value_if_true : value_if_false:&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;age&lt;/span&gt; &lt;span class="o"&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;let&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Adult&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;Minor&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;type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: Minor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Array Methods (map, reduce, filter):&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;JavaScript provides powerful array methods like map, reduce, and filter. They are more efficient and elegant to use in comparison to traditional for-loops:&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;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;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;doubled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&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;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// outputs: [2, 4, 6, 8, 10]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Async/Await:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Async/Await makes asynchronous code look and behave more like synchronous code. This syntactic sugar on top of promises makes asynchronous code easier to understand and write:&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;fetchUser&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;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.github.com/users&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;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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for this post. I hope you found these JavaScript tricks useful. Happy coding!&lt;/p&gt;

&lt;p&gt;MERN Stack Login/Signup in detailed&lt;br&gt;
&lt;a href="https://youtu.be/OYkmIIKfWq4?si=xLtWbtVJTF131vqL" rel="noopener noreferrer"&gt;https://youtu.be/OYkmIIKfWq4?si=xLtWbtVJTF131vqL&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tech</category>
      <category>blog</category>
    </item>
    <item>
      <title>Mastering React Challenge: Building a Timer App</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Tue, 23 Apr 2024 17:07:11 +0000</pubDate>
      <link>https://forem.com/imranmind/mastering-react-challenge-building-a-timer-app-1b6b</link>
      <guid>https://forem.com/imranmind/mastering-react-challenge-building-a-timer-app-1b6b</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9yyrtq1jictj2mt227r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9yyrtq1jictj2mt227r.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;In this blog post, I'll guide you through building a robust timer application using React that meets specific requirements and integrates notifications for a seamless user experience. We'll cover essential features such as starting, pausing, resetting the timer, and ensuring accessibility for screen readers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenge Overview
&lt;/h2&gt;

&lt;p&gt;The challenge involves creating a timer app with the following key features:&lt;br&gt;
Displaying 'Hours', 'Minutes', and 'Seconds' fields without visible labels but ensuring accessibility and screen reader compatibility.&lt;br&gt;
Implementing placeholder text for input fields.&lt;br&gt;
Transitioning to a running timer with plain text representation of hours, minutes, and seconds upon pressing 'Start', along with 'Pause' and 'Reset' functionalities.&lt;br&gt;
Zero-padding numbers during countdown (e.g., displaying '01' instead of '1').&lt;br&gt;
Displaying notifications or alerts upon timer completion based on app permissions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Video Preview
&lt;/h2&gt;

&lt;p&gt;To get a quick glimpse of the timer app in action and follow along with detailed implementation steps, watch my video tutorial here: Watch the Timer App Challenge Video&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=REVHmBek3kA&amp;amp;list=PLF7ny2pVBcm29ri1Z50QXOo3gIiWbVs6u&amp;amp;index=2" rel="noopener noreferrer"&gt;React Timer App Video is here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Learnings
&lt;/h2&gt;

&lt;p&gt;Throughout the tutorial video and accompanying blog post, you'll learn:&lt;br&gt;
How to structure a React component-based timer app.&lt;br&gt;
Techniques for managing timer state and handling user interactions.&lt;br&gt;
Integration of notifications for timer completion using browser APIs.&lt;br&gt;
Strategies for handling app permissions and fallbacks for unsupported features.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Building a timer app is a great way to enhance your React skills and understand important concepts in modern web development. By following this tutorial and watching the video, you'll gain practical experience and insights into creating interactive and accessible React applications.&lt;br&gt;
Ready to dive in? Watch the video above and let's build an awesome timer app together!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Flatten an Object in Javascript ?</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Tue, 02 May 2023 18:02:34 +0000</pubDate>
      <link>https://forem.com/imranmind/how-to-flatten-an-object-in-javascript--4ojl</link>
      <guid>https://forem.com/imranmind/how-to-flatten-an-object-in-javascript--4ojl</guid>
      <description>&lt;p&gt;&lt;strong&gt;Interviewer :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvkle54k7z45oqdrllvju.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvkle54k7z45oqdrllvju.jpg" alt="Image description" width="600" height="600"&gt;&lt;/a&gt; Can you flatten an object, Please take below as input&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
    name: "test",
    address: {
        personal: "abc",
        office: {
            building: 'random',
            street: 'some street'
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and produce output like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    name : "test",
    address_personal: "abc"
    address_office_building: "random"
    address_office_street: "some street"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const flattenObject = (obj, parentKey = '') =&amp;gt; {
    if (parentKey !== '')
        parentKey = parentKey + '_';

    let flattened = {};
    console.log(flattened)
    Object.keys(obj).forEach((key) =&amp;gt; {
        if (typeof obj[key] === 'object' &amp;amp;&amp;amp; obj[key] !== null) {
            Object.assign(flattened, flattenObject(obj[key], parentKey + key))
        } else {
            flattened[parentKey + key] = obj[key]
        }
    })
    return flattened;
}


const obj = {
    name: "test",
    address: {
        personal: "abc",
        office: {
            building: 'random',
            street: 'some street'
        }
    }
}

let flat = flattenObject(obj);
console.log(flat);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to see more interview questions please reach out my GitHub profile&lt;/p&gt;

&lt;p&gt;Github: &lt;a href="https://github.com/imran-mind/javascript-notes/blob/master/JS-Interview-coding-ques/objectFlatten.js" rel="noopener noreferrer"&gt;https://github.com/imran-mind/javascript-notes/blob/master/JS-Interview-coding-ques/objectFlatten.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Linkedin: &lt;a href="https://www.linkedin.com/in/imran-mind" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/imran-mind&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Twitter: &lt;a href="https://twitter.com/imran1mind" rel="noopener noreferrer"&gt;https://twitter.com/imran1mind&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My Blogs: &lt;a href="https://imranshaikh.co.in/" rel="noopener noreferrer"&gt;https://imranshaikh.co.in/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you find this blog helpful, please like and comment and don't forget to share this.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>Array-Flatten Array Javascript interview questions-1</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Mon, 01 May 2023 16:37:02 +0000</pubDate>
      <link>https://forem.com/imranmind/array-flatten-array-javascript-interview-questions-1-4bba</link>
      <guid>https://forem.com/imranmind/array-flatten-array-javascript-interview-questions-1-4bba</guid>
      <description>&lt;p&gt;Frontend Interview or Nodejs Interview it's the most frequently asked interview question.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function doFlat(){
    let flatten = [];
    function flat(arr){    
        for(let i=0; i&amp;lt;arr.length; i++){
            const item = arr[i];
            if(Array.isArray(item)){
                // Object.assign(flatten,flat(item));
               [...flatten,flat(item)]
            }else{
                flatten.push(item);
            }
        }
        return flatten;
    }
    return {
        flat
    }
}

const {flat} = doFlat();

console.log(flat([1,2,[3,[0,4,5],4,[5,6]]]))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to see more interview questions please reach out my GitHub profile&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/imran-mind/javascript-notes/blob/master/JS-Interview-coding-ques/" rel="noopener noreferrer"&gt;https://github.com/imran-mind/javascript-notes/blob/master/JS-Interview-coding-ques/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you find this blog helpfull, please like and comment and don't forget to share this.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
      <category>html</category>
    </item>
    <item>
      <title>How to use JavaScript Promise.all with realtime code example [Axios GET Calls]</title>
      <dc:creator>Imran shaikh</dc:creator>
      <pubDate>Sat, 06 Aug 2022 08:53:00 +0000</pubDate>
      <link>https://forem.com/imranmind/how-to-use-javascript-promiseall-with-realtime-code-example-axios-get-calls-1hnk</link>
      <guid>https://forem.com/imranmind/how-to-use-javascript-promiseall-with-realtime-code-example-axios-get-calls-1hnk</guid>
      <description>&lt;h2&gt;
  
  
  What is Promise.all
&lt;/h2&gt;

&lt;p&gt;A Promise.all() is a static method which takes array (group) of promises as an input and do the process as single promise and return in than-able or catch.&lt;/p&gt;

&lt;p&gt;The basic syntax of Promise.all is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Promise.all([promise1, promise2,.....])
   .then(result=&amp;gt;{
     //Congrats! Promise got resolved.
    }).catch(err=&amp;gt;{
     //Ohh ! Promise got rejected.
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where promise1, promise2 are async functions which also returns promise.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use Promise.all method
&lt;/h2&gt;

&lt;p&gt;When you have group of asynchronous task to process in single shot then it's recommended to use Promise.all().&lt;/p&gt;

&lt;p&gt;To understand clearly let's take an example of async functions which is getting resolved by the Promise.all() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const p1 = new Promise((resolve,reject)=&amp;gt;{
    setTimeout(()=&amp;gt;{
        resolve('p1 completed');
    },1000);
})

const p2 = new Promise((resolve,reject)=&amp;gt;{
    setTimeout(()=&amp;gt;{
        resolve('p2 completed');
    },2000);
})

const p3 = new Promise((resolve,reject)=&amp;gt;{
    setTimeout(()=&amp;gt;{
        resolve('p3 completed');
    },3000);
})

Promise.all([p1,p2,p3,])
    .then(result=&amp;gt;{
        console.log(result);
    }).catch(err=&amp;gt;{
        console.log(err);
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result returned from the above code snippet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ 'p1 completed', 'p2 completed', 'p3 completed' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we can see that p1,p2 and p3 are the async functions because it's having setTimeout as function so each function will resolved respectively after 1,2 and 3 seconds but Promise.all will resolved once the last function will be resolved that's the beauty of the Promise.all() method.&lt;/p&gt;

&lt;p&gt;The core property of Promise.all() method is that It will resolved all function and returns result in the same order in which order we have given input functions array.&lt;/p&gt;

&lt;p&gt;If all array functions (p1,p2,p3) will resolve then only it will give result in then-able.&lt;br&gt;
Otherwise if any one of the Promise function get rejected for any reason then the Promise.all() method will go into catch() block. you can see the below example for this scenario.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const p1 = new Promise((resolve,reject)=&amp;gt;{
    setTimeout(()=&amp;gt;{
        resolve('p1 completed');
    },1000);
})

const p2 = new Promise((resolve,reject)=&amp;gt;{
    setTimeout(()=&amp;gt;{
        resolve('p2 completed');
    },2000);
})

const p3 = new Promise((resolve,reject)=&amp;gt;{
    setTimeout(()=&amp;gt;{
        reject(new Error('p3 rejected'));
    },3000);
})


Promise.all([p1,p2,p3,])
    .then(result=&amp;gt;{
        console.log(result);
    }).catch(err=&amp;gt;{
        console.log(err);
    })

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code snipped will be coming in catch block&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: p3 rejected
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Now Lets see the realtime example of Promise.all().
&lt;/h2&gt;

&lt;p&gt;Suppose you have dashboard where you have to call 3 APIs&lt;br&gt;
Till the time api is calling you have to show loader on the screen. So you can achieve this very easily by using Promise.all() method. &lt;br&gt;
Please refer the below example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const axios = require('axios');

const task = async (id)=&amp;gt;{
    const response = await axios(`https://reqres.in/api/users/${id}`);
    const {data} = response;
    return data.data;
}
const ids = [1,2,3];
const allTasks = ids.map((pageNumber)=&amp;gt;{
    return task(pageNumber);
});

// you can start page loader here
Promise.all(allTasks)
    .then((result)=&amp;gt;{
        console.log(result);
        // once the task is finished then you stop loader over here
    }).catch((err)=&amp;gt;{
        //If error comes then also you have to stop loader
        console.log(err);
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example I have created a function called 'task' which async function basically calling the api.&lt;/p&gt;

&lt;p&gt;So we have to calls 3 api for UserID 1,2 and 3 so for that we have grouped 3 apis call in a single array (allTasks) and passing the array to Promise.all(allTasks) as an input and waiting for resolve or reject.&lt;/p&gt;

&lt;p&gt;If all the apis runs successfully then we are expecting the result in an array format (users response)&lt;/p&gt;

&lt;p&gt;If any one of the api gets rejected then we should expect error in catch block.&lt;/p&gt;

&lt;p&gt;Thank you guys for reading my little effort.&lt;/p&gt;

&lt;p&gt;Please like and comment the article if you really like it.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>react</category>
    </item>
  </channel>
</rss>
