<?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>DEV Community: Ritik Banger</title>
    <description>The latest articles on DEV Community by Ritik Banger (@ritikbanger).</description>
    <link>https://dev.to/ritikbanger</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%2F859437%2F4a5bd95e-489f-431a-baab-c8e66fc528b5.jpeg</url>
      <title>DEV Community: Ritik Banger</title>
      <link>https://dev.to/ritikbanger</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ritikbanger"/>
    <language>en</language>
    <item>
      <title>Best Practices for Designing Scalable and Resilient Systems</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Wed, 13 Mar 2024 14:32:14 +0000</pubDate>
      <link>https://dev.to/ritikbanger/best-practices-for-designing-scalable-and-resilient-systems-2i6h</link>
      <guid>https://dev.to/ritikbanger/best-practices-for-designing-scalable-and-resilient-systems-2i6h</guid>
      <description>&lt;p&gt;Hey developers! Are you looking to design robust systems that can handle changing requirements and unexpected failures? Here are some key points to consider:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prioritize Requirements&lt;/strong&gt;: Before diving into design, ensure you understand both the functional and non-functional requirements of your system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Define Use Cases Clearly&lt;/strong&gt;: Clearly define the system's use cases and constraints to guide your design decisions effectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Embrace Tradeoffs&lt;/strong&gt;: Remember, there's no perfect solution. Design is about making tradeoffs that best suit your project's needs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexibility is Key&lt;/strong&gt;: Assume requirements will change, and design your system with flexibility in mind to adapt to these changes smoothly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ensure Fault Tolerance&lt;/strong&gt;: Plan for failures by making your system fault-tolerant, anticipating that everything can and will fail at some point.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid Over-Engineering&lt;/strong&gt;: Don't add functionality until it's necessary. Over-engineering can lead to unnecessary complexity and maintenance overhead.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability Matters&lt;/strong&gt;: Design your system with scalability in mind from the beginning.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Horizontal Scaling Preference&lt;/strong&gt;: Prefer horizontal scaling over vertical scaling for better scalability and resource utilization.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Implement Load Balancers&lt;/strong&gt;: Ensure high availability and evenly distribute traffic with load balancers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Choose Databases Wisely&lt;/strong&gt;: Use SQL databases for structured data and ACID transactions, while opting for NoSQL databases for unstructured data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Utilize Database Sharding&lt;/strong&gt;: Scale SQL databases horizontally with database sharding for improved performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimize Data Retrieval&lt;/strong&gt;: Utilize database indexing and search engines for efficient data retrieval.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prevent Overloads&lt;/strong&gt;: Implement rate limiting to prevent system overload and potential denial-of-service (DOS) attacks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Real-Time Communication&lt;/strong&gt;: Use WebSockets for real-time communication needs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Failure Detection&lt;/strong&gt;: Employ heartbeat mechanisms for timely failure detection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Asynchronous Communication&lt;/strong&gt;: Consider using message queues for asynchronous communication between components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Partition Large Datasets&lt;/strong&gt;: Implement data partitioning and sharding for handling large datasets effectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Denormalize for Performance&lt;/strong&gt;: Consider denormalizing databases for read-heavy workloads to improve performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Decoupled Systems&lt;/strong&gt;: Utilize event-driven architecture for building decoupled systems.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduce Latency&lt;/strong&gt;: Use content delivery networks (CDNs) to reduce latency for a global user base.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimize for Write-Heavy Apps&lt;/strong&gt;: Implement write-through cache for write-heavy applications to improve performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimize for Read-Heavy Apps&lt;/strong&gt;: Use read-through cache for read-heavy applications to enhance performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Media Storage&lt;/strong&gt;: Utilize blob/object storage for storing media files like images, videos, and documents.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ensure Redundancy&lt;/strong&gt;: Implement data replication and redundancy to avoid single points of failure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Handle Traffic Spikes&lt;/strong&gt;: Implement autoscaling to handle sudden traffic spikes seamlessly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Background Task Processing&lt;/strong&gt;: Utilize asynchronous processing for running background tasks efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Idempotent Operations&lt;/strong&gt;: Make operations idempotent wherever possible to simplify retry logic and error handling.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Microservices Architecture&lt;/strong&gt;: Consider using microservices for flexibility, scalability, and maintainability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Analytics and Reporting&lt;/strong&gt;: Utilize data lakes or data warehouses for analytics and reporting purposes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By incorporating these best practices into your system design, you can create scalable, resilient, and high-performing applications that meet the demands of today's dynamic environments.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>productivity</category>
      <category>development</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Build a Serverless Web Application</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Tue, 12 Mar 2024 18:45:31 +0000</pubDate>
      <link>https://dev.to/ritikbanger/how-to-build-a-serverless-web-application-23i4</link>
      <guid>https://dev.to/ritikbanger/how-to-build-a-serverless-web-application-23i4</guid>
      <description>&lt;p&gt;Ah, serverless computing - the buzzword that's been taking the tech world by storm. But what exactly is it, and why should you care? Simply put, serverless computing allows you to build and run applications without worrying about provisioning or managing servers. It's like having an army of invisible servers at your beck and call, ready to handle your application's logic whenever it's needed.&lt;/p&gt;

&lt;p&gt;One of the coolest use cases for serverless technology is building web applications. Imagine a world where you can create a website without having to worry about scaling, patching, or maintaining servers. Sounds too good to be true, right? Well, buckle up, because that's exactly what we're going to do in this article.&lt;/p&gt;

&lt;p&gt;Serverless is a cloud computing execution model where the cloud provider dynamically manages the allocation and provisioning of servers. With serverless, you don't have to worry about provisioning, scaling, or managing servers yourself. Instead, you deploy your code, and the cloud provider handles the server management and execution for you.&lt;/p&gt;

&lt;p&gt;In a serverless architecture, your application's code runs in stateless compute containers that are event-triggered, ephemeral (they only run when needed and shut down automatically after use), and fully managed by the cloud provider. These containers, called "functions" in the serverless world, are typically triggered by events such as HTTP requests, database events, queue services, monitoring alerts, or other custom triggers.&lt;/p&gt;

&lt;p&gt;The key characteristics of serverless computing include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Server Management&lt;/strong&gt;: You don't have to provision, scale, or manage any servers. The cloud provider handles all the infrastructure management for you.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event-Driven&lt;/strong&gt;: Functions are triggered by specific events or requests, rather than running continuously.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Automatic Scaling&lt;/strong&gt;: The cloud provider automatically allocates the necessary resources to handle the incoming workload, scaling out seamlessly as needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pay-per-Use Billing&lt;/strong&gt;: You only pay for the compute time your functions consume when they are running, plus any other cloud services used. This can result in significant cost savings compared to maintaining always-on servers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;High Availability and Fault Tolerance&lt;/strong&gt;: Cloud providers ensure high availability and fault tolerance for serverless functions across multiple availability zones.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Popular serverless compute services include AWS Lambda, Google Cloud Functions, Azure Functions, and IBM Cloud Functions. These services allow you to deploy and run your code without provisioning or managing servers, enabling you to focus on writing application logic instead of infrastructure management.&lt;/p&gt;

&lt;p&gt;Serverless architectures are well-suited for various workloads, including web applications, APIs, data processing, IoT backends, and more. They can help reduce operational overhead, increase scalability, and potentially lower costs for certain types of applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting the Stage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we dive into the code, let's set up our development environment. You'll need an AWS account and the AWS CLI installed on your machine. We'll also be using Node.js and the Serverless Framework, a popular open-source tool that makes building and deploying serverless applications a breeze.&lt;/p&gt;

&lt;p&gt;First, install the Serverless Framework globally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; serverless
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create a new directory for your project and navigate to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;serverless-web-app
&lt;span class="nb"&gt;cd &lt;/span&gt;serverless-web-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialize a new Serverless project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;serverless create &lt;span class="nt"&gt;--template&lt;/span&gt; aws-nodejs &lt;span class="nt"&gt;--path&lt;/span&gt; my-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new directory &lt;code&gt;my-service&lt;/code&gt; with a basic Lambda function and other boilerplate files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building the Web Application&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our serverless web application will consist of two main components: a static website hosted on AWS S3, and a Lambda function to handle server-side logic. Let's start with the static website.&lt;/p&gt;

&lt;p&gt;Create a new directory called &lt;code&gt;website&lt;/code&gt; in your project's root directory, and add an &lt;code&gt;index.html&lt;/code&gt; file with some basic HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- website/index.html --&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Serverless Web App&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome to my Serverless Web App!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is a static website hosted on AWS S3.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's configure our Serverless project to deploy this website to an S3 bucket. Open &lt;code&gt;serverless.yml&lt;/code&gt; and add the following resources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# serverless.yml&lt;/span&gt;
&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;serverless-web-app&lt;/span&gt;

&lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;aws&lt;/span&gt;
  &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nodejs18.x&lt;/span&gt;

&lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;Resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;WebsiteBucket&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::S3::Bucket&lt;/span&gt;
      &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;BucketName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:service}-${opt:stage, self:provider.stage}&lt;/span&gt;
        &lt;span class="na"&gt;WebsiteConfiguration&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;IndexDocument&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;index.html&lt;/span&gt;

