<?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: Shubham Dutta</title>
    <description>The latest articles on Forem by Shubham Dutta (@shubhamdutta2000).</description>
    <link>https://forem.com/shubhamdutta2000</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%2F490546%2F757d5fdf-a4da-4db3-a4ab-3747ec4b62b4.jpeg</url>
      <title>Forem: Shubham Dutta</title>
      <link>https://forem.com/shubhamdutta2000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shubhamdutta2000"/>
    <language>en</language>
    <item>
      <title>Mastering JavaScript Debugging: Best Practices and Traps to Avoid</title>
      <dc:creator>Shubham Dutta</dc:creator>
      <pubDate>Thu, 20 Apr 2023 05:43:25 +0000</pubDate>
      <link>https://forem.com/shubhamdutta2000/mastering-javascript-debugging-best-practices-and-traps-to-avoid-41h6</link>
      <guid>https://forem.com/shubhamdutta2000/mastering-javascript-debugging-best-practices-and-traps-to-avoid-41h6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The process of developing software always involves debugging, and JavaScript programmers frequently devote a lot of effort to doing so. A systematic approach is necessary for good debugging, and there are a number of best practises that developers may use to streamline the procedure. The recommended practises for debugging JavaScript code and typical traps to avoid will be covered in this blog post.&lt;/p&gt;




&lt;h2&gt;
  
  
  General Best Practices:
&lt;/h2&gt;

&lt;p&gt;Start with a plan: Before you begin debugging, make sure you have a clear plan for what you want to achieve. Identify the problem you're trying to solve, and break it down into smaller, more manageable steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Reproduce the bug&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In order to debug a bug, you need to be able to reproduce it consistently. &lt;/li&gt;
&lt;li&gt;Try to identify the conditions that cause the bug to occur, and replicate those conditions as closely as possible.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check for syntax errors&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Syntax errors are one of the most common causes of bugs in JavaScript code. &lt;/li&gt;
&lt;li&gt;Make sure your code is free of syntax errors before you begin debugging.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use meaningful variable names&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using clear and descriptive variable names can make your code easier to understand and debug. &lt;/li&gt;
&lt;li&gt;Avoid using generic names like "x" or "y".&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Keep your code organized&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Well-organized code is easier to read and debug. &lt;/li&gt;
&lt;li&gt;Use proper indentation and formatting, and break your code into logical sections.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stay calm and focused&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Debugging can be frustrating, but it's important to stay calm and focused. &lt;/li&gt;
&lt;li&gt;Take breaks when you need to, and approach the problem with a clear and open mind.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Specific Techniques for Debugging JS Code:
&lt;/h2&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;h3&gt;
  
  
  A. Use console.log():
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Console.log()&lt;/code&gt; is a simple and effective way to debug JavaScript code. &lt;/li&gt;
&lt;li&gt;By placing &lt;code&gt;console.log()&lt;/code&gt; statements in your code, you can see the values of variables and functions at different points in the code execution. &lt;/li&gt;
&lt;li&gt;This can help you identify bugs and understand the flow of the code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2sNj_iAM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7ve42i86v4bl1ll38lg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2sNj_iAM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7ve42i86v4bl1ll38lg.png" alt="Console.log" width="800" height="235"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  B. Display objects as a table:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Sometimes you want to view a complicated collection of objects.&lt;/li&gt;
&lt;li&gt;Either log them and browse the list, or use the &lt;code&gt;console.table&lt;/code&gt; helper. It makes it simpler to understand what you're up against!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zfoNxpMx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hgqzn97b6jy9iwchf4d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zfoNxpMx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hgqzn97b6jy9iwchf4d.png" alt="Console.table" width="800" height="285"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  C. Using the console.time() and console.timeEnd() benchmark functions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Knowing an item's precise execution time can be quite helpful, especially when troubleshooting slow loops. By giving the method a label, you can even create numerous timers. Let's examine its operation:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BMIf0Ne5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1iuf21cc8vaezd17hhev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BMIf0Ne5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1iuf21cc8vaezd17hhev.png" alt="console.time" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  D. Get a function's stack trace.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The Web console receives a stack trace through the &lt;code&gt;console.trace()&lt;/code&gt; method &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Console/trace"&gt;(MDN Reference)&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UMAmhAli--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6i64fvpxzxrxfxvg1pv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UMAmhAli--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6i64fvpxzxrxfxvg1pv.png" alt="Console.trace" width="800" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Output:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cLroeomQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtg6mge3jp2e0z858xxt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cLroeomQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtg6mge3jp2e0z858xxt.png" alt="console.trace_output" width="800" height="274"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  E. Use breakpoints:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Breakpoints are a debugging tool that let you pause a piece of code from running at a particular place. &lt;/li&gt;
&lt;li&gt;This may help in finding problems and understanding the logic of the code. &lt;/li&gt;
&lt;li&gt;You may see the values of variables and functions at that time in the execution of the code by inserting breakpoints at strategic locations in the code.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  F. Use conditional breakpoints:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Conditional breakpoints are a particular kind of breakpoint that only causes the execution of the code to pause when a specific circumstance occurs. &lt;/li&gt;
&lt;li&gt;This can be useful in locating bugs that only appear under specific circumstances. &lt;/li&gt;
&lt;li&gt;You can reduce the scope of the bug and find the reason more quickly by utilising conditional breakpoints.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  G. Use the debugger statement:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The debugger statement is a simple way to pause the execution of code and launch the browser's developer tools. &lt;/li&gt;
&lt;li&gt;By placing the debugger statement in your code, you can inspect the values of variables and functions at that point in the execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nhk4J85P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fu3sj08y6n2p9s3kvnvb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nhk4J85P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fu3sj08y6n2p9s3kvnvb.png" alt="debugger-statement" width="800" height="272"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  H. Use a linter:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A linter is a tool that analyzes your code and flags potential errors or issues. &lt;/li&gt;
&lt;li&gt;By using a linter, you can catch potential bugs before they become major issues. &lt;/li&gt;
&lt;li&gt;This can help you save time and improve the effectiveness of the debugging process.&lt;/li&gt;
&lt;li&gt;Let's take the scenario where we want to utilise a linter to search for unused variables. We may utilise a well-liked linter called ESLint by doing the following:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install ESLint by running the following command in your terminal:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install eslint --save-dev
&lt;/code&gt;&lt;/pre&gt;