&lt;span class="c1"&gt;# ... (existing Lambda function configuration)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create an S3 bucket with the name &lt;code&gt;serverless-web-app-[stage]&lt;/code&gt;, where &lt;code&gt;[stage]&lt;/code&gt; is the deployment stage (e.g., &lt;code&gt;dev&lt;/code&gt;, &lt;code&gt;prod&lt;/code&gt;). The bucket will be configured to serve the &lt;code&gt;index.html&lt;/code&gt; file as the website index.&lt;/p&gt;

&lt;p&gt;To deploy the website, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;serverless deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create the S3 bucket and upload the &lt;code&gt;index.html&lt;/code&gt; file. The CLI output will include the website URL, which you can visit in your browser to see the static website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding Server-Side Logic with Lambda&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have our static website set up, let's add some server-side logic using AWS Lambda. We'll build a simple "contact form" functionality, where users can submit their name and email, and we'll save the data to a DynamoDB table.&lt;/p&gt;

&lt;p&gt;First, we need to create the DynamoDB table. Add the following resource to &lt;code&gt;serverless.yml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# serverless.yml&lt;/span&gt;
&lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;Resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# ... (existing resources)&lt;/span&gt;
    &lt;span class="na"&gt;ContactsTable&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::DynamoDB::Table&lt;/span&gt;
      &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:service}-contacts-${opt:stage, self:provider.stage}&lt;/span&gt;
        &lt;span class="na"&gt;AttributeDefinitions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;AttributeName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;id&lt;/span&gt;
            &lt;span class="na"&gt;AttributeType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;S&lt;/span&gt;
        &lt;span class="na"&gt;KeySchema&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;AttributeName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;id&lt;/span&gt;
            &lt;span class="na"&gt;KeyType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;HASH&lt;/span&gt;
        &lt;span class="na"&gt;BillingMode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;PAY_PER_REQUEST&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a DynamoDB table called &lt;code&gt;serverless-web-app-contacts-[stage]&lt;/code&gt; with a hash key &lt;code&gt;id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, let's create the Lambda function that will handle the contact form submissions. In the &lt;code&gt;my-service&lt;/code&gt; directory, create a new file called &lt;code&gt;handler.js&lt;/code&gt; with 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="c1"&gt;// handler.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;AWS&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-sdk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dynamoDb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AWS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DynamoDB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DocumentClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TABLE_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CONTACTS_TABLE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="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;email&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TABLE_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`contact-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;email&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="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;dynamoDb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Contact form submitted successfully&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error submitting contact form&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Lambda function expects a JSON payload with &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt; properties in the event body. It generates a unique ID for the contact, and stores the contact data in the DynamoDB table.&lt;/p&gt;

&lt;p&gt;Finally, we need to configure the Lambda function in &lt;code&gt;serverless.yml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# serverless.yml&lt;/span&gt;
&lt;span class="na"&gt;functions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;handler.handler&lt;/span&gt;
    &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;http&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;submit-contact&lt;/span&gt;
          &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;post&lt;/span&gt;
          &lt;span class="na"&gt;cors&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;CONTACTS_TABLE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:service}-contacts-${opt:stage, self:provider.stage}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sets up an HTTP endpoint (&lt;code&gt;/submit-contact&lt;/code&gt;) that will trigger the Lambda function when a POST request is made. The function will have access to the DynamoDB table name through the &lt;code&gt;CONTACTS_TABLE&lt;/code&gt; environment variable.&lt;/p&gt;

&lt;p&gt;To deploy the Lambda function and DynamoDB table, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;serverless deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The complete &lt;code&gt;severless.yml&lt;/code&gt; file looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;serverless-web-app&lt;/span&gt;

&lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;aws&lt;/span&gt;
  &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nodejs18.x&lt;/span&gt;

&lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;Resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;WebsiteBucket&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::S3::Bucket&lt;/span&gt;
      &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;BucketName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:service}-${opt:stage, self:provider.stage}&lt;/span&gt;
        &lt;span class="na"&gt;WebsiteConfiguration&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;IndexDocument&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;index.html&lt;/span&gt;
    &lt;span class="na"&gt;ContactsTable&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::DynamoDB::Table&lt;/span&gt;
      &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:service}-contacts-${opt:stage, self:provider.stage}&lt;/span&gt;
        &lt;span class="na"&gt;AttributeDefinitions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;AttributeName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;id&lt;/span&gt;
            &lt;span class="na"&gt;AttributeType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;S&lt;/span&gt;
        &lt;span class="na"&gt;KeySchema&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;AttributeName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;id&lt;/span&gt;
            &lt;span class="na"&gt;KeyType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;HASH&lt;/span&gt;
        &lt;span class="na"&gt;BillingMode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;PAY_PER_REQUEST&lt;/span&gt;

&lt;span class="na"&gt;functions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;handler.handler&lt;/span&gt;
    &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;http&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;submit-contact&lt;/span&gt;
          &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;post&lt;/span&gt;
          &lt;span class="na"&gt;cors&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;CONTACTS_TABLE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:service}-contacts-${opt:stage, self:provider.stage}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Connecting the Frontend&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With the backend logic in place, let's update the frontend to allow users to submit the contact form. Open &lt;code&gt;website/index.html&lt;/code&gt; and add a form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- website/index.html --&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Serverless Web App&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome to my Serverless Web App!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is a static website hosted on AWS S3.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Contact Us&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"contact-form"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Email:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;contact-form&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/submit-contact&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&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="s1"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&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;email&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="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="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This adds a simple contact form to the HTML, and includes a JavaScript script that sends a POST request to the &lt;code&gt;/submit-contact&lt;/code&gt; endpoint (which is the Lambda function we deployed earlier) when the form is submitted.&lt;/p&gt;

&lt;p&gt;To deploy the updated website, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;serverless client deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will upload the &lt;code&gt;index.html&lt;/code&gt; file to the S3 bucket again.&lt;/p&gt;

&lt;p&gt;Now, if you visit the website URL and submit the contact form, you should see a success message. You can also check the DynamoDB table in the AWS console to see the submitted contact data.&lt;/p&gt;

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

&lt;p&gt;And there you have it, folks! We've built a serverless web application using AWS Lambda, S3, and DynamoDB. We've covered setting up the development environment, deploying a static website, creating a Lambda function for server-side logic, and connecting the frontend to the backend.&lt;/p&gt;

&lt;p&gt;Serverless architectures offer numerous benefits, including scalability, cost-efficiency, and reduced operational overhead. By leveraging services like AWS Lambda, you can focus on writing code and building features, without worrying about server management. It's like having a team of hardworking elves handling the tedious infrastructure tasks for you.&lt;/p&gt;

&lt;p&gt;Of course, this is just a simple example, and real-world serverless applications can become much more complex. But hey, we all have to start somewhere, right? The principles and services demonstrated in this article provide a solid foundation for building more advanced serverless web applications.&lt;/p&gt;

&lt;p&gt;So, what are you waiting for? Embrace the serverless revolution and start building your next big idea today!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloud</category>
      <category>serverless</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Level Up Your Skills with the Ultimate TypeScript Guide!</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Sat, 05 Aug 2023 08:28:11 +0000</pubDate>
      <link>https://dev.to/ritikbanger/level-up-your-skills-with-the-ultimate-typescript-guide-36ie</link>
      <guid>https://dev.to/ritikbanger/level-up-your-skills-with-the-ultimate-typescript-guide-36ie</guid>
      <description>&lt;h2&gt;
  
  
  🚀 Unleash Your Inner TypeScript Hero: From Zero to Super Coder! 📚🦸‍♂️
&lt;/h2&gt;

&lt;p&gt;Hey fellow developers! Are you ready to become a TypeScript hero? 🔥 Whether you're a seasoned pro looking to expand your skillset or a TypeScript beginner eager to dive into the world of typed JavaScript, I've got some exciting news for you!&lt;/p&gt;

&lt;p&gt;I created the most comprehensive TypeScript guide that will take you from Zero to Hero in no time! 🎓 This guide covers all the essentials, from TypeScript basics to advanced type systems, and even industry-proven best practices for writing clean and scalable TypeScript code. It's a game-changer!&lt;/p&gt;

&lt;h3&gt;
  
  
  🔍 What's Inside?
&lt;/h3&gt;

&lt;p&gt;✅ &lt;strong&gt;TypeScript Basics:&lt;/strong&gt; Master the fundamentals of TypeScript, including types and interfaces.&lt;br&gt;
✅ &lt;strong&gt;Advanced Type System:&lt;/strong&gt; Dive deep into TypeScript's powerful type system and unleash its full potential in your projects.&lt;br&gt;
✅&lt;strong&gt;Best Practices:&lt;/strong&gt; Discover battle-tested techniques for writing maintainable and efficient TypeScript code.&lt;br&gt;
✅ &lt;strong&gt;Real-World Examples:&lt;/strong&gt; Learn by doing with practical examples that reinforce your learning.&lt;/p&gt;

&lt;p&gt;This guide is a treasure trove of knowledge that aims to empower developers of all levels to confidently embrace TypeScript in their workflows. Whether you're building web applications, backend services, or just exploring TypeScript, this guide will equip you with the knowledge and skills to take your projects to new heights.&lt;/p&gt;

&lt;p&gt;So, what are you waiting for? Don't miss out on this amazing opportunity to level up your TypeScript journey!&lt;/p&gt;

&lt;p&gt;Grab the PDF of this incredible &lt;a href="https://www.linkedin.com/posts/ritikbanger_typescript-the-ultimate-guide-activity-7093124357652135936-eXXi?utm_source=li_share&amp;amp;utm_content=feedcontent&amp;amp;utm_medium=g_dt_web&amp;amp;utm_campaign=copy"&gt;guide here&lt;/a&gt; and start your transformation from a TypeScript novice to a TypeScript hero today!&lt;/p&gt;

&lt;p&gt;And let's not keep this treasure to ourselves – share this post with your fellow developers who are as passionate about TypeScript as we are. Let's grow as a community and empower each other in our development journeys!&lt;/p&gt;

&lt;p&gt;Remember, the power of TypeScript is at your fingertips, and with this guide, you'll be unstoppable! Happy coding! 💻💪&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:7093124354753859584" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--JPv3GsrI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://static.licdn.com/aero-v1/sc/h/c45fy346jw096z9pbphyyhdz7" height="457" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:7093124354753859584" rel="noopener noreferrer" class="c-link"&gt;
          Ritik Banger (रितिक बांगड़) 🇮🇳 on LinkedIn: Typescript- the ultimate guide | 14 comments
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Be the TypeScript Hero from Zero! 🔥

I am thrilled to share with you the ultimate TypeScript guide, the final part that will transform you from a TypeScript… | 14 comments on LinkedIn
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--aGQ1YUtN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://static.licdn.com/aero-v1/sc/h/al2o9zrvru7aqj8e1x2rzsrca" width="64" height="64"&gt;
        linkedin.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Boost Your React Performance with useDeferredValue Hook: An Example-driven Guide</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Tue, 04 Apr 2023 16:25:43 +0000</pubDate>
      <link>https://dev.to/ritikbanger/boost-your-react-performance-with-usedeferredvalue-hook-an-example-driven-guide-24f5</link>
      <guid>https://dev.to/ritikbanger/boost-your-react-performance-with-usedeferredvalue-hook-an-example-driven-guide-24f5</guid>
      <description>&lt;p&gt;One of the most popular JavaScript frameworks is React, which provides a number of hooks to make the process of creating online apps easier. &lt;code&gt;UseDeferredValue&lt;/code&gt; hook is one such hook. The speed of the application can be enhanced by using this hook to delay state updates until the user has finished engaging with it. The &lt;code&gt;useDeferredValue&lt;/code&gt; hook, its advantages, and how to use it in your React application will all be covered in this essay.&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%2Ftbblosu6wkenlrh2nfwg.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%2Ftbblosu6wkenlrh2nfwg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of useDeferredValue Hook
&lt;/h2&gt;

&lt;p&gt;The useDeferredValue hook allows you to defer the updates of state changes until the user is done interacting with the application. This approach has several benefits, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improving application performance&lt;/strong&gt;: By deferring state updates, you can avoid unnecessary re-renders, which can significantly improve the performance of your application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Smoother user experience&lt;/strong&gt;: By delaying updates until the user is done interacting with the application, you can provide a smoother user experience, with fewer interruptions or unexpected changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;More efficient rendering&lt;/strong&gt;: With deferred updates, React can batch multiple updates into a single render pass, which can make rendering more efficient and reduce the overall workload on your application.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to use the useDeferredValue Hook?
&lt;/h2&gt;

&lt;p&gt;Utilizing the useDeferredValue method is not too difficult. You need to perform these actions in order to use it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Import the hook&lt;/strong&gt;: To use the useDeferredValue hook, you need to import it from the React library.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create a deferred state&lt;/strong&gt;: You can create a deferred state by calling the useDeferredValue hook and passing in the initial value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use the deferred state&lt;/strong&gt;: You can use the deferred state just like any other state in your application, but React will defer updates to it until the user is done interacting with the application.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is an example of how to use the useDeferredValue hook in a React component:&lt;/p&gt;

&lt;p&gt;Let's assume your React application has a search bar component that enables users to look for things in a list. You want to update the search results in real-time as the user enters a question into the search box, but you don't want to update the search results until the user has completed typing to prevent pointless re-renders. Here's an illustration of how to accomplish this using the useDeferredValue hook:&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useDeferredValue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&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;SearchBar&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setQuery&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deferredQuery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useDeferredValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;timeoutMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;searchResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;item&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="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deferredQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&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="nf"&gt;handleQueryChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleQueryChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;searchResults&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;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;))}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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 this example, we create a new state value called query using the useState hook. We also create a deferred state value called &lt;code&gt;deferredQuery&lt;/code&gt; using the &lt;code&gt;useDeferredValue&lt;/code&gt; hook, and we initialize it with the same initial value as query. We pass an options object to &lt;code&gt;useDeferredValue&lt;/code&gt; to specify a timeout of 500 milliseconds, which means that updates to &lt;code&gt;deferredQuery&lt;/code&gt; will be deferred for at least 500 milliseconds after the last change to query.&lt;/p&gt;

&lt;p&gt;We use the &lt;code&gt;searchResults&lt;/code&gt; variable to filter the items array based on the &lt;code&gt;deferredQuery&lt;/code&gt;. The searchResults variable is re-computed whenever &lt;code&gt;deferredQuery&lt;/code&gt; changes, which will happen after a short delay once the user has finished typing.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;handleQueryChange&lt;/code&gt; function, we update the query state value whenever the user types into the search bar. This will cause &lt;code&gt;deferredQuery&lt;/code&gt; to be updated, but the actual update will be deferred until the user has finished typing for at least 500 milliseconds.&lt;/p&gt;

&lt;p&gt;By using the &lt;code&gt;useDeferredValue&lt;/code&gt; hook in this way, we can provide real-time search results without causing unnecessary re-renders and without making the user interface feel sluggish or unresponsive.&lt;/p&gt;

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

&lt;p&gt;The useDeferredValue hook is a powerful tool for improving the performance and user experience of your React applications. By deferring state updates until the user is done interacting with the application, you can reduce the number of unnecessary re-renders, provide a smoother user experience, and make rendering more efficient. If you are building a React application, consider using the useDeferredValue hook to improve your application's performance and user experience.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Cracking the Interview: Essential Advanced JavaScript Concepts You Need to Know</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Sun, 12 Mar 2023 07:38:04 +0000</pubDate>
      <link>https://dev.to/ritikbanger/cracking-the-interview-essential-advanced-javascript-concepts-you-need-to-know-2e57</link>
      <guid>https://dev.to/ritikbanger/cracking-the-interview-essential-advanced-javascript-concepts-you-need-to-know-2e57</guid>
      <description>&lt;p&gt;JavaScript is an incredibly powerful and popular programming language that is used in a wide range of applications. Whether you are a beginner or an experienced developer, there are many advanced JavaScript concepts that can take your skills to the next level. These concepts can help you write more efficient and maintainable code, as well as improve the performance and functionality of your applications. In this article, we'll explore some of the most important advanced JavaScript concepts that you should know to impress your interviewer and take your skills to the next level.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;They say that learning advanced JavaScript is like unlocking a superpower - except instead of flying or x-ray vision, you can make websites that are way cooler than your competitors'. And let's be real, who needs to fly when you can make your web page elements dance around like nobody's watching? So get ready to level up your coding game and become the superhero of the internet - no cape required!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We'll cover some advanced JavaScript concepts such as web workers, proxies, the Reflect API, symbols, and iterators and generators. Each of these concepts has its own unique benefits and use cases, and mastering them can help you write more efficient, flexible, and maintainable code.&lt;/p&gt;

&lt;p&gt;For example, web workers allow you to execute code in a separate thread, which can improve the performance of your web applications. Proxies let you intercept and modify the behavior of object properties, enabling you to implement advanced features like object privacy. The Reflect API provides a more flexible and consistent way to define object properties, access object properties, and invoke object methods. Symbols allow you to create unique, immutable values that can be used as object keys to implement object privacy. And iterators and generators give you more control over how your objects are iterated, as well as the ability to generate infinite sequences of values.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  1. Web Workers
&lt;/h2&gt;

&lt;p&gt;Web Workers are a powerful feature of JavaScript that allow you to run scripts in the background, without blocking the main thread. Web Workers can help you build more responsive web applications by offloading complex and time-consuming tasks to separate threads.&lt;/p&gt;

&lt;p&gt;Use web workers to execute expensive or long-running tasks in a separate thread, leaving the main thread free to handle user input and updates. This can improve the overall responsiveness and performance of your web application. For example, you could use web workers to perform complex calculations or run data-intensive algorithms without blocking the user interface.&lt;/p&gt;

&lt;p&gt;Use Case: A web-based game that requires complex physics calculations or artificial intelligence algorithms to be run in real-time could use web workers to handle those processes in a separate thread, freeing up the main thread to handle user input and rendering.&lt;/p&gt;