&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a new file called &lt;code&gt;.eslintrc.json&lt;/code&gt; in your project root directory, and add the following configuration:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pdvydnU6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwpeofx7j4hfrpjmr85g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pdvydnU6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwpeofx7j4hfrpjmr85g.png" alt="eslint" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter the following command in your terminal to launch ESLint:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx eslint &amp;lt;file_name&amp;gt;.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  I. Avoid using global variables:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Global variables can be difficult to debug, as they can be accessed from anywhere in your code. &lt;/li&gt;
&lt;li&gt; Global Variables can also lead to naming conflicts, code maintainability issues, and scope issues&lt;/li&gt;
&lt;li&gt;To make your code easier to debug, try to avoid using global variables whenever possible for more modular approach.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_ygVJfoy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lmj2leb4a1hont6z3sf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_ygVJfoy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lmj2leb4a1hont6z3sf.png" alt="not_use_global_var" width="800" height="590"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  J. Understand scoping rules:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript contains complex scoping restrictions that are occasionally challenging to figure out.&lt;/li&gt;
&lt;li&gt;By understanding how scoping works in JavaScript, you can avoid common scoping-related bugs and make your code easier to &lt;code&gt;debug&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WxNFp1ir--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylz87gfzshdpdaxqcohv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WxNFp1ir--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylz87gfzshdpdaxqcohv.png" alt="scoping_rules" width="800" height="349"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  K. Develop unit tests
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Automated tests called &lt;code&gt;unit tests&lt;/code&gt; are used to check the functionality of your code. &lt;/li&gt;
&lt;li&gt;You can ensure that your code functions as intended and find errors before they reach the &lt;code&gt;production&lt;/code&gt; environment by writing unit tests. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can create and execute unit tests for your JavaScript code using testing frameworks like &lt;code&gt;Jest&lt;/code&gt; or &lt;code&gt;Mocha&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consider a &lt;code&gt;JavaScript&lt;/code&gt; function that takes a number array as input and outputs the total of all the even integers in the array. The function's code is provided below:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xItfL1yk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4anya7qllctx3e91u9ny.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xItfL1yk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4anya7qllctx3e91u9ny.png" alt="js_code" width="800" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You might use a testing framework like &lt;code&gt;Jest&lt;/code&gt; to create a unit test case for this function. Here is a sample test scenario:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d7JFXV1e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iiyk487slw3ctrl23sd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d7JFXV1e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iiyk487slw3ctrl23sd.png" alt="test_case" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Other Best Practices:
&lt;/h2&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;h3&gt;
  
  
  Use the browser's developer tools:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The majority of modern browsers have robust developer tools that let you examine a web page's &lt;code&gt;HTML&lt;/code&gt;, &lt;code&gt;CSS&lt;/code&gt;, and &lt;code&gt;JavaScript&lt;/code&gt; code. &lt;/li&gt;
&lt;li&gt;By utilising these tools, you can test your code across various devices and screen sizes and find and repair errors more quickly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mifJpP9j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wc9qaxgjvytebg5ifddl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mifJpP9j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wc9qaxgjvytebg5ifddl.png" alt="devtools" width="800" height="775"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Write modular code:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Modular code is a type of code that has been divided up into smaller, easier-to-maintain chunks. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;You may more easily find and repair &lt;code&gt;errors&lt;/code&gt; as well as reuse code across many projects by segmenting your code into smaller modules.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Example:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;utils.js&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JYBPF3Uu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nos0mj2jajtn6cy4b3zz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JYBPF3Uu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nos0mj2jajtn6cy4b3zz.png" alt="modular_utils" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;index.js&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wR7WDITw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsmk1ic7xj8p2jfaehv7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wR7WDITw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsmk1ic7xj8p2jfaehv7.png" alt="modular_index" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Document your code:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Documenting your code can assist other developers working on the same project understand and troubleshoot it more easily. &lt;/li&gt;
&lt;li&gt;Documenting your code will help&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f8dab8LZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8v3wr9aenh1pxvtbu15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f8dab8LZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8v3wr9aenh1pxvtbu15.png" alt="document_code" width="800" height="590"&gt;&lt;/a&gt;&lt;/p&gt;




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

&lt;p&gt;In conclusion, successful debugging is a crucial step in the software development process, and you may improve the process by adhering to these recommended practises. You can find and fix bugs more quickly and effectively, producing more robust and reliable code, by using &lt;code&gt;console.log()&lt;/code&gt; statements, understanding error messages, using &lt;code&gt;breakpoints&lt;/code&gt;, &lt;code&gt;testing&lt;/code&gt; your code incrementally, using a &lt;code&gt;linter&lt;/code&gt;, avoiding &lt;code&gt;global variables&lt;/code&gt;, using &lt;code&gt;descriptive variable names&lt;/code&gt;, understanding &lt;code&gt;scoping&lt;/code&gt; rules, and using a &lt;code&gt;debugger&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Follow &lt;a href="https://www.linkedin.com/in/shubham--dutta/"&gt;Shubham Dutta &lt;/a&gt;for more content like this.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>debug</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Polyfills in React Application: A Complete Guide</title>
      <dc:creator>Shubham Dutta</dc:creator>
      <pubDate>Mon, 17 Apr 2023 20:54:03 +0000</pubDate>
      <link>https://forem.com/shubhamdutta2000/polyfills-in-react-application-a-complete-guide-523l</link>
      <guid>https://forem.com/shubhamdutta2000/polyfills-in-react-application-a-complete-guide-523l</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;A well-liked JavaScript library for creating user interfaces is called React. Developers can easily and effectively manage state with React and build reusable components. But when several browsers and gadgets don't support the same functionalities, there may be compatibility problems, just as with any JavaScript programme. Polyfills are useful in this situation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Describe Polyfills
&lt;/h2&gt;

&lt;p&gt;Code fragments called &lt;code&gt;polyfills&lt;/code&gt; offer functionality for features that might not be supported by all &lt;code&gt;browsers&lt;/code&gt; or &lt;code&gt;operating systems&lt;/code&gt;. They enable programmers to guarantee that their code operates as intended across all platforms, regardless of any restrictions on feature support.&lt;/p&gt;

&lt;p&gt;If you want to know more about &lt;code&gt;polyfills&lt;/code&gt; go to this &lt;a href="https://dev.to/shubhamdutta2000/polyfills-for-javascript-a-full-overview-3f7m"&gt;link&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Do React Users Use Polyfills?
&lt;/h2&gt;

&lt;p&gt;Making sure a React application is compatible with as many browsers as feasible is crucial when developing one. This is crucial for developing online applications that consumers may access on a variety of platforms and devices.&lt;/p&gt;

&lt;p&gt;Many of the contemporary JavaScript features used by React might not be supported by earlier browsers like Internet Explorer. By providing fallback code for features that aren't supported, polyfills can aid in ensuring that these features function properly across all browsers.&lt;/p&gt;




&lt;h2&gt;
  
  
  Examples of Polyfills in React
&lt;/h2&gt;