&lt;p&gt;Example 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="c1"&gt;// Create a new web worker&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;worker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;worker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Send a message to the worker&lt;/span&gt;
&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postMessage&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 from main thread!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Receive a message from the worker&lt;/span&gt;
&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;Message received from worker:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&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="c1"&gt;// worker.js&lt;/span&gt;
&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;Message received from main thread:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&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="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postMessage&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 from worker!&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;This example shows how to create and communicate with a web worker using the Worker API.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Proxies
&lt;/h2&gt;

&lt;p&gt;Proxies are a feature of JavaScript that allow you to intercept and modify the behavior of objects and functions. Proxies can be used to implement advanced features like access control, data validation, and dynamic code generation.&lt;/p&gt;

&lt;p&gt;For example, you could use proxies to implement object privacy, ensuring that certain properties or methods cannot be accessed or modified from outside the object.&lt;/p&gt;

&lt;p&gt;Use Case: A JavaScript library or framework that needs to provide a secure and reliable way to manage user data could use proxies to enforce strict access controls on certain properties or methods of the data objects, ensuring that sensitive information is protected.&lt;/p&gt;

&lt;p&gt;Example 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="c1"&gt;// Create a proxy object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;property&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Getting property "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Setting property "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;" to "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Proxy&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Use the proxy object&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ritik&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example shows how to create a proxy object using the Proxy constructor, and how to intercept and modify the behavior of object properties using the get and set traps.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Reflect API
&lt;/h2&gt;

&lt;p&gt;The Reflect API is a powerful set of methods that allow you to interact with JavaScript objects in a more flexible and consistent way. The Reflect API includes methods for creating and manipulating objects, defining and accessing object properties, and invoking object methods.&lt;/p&gt;

&lt;p&gt;This can help you write cleaner and more maintainable code. For example, you could use the Reflect API to define object properties with descriptors, access and set object properties, and invoke object methods.&lt;/p&gt;

&lt;p&gt;Use Case: An e-commerce website that needs to display product data from a third-party API could use the Reflect API to define and manipulate the properties and methods of the product objects in a consistent and flexible way, allowing for easier integration and customization.&lt;/p&gt;

&lt;p&gt;Example 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="c1"&gt;// Create an object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&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="s1"&gt;Ritik&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Define a property using the Reflect API&lt;/span&gt;
&lt;span class="nb"&gt;Reflect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defineProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;writable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Get the value of a property using the Reflect API&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Reflect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;age&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="nx"&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;// Invoke a method using the Reflect API&lt;/span&gt;
&lt;span class="nb"&gt;Reflect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&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="nx"&gt;log&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example shows how to use the Reflect API to define object properties, access object properties, and invoke object methods in a more flexible and consistent way.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Symbols
&lt;/h2&gt;

&lt;p&gt;Symbols are a feature of JavaScript that allow you to create unique, immutable values that can be used as object keys or method names. Symbols can be used to implement advanced features like object privacy, method customization, and interface design.&lt;/p&gt;

&lt;p&gt;This can help you implement object privacy, since symbols cannot be accessed or modified by outside code. For example, you could use symbols to define private methods or properties that can only be accessed from within the object.&lt;/p&gt;

&lt;p&gt;Use Case: A password manager application that needs to store user credentials securely could use symbols to define private methods and properties within the password object, preventing unauthorized access or modification.&lt;/p&gt;

&lt;p&gt;Example 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="c1"&gt;// Define a symbol&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my secret key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Create an object with a private property&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&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="nx"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;secret value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;publicProperty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;public value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Access the private property using the symbol&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publicProperty&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This example shows how to use symbols to create unique, immutable values that can be used as object keys to implement object privacy.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Iterators and Generators
&lt;/h2&gt;

&lt;p&gt;Iterators and Generators are powerful features of JavaScript that allow you to iterate over collections of values in a more flexible and efficient way. Iterators and Generators can be used to implement advanced features like lazy evaluation, infinite sequences, and custom data structures.&lt;/p&gt;

&lt;p&gt;Use iterators and generators to control the iteration of objects and generate sequences of values. This can help you write more efficient and expressive code, as well as enable advanced features like lazy evaluation and infinite sequences. For example, you could use iterators and generators to implement lazy loading of data or generate infinite sequences of random numbers.&lt;/p&gt;

&lt;p&gt;Use Case: A data visualization tool that needs to handle large or complex datasets could use iterators and generators to implement lazy loading of data, allowing only the necessary data to be loaded and displayed at any given time, improving performance and reducing memory usage.&lt;/p&gt;

&lt;p&gt;Example 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="c1"&gt;// Define an iterable object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&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="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&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="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Use the iterator to iterate over the object&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;obj&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Define a generator function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&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="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Use the generator to generate an infinite sequence&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sequence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This example shows how to use iterators to implement custom iteration behavior for objects, and how to use generators to generate infinite sequences of values.&lt;/p&gt;

&lt;p&gt;In Conclusion, having a solid grasp of these advanced JavaScript concepts can help you stand out in a crowded job market and impress your interviewers with your skills and knowledge. By understanding the benefits and use cases of web workers, proxies, the Reflect API, symbols, iterators, and generators, you can write more efficient, flexible, and maintainable code that meets the needs of your users and stakeholders. Whether you're a front-end developer, back-end developer, or full-stack developer, these concepts are essential to take your skills to the next level and achieve success in your career.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Node.js Revealed: A Comprehensive Guide to Its Inner Workings</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Thu, 09 Mar 2023 18:50:17 +0000</pubDate>
      <link>https://dev.to/ritikbanger/nodejs-revealed-a-comprehensive-guide-to-its-inner-workings-209i</link>
      <guid>https://dev.to/ritikbanger/nodejs-revealed-a-comprehensive-guide-to-its-inner-workings-209i</guid>
      <description>&lt;p&gt;Have you ever wanted to build a web application that can handle a massive amount of traffic without slowing down? Or wondered how some of the world's most popular websites, like Netflix and LinkedIn, can handle millions of users at once? The answer lies in Node.js, the innovative tool that's taking the web development world by storm.&lt;/p&gt;

&lt;p&gt;Node.js is a unique platform that can handle multiple requests at once, making it ideal for building high-performance web applications. Node.js is a powerful open-source, cross-platform, JavaScript runtime environment designed for building scalable network applications. It allows developers to write server-side applications in JavaScript. Node.js works on the event-driven, non-blocking I/O model, which makes it ideal for building high-performance, real-time web applications.&lt;/p&gt;

&lt;p&gt;In this article, we'll take a deep dive into the technical workings of Node.js, exploring how it uses single-threaded event looping to handle multiple requests and how it's designed to handle asynchronous operations. So if you're ready to take your web development skills to the next level, join us as we explore the exciting world of Node.js!&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;p&gt;Node.js architecture consists of three main components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The V8 engine&lt;/li&gt;
&lt;li&gt;Libuv&lt;/li&gt;
&lt;li&gt;Node.js core&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The V8 Engine
&lt;/h3&gt;

&lt;p&gt;The V8 engine is an open-source JavaScript engine developed by Google for use in the Chrome browser. It compiles JavaScript code to native machine code, which makes it incredibly fast. Node.js uses the V8 engine to execute JavaScript code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Libuv
&lt;/h3&gt;

&lt;p&gt;Libuv is a multi-platform support library with a focus on asynchronous I/O. It provides Node.js with an event loop, a thread pool, and an API for handling file system operations, TCP/UDP sockets, and other low-level tasks. The event loop is the core of Node.js, and it is responsible for managing all asynchronous operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Node.js Core
&lt;/h3&gt;

&lt;p&gt;The Node.js core is a collection of modules that provide various functionalities, such as network I/O, file system operations, and buffer manipulation. These modules are written in C++ and are exposed to JavaScript via a set of JavaScript APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Loop
&lt;/h2&gt;

&lt;p&gt;The event loop is the most critical component of Node.js. It is responsible for managing all asynchronous I/O operations, including network I/O, file system operations, and timers. The event loop is a single-threaded loop that runs continuously, waiting for events to occur.&lt;/p&gt;

&lt;p&gt;When an event occurs, such as a new connection or a completed I/O operation, the event loop adds the corresponding callback function to a queue. The event loop then continues to run, processing the next event in the queue. When the event loop encounters a callback function in the queue, it executes the function, and the associated I/O operation completes.&lt;/p&gt;

&lt;p&gt;Node.js uses an event-driven, non-blocking I/O model, which means that it can handle a large number of concurrent connections without blocking the event loop. This allows Node.js to be highly scalable and efficient.&lt;/p&gt;

&lt;p&gt;Consider a simple example that shows how asynchronous operations work in Node.js using a simple addition operation and setTimeout function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Start");

setTimeout(() =&amp;gt; {
  console.log("Addition operation");
  const result = 2 + 2;
  console.log("Result:", result);
}, 2000);

console.log("Finish");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a simple addition operation that takes 2 seconds to complete, which we simulate using the setTimeout function. Here's how the flow would look like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Node.js application starts by logging a message to the console indicating that it has started.&lt;/li&gt;
&lt;li&gt;The application calls the setTimeout function to execute the addition operation after a delay of 2 seconds.&lt;/li&gt;
&lt;li&gt;While the setTimeout function is in progress, the event loop continues to process other events in the queue, such as incoming HTTP requests or other code in the application.&lt;/li&gt;
&lt;li&gt;After 2 seconds, the setTimeout function sends an event to the event loop indicating that the delay has ended and the addition operation should be executed.&lt;/li&gt;
&lt;li&gt;The event loop processes the event and executes the corresponding callback function, which performs the addition operation and logs the result to the console.&lt;/li&gt;
&lt;li&gt;Finally, the application logs a message to the console indicating that it has finished.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note that the setTimeout function is an example of an asynchronous function that allows Node.js to continue &lt;br&gt;
processing other events while waiting for the delay to complete. This is a key feature of Node.js that allows it to handle multiple concurrent requests efficiently, making it an ideal choice for building scalable web applications.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Is Node.js single threaded or multi threaded?
&lt;/h2&gt;

&lt;p&gt;Node.js is a single-threaded runtime environment. However, it is important to note that Node.js is also capable of handling multiple concurrent requests and performing I/O operations in parallel.&lt;/p&gt;

&lt;p&gt;Node.js achieves this concurrency by leveraging the event-driven, non-blocking I/O model. When a request is received, Node.js adds it to the event loop's event queue and continues to process other events in the queue. This allows Node.js to handle multiple requests simultaneously, without blocking the event loop or the thread.&lt;/p&gt;

&lt;p&gt;Node.js also utilizes a thread pool to perform CPU-intensive operations asynchronously. When a CPU-bound operation is encountered, Node.js hands it off to the thread pool, which executes the operation in a separate thread. Once the operation is complete, the thread pool returns the result to the event loop, which then executes the corresponding callback function.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Node.js do multiple concurrent operations?
&lt;/h2&gt;

&lt;p&gt;Node.js can perform multiple operations concurrently using its event-driven, non-blocking I/O model. This means that Node.js can handle multiple requests and perform I/O operations in parallel without blocking the event loop.&lt;/p&gt;

&lt;p&gt;When a request or an I/O operation is initiated in Node.js, it is added to the event loop's event queue. The event loop continuously checks the event queue for new events and processes them in the order they were received. However, if an event takes a long time to complete, such as reading a large file from disk or making an HTTP request to an external API, Node.js will not block the event loop while waiting for the operation to complete. Instead, Node.js will add the operation to a separate thread and continue to process other events in the event loop.&lt;/p&gt;

&lt;p&gt;For example, let's say we have a Node.js application that needs to read data from a database and then send an HTTP response to the client. The flow would look something like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Node.js application receives an HTTP request from the client.&lt;/li&gt;
&lt;li&gt;The application initiates a read operation on the database to retrieve the required data.&lt;/li&gt;
&lt;li&gt;The read operation is added to the event loop's event queue.&lt;/li&gt;
&lt;li&gt;While the read operation is in progress, the event loop continues to process other events in the queue, such as processing other incoming HTTP requests or executing other code in the application.&lt;/li&gt;
&lt;li&gt;Once the read operation is complete, the database driver sends an event to the event loop indicating that the operation is done.&lt;/li&gt;
&lt;li&gt;The event loop processes the event and executes the corresponding callback function.&lt;/li&gt;
&lt;li&gt;The callback function retrieves the data from the database and generates an HTTP response to send back to the client.
The response is added to the event loop's event queue, and the event loop sends it back to the client.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this way, Node.js can perform multiple operations concurrently and efficiently without blocking the event loop or the thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Node.js handle memory management?
&lt;/h2&gt;

&lt;p&gt;Node.js uses a garbage collector to automatically manage memory. When a variable or object is no longer needed, the garbage collector will free up the memory it was using. Node.js also uses a technique called "buffering" to handle memory-intensive tasks like reading and writing large files.&lt;/p&gt;

&lt;p&gt;Node.js uses a custom memory allocator called "v8::Malloc", which is optimized for handling small, short-lived objects. Node.js also provides several tools for monitoring memory usage and detecting memory leaks, such as the "heapdump" and "memwatch" modules.&lt;/p&gt;

&lt;p&gt;Node.js uses a generational garbage collector that separates objects into two generations: the young generation and the old generation. Objects in the young generation are frequently garbage collected, while objects in the old generation are garbage collected less frequently. Node.js also provides several tools for tuning the garbage collector, such as the "gc" module and the "--max-old-space-size" flag.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Node.js handle security?
&lt;/h2&gt;

&lt;p&gt;Node.js provides several built-in security features, including the ability to enforce SSL/TLS encryption, perform input validation and sanitization, and prevent cross-site scripting (XSS) and other common web vulnerabilities. Developers can also use third-party security libraries and tools to further enhance the security of their Node.js applications. Node.js uses a sandboxed environment to execute code, which isolates the code from the rest of the system and prevents it from accessing sensitive resources or data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can Node.js be used for machine learning and AI?
&lt;/h2&gt;

&lt;p&gt;Yes, Node.js can be used for machine learning and AI applications through libraries like TensorFlow.js, Brain.js, and Nodebrain. These libraries allow developers to build and train machine learning models using JavaScript and Node.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Node.js handle file I/O?
&lt;/h2&gt;

&lt;p&gt;Node.js uses non-blocking I/O to handle file operations, allowing it to read and write files without blocking the event loop. Node.js also provides a built-in "fs" module for working with the file system, which includes methods for reading, writing, and manipulating files.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Node.js handle long-running processes?
&lt;/h2&gt;

&lt;p&gt;Node.js provides several techniques for handling long-running processes, including child processes, worker threads, and cluster modules. These techniques allow Node.js to handle CPU-intensive tasks without blocking the event loop or slowing down the entire application.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Node.js handle networking?
&lt;/h2&gt;

&lt;p&gt;Node.js provides several built-in modules for handling networking, including the "http" and "net" modules. These modules allow developers to create servers, clients, and sockets for handling HTTP, TCP, and UDP traffic. Node.js also provides a built-in DNS resolver for resolving domain names.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Node.js handle multi-core processors?
&lt;/h2&gt;

&lt;p&gt;Node.js uses a technique called "cluster modules" to handle multi-core processors. This allows Node.js to create multiple processes (or "workers") to handle incoming requests, spreading the load across multiple CPU cores. Node.js also provides a built-in load balancing mechanism to distribute requests evenly across workers.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Node.js handle memory leaks?
&lt;/h2&gt;

&lt;p&gt;Node.js provides several built-in tools for detecting and resolving memory leaks, including the "heapdump" and "memwatch" modules. These tools allow developers to analyze memory usage and identify memory leaks, as well as take action to free up memory when it is no longer needed.&lt;/p&gt;

&lt;p&gt;In conclusion, Node.js is a game-changing tool that's revolutionizing the world of web development. Its ability to handle multiple requests with ease and speed makes it the perfect solution for building high-performance web applications. By using single-threaded event looping, Node.js can handle hundreds, if not thousands of requests simultaneously without slowing down. And with its ability to handle asynchronous operations, Node.js is designed to tackle the challenges of modern web development head-on.&lt;/p&gt;

&lt;p&gt;Whether you're a seasoned developer or just starting out, Node.js is an essential tool to have in your toolkit. With its power and versatility, Node.js can help you build robust and scalable web applications that can handle any amount of traffic.&lt;/p&gt;

&lt;p&gt;So why wait? Start exploring the exciting world of Node.js today and unlock the full potential of modern web development!&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to write Git commit messages like a Pro!</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Mon, 10 Oct 2022 18:59:43 +0000</pubDate>
      <link>https://dev.to/ritikbanger/how-to-write-git-commit-messages-like-a-pro-dn</link>
      <guid>https://dev.to/ritikbanger/how-to-write-git-commit-messages-like-a-pro-dn</guid>
      <description>&lt;p&gt;When a developer go back into time to look for something he has worked on six months ago, many times he does not understand why he made that particular commit and the only reason is that he has not followed the right way to write the commit message. &lt;/p&gt;

&lt;p&gt;There are commit message standards that devs practice around the world, and it is good to follow popular standards so that when you come back after a good amount of time or someone else looks at your commit messages, it would not look like cringe!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The most effective technique to inform other developers of the context of a change is with a well-written Git commit message.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Teams should first decide on a commit message convention that specifies the version control history of the product they are building.&lt;/p&gt;

&lt;p&gt;A great Git commit message should have a proper style, content, and the metadata.&lt;/p&gt;

&lt;p&gt;A known Git commit follows this convention:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;type&amp;gt;(&amp;lt;scope&amp;gt;): &amp;lt;message&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;type&amp;gt;&lt;/code&gt; can be one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;feat&lt;/code&gt; for a new feature.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;refactor&lt;/code&gt; for refactoring production code, e.g. renaming a function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docs&lt;/code&gt; for changes to the documentation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fix&lt;/code&gt; for a bug fix for the user.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;perf&lt;/code&gt; for performance improvements.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;style&lt;/code&gt; for formatting changes, missing semicolons, etc.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;test&lt;/code&gt; for adding missing tests, refactoring tests.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;build&lt;/code&gt; for updating build configuration, development tools or other changes irrelevant to the user.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also add your custom type too, depending on the standards your team follows. The above standards are followed by ESlint team. You can check their commit messages &lt;a href="https://github.com/eslint/eslint/commits/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The scope is optional, and the message part should include a single line statement, not more than 72 characters, to sum up what the commit is for.&lt;/p&gt;