&lt;p&gt;Here are some more examples of popular JavaScript features used in React that may require polyfills:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Promise&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promises are used in React for asynchronous operations. However, they are not supported in older versions of Internet Explorer. &lt;/li&gt;
&lt;li&gt;To use Promises in all browsers, you can add a polyfill for them, such as the es6-promise package. &lt;/li&gt;
&lt;li&gt;This package provides a Promise implementation that works in older browsers, allowing you to use Promises in your React application without any compatibility issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Object.assign()&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This method is used in React for merging objects. However, it is not supported in older versions of Internet Explorer. &lt;/li&gt;
&lt;li&gt;To use Object.assign() in all browsers, you can add a polyfill for it, such as the object-assign package. &lt;/li&gt;
&lt;li&gt;This package provides an Object.assign() implementation that works in older browsers, allowing you to use this method in your React application without any compatibility issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Fetch API&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Fetch API&lt;/code&gt; is used in React for making HTTP requests. However, it is not supported in older versions of &lt;code&gt;Internet Explorer&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;To use the Fetch API in all browsers, you can add a polyfill for it, such as the &lt;code&gt;whatwg-fetch&lt;/code&gt; package. &lt;/li&gt;
&lt;li&gt;This package provides a Fetch API implementation that works in &lt;code&gt;older browsers&lt;/code&gt;, allowing you to use this feature in your React application without any compatibility issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Symbol&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Symbols&lt;/code&gt; are used in React for creating unique identifiers. However, they are not supported in some older browsers. &lt;/li&gt;
&lt;li&gt;To use &lt;code&gt;Symbols&lt;/code&gt; in your React application, you can add a polyfill for them, such as the &lt;code&gt;core-js&lt;/code&gt;package. &lt;/li&gt;
&lt;li&gt;This package provides a &lt;code&gt;Symbol&lt;/code&gt; implementation that works in &lt;code&gt;older browsers&lt;/code&gt;, allowing you to use this feature in your React application without any compatibility issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;String.startsWith()&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;startsWith&lt;/code&gt; method determines whether a string begins with the characters of a specified string, returning true or false as appropriate. However, it is not supported in older versions of &lt;code&gt;Internet Explorer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To use &lt;code&gt;startsWith&lt;/code&gt; in your React application, you can add a polyfill for them, such as the core-js package.&lt;/li&gt;
&lt;li&gt;This package provides a &lt;code&gt;String.prototype.startsWith()&lt;/code&gt; implementation that works in &lt;code&gt;older browsers&lt;/code&gt;, allowing you to use this method in your React application without any compatibility issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Array.includes()&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Array.includes()&lt;/code&gt; method in React is used to determine whether an array contains a specific value. Older versions of &lt;code&gt;Internet Explorer&lt;/code&gt; do not, however, support it. &lt;/li&gt;
&lt;li&gt;You can include a &lt;code&gt;polyfill&lt;/code&gt; for &lt;code&gt;Array.includes()&lt;/code&gt;, such as the &lt;code&gt;array-includes&lt;/code&gt; package, to make it work in all browsers. &lt;/li&gt;
&lt;li&gt;You can use the &lt;code&gt;Array.includes()&lt;/code&gt; method in your &lt;code&gt;React&lt;/code&gt; application without encountering any compatibility problems thanks to this package's implementation, which supports older browsers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Object.entries()&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This method is used in React for getting an object's key-value pairs as an array. However, it is not supported in older versions of &lt;code&gt;Internet Explorer&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;To use &lt;code&gt;Object.entries()&lt;/code&gt; in all browsers, you can add a polyfill for it, such as the &lt;code&gt;object.entries&lt;/code&gt; package. &lt;/li&gt;
&lt;li&gt;This package provides an &lt;code&gt;Object.entries()&lt;/code&gt; implementation that works in &lt;code&gt;older browsers&lt;/code&gt;, allowing you to use this method in your React application without any compatibility issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Writing polyfills from scratch
&lt;/h2&gt;

&lt;p&gt;Here is an example of how to write a polyfill from scratch for the &lt;code&gt;startsWith&lt;/code&gt; method in React:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F488oh1n8xc4nkf7p5nij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F488oh1n8xc4nkf7p5nij.png" alt="Polyfills from scratch 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we first determine whether the String prototype's &lt;code&gt;startsWith&lt;/code&gt; method already exists. If it doesn't already exist, we define it as a function that accepts &lt;code&gt;search&lt;/code&gt; and &lt;code&gt;pos&lt;/code&gt; as parameters.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;substr&lt;/code&gt; method is then used by the function to extract a section of the string beginning with the &lt;code&gt;pos&lt;/code&gt; argument, or 0 in the absence of &lt;code&gt;pos&lt;/code&gt; or if &lt;code&gt;pos&lt;/code&gt; is less than 0. The search parameter is then compared to this substring using rigorous equality (===), and a &lt;code&gt;boolean&lt;/code&gt; result is returned.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;startsWith&lt;/code&gt; method may be made to function as intended in all browsers, even those that don't support it natively, by specifying this polyfill.&lt;/p&gt;




&lt;p&gt;Here's another example of how to create a polyfill for the &lt;code&gt;array includes&lt;/code&gt; method:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F1xo1s3t22xz1359kd7h4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F1xo1s3t22xz1359kd7h4.png" alt="Polyfills from scratch 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This polyfill verifies whether the &lt;code&gt;Array.prototype&lt;/code&gt; object already defines the &lt;code&gt;includes&lt;/code&gt; method. It generates a new function that accepts an &lt;code&gt;elementToBeSearched&lt;/code&gt; parameter if it is not defined.&lt;/p&gt;

&lt;p&gt;The function then used a for loop to iterate through the array, inspecting each element to see if it matches the &lt;code&gt;elementToBeSearched&lt;/code&gt; parameter. It returns &lt;code&gt;true&lt;/code&gt; if it discovers a match. The function returns &lt;code&gt;false&lt;/code&gt; if it cannot locate a match.&lt;/p&gt;

&lt;p&gt;If you want to write more &lt;code&gt;polyfill&lt;/code&gt; from scratch go to this &lt;a href="https://dev.to/shubhamdutta2000/polyfills-for-javascript-a-full-overview-3f7m"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  NOTE:
&lt;/h3&gt;

&lt;p&gt;It's important to note that writing a &lt;code&gt;polyfill&lt;/code&gt; from &lt;code&gt;scratch&lt;/code&gt; can be challenging, especially for &lt;code&gt;complex features&lt;/code&gt;. In addition, it's important to thoroughly test the polyfill in various &lt;code&gt;browsers&lt;/code&gt; and &lt;code&gt;environments&lt;/code&gt; to ensure it behaves correctly and doesn't cause any unexpected issues.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Add Polyfills in React
&lt;/h2&gt;

&lt;p&gt;You can use a library containing a collection of polyfills that will perform the same function for you instead of manually checking for the existence of a feature and adding a polyfill.&lt;/p&gt;

&lt;p&gt;Use pre-existing polyfills or packages whenever possible; only create your own if absolutely essential and if you have the necessary knowledge and experience.&lt;/p&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1:
&lt;/h3&gt;

&lt;p&gt;Adding polyfills in React is a simple process. Here's how you can add a polyfill for &lt;code&gt;startsWith&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the core-js package as a dependency in your project:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  npm install core-js


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Import the startsWith polyfill at the top of your JavaScript file:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  import 'core-js/actual/string/starts-with';


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Now you can use the startsWith method in your React components, like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  const testString = 'Hi all JavaScript Developer out there!';
  const startsWithHi = testString.startsWith('Hi');

  console.log(startsWithHi); // true


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