&lt;p&gt;Many developers also use the message as the subject line and add a body too, that is basically the description of the commit, but a one-liner commit message is preferable as long as you can tell about the context (commit what's and why's), if the commit demands a more detailed description that can not be explained in a single line, commit body is always necessary.&lt;/p&gt;

&lt;p&gt;You can also use tools like &lt;a href="https://github.com/Milo123459/glitter"&gt;Glitter&lt;/a&gt; or &lt;a href="http://commitizen.github.io/cz-cli/"&gt;Commitizen&lt;/a&gt; to standardize your commit messages.&lt;/p&gt;

&lt;p&gt;Not only this, You might also wonder that there is a tool that checks for your commit message and pops an error if it does not follow the guidelines. &lt;a href="https://commitlint.js.org/#/"&gt;Commit lint&lt;/a&gt; is one of them. It helps your team adhere to a commit convention.&lt;/p&gt;

&lt;p&gt;Many times, industry experts use their JIRA or Click Up ticket as the commit message so that everything can be link or trace back anytime and the codebase remain maintainable for future developers.&lt;/p&gt;

&lt;p&gt;Some teams also like to add emoji to their commit messages. I have curated a list of emojis and their respective meanings, you can check it out &lt;a href="https://gist.github.com/ritikbanger/649fc5a1694a10dfb274a50ac8d643a4"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;At the end, the important thing is that your commit message should be meaningful and does not confuse your fellow developers or the future developers about what a particular change is up to.&lt;/p&gt;

&lt;p&gt;If you wish to learn more about conventional commits, semantic commits, or the practices that industry follows, here are some resources for you:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.conventionalcommits.org/en/v1.0.0/"&gt;Conventional Commits&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/fteem/git-semantic-commits"&gt;Semantic Commits&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cbea.ms/git-commit/"&gt;How to write a commit message by CBeams&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hacktoberfest 2022 is here. Git repo to contribute, participate, and win SWAG 🤯</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Sat, 01 Oct 2022 07:58:22 +0000</pubDate>
      <link>https://dev.to/ritikbanger/hacktoberfest-2022-is-here-and-we-are-open-for-contribution-32j4</link>
      <guid>https://dev.to/ritikbanger/hacktoberfest-2022-is-here-and-we-are-open-for-contribution-32j4</guid>
      <description>&lt;p&gt;If you already know about open-source and hacktoberfest-&lt;br&gt;
Go to &lt;a href="https://github.com/ritikbanger/Hacktoberfest2022-DSA"&gt;Hacktoberfest2022-DSA&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Event details :
&lt;/h2&gt;

&lt;p&gt;Hacktoberfest is a month-long challenge. It happens every year in the month of October.&lt;/p&gt;

&lt;p&gt;Hacktoberfest is open to everyone, and it marks the celebration of Open Source. It's the biggest Open Source event that encourages newbies to participate in Open Source and create their 1st meaningful PR.&lt;/p&gt;

&lt;p&gt;Hacktoberfest will be hosted by Digital Ocean for the 9th year in a row in partnership with GitHub and other companies.&lt;/p&gt;

&lt;p&gt;Hacktoberfest® is open to everyone in our global community. Whether you’re a developer, student learning to code, event host, or company of any size, you can help drive the growth of open source and make positive contributions to an ever-growing community.&lt;/p&gt;

&lt;p&gt;All backgrounds and skill levels are encouraged to complete the challenge.&lt;/p&gt;

&lt;p&gt;Hacktoberfest is a celebration open to everyone in our global community.&lt;/p&gt;

&lt;p&gt;You can sign up anytime between September 26 and October 31.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can you join?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step - 1:&lt;/strong&gt;&lt;br&gt;
Create a GitHub account, if you don't already have one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step - 2:&lt;/strong&gt;&lt;br&gt;
Register for Hacktoberfest: Navigate to the Hacktoberfest registration page and follow the instructions. But, read the rules carefully before you do, and then sign In using your GitHub credentials.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step - 3:&lt;/strong&gt;&lt;br&gt;
Find good projects worth contributing on the GitHub page. You can type label:hactoberfest is:issue is:open&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You can contribute for DSA related problems here:&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/ritikbanger/Hacktoberfest2022-DSA"&gt;Hacktoberfest2022-DSA&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;You can add a language label of your choice to filter open issues. If you are a beginner and can’t find good issues then there’s tag label:good first issue which filters out issues for beginners who want to contribute.&lt;/p&gt;

&lt;p&gt;Here's something beginner-friendly for you:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.firsttimersonly.com/"&gt;https://www.firsttimersonly.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/mungell/awesome-for-beginners"&gt;https://github.com/mungell/awesome-for-beginners&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step - 4:&lt;/strong&gt;&lt;br&gt;
Submit PRs: Try and submit at least 4 PRs and wait for it to be verified. Make sure to submit quality PRs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hacktoberfest Rules :
&lt;/h2&gt;

&lt;p&gt;To earn your Hacktoberfest tee or tree reward, you must register and make four valid pull requests (PRs) between October 1-31 (in any time zone).&lt;/p&gt;

&lt;p&gt;Pull requests can be made in any participating GitHub or GitLab hosted repository/project. Look for the Hacktoberfest topic to know if a repository/project is participating in Hacktoberfest.&lt;/p&gt;

&lt;p&gt;Pull requests must be approved by a maintainer of the repository/project to count.&lt;/p&gt;

&lt;p&gt;If a maintainer reports your pull request as spam or behaviour not in line with the project’s code of conduct, you will be ineligible to participate.&lt;/p&gt;

&lt;p&gt;This year, the first 40,000 participants who successfully complete the challenge will be eligible to receive a prize.&lt;br&gt;
For your PR to count, it must be:&lt;/p&gt;

&lt;p&gt;Submitted in a public repo, AND the PR is labelled as hacktoberfest-accepted by a maintainer. Or&lt;br&gt;
Submitted in a repo labelled hacktoberfest , AND Merged, OR Approved&lt;/p&gt;

&lt;p&gt;You can opt not to receive a t-shirt and stickers, and you can choose to have a tree planted in your name and help make Hacktoberfest 2022 more carbon-neutral.&lt;/p&gt;

&lt;p&gt;Whether it’s your first or fiftieth pull request, there’s always more to learn! &lt;/p&gt;

&lt;h2&gt;
  
  
  Rules To Contribute To This Repo- &lt;a href="https://github.com/ritikbanger/Hacktoberfest2022-DSA"&gt;Hacktoberfest2022-DSA&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You can write solutions in C/C++/Java/Python for Data Structure and Algorithms.&lt;br&gt;
Follow file naming convention for all your pull requests.&lt;br&gt;
While adding any content, it should be inside its appropriate directory&lt;br&gt;
if there is any problem with inaccurate solution, create an issue!&lt;/p&gt;

&lt;p&gt;Repo contain folders of all the data structures and algorithms topics with some pre-addressed questions. You can add a solution to a question (present in the readme of particular topic folder) that is unanswered.&lt;/p&gt;

&lt;p&gt;You can update existing solution with a better one ( better complexity).&lt;/p&gt;

&lt;p&gt;Participants can even add new questions and solutions of their interest.&lt;/p&gt;

&lt;p&gt;Solve issues raised by other people or yourself.&lt;/p&gt;

&lt;p&gt;Make webpage ( hosted from GitHub readme ) more appealing and updated.&lt;/p&gt;

&lt;p&gt;Well-documented source code with detailed explanations provides a valuable resource for educators and students alike.&lt;br&gt;
Steps For Contribution&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fork this repo&lt;/li&gt;
&lt;li&gt;Star this repo&lt;/li&gt;
&lt;li&gt;Add a file ( txt/cpp ) inside appropriate folder&lt;/li&gt;
&lt;li&gt;commit the code&lt;/li&gt;
&lt;li&gt;Make pull request&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Features
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Beginner-friendly&lt;/li&gt;
&lt;li&gt;Targeted for developers, content writers, and programming enthusiasts.&lt;/li&gt;
&lt;li&gt;Would also help participants who are not familiar with development but are eager to participate in open source.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>hacktoberfest</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>I have created a new npm package to take OTP Inputs, here is the why and how?</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Sun, 14 Aug 2022 11:30:00 +0000</pubDate>
      <link>https://dev.to/ritikbanger/i-have-created-a-new-package-to-take-otp-inputs-here-is-the-why-and-how-eo8</link>
      <guid>https://dev.to/ritikbanger/i-have-created-a-new-package-to-take-otp-inputs-here-is-the-why-and-how-eo8</guid>
      <description>&lt;p&gt;Before starting, please give a star to this project by &lt;a href="https://github.com/ritikbanger/react18-input-otp"&gt;clicking here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A few days back, I was stuck with an #NPM package that is an #OTP input component (&lt;a href="https://www.npmjs.com/package/react-otp-input"&gt;react-otp-input&lt;/a&gt;) module with an average of 65,000 weekly downloads. Quite good numbers. I was facing an issue that the package has a peer dependency of react v16 while I am currently working on v18, but I used the npm i react-otp-input with the -f option and forcefully installed the package. Sooner or later I realised that there is no effects on pressing the "enter" key while working with this package. The requirement was to hit the submit button on keydown. I had an option to put the package component under a form tag and use preventdefault() to fix it but it may generate any side effects too. Looking for the solution to the issue I was facing, I went to the Git repository of the package and found that there are a lot of bugs and issues with the package with no repsonse from the maintainer at all.&lt;/p&gt;

&lt;p&gt;So, I decided to create my own package. I noticed that the package I have installed is licenced under MIT that means I can modify the package and publish it as my own with the modifications. So I took a clone and started working on the project. After lots of challenges and logic, I have published my very own NPM package over the internet with the name &lt;a href="https://www.npmjs.com/package/react18-input-otp"&gt;React18-input-otp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rZ5Fok3e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wuulmbyg0hbl0xamllnf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rZ5Fok3e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wuulmbyg0hbl0xamllnf.gif" alt="Image description" width="600" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have tried different logics and almost tweaked the code to create this out and at the end, I have create an &lt;a href="https://www.npmjs.com/"&gt;NPM account&lt;/a&gt; and followed these steps to publish the package using terminal:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;cd react18-input-otp folder&lt;/li&gt;
&lt;li&gt;Login using npm login&lt;/li&gt;
&lt;li&gt;npm publish&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it and your package will be live.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/react18-input-otp-0nxzx7?file=/src/App.js"&gt;Codesandbox is here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us talk about the package I have created:&lt;/p&gt;

&lt;p&gt;The package is a fully customizable, one-time password (OTP) and phone number with separator input component for the web built with React. Tested on Web, Android, and iOS. This package supports all react versions.&lt;/p&gt;

&lt;p&gt;You can use &lt;strong&gt;npm i react18-input-otp&lt;/strong&gt; to install it in your web app.&lt;/p&gt;

&lt;p&gt;Features of the package:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A fully customisable OTP input package with support of React 18 and Typescript.&lt;/li&gt;
&lt;li&gt;It works great on both react and ionic app. Works like a charm on mobile too.&lt;/li&gt;
&lt;li&gt;You can specify only numeric inputs with inputNum prop.&lt;/li&gt;
&lt;li&gt;Works perfectly with clipboard paste feature on web and mobile.&lt;/li&gt;
&lt;li&gt;The only OTP input package on npm that supports 'enter' key to submit.&lt;/li&gt;
&lt;li&gt;Zero OTP paste issues on Android.&lt;/li&gt;
&lt;li&gt;Easy paste on iOS chrome, read from SMS feature.&lt;/li&gt;
&lt;li&gt;Supports onSubmit optional prop. You do not even need a button to submit.&lt;/li&gt;
&lt;li&gt;You can use this package for phone number inputs too.&lt;/li&gt;
&lt;li&gt;You can use this package for passcode fields too with inputSecure prop.&lt;/li&gt;
&lt;li&gt;You can provide custom css as well as input props to the React18-input-otp.&lt;/li&gt;
&lt;li&gt;0 dependencies.&lt;/li&gt;
&lt;li&gt;Minzipped size only 3.6 kb.&lt;/li&gt;
&lt;li&gt;No open issues.&lt;/li&gt;
&lt;li&gt;No security bugs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you use OTP inputs in your web/mobile apps that are built over #react. Do give it a try and let me know if there is anything I can enhance.&lt;/p&gt;

&lt;p&gt;Don't forget to &lt;a href="https://github.com/ritikbanger/"&gt;follow me&lt;/a&gt; and to give this project a &lt;a href="https://github.com/ritikbanger/react18-input-otp"&gt;star&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Advanced Typescript Series: Generics</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Thu, 04 Aug 2022 08:31:33 +0000</pubDate>
      <link>https://dev.to/ritikbanger/advanced-typescript-series-generics-2444</link>
      <guid>https://dev.to/ritikbanger/advanced-typescript-series-generics-2444</guid>
      <description>&lt;p&gt;As we all know, the power of Typescript and how typescript leverage the power of types to make the codebase robust. I am coming up with a full-fledged series on advanced typescript topics.&lt;/p&gt;

&lt;p&gt;I would be covering Generics, Intersection types, Union types, Utility types, Infer types, conditional types, mapped types, template literal types as well as type guards in this series.&lt;/p&gt;

&lt;p&gt;Today, I am covering the &lt;strong&gt;generics&lt;/strong&gt; in typescript.&lt;/p&gt;

&lt;p&gt;The development in today's world is focused on component reusability so that not only today but the component can be reused tomorrow too. The word 'reusable' itself is self-explanatory, and it makes our codebase flexible.&lt;/p&gt;

&lt;p&gt;But there is a problem that it is not easy to write reusable code, and we simply write code while focusing on a single kind of data or type and later when some new data or type come into picture, we need to write another component rather than using the previous one.&lt;/p&gt;

&lt;p&gt;One another scenario a developer working with typescript and some package for example, say, react hook form faces when he writes a reusable component like, common input component, but he is unable to give type to the register or the control function of the react hook form and typescript infer somethng by itself and the developer will face errors at the end. Here generics can help out. Depending on your field values, you can pass the interface to generic component, and it would do the rest.&lt;/p&gt;

&lt;p&gt;Now, let us understand how generics can help us to solve such problems.&lt;/p&gt;

&lt;p&gt;Generics is the way which allow us to write such flexible code that can work with different data types rather than a single data type. This enforces code reusability and avoid duplication of the code. It also adds an extra layer of abstraction.&lt;/p&gt;

&lt;p&gt;Generics is a fully supported feature of typescript. With generics, one can pass types as parameters to another type, function, class, or interface.&lt;/p&gt;

&lt;p&gt;Generics in TypeScript code can be written in angle brackets &amp;lt;&amp;gt;, in the format , where T represents a passed-in type.  can be read as a generic of type T. The T is also known as the type parameters.&lt;/p&gt;