&lt;/div&gt;

&lt;p&gt;We can make sure that the &lt;code&gt;startsWith&lt;/code&gt; method functions as intended in all browsers, even those that don't support it natively, by importing the &lt;code&gt;startsWith&lt;/code&gt; polyfill from &lt;code&gt;core-js&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 2:
&lt;/h3&gt;

&lt;p&gt;Let's take another example of how to add a polyfill for Array.includes():&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the array-includes package using npm:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  npm install array-includes


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Import the package in your JavaScript code:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  import 'array-includes/auto';


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

&lt;/div&gt;

&lt;p&gt;This will automatically add the &lt;code&gt;polyfill&lt;/code&gt; for &lt;code&gt;Array.includes()&lt;/code&gt; to your React application.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 3:
&lt;/h3&gt;

&lt;p&gt;Here is another example of how to use a polyfill for &lt;code&gt;Promise&lt;/code&gt; in a React project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the &lt;code&gt;es6-promise&lt;/code&gt; package using &lt;code&gt;npm&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  npm install es6-promise


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Import the package in your JavaScript code:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  import 'es6-promise/auto';


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

&lt;/div&gt;

&lt;p&gt;This will automatically add the polyfill for Promise to your React application.&lt;/p&gt;




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

&lt;p&gt;Polyfills are an essential tool for ensuring browser compatibility in React applications. By providing fallback code for unsupported features, polyfills help developers create web applications that work seamlessly across all browsers and platforms. &lt;/p&gt;

&lt;p&gt;If you're developing a React application, it's important to consider the use of polyfills to ensure maximum compatibility. With the right polyfills in place, you can be confident that your application will work as intended, regardless of any feature support limitations in the browser or environment.&lt;/p&gt;

&lt;p&gt;Follow &lt;a href="https://www.linkedin.com/in/shubham--dutta/" rel="noopener noreferrer"&gt;Shubham Dutta&lt;/a&gt; for more content like this.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>polyfills</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Polyfills for JavaScript: A Full Overview</title>
      <dc:creator>Shubham Dutta</dc:creator>
      <pubDate>Sun, 16 Apr 2023 21:31:57 +0000</pubDate>
      <link>https://forem.com/shubhamdutta2000/polyfills-for-javascript-a-full-overview-3f7m</link>
      <guid>https://forem.com/shubhamdutta2000/polyfills-for-javascript-a-full-overview-3f7m</guid>
      <description>&lt;h2&gt;
  
  
  Overview:
&lt;/h2&gt;

&lt;p&gt;You may be familiar with the phrase &lt;code&gt;"polyfill"&lt;/code&gt; as a developer. In a continuously changing technological landscape where new capabilities are frequently added to programming languages and online platforms, polyfills are an essential component of web development. Since not all browsers and environments immediately support the newest features, it can be difficult for developers to stay up to date with these changes.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;polyfill&lt;/code&gt; is a piece of code that enables the usage of new programming language or web platform features in &lt;code&gt;outdated browsers&lt;/code&gt; or environments that do not support them. We will look deeply into JavaScript polyfills in this blog article, discussing their advantages and how to use them in your code.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Need for Polyfills
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;New features&lt;/strong&gt;: Polyfills enable programmers to use &lt;code&gt;cutting-edge functions&lt;/code&gt; of a web platform or programming language in older browsers or environments that don't support them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Browser Compatibility&lt;/strong&gt;: The latest features of a programming language or web platform may not be supported by all browsers and environments, which can cause compatibility problems for developers. Polyfills aid in ensuring that the code functions consistently on various browsers and gadgets.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Benefits of Using Polyfills
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Better User Experience&lt;/strong&gt;: Polyfills allow developers to improve &lt;code&gt;user experience&lt;/code&gt; by adding features and capabilities, even in older browsers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced development time&lt;/strong&gt;: By enabling developers to use new capabilities without having to write specific code for &lt;code&gt;older browsers&lt;/code&gt;, polyfills can save developers a significant amount of effort.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cost-effective&lt;/strong&gt;: Using polyfills saves money by preventing the need to design specialised solutions for every browser or device by ensuring that your code works on a variety of &lt;code&gt;browsers and mobile devices&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enhanced security&lt;/strong&gt;: Using polyfills can also increase &lt;code&gt;security&lt;/code&gt; by offering a different implementation of a feature that might have security flaws in &lt;code&gt;earlier browser versions&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Better cross-browser compatibility&lt;/strong&gt;: Polyfills enable programmers to use new capabilities of a &lt;code&gt;programming language&lt;/code&gt; or &lt;code&gt;web platform&lt;/code&gt; in situations or environments that don't support them, ensuring that their code functions reliably across various &lt;code&gt;browsers and devices&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Future-proofing&lt;/strong&gt;: By incorporating new capabilities that might become standards in upcoming iterations of the programming language or web platform, developers can &lt;code&gt;future-proof&lt;/code&gt; their code by utilising polyfills.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Open-source community&lt;/strong&gt;: Developers can use a variety of &lt;code&gt;open-source&lt;/code&gt; polyfill libraries to take use of the skills and knowledge of other developers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Better accessibility&lt;/strong&gt;: By offering fallbacks for features that may not be supported by &lt;code&gt;assistive technologies&lt;/code&gt;, polyfills can help make web applications more accessible to users with impairments.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Implementing Polyfills in JavaScript
&lt;/h2&gt;

&lt;p&gt;The method of implementing polyfills in &lt;code&gt;JavaScript&lt;/code&gt; is simple. In this part, we'll examine a couple of code examples that demonstrate how to use polyfills. We will go over the steps for putting polyfills into JavaScript in this section:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Identify the missing feature&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finding the feature that is absent from the &lt;code&gt;browser or environment&lt;/code&gt; is the first step in constructing a polyfill. &lt;/li&gt;
&lt;li&gt;This can be accomplished by determining whether the feature is compatible with the browser or by determining whether it is present using feature detection methods like &lt;code&gt;typeof&lt;/code&gt;, &lt;code&gt;in&lt;/code&gt;, or &lt;code&gt;window&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Write the fallback implementation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The next step is to develop the fallback implementation that offers a similar functionality once the missing feature has been located. &lt;/li&gt;
&lt;li&gt;A pre-existing &lt;code&gt;polyfill library&lt;/code&gt; or pure &lt;code&gt;JavaScript code&lt;/code&gt; can both be used to do this.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Examine the polyfill&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is crucial to properly test the polyfill after implementation to make sure it &lt;code&gt;functions&lt;/code&gt; as intended. &lt;/li&gt;
&lt;li&gt;To check that the code provides the same functionality across various contexts and &lt;code&gt;browsers&lt;/code&gt;, you can test it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Implement the polyfill in your code&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implementing the polyfill in your code comes after &lt;code&gt;testing&lt;/code&gt; it. &lt;/li&gt;
&lt;li&gt;To achieve this, enclose the code that makes use of the lacking feature in an if statement that verifies the feature's support. &lt;/li&gt;
&lt;li&gt;The polyfill code is run in its place if the feature is &lt;code&gt;not supported&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Example 1:
&lt;/h3&gt;