&lt;p&gt;Let us start with a very basic code to understand this.&lt;br&gt;
We have a function here that takes a string argument and returns a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function returnString(arg: string): string {
  return arg;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another such function doing the same kind of task for number can be,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function returnNumber(arg: number): number {
  return arg;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can create a common function for both the use cases with the help of generics.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function returnGeneric&amp;lt;Type&amp;gt;(arg: Type): Type {
  return arg;
}

const output = returnGeneric&amp;lt;string&amp;gt;("some string goes here");

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

&lt;/div&gt;



&lt;p&gt;Pretty simple right, now let us take another example for API Call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Students = {
  name: string;
  section: string;
}

type Faculty = {
  name: string;
  subject: string;
}

async function genericFetchApi&amp;lt;T&amp;gt;(path: string): Promise&amp;lt;T&amp;gt; {
  const response = await fetch(`https://yourdomain.com/api${path}`);
  return response.json();
}

const studentsData = await fetchApi&amp;lt;Students[]&amp;gt;('/students')
const facultyData = await fetchApi&amp;lt;Faculty[]&amp;gt;('/faculty')

export {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, this is how we can consume a common function with different type of arguments and different return type for different use cases.&lt;/p&gt;

&lt;p&gt;On moving further, there is Something in generics that you should have an idea of. It is generic type constraint. Using constraints is basically to put restrictions. JavaScript revolves around objects, and some scenario exists where you expect something and receive something else from the backend. In such cases or other, these constraints are helpful. Only specific types must be permitted to be given into the generic via a generic type argument. You can impose restrictions on your parameter to offer an extra level of precision and narrowing the types to your generic.&lt;/p&gt;

&lt;p&gt;You can use something like .&lt;br&gt;
'extends Record' is known as generic type constraint. It enables you to define that the type that follows the extends keyword must be assignable to your generic type. Record in this context denotes an object with strings as the key type and any as the value type. You can replace the 'any' with the value type of your object. Also, &lt;em&gt;&lt;strong&gt;Any valid TypeScript type may be extended by your type parameter&lt;/strong&gt;&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const genericFunc = &amp;lt;T extends number&amp;gt;(x: T) =&amp;gt; x;
const stringTest = genericFunc('a'); //  Argument of type 'string' is not assignable to parameter of type 'number'
const numberTest = genericFunc(99); //passes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fe3z2cozyyfycniyre5yu.jpg" 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%2Fe3z2cozyyfycniyre5yu.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;End Note&lt;/strong&gt;: Generics will make your codebase robust, and they really help in some way or other. You can anytime start using generics. Let me know in the discussions if you like this article or not, also, if you have any suggestions, do let me know.&lt;/p&gt;

&lt;p&gt;I would be coming out with the next learning in this series very soon. Stay tuned and follow me for more updates.&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to setup Infinite Scroll in your react component?</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Sun, 03 Jul 2022 18:07:36 +0000</pubDate>
      <link>https://dev.to/ritikbanger/how-to-setup-infinite-scroll-in-your-react-component-3mop</link>
      <guid>https://dev.to/ritikbanger/how-to-setup-infinite-scroll-in-your-react-component-3mop</guid>
      <description>&lt;p&gt;Infinite scroll is the thing of today. Unlike pagination with pages, people prefer scrolling infinitely to get more and more data, Facebook and Instagram are very fine example leveraging the power of Infinite Scroll.&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%2Fz20zknk0dcqfj36gebyb.gif" 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%2Fz20zknk0dcqfj36gebyb.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have come across an infinite scroll package which is quite popular and many developers across the world are using this fine &lt;a href="https://www.npmjs.com/package/react-infinite-scroll-component" rel="noopener noreferrer"&gt;npm package&lt;/a&gt; in their web apps.&lt;/p&gt;

&lt;p&gt;This npm package is of small size, i.e., 177kb. It takes a few props, which are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The first prop is &lt;strong&gt;dataLength&lt;/strong&gt; which is the length of the data.&lt;/li&gt;
&lt;li&gt;After dataLength, we have the &lt;strong&gt;next&lt;/strong&gt; prop. This is one of the most important prop, We pass a loadMoreData function to it that triggers some action which fetches the next data. This fetched data is passed as children to the InfiniteScroll component, and note that the data should contain previous data too. So, it is basically, [newData, ...PreviousData]&lt;/li&gt;
&lt;li&gt;The third prop is &lt;strong&gt;hasMore&lt;/strong&gt;, it is clear by its name that it takes a boolean value and tells InfiniteScroll whether to call next function on reaching the bottom or shows an endMessage to the user if next is not called i.e., hasMore is false.&lt;/li&gt;
&lt;li&gt;The fourth prop, &lt;strong&gt;loader&lt;/strong&gt; takes an element which is shown as a fallback to show while the component waits for the next load of data.&lt;/li&gt;
&lt;li&gt;The fifth prop is &lt;strong&gt;endMessage&lt;/strong&gt;, which tells the user that there is no more data and hasMore is false.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can check out a codesandbox illustrating InfiniteScroll &lt;a href="https://codesandbox.io/s/yk7637p62z" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Don't forget to give your data as children in between the opening and closing tag of InfiniteScroll.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;InfiniteScroll
  dataLength={dataItems.length}
  next={LoadMoreData}
  hasMore={true}
  loader={&amp;lt;h4&amp;gt;Loading...&amp;lt;/h4&amp;gt;}
  endMessage={
    &amp;lt;p style={{ textAlign: 'center' }}&amp;gt;
      &amp;lt;b&amp;gt;You have seen all the data&amp;lt;/b&amp;gt;
    &amp;lt;/p&amp;gt;
  }
&amp;gt;
  {dataItems}
&amp;lt;/InfiniteScroll&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are few &lt;a href="https://www.npmjs.com/package/react-infinite-scroll-component" rel="noopener noreferrer"&gt;other props&lt;/a&gt; too that are useful in different scenarios but the above five props are sufficient to have a cool infinite scroll on board.&lt;/p&gt;

&lt;p&gt;Note: If you are working with InfiniteScroll inside a popup or a modal, then &lt;strong&gt;ScrollableTarget&lt;/strong&gt; prop would help. You need to create a parent div of InfiniteScroll with an id attribute and give this id as an argument to ScrollableTarget prop. A codesandbox is available &lt;a href="https://codesandbox.io/s/r7rp40n0zm" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you came across any other Infinite Scroll package then do mention it in the discussion and I would be happy to give it a try.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Event Emitter with Typescript- Advanced Usage</title>
      <dc:creator>Ritik Banger</dc:creator>
      <pubDate>Sun, 19 Jun 2022 08:02:58 +0000</pubDate>
      <link>https://dev.to/ritikbanger/event-emitter-with-typescript-advanced-usage-328c</link>
      <guid>https://dev.to/ritikbanger/event-emitter-with-typescript-advanced-usage-328c</guid>
      <description>&lt;p&gt;Passing functional props to great grand child components and then invoking the function on some data changes is hectic and do side effects. Also, such passing down of props is not a very good way to write react or typescript code.&lt;/p&gt;

&lt;p&gt;Here comes the event emitter. Event Emitter is a common term if you are working with an Angular or NodeJs project but when it comes to react, developers hardly heard this term. So, let me straight down to what event emitter is, what it do, how it do?&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an Event Emitter?
&lt;/h3&gt;

&lt;p&gt;An event emitter is a code pattern that listens to a named event, fires (or calls) a callback function, then emits that event with a value. Sometimes this is referred to as a “pub/sub (publisher-subscriber)” model, or listener.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why do we need Event Emitter?
&lt;/h3&gt;

&lt;p&gt;They are very useful when you have some function that needs to execute “whenever this other thing happens”, without requiring that function to finish or even work for that matter.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Event Emitter do?
&lt;/h3&gt;

&lt;p&gt;Event Emitter solve complex business solutions that requires to invoke certain functionality on the basis of change in some other thing.&lt;/p&gt;

&lt;p&gt;A complex Use Case: I have a sale post component in which I can post some title, price and images and other users can offer me a price to buy the item. Now, I wish to list down all the offers in the form of comments below the post, then this can be done with the help of backend.&lt;br&gt;
If somebody gives an offer then save the offer detail in the offer table and also save a comment &lt;code&gt;User.name gives $offer&lt;/code&gt; in the comments table in database.&lt;/p&gt;

&lt;p&gt;Now there exists an entry for every offer in the comments table but the comment component on the frontend does not know this. Here, event emitter can helps. Whenever somebody gives an offer, emit an event to re-fetch the comments and thus a complex problem to show offer comments as soon as an offer is given is solved.&lt;/p&gt;
&lt;h3&gt;
  
  
  How Event Emitter works?
&lt;/h3&gt;

&lt;p&gt;Now, let us jump to the coding part. I know it may be complex for some users to understand this as it is advanced react, but still in many cases it helps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; We will create a file eventEmitter.ts that will be the core part of our event emitter.&lt;/p&gt;

&lt;p&gt;In this we will have an enum EventType that will keep track of events similar to action types set as variables in redux.&lt;/p&gt;

&lt;p&gt;Then we have a complex object eventEmitter, Many of you would wonder that I have created an object in which I am executing functions, quite complex but cool.&lt;/p&gt;

&lt;p&gt;We have an events properties that is basically another object that will list the event name with their respective callbacks. It is readonly as we do not wish it to be changed by an outside function. It is similar to the use of &lt;code&gt;private&lt;/code&gt; access modifier in classes.&lt;/p&gt;

&lt;p&gt;After that we have dispatch function that will dispatch the event and call the callback for every event.&lt;/p&gt;

&lt;p&gt;Subscribe will subscribe the event with a specific callback and Unsubscribe will unsubscribe the event to avoid unnecessary event calls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export enum EventType {
  REFETCH_COMMENT = 'refetchComment',
}

/**
 * Event emitter to subscribe, dispatch, and unsubscribe to events.
 */
export const eventEmitter: {
  readonly events: Record&amp;lt;string, (() =&amp;gt; void)[]&amp;gt;
  dispatch(eventType: EventType, uniqueSuffix: string | number): void
  subscribe(eventType: EventType, uniqueSuffix: string | number, callback: () =&amp;gt; void): void
  unsubscribe(eventType: EventType, uniqueSuffix: string | number): void
  getEventKey(eventType: EventType, uniqueSuffix: string | number): string
} = {
  //This is event object to store events.
  events: {},
  //Internal function to get event name from type and suffix
  getEventKey(eventType: EventType, uniqueSuffix: string | number) {
    return `${eventType} ${uniqueSuffix}`
  },
  //This will dispatch the event and call the callback for every event.
  dispatch(event, uniqueSuffix) {
    const eventName = this.getEventKey(event, uniqueSuffix)
    if (!this.events[eventName]) return
    this.events[eventName].forEach((callback: () =&amp;gt; void) =&amp;gt; callback())
  },
  //This will subscribe the event with a specific callback
  subscribe(event, uniqueSuffix, callback) {
    const eventName = this.getEventKey(event, uniqueSuffix)
    if (!this.events[eventName]) this.events[eventName] = []
    if (!this.events[eventName]?.includes(this.events[eventName][0])) this.events[eventName]?.push(callback)
  },
  //This will unsubscribe the event to avoid unnecessary event calls
  unsubscribe(event, uniqueSuffix) {
    const eventName = this.getEventKey(event, uniqueSuffix)
    if (!this.events[eventName]) return
    delete this.events[eventName]
  },
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; Now, In the offer component where we would be sending offers, we will dispatch the events and unsubscribe the events after dispatch like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;eventEmitter.dispatch(EventType.REFETCH_COMMENT, uniqueSuffix)
eventEmitter.unsubscribe(EventType.REFETCH_COMMENT, uniqueSuffix)

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Here, we would be subscribing the event with a callback in the comments component that will re-fetch the comments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; eventEmitter.subscribe(EventType.REFETCH_COMMENT, uniqueSuffix, () =&amp;gt; fetchLatestPostComments())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, fetchLatestPostComments is the function that will refetch the comments from the backend.&lt;/p&gt;

&lt;p&gt;This is how we have solved a complex business problem with the help of event emitters.&lt;/p&gt;

&lt;p&gt;Though, with day-to-day developments, mutation comes into existence and these complex tasks can be performed by packages like React-Query too.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