&lt;p&gt;Let's imagine we wish to use the &lt;code&gt;Array.includes()&lt;/code&gt; method. This method was added in &lt;code&gt;ECMAScript 2016&lt;/code&gt;, but earlier browsers like &lt;code&gt;Internet Explorer 11&lt;/code&gt; do not support it. We may build a polyfill in the manner described below to use this approach on earlier browsers:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.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%2F00i0l73evhfzpo3s14t2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F00i0l73evhfzpo3s14t2.png" alt="Array.includes" width="800" height="573"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we use the! operator to see if &lt;code&gt;Array.prototype.includes()&lt;/code&gt; is available. We implement the polyfill that offers a similar capability if it is not supported.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 2:
&lt;/h3&gt;

&lt;p&gt;Here's an example of implementing a polyfill for the &lt;code&gt;Object.assign()&lt;/code&gt; method:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.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%2Fqkfpiqn05l6n37xykn0y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fqkfpiqn05l6n37xykn0y.png" alt="Object.assign" width="800" height="707"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we first check whether &lt;code&gt;Object.assign()&lt;/code&gt; is supported using the &lt;code&gt;typeof&lt;/code&gt; operator. If it is not supported, we implement the polyfill that provides a similar functionality.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 3:
&lt;/h3&gt;

&lt;p&gt;Let's take an example of polyfill implementation for the &lt;code&gt;Math.trunc()&lt;/code&gt; method. The function &lt;code&gt;Math.trunc(n)&lt;/code&gt; "cuts off" the decimal portion of an integer. For example, &lt;code&gt;Math.trunc(1.23)&lt;/code&gt; returns 1.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.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%2Frdoo4uzzjp6x8ji7x549.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Frdoo4uzzjp6x8ji7x549.png" alt="Math.trunc" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 4:
&lt;/h3&gt;

&lt;p&gt;Here's another example of a polyfill for the &lt;code&gt;Array.from()&lt;/code&gt; method:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.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%2Fvt3d3cz9pbykko5gtmyl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fvt3d3cz9pbykko5gtmyl.png" alt="Array.from" width="800" height="644"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we use the &lt;code&gt;!&lt;/code&gt; operator to first determine whether &lt;code&gt;Array.from()&lt;/code&gt; is supported. We implement the polyfill that offers a similar capability if it is not supported.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 5:
&lt;/h3&gt;

&lt;p&gt;Here's another example of a polyfill for the &lt;code&gt;Promise&lt;/code&gt; object:&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// Check if Promise is supported
if (!window.Promise) {
    // Implement the polyfill
    window.Promise = function (executor) {
      if (typeof executor !== 'function') {
        throw new TypeError('Promise resolver ' + executor + ' is not a function');
      }

      var self = this;
      self.status = 'pending';
      self.value = undefined;
      self.reason = undefined;
      self.onFulfilledCallbacks = [];
      self.onRejectedCallbacks = [];

      function resolve(value) {
        if (self.status === 'pending') {
          self.status = 'fulfilled';
          self.value = value;
          self.onFulfilledCallbacks.forEach(function (callback) {
            callback(value);
          });
        }
      }

      function reject(reason) {
        if (self.status === 'pending') {
          self.status = 'rejected';
          self.reason = reason;
          self.onRejectedCallbacks.forEach(function (callback) {
            callback(reason);
          });
        }
      }

      try {
        executor(resolve, reject);
      } catch (e) {
        reject(e);
      }
    };

    window.Promise.prototype.then = function (onFulfilled, onRejected) {
      var self = this;
      var newPromise;

      if (self.status === 'fulfilled') {
        return newPromise = new Promise(function (resolve, reject) {
          try {
            var x = onFulfilled(self.value);
            resolve(x);
          } catch (e) {
            reject(e);
          }
        });
      } else if (self.status === 'rejected') {
        return newPromise = new Promise(function (resolve, reject) {
          try {
            var x = onRejected(self.reason);
            resolve(x);
          } catch (e) {
            reject(e);
          }
        });
      } else if (self.status === 'pending') {
        return newPromise = new Promise(function (resolve, reject) {
          self.onFulfilledCallbacks.push(function (value) {
            try {
              var x = onFulfilled(value);
              resolve(x);
            } catch (e) {
              reject(e);
            }
          });

          self.onRejectedCallbacks.push(function (reason) {
            try {
              var x = onRejected(reason);
              resolve(x);
            } catch (e) {
              reject(e);
            }
          });
        });
      }
    };

    window.Promise.prototype.catch = function (onRejected) {
      return this.then(null, onRejected);
    };
  }


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

&lt;/div&gt;

&lt;p&gt;In this example, we use the! operator to first determine whether the &lt;code&gt;Promise&lt;/code&gt; object is supported. We implement the polyfill that offers a similar capability if it is not supported.&lt;/p&gt;




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

&lt;p&gt;Developers need &lt;code&gt;polyfills&lt;/code&gt; because they enable them to use new capabilities in less modern browsers and contexts that don't support them. By utilising polyfills instead of writing special code for outdated browsers, developers can increase &lt;code&gt;browser compatibility&lt;/code&gt;, improve &lt;code&gt;user experience&lt;/code&gt;, increase &lt;code&gt;security&lt;/code&gt; and save &lt;code&gt;time&lt;/code&gt;. Using polyfills in your JavaScript code is a simple and straightforward process if you have the correct information and resources. Follow &lt;a href="https://www.linkedin.com/in/shubham--dutta/" rel="noopener noreferrer"&gt;Shubham Dutta&lt;/a&gt; for more content like this.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>polyfills</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>MongoDB Aggregation Pipeline - $lookup</title>
      <dc:creator>Shubham Dutta</dc:creator>
      <pubDate>Sun, 12 Feb 2023 20:35:00 +0000</pubDate>
      <link>https://forem.com/shubhamdutta2000/mongodb-aggregaation-pipeline-lookup-3pbl</link>
      <guid>https://forem.com/shubhamdutta2000/mongodb-aggregaation-pipeline-lookup-3pbl</guid>
      <description>&lt;h3&gt;
  
  
  Introduction:
&lt;/h3&gt;

&lt;p&gt;The Aggregation Pipeline in MongoDB is a strong tool that lets you handle and examine data kept in a MongoDB collection. The $lookup step, which enables you to carry out a left outer join between two collections, is one of the crucial stages in the Aggregation Pipeline. When you need to compile data from various collections into a single document, this stage is quite helpful.&lt;/p&gt;

&lt;p&gt;A versatile and effective tool that can handle a variety of usage situations is the $lookup stage. For instance, you could use it to unite a collection with a reference data collection to add more information to your papers or to integrate data from two collections to create a customer order report.&lt;/p&gt;

&lt;p&gt;To use the $lookup stage, you need to specify the following parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;from&lt;/code&gt;: the collection's name that will be merged with the existing one &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;localField&lt;/code&gt;: the current collection's field that serves as the join key &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;foreignField&lt;/code&gt;: the from collection field that serves as the join key&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;as&lt;/code&gt;: The new array field's name in the result document, which will include the join's outcomes&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   $lookup:
     {
       from: &amp;lt;collection to join&amp;gt;,
       localField: &amp;lt;field from the input documents&amp;gt;,
       foreignField: &amp;lt;field from the documents of the "from" collection&amp;gt;,
       as: &amp;lt;output array field&amp;gt;
     }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Pseudo SQL Statement
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *, &amp;lt;output array field&amp;gt;
FROM collection
WHERE &amp;lt;output array field&amp;gt; IN (
   SELECT *
   FROM &amp;lt;collection to join&amp;gt;
   WHERE &amp;lt;foreignField&amp;gt; = &amp;lt;collection.localField&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Orders Collection:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "_id": 1,
  "user_id": 1,
  "total": 100
}

{
  "_id": 2,
  "user_id": 2,
  "total": 200
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  User Collection:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "_id": 1,
  "name": "John"
}

{
  "_id": 2,
  "name": "Jane"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can use the following aggregation pipeline to combine user and order data:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.orders.aggregate([
  {
    $lookup: {
      from: "users",
      localField: "user_id",
      foreignField: "_id",
      as: "user_info"
    }
  }
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Output:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "_id": 1,
  "user_id": 1,
  "total": 100,
  "user_info": [
    {
      "_id": 1,
      "name": "John"
    }
  ]
}

{
  "_id": 2,
  "user_id": 2,
  "total": 200,
  "user_info": [
    {
      "_id": 2,
      "name": "Jane"
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;









&lt;h2&gt;
  
  
  Join Conditions and Subqueries on a Joined Collection
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A powerful tool for combining data from two or more collections is the MongoDB $lookup stage. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To further improve the results, you might need to specify conditions or run subqueries on the joined collection in some circumstances. This is where the let and pipeline options come in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can specify expressions that can be used in the pipeline option by using the let option. Variables that can be referred to in the pipeline stages can be defined using these expressions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Syntax&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   $lookup:
      {
         from: &amp;lt;combined collection&amp;gt;,
         let: { &amp;lt;variable-1&amp;gt;: &amp;lt;expr&amp;gt;, …, &amp;lt;variable-N&amp;gt;: &amp;lt;expr&amp;gt; },
         pipeline: [ &amp;lt;pipeline to run on combined collection&amp;gt; ],
         as: &amp;lt;resultant array field&amp;gt;
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Orders Collection:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "_id": 1,
  "user_id": 1,
  "amount": 300
}

{
  "_id": 2,
  "user_id": 2,
  "amount": 400
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  User Collection:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "_id": 1,
  "name": "Shubham",
  "created_at": ISODate("2021-02-11")
}

{
  "_id": 2,
  "name": "Random",
  "created_at": ISODate("2022-02-12")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To join the Orders and Users collections and return only customers who joined in the last year, you can use the following aggregation pipeline:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.orders.aggregate([
  {
    $lookup: {
      from: "users",
      let: { created_at: "$created_at" },
      pipeline: [
        {
          $match: {
            $expr: {
              $gt: [ "$$created_at", new Date(ISODate().getTime() - 365 * 24 * 60 * 60 * 1000) ]
            }
          }
        }
      ],
      as: "user_info"
    }
  }
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL Statement for the above query&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM Orders
JOIN Users
ON Orders.customer_id = Users._id
WHERE Users.created_at &amp;gt; DATEADD(year, -1, GETDATE());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Output&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "_id": 2,
  "user_id": 2,
  "amount": 400,
  "user_info": {
    "_id": 2,
    "name": "Random",
    "created_at": ISODate("2022-02-12")
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;









&lt;h2&gt;
  
  
  Correlated Subqueries
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Correlated subqueries are an effective tool for running complex queries. They enable you to improve the results of a different query by applying the findings from the first.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A correlated subquery is a subquery that references a column from the outer query. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To perform a correlated subquery in MongoDB's aggregation pipeline, you can use the $lookup stage to join a collection to itself. For example, consider the following aggregation pipeline:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.Orders.aggregate([
  {
    $lookup: {
      from: "Orders",
      let: { user_id: "$user_id" },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                "$user_id",
                "$$user_id"
              ]
            }
          }
        },
        {
          $group: {
            _id: "$user_id",
            avg_order_amount: { $avg: "$amount" }
          }
        }
      ],
      as: "user_orders"
    }
  },
  {
    $match: {
      "users.created_at": {
        $gt: ISODate("2022-02-05T00:00:00.000Z")
      }
    }
  }
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;$lookup&lt;/code&gt; stage in this pipeline performs a left outer join with the &lt;code&gt;Orders&lt;/code&gt; collection and with itself. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The value of the &lt;code&gt;user id&lt;/code&gt; field to be used in the subquery is specified by the let expression. The subquery to be executed on the joined collection is specified by the pipeline expression. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;$match&lt;/code&gt; stage of this subquery specifies the requirement that only orders placed by the relevant user should be taken into account, and the &lt;code&gt;$group&lt;/code&gt; stage determines the average order value for each user. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;as&lt;/code&gt; expression specifies the name of the new array field in the result document that will contain the results of the subquery.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Only users who joined within the past year are included in the result set after being filtered by the pipeline's final &lt;code&gt;$match&lt;/code&gt; stage. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A document containing the &lt;code&gt;user ID&lt;/code&gt;, &lt;code&gt;order date&lt;/code&gt;, and &lt;code&gt;average order&lt;/code&gt; amount for each new customer added in the previous year is the output of this pipeline.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL Statement for the above query&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT customer_id, order_date,
(SELECT AVG(total_amount)
FROM Orders
WHERE Orders.customer_id = Customers._id) AS avg_order_amount
FROM Customers
WHERE joined_date &amp;gt; DATEADD(year, -1, GETDATE());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;An effective feature that enables users to carry out complicated data analysis and manipulation is the MongoDB aggregation pipeline. You can make the most of this feature and maximise the potential of your MongoDB data by comprehending the architecture and pipeline stages as well as the usage scenarios for which they are best suited. Follow &lt;a href="https://www.linkedin.com/in/shubham--dutta/" rel="noopener noreferrer"&gt;Shubham Dutta&lt;/a&gt; for more content like this.&lt;/p&gt;

</description>
      <category>devmeme</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>MongoDB Aggregation Pipeline - $function Stage</title>
      <dc:creator>Shubham Dutta</dc:creator>
      <pubDate>Sun, 05 Feb 2023 18:45:48 +0000</pubDate>
      <link>https://forem.com/shubhamdutta2000/the-function-stage-in-mongodb-aggregation-pipeline-590d</link>
      <guid>https://forem.com/shubhamdutta2000/the-function-stage-in-mongodb-aggregation-pipeline-590d</guid>
      <description>&lt;h3&gt;
  
  
  An Overview
&lt;/h3&gt;

&lt;p&gt;A popular NoSQL database called MongoDB offers a scalable and adaptable option for storing and handling enormous amounts of data. The aggregation pipeline, which enables users to carry out complex data analysis and manipulation, is one of MongoDB's core features. The &lt;code&gt;$function&lt;/code&gt; stage of the MongoDB aggregation pipeline and its use cases will be the main topic of this blog.&lt;/p&gt;




&lt;h3&gt;
  
  
  What is the $function Stage in MongoDB Aggregation Pipeline?
&lt;/h3&gt;

&lt;p&gt;The MongoDB aggregation pipeline has an additional stage called &lt;code&gt;$function&lt;/code&gt; that enables users to write their own unique JavaScript functions for data processing. When there is no built-in stage that can carry out the needed operation, this stage is especially helpful. Users have access to the complete JavaScript runtime and the MongoDB aggregation framework because the $function stage is executed within the context of the MongoDB server.&lt;/p&gt;




&lt;h3&gt;
  
  
  Syntax:
&lt;/h3&gt;

&lt;p&gt;The following syntax is used to define the &lt;code&gt;$function&lt;/code&gt; stage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  $function: {
    body: &amp;lt;code&amp;gt;,
    args: &amp;lt;array expression&amp;gt;,
    lang: "js"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;args&lt;/code&gt; field specifies an optional initial value that will be provided to the function, and the &lt;code&gt;body&lt;/code&gt; field specifies the JavaScript function definition that will be performed. You can specify an empty array []. If the body function does not accept an argument. lang is The language used in the body need to specify &lt;code&gt;lang: "js"&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Use Cases for the &lt;code&gt;$function&lt;/code&gt; Stage in MongoDB Aggregation Pipeline
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Running totals computation&lt;/strong&gt;: The &lt;code&gt;$function&lt;/code&gt; stage can compute running totals by storing the cumulative sum in the state object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Implement custom algorithms&lt;/strong&gt;: Custom algorithms that cannot be implemented with the built-in stages can be implemented using the &lt;code&gt;$function&lt;/code&gt; stage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Transforming Array&lt;/strong&gt;: Arrays can be transformed by mapping, filtering, or reducing the items of the array using the &lt;code&gt;$function&lt;/code&gt; stage.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;Consider the following input document in the "&lt;code&gt;customer&lt;/code&gt;" collection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "_id": 1,
  "name": "Shubham Dutta",
  "email": "shubham_dutta@gmail.com",
  "position": "Developer"
},

{
  "_id": 2,
  "name": "Arjun Dutta",
  "email": "arjun.dutta@gmail.com",
  "position": "Designer"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we need to separate out each customer's first name from their entire name and lowercase the position. The aggregate process listed below can be used to do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.aggregate([
  {
    $project: {
      'name': {
        $function: {
          body: function (name) {
            let firstName = name.split(" ")[0];
            return firstName;
          },
          args: {"$name"},
          lang: "js",
        },
      },
      'position': {
        $function: {
          body: function (position) {
            let position = position.toLowerCase();
            return position;
          },
          args: {"$position"},
          lang: "js",
        },
      },
    },
    '_id': 0,
    'email': 1
  },
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following is the result of the above pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "name": "Shubham",
    "email": "shubham_dutta@gmail.com",
    "position": "Developer"
  },
  {
    "name": "Arjun",
    "email": "arjun.dutta@gmail.com",
    "position": "designer"
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By splitting the name field on the space character and returning the first element of the resulting array, the first name of each customer is extracted from their entire name  using the &lt;code&gt;$function&lt;/code&gt; stage in this example. The toLowerCase() method is used to change the position field's case to lowercase. Each customer's name, email address, and position are included in a new object that is returned by the function argument using $project.&lt;/p&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The MongoDB aggregation pipeline has a significant feature called the &lt;code&gt;$function&lt;/code&gt; stage that enables users to write unique JavaScript functions for data processing. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;$function&lt;/code&gt; stage gives you the freedom and power you need to design custom algorithms, manipulate arrays, or compute running totals. &lt;/p&gt;

&lt;p&gt;You can fully utilize this feature and unleash the complete power of your MongoDB data by understanding the syntax and application of the &lt;code&gt;$function&lt;/code&gt; stage.&lt;/p&gt;

</description>
      <category>management</category>
      <category>productivity</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Prototype Design Pattern in JavaScript</title>
      <dc:creator>Shubham Dutta</dc:creator>
      <pubDate>Sun, 25 Sep 2022 08:00:26 +0000</pubDate>
      <link>https://forem.com/shubhamdutta2000/prototype-pattern-in-javascript-25pl</link>
      <guid>https://forem.com/shubhamdutta2000/prototype-pattern-in-javascript-25pl</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Objects can contain both methods and properties. Methods are functions called when we interact with an object, and properties are data associated with an object.&lt;/p&gt;

&lt;p&gt;The Prototype Pattern is a technique for extending objects by giving the current objects new properties and methods. Although it has been a design pattern for many years, it has only recently become popular.&lt;/p&gt;

&lt;p&gt;By using the prototype chain, we can avoid duplicating methods and properties, which saves memory by allowing us to access properties that aren't directly defined on the object itself.&lt;/p&gt;

&lt;p&gt;The prototype chain is very useful because it allows you to create an object which inherits from another object, without having to copy all its properties and methods into your own object.&lt;/p&gt;

&lt;p&gt;The primary goal of this pattern is to facilitate the creation of objects that can serve as blueprints for all constructor-created objects. It accomplishes this through a process known as prototypal inheritance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Focdwohs17xzmydszgvhf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Focdwohs17xzmydszgvhf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;








&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;To better understand, let's use the example of a car and begin implementing Factory Pattern and Prototype Pattern and difference between them.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;With Factory Pattern&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Imagine using a Car factory function to produce numerous cars.&lt;br&gt;
This way, we can easily create many car objects with the same properties.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fgmrsdh0qi6lxtc2xb10w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fgmrsdh0qi6lxtc2xb10w.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To each car object, we are unnecessarily adding new drive and accelerate methods. We're using memory to create two new functions for each car object internally. JavaScript has duplicated the same method for every instance:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F2gut5w5t2b6ki9x0t815.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F2gut5w5t2b6ki9x0t815.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to know more about factory pattern read out this &lt;a href="https://dev.to/shubhamdutta2000/factory-design-pattern-in-javascript-3ecj"&gt;blog&lt;/a&gt;.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;With Prototype pattern&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To distribute these methods among numerous car objects, we can use the prototype pattern.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Method 1&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fktaqghoyj51k0669vktt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fktaqghoyj51k0669vktt.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By using &lt;code&gt;Car.prototype&amp;lt;method&amp;gt; = function()...&lt;/code&gt;, we define a car's methods in our example code. Now that the &lt;code&gt;new&lt;/code&gt; keyword has been instantiated on some cars, we are looking at all the instances. According to the arguments that the caller passed in, we set their &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;passengers&lt;/code&gt;, &lt;code&gt;speed&lt;/code&gt;, and &lt;code&gt;color&lt;/code&gt; properties.&lt;/p&gt;

&lt;p&gt;The instances we're looking at are actually making reference to the same &lt;code&gt;drive&lt;/code&gt; and &lt;code&gt;accelerate&lt;/code&gt; methods that we defined in the prototype, as shown!&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Method 2&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fq6r6lr0s18dbyy9lx02l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fq6r6lr0s18dbyy9lx02l.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can easily share properties, like drive and acceleration in this case, among the numerous instances, thanks to &lt;strong&gt;ES6 classes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F7gubk0xxjp7dgbp7n7h1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F7gubk0xxjp7dgbp7n7h1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;








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

&lt;p&gt;The performance increase over object-oriented classes is the main advantage of using the pattern in JavaScript. This indicates that the functions defined inside of an object will be created by reference. In simple words, rather than making separate copies of themselves, all child objects will all point to the same method!&lt;/p&gt;

&lt;p&gt;We can avoid duplicating methods and properties by using the prototype chain, which also enables us to access properties that aren't directly defined on the object itself. This helps conserve memory.&lt;/p&gt;

&lt;p&gt;I hope you found this useful. Please keep an eye out for more in the future. &lt;/p&gt;

&lt;p&gt;If you have any queries, then feel free to connect with me on my LinkedIn profile - &lt;a href="https://www.linkedin.com/in/shubham--dutta/" rel="noopener noreferrer"&gt;Shubham Dutta&lt;/a&gt;. Please get in touch with me if you have any inquiries.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>designpattern</category>
      <category>prototypepattern</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Factory Design Pattern in JavaScript</title>
      <dc:creator>Shubham Dutta</dc:creator>
      <pubDate>Sat, 17 Sep 2022 18:56:35 +0000</pubDate>
      <link>https://forem.com/shubhamdutta2000/factory-design-pattern-in-javascript-3ecj</link>
      <guid>https://forem.com/shubhamdutta2000/factory-design-pattern-in-javascript-3ecj</guid>
      <description>&lt;h1&gt;
  
  
  Design Pattern
&lt;/h1&gt;

&lt;p&gt;Design patterns are models or plans for resolving typical issues in software development. They are the best practises that programmers can adhere to when creating software applications.&lt;/p&gt;

&lt;p&gt;There are numerous varieties of design patterns. 3 main categories of design patterns are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Structural patterns&lt;/li&gt;
&lt;li&gt;Behavioral patterns&lt;/li&gt;
&lt;li&gt;Creational patterns (Factory Pattern falls under this category)&lt;/li&gt;
&lt;/ul&gt;



&lt;h1&gt;
  
  
  Introduction on Factory Pattern
&lt;/h1&gt;

&lt;p&gt;We can generate multiple of the same objects using the Factory Pattern by using a unique function called the factory function.&lt;br&gt;
A general interface for creating objects is offered by the factory design pattern, which is a creational design pattern. &lt;/p&gt;

&lt;p&gt;We do not have to explicitly require a constructor function when using the factory pattern as we can indicate the type of object that is being constructed.&lt;/p&gt;

&lt;p&gt;In the functional programming paradigm, a factory is an entity that produces objects, such as a class or function.&lt;/p&gt;



&lt;h1&gt;
  
  
  In-depth explanation
&lt;/h1&gt;

&lt;p&gt;Factory Pattern is not indeed a pattern. The factory pattern in JavaScript essentially consists of a function that returns an object without utilizing the new keyword. &lt;/p&gt;

&lt;p&gt;We can build quick factory functions that always return an object thanks to ES6's arrow functions.&lt;/p&gt;

&lt;p&gt;In many cases, to create new instances, it may often be more memory-efficient instead of creating new objects every time. &lt;/p&gt;

&lt;p&gt;Let's take the example of creating &lt;strong&gt;Product&lt;/strong&gt; and implement it by creating new instances.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--StC9nbkX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxjbhbez8o5hjat8y8v7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--StC9nbkX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxjbhbez8o5hjat8y8v7.png" alt="classBased" width="880" height="661"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Factory Pattern follows &lt;strong&gt;Object Oriented Programming&lt;/strong&gt;, Design Pattern which involves creating objects.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;constructor&lt;/strong&gt; function for various object types is wrapped by the factory pattern, which then returns instances of the objects via a simple API. It facilitates the creation of various objects that returns the desired object type.&lt;/p&gt;

&lt;p&gt;Despite the fact that &lt;strong&gt;JavaScript&lt;/strong&gt; offers a variety of object generation techniques, the factory pattern helps us to keep our object creation code streamlined and reusable.&lt;/p&gt;



&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Let’s start by building our own function which will be responsible for returning new objects of a specific type whenever called.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;factory&lt;/code&gt; folder, let’s create a file named &lt;code&gt;product.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FqqCkz-R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtvnqb9e12j5wgbcasqq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FqqCkz-R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtvnqb9e12j5wgbcasqq.png" alt="factoryPattern" width="880" height="853"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this file, we build a &lt;code&gt;createProduct&lt;/code&gt; constructor function. It accepts an object as a parameter attributes for initializing the object with various info i.e name, description, brand, size, color, createdAt timestamp. &lt;br&gt;
Then we can create multiple objects with the same properties and different values of those.&lt;/p&gt;



&lt;h2&gt;
  
  
  When to use a factory pattern
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If a class is unable to identify the subclass, it must construct one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When we need to generate several objects with the same properties without having to duplicate the same code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever the process of creating an object is particularly complex&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When we need to return a custom object based on the existing environment, or when a user-specific configuration is required.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you don't know in advance the precise types and dependencies of the objects your code needs to work with, use the factory method.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Advantage of using factory pattern
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Encourage code reusability and maintainability&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Allowing you more freedom to develop different types of objects that use the same API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Helps to promote loose decoupling by isolating the construction of the object from its implementation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Helps in memory conservation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Disadvantage of using factory pattern
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In some circumstances, implementation can be challenging.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Due to the level of abstraction it introduces, testing might be challenging depending on complexity.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Use the factory method when you are unsure of the exact types and dependencies of the objects your code will be working with in advance.&lt;/p&gt;

&lt;p&gt;When you want to conserve system resources by reusing existing objects rather than rebuilding them each time, use the factory method. When working with large, resource-intensive objects like database connections, file systems, and network resources, you frequently run into this requirement. &lt;/p&gt;

&lt;p&gt;This pattern not only helps in code reusability and maintainability but also preserve memory and you have more control over objects.&lt;/p&gt;

&lt;p&gt;If you have any queries, then feel free to connect with me on my LinkedIn profile - &lt;a href="https://www.linkedin.com/in/shubham--dutta/"&gt;Shubham Dutta&lt;/a&gt;. Please get in touch with me if you have any inquiries.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>productivity</category>
      <category>designpattern</category>
      <category>factorypattern</category>
    </item>
  </channel>
</rss>
