<?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: Pratap Sharma</title>
    <description>The latest articles on DEV Community by Pratap Sharma (@pratap2210).</description>
    <link>https://dev.to/pratap2210</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%2F636657%2F8b79e428-ccc9-41b7-88e6-73bd8b691614.jpeg</url>
      <title>DEV Community: Pratap Sharma</title>
      <link>https://dev.to/pratap2210</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pratap2210"/>
    <language>en</language>
    <item>
      <title>My Journey as a Lead Engineer at Rippey AI</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Wed, 19 Jun 2024 08:04:47 +0000</pubDate>
      <link>https://dev.to/pratap2210/my-journey-as-a-lead-engineer-at-rippey-ai-1f77</link>
      <guid>https://dev.to/pratap2210/my-journey-as-a-lead-engineer-at-rippey-ai-1f77</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Three months ago, I was promoted to Lead Engineer at Rippey AI, a company at the forefront of revolutionizing logistics and document processing through artificial intelligence. The journey has been both challenging and rewarding, filled with invaluable learning experiences and professional growth. Here, I’ll share my experiences, the challenges I faced, the lessons I learned, and what keeps me motivated in this dynamic role.&lt;/p&gt;

&lt;h2&gt;
  
  
  Transitioning to Lead Engineer
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Promotion
&lt;/h3&gt;

&lt;p&gt;After seven months as a Senior Software Engineer, I was thrilled to take on the responsibilities of a Lead Engineer. My new role involved leading a team of 11 engineers in the design, development, and deployment of Rippey.ai, our flagship AI solution for automating logistics and supply chain processes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initial Challenges
&lt;/h3&gt;

&lt;p&gt;Stepping into a leadership role brought its set of challenges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Team Management:&lt;/strong&gt; Managing a diverse team required honing my interpersonal and communication skills. Balancing different personalities and working styles was crucial to fostering a collaborative environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintaining Quality:&lt;/strong&gt; Overseeing code reviews, architectural designs, and system integrations meant ensuring that our solutions met high-quality standards consistently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Technical Guidance:&lt;/strong&gt; Providing technical guidance and mentorship to junior engineers was essential for their growth and the team's success.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Responsibilities
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Leading Development
&lt;/h3&gt;

&lt;p&gt;My primary responsibility was to lead the development, implementation, and deployment of Rippey.ai. This involved:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Architectural Oversight:&lt;/strong&gt; Designing robust architectures that could handle complex logistics automation tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Reviews:&lt;/strong&gt; Conducting thorough code reviews to maintain code quality and ensure best practices were followed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System Integrations:&lt;/strong&gt; Overseeing the integration of various systems and services to ensure seamless operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Mentorship
&lt;/h3&gt;

&lt;p&gt;Mentoring junior engineers has been one of the most rewarding aspects of my role. Guiding them through technical challenges, helping them grow professionally, and seeing them succeed has been incredibly fulfilling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Major Projects and Achievements
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Rippey.ai Development
&lt;/h3&gt;

&lt;p&gt;Leading the team in the development of Rippey.ai was a monumental task. We aimed to create an AI-driven solution that could revolutionize logistics and supply chain management. Key aspects included:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;System Stability and Scalability:&lt;/strong&gt; Implementing RabbitMQ to optimize inter-service communication, enhancing system stability and scalability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Advanced Security:&lt;/strong&gt; Designing and implementing Attribute-Based Access Control (ABAC) to replace traditional single-credential login systems, thereby enhancing security.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Enhancing Inter-Service Communication
&lt;/h3&gt;

&lt;p&gt;One of my significant contributions was the implementation of RabbitMQ. This not only improved system stability and scalability but also mitigated potential system breaks, ensuring smoother operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Security Overhaul
&lt;/h3&gt;

&lt;p&gt;Transitioning from single-credential login to ABAC was another critical project. This change provided a more granular and flexible approach to access control, significantly enhancing our system's security.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Leadership and Communication
&lt;/h3&gt;

&lt;p&gt;Effective communication is key to successful leadership. I learned the importance of clear, concise communication, whether in giving instructions, providing feedback, or resolving conflicts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Balancing Technical and Managerial Roles
&lt;/h3&gt;

&lt;p&gt;Finding the right balance between technical responsibilities and managerial duties was crucial. It required efficient time management and the ability to delegate tasks appropriately.&lt;/p&gt;

&lt;h3&gt;
  
  
  Continuous Learning
&lt;/h3&gt;

&lt;p&gt;The tech industry is ever-evolving, and staying updated with the latest trends and technologies is essential. Continuous learning and adapting to new tools and methodologies have been vital to my role.&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation and Future Goals
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Driving Innovation
&lt;/h3&gt;

&lt;p&gt;The opportunity to drive innovation in logistics automation keeps me motivated. Knowing that our work at Rippey AI has the potential to transform industries is incredibly inspiring.&lt;/p&gt;

&lt;h3&gt;
  
  
  Team Success
&lt;/h3&gt;

&lt;p&gt;Seeing my team members grow and succeed is a significant source of motivation. Their achievements reflect the collaborative efforts and collective success of the team.&lt;/p&gt;

&lt;h3&gt;
  
  
  Personal Growth
&lt;/h3&gt;

&lt;p&gt;This role has provided immense opportunities for personal and professional growth. I aim to continue developing my skills, taking on new challenges, and contributing to the success of Rippey AI.&lt;/p&gt;

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

&lt;p&gt;The journey as a Lead Engineer at Rippey AI has been an incredible experience. The challenges faced, lessons learned, and the motivation derived from driving innovation and team success have been instrumental in my growth. As I look forward to the future, I am excited about the possibilities that lie ahead and remain committed to contributing to Rippey AI's mission of revolutionizing logistics automation.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;For more about my work and experiences, visit my &lt;a href="https://pratapsharma.io"&gt;website&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>journey</category>
      <category>leadership</category>
      <category>career</category>
      <category>grow</category>
    </item>
    <item>
      <title>How to Provide Better Task Estimations for Your Team?</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Wed, 19 Jun 2024 07:59:55 +0000</pubDate>
      <link>https://dev.to/pratap2210/how-to-provide-better-task-estimations-for-your-team-3onm</link>
      <guid>https://dev.to/pratap2210/how-to-provide-better-task-estimations-for-your-team-3onm</guid>
      <description>&lt;p&gt;Providing accurate task estimations is a perennial challenge for software development teams. Inaccurate estimations can lead to missed deadlines, budget overruns, and undue stress for your team. As a lead engineer or manager, there are several strategies you can implement to help your team improve their estimations. This blog will explore these strategies in detail, drawing from real-world experiences and best practices in the industry.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understand and Compensate for Bias
&lt;/h2&gt;

&lt;p&gt;One of the first steps in improving task estimations is to help your team recognize and adjust for their own biases. Estimations can be skewed due to over-optimism or excessive pessimism. Encourage your team members to reflect on their past estimations and compare them with the actual time taken to complete tasks. This can help them identify patterns in their biases and make more informed adjustments in future estimates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Broader Value Ranges
&lt;/h2&gt;

&lt;p&gt;Rather than pushing for precise estimates, encourage the use of broader value ranges. For example, distinguishing between tasks that will take 3 weeks versus 4 weeks can be challenging and often unnecessary. Instead, categorizing tasks into broader timeframes such as half-days, days, weeks, and months can provide sufficient detail for planning purposes without demanding unrealistic precision.&lt;/p&gt;

&lt;h2&gt;
  
  
  Group Estimations and Discussions
&lt;/h2&gt;

&lt;p&gt;Conducting group estimations can be highly effective. When team members provide their estimates individually and then discuss any significant discrepancies, it leads to more accurate and well-rounded estimations. Group discussions allow team members to consider different perspectives and factors they might not have initially accounted for, leading to more accurate and realistic estimates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Agile and Continuous Delivery
&lt;/h2&gt;

&lt;p&gt;Agile methodologies have transformed the approach to software delivery and task estimation. Agile promotes the idea that software is never truly "done" and prioritizes continuous delivery of valuable features. By using Fibonacci sequences (e.g., 1, 2, 3, 5, 8, etc.) for estimations, teams can better gauge the complexity and effort required for each task. Tasks estimated at 8 or higher are typically split into smaller, more manageable pieces.&lt;/p&gt;

&lt;p&gt;Continuous backlog grooming and prioritization ensure that the most valuable tasks are always addressed first, without the need for rigid time commitments. Implementing automated tests and quality gates for each check-in helps maintain a robust and reliable codebase, facilitating continuous delivery.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ponderation Sheets for Legacy Projects
&lt;/h2&gt;

&lt;p&gt;For teams working on legacy software, where changes can have unpredictable consequences, creating a "ponderation sheet" can be useful. This involves adjusting initial estimates based on known complexities and historical data. For example, touching a specific module might multiply the estimate by 1.5, 2, or even 3 times, or add a fixed number of days based on past experiences. While these adjustments are somewhat arbitrary, they can significantly improve estimation accuracy in complex environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  T-Shirt Sizing for Younger Projects
&lt;/h2&gt;

&lt;p&gt;In younger projects, using T-shirt sizing (small, medium, large) or points can be effective. This method translates to broader timeframes, such as whether a task will take a day, a week, or an entire sprint. Tasks that require more than a sprint should be broken down into smaller, more manageable stories that can be completed within a week.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retrospectives
&lt;/h2&gt;

&lt;p&gt;Regular retrospectives are crucial for improving estimations over time. During retrospectives, teams can review what worked well and what didn’t in previous sprints, including the accuracy of their estimations. This reflection helps teams continuously refine their estimation processes and adapt to changing circumstances.&lt;/p&gt;

&lt;h2&gt;
  
  
  Consider the Working Environment
&lt;/h2&gt;

&lt;p&gt;The working environment can significantly impact task estimations. For instance, bureaucratic environments may slow down progress, making tasks that would take a few days in a startup take much longer. Encourage your team to account for these environmental factors when making their estimates. This awareness comes with experience and should be continually evaluated and adjusted.&lt;/p&gt;

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

&lt;p&gt;Improving task estimations is an ongoing process that requires a combination of self-awareness, collaboration, agile practices, and continuous reflection. By implementing these strategies, you can help your team provide more accurate and realistic estimations, leading to better project planning and execution. Remember, the goal is not to achieve perfect precision but to make informed, realistic estimations that guide your team effectively.&lt;/p&gt;

&lt;p&gt;By fostering a culture of continuous improvement and learning, your team can navigate the complexities of software development with greater confidence and success.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>career</category>
      <category>softwaredevelopment</category>
      <category>community</category>
    </item>
    <item>
      <title>Understanding Proxies: The Differences Between Forward and Reverse Proxies</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Mon, 17 Apr 2023 08:55:07 +0000</pubDate>
      <link>https://dev.to/pratap2210/understanding-proxies-the-differences-between-forward-and-reverse-proxies-kjj</link>
      <guid>https://dev.to/pratap2210/understanding-proxies-the-differences-between-forward-and-reverse-proxies-kjj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Proxy servers are a popular tool for managing and securing network traffic. A proxy server acts as an intermediary between clients and servers on a network. It can intercept and modify requests and responses, adding an extra layer of control and security to network traffic. There are two primary types of proxy servers: forward and reverse proxies. In this article, we’ll take a detailed look at both of these types of proxies.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Forward Proxy?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8q2b1dsax8gyych306lq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8q2b1dsax8gyych306lq.jpeg" alt="Image description" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A forward proxy, also known as an outgoing proxy, is a server that sits between a client and the internet. When a client requests access to a resource on the internet, the request is first sent to the forward proxy server. The proxy server then forwards the request to the internet on behalf of the client, and returns the response to the client.&lt;/p&gt;

&lt;p&gt;A forward proxy is typically used to provide anonymity to clients by hiding their IP address. The proxy server acts as an intermediary, so the destination server only sees the IP address of the proxy server, not the client’s IP address. This can be useful for clients who want to access resources on the internet without revealing their location or identity.&lt;/p&gt;

&lt;p&gt;Forward proxies can also be used to enforce content filtering policies. For example, a company may use a forward proxy to prevent employees from accessing certain websites during work hours. The proxy server can be configured to block requests to specific websites or categories of websites, such as social media or gambling sites.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Reverse Proxy?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8l97sgieiyrgmv2oxwn.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8l97sgieiyrgmv2oxwn.jpeg" alt="Image description" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A reverse proxy, also known as an incoming proxy, is a server that sits between the internet and a server. When a client requests access to a resource on a server, the request is first sent to the reverse proxy server. The proxy server then forwards the request to the server on behalf of the client, and returns the response to the client.&lt;/p&gt;

&lt;p&gt;A reverse proxy is typically used to improve performance, scalability, and security of a server by caching content, load balancing traffic, and providing a single point of entry for incoming requests. By caching frequently accessed content, a reverse proxy can reduce the load on the server and improve response times for clients. By load balancing traffic across multiple servers, a reverse proxy can prevent overloading and ensure that all clients receive a fast and responsive service. By providing a single point of entry for incoming requests, a reverse proxy can improve security by reducing the number of entry points into the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is Nginx called a “reverse” proxy?
&lt;/h2&gt;

&lt;p&gt;Nginx is often referred to as a "reverse" proxy because it operates in the opposite direction of a traditional forward proxy. A forward proxy is a server that sits between a client and the internet, forwarding requests from the client to the destination server. On the other hand, a reverse proxy is a server that sits between the internet and a server, forwarding requests from the internet to the server.&lt;/p&gt;

&lt;p&gt;Nginx is a web server and load balancer that can act as a reverse proxy. When acting as a reverse proxy, Nginx receives incoming requests from the internet on behalf of a server and forwards those requests to the server. Nginx can also be configured to load balance incoming requests across multiple servers to improve performance and scalability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Forward and Reverse Proxies
&lt;/h2&gt;

&lt;p&gt;Forward and reverse proxies provide a number of benefits for network management and security:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Anonymity:&lt;/strong&gt; Forward proxies can be used to provide anonymity to clients by hiding their IP address from the destination server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Content filtering:&lt;/strong&gt; Forward proxies can be used to enforce content filtering policies, blocking requests to specific websites or categories of websites.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Reverse proxies can improve performance by caching frequently accessed content and load balancing traffic across multiple servers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; Reverse proxies can improve scalability by distributing traffic across multiple servers, preventing overloading and ensuring that all clients receive a fast and responsive service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; Reverse proxies can improve security by providing a single point of entry for incoming requests and reducing the number of entry points into the server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility:&lt;/strong&gt; Proxies can be configured and customized to meet the specific needs of a network, allowing administrators to control and manage traffic in a way that is tailored to their requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Forward Proxy vs Reverse Proxy
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Forward Proxy&lt;/th&gt;
&lt;th&gt;Reverse Proxy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Avoid browsing restrictions&lt;/td&gt;
&lt;td&gt;Load Balancing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Improve security by reducing the number of entry points into a network&lt;/td&gt;
&lt;td&gt;Protects from DDos attacks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Protects user identity online&lt;/td&gt;
&lt;td&gt;Cache static contents&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Blocks access to certain contents&lt;/td&gt;
&lt;td&gt;Encrypts and Decrypts SSL communications&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;Forward and reverse proxies are powerful tools for managing and securing network traffic. They provide a number of benefits, including anonymity, content filtering, performance, scalability, security, and flexibility. By using proxies, administrators can gain greater control over their network traffic, improving performance, security, and overall network management. Whether you need to provide anonymous access to the internet, enforce content filtering policies, or improve server performance and scalability, proxies are a valuable tool for achieving your goals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read more articles
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/why-is-system-design-important/"&gt;Why is System Design Important?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/closure-in-javascript/"&gt;Closures in JavaScript: Understanding and Applications&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/architecture-of-food-delivery-app/"&gt;Architecture and Design Principle for Online Food Delivery System&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;💌 If you'd like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please let me know if there's anything else I can add or if there's any way to improve the post. Also, leave a comment if you have any feedback or suggestions.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>productivity</category>
      <category>cloud</category>
    </item>
    <item>
      <title>useMemo - React Hooks Series</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Mon, 28 Jun 2021 02:57:36 +0000</pubDate>
      <link>https://dev.to/pratap2210/usememo-react-hooks-series-2074</link>
      <guid>https://dev.to/pratap2210/usememo-react-hooks-series-2074</guid>
      <description>&lt;p&gt;Welcome back to the series of &lt;strong&gt;React Hooks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Series path&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/use-state-react-hook"&gt;useState&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/use-effect-react-hook"&gt;useEffect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;useMemo&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main objective will be to explore how re-rendering works in React, why that is a critical factor for React applications, and how the &lt;code&gt;useMemo&lt;/code&gt; hook can leverage it to build a performance boost in your applications. We will also see when &lt;code&gt;useMemo&lt;/code&gt; can cause performance issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;useMemo&lt;/code&gt; hook?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useMemo()&lt;/code&gt; is a built-in React hook that accepts two arguments — a function compute that computes a result and the dependencies array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;memoizedResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;compute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us try to get the essence of the &lt;code&gt;useMemo&lt;/code&gt; hook. During the first rendering, &lt;code&gt;useMemo(compute, dependencies)&lt;/code&gt; invokes the &lt;code&gt;compute&lt;/code&gt; function, which is the first argument, memoizes the calculation result, and returns it to the component.&lt;/p&gt;

&lt;p&gt;If during the next renderings the dependencies change, then &lt;code&gt;useMemo()&lt;/code&gt; invokes &lt;code&gt;compute&lt;/code&gt;, memoizes the new value, and returns it. But if dependencies don't change during re-rendering, then &lt;code&gt;useMemo()&lt;/code&gt; doesn't invoke &lt;code&gt;compute&lt;/code&gt; but returns the &lt;strong&gt;memoized&lt;/strong&gt; value.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Use &lt;code&gt;useMemo&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;The basic and first step is to write the code first and then revisit it to see if any optimization can be made.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Implementing &lt;code&gt;useMemo&lt;/code&gt; too often in an application can harm the performance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When looking to implement &lt;code&gt;useMemo&lt;/code&gt;, you can check with profiling tools in the debugger to identify any expensive performance issues. By expensive, I mean if the application is consuming a lot of resources (memory). It makes sense to memoize with &lt;code&gt;useMemo&lt;/code&gt; only if you declare a large number of variables in a function at render.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;A component &lt;code&gt;&amp;lt;ComputeFactorial /&amp;gt;&lt;/code&gt; calculates the factorial of a number introduced into an input field.&lt;/p&gt;

&lt;p&gt;Here’s a possible implementation of &lt;code&gt;&amp;lt;ComputeFactorial /&amp;gt;&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ComputeFactorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setNumber&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="mi"&gt;1&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;inc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setInc&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="mi"&gt;0&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;factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getFactorialOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      Factorial of
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Number&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="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      is &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setInc&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Force Re-render&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getFactorialOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;getFactorialOf(n) is called!&lt;/span&gt;&lt;span class="dl"&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;getFactorialOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time the input value is changed, the factorial is calculated using&lt;code&gt;getFactorialOf(n)&lt;/code&gt; and '&lt;code&gt;getFactorialOf(n)&lt;/code&gt; is called!' is logged to console.&lt;/p&gt;

&lt;p&gt;On the other hand, each time you click the &lt;code&gt;Force Re-render&lt;/code&gt; button, the &lt;code&gt;inc&lt;/code&gt; state value is updated. Updating the &lt;code&gt;inc&lt;/code&gt; state value triggers &lt;code&gt;&amp;lt;ComputeFactorial /&amp;gt;&lt;/code&gt; component to re-render. When a component is re-rendered, the factorial is recalculated again — '&lt;code&gt;getFactorialOf(n)&lt;/code&gt; is called!' is logged to console.&lt;/p&gt;

&lt;p&gt;How can you memoize the factorial calculation when the component re-renders? Welcome &lt;code&gt;useMemo()&lt;/code&gt; hook!&lt;/p&gt;

&lt;p&gt;By using &lt;code&gt;useMemo(() =&amp;gt; getFactorialOf(number), [number])&lt;/code&gt; instead of simple &lt;code&gt;getFactorialOf(number)&lt;/code&gt;, React memoizes the factorial calculation.&lt;/p&gt;

&lt;p&gt;Let’s improve &lt;code&gt;&amp;lt;ComputeFactorial /&amp;gt;&lt;/code&gt; and memoize the factorial calculation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&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="nx"&gt;useMemo&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ComputeFactorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setNumber&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="mi"&gt;1&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;inc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setInc&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="mi"&gt;0&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;factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getFactorialOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;number&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      Factorial of
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Number&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="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      is &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setInc&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Force Re-render&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getFactorialOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;getFactorialOf(n) is called!&lt;/span&gt;&lt;span class="dl"&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;getFactorialOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time the value of the number is changed, '&lt;code&gt;getFactorialOf(n)&lt;/code&gt; called!' is logged to console, which is expected.&lt;/p&gt;

&lt;p&gt;However, if you click Force Re-render button, '&lt;code&gt;getFactorialOf(n) is called!&lt;/code&gt;' isn’t logged to console because &lt;code&gt;useMemo(() =&amp;gt; getFactorialOf(number), [number])&lt;/code&gt; returns the memoized factorial calculation. How cool is that!&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;This article explored the &lt;code&gt;useMemo&lt;/code&gt; hook and when it is appropriate to use it in a React application.&lt;/p&gt;

&lt;p&gt;We also see that &lt;code&gt;useMemo&lt;/code&gt; helps in boosting the performance of an application by "remembering" or &lt;strong&gt;memoizing&lt;/strong&gt; expensive functions and preventing a re-render every time there is a change in the application.&lt;/p&gt;

&lt;p&gt;We also learnt that while using &lt;code&gt;useMemo&lt;/code&gt; can improve the application's performance, overusing &lt;code&gt;useMemo&lt;/code&gt; can slow down your application. This means the more &lt;code&gt;useMemo&lt;/code&gt; hook is used, and the memory has to be allocated for the memory.&lt;/p&gt;

&lt;p&gt;If you have any questions, suggestions, corrections, I will look forward to it. Thank you for making it this far.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Series path&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/use-state-react-hook"&gt;useState&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/use-effect-react-hook"&gt;useEffect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;useMemo&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💌 If you'd like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>useEffect - React Hooks Series</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Sat, 26 Jun 2021 04:24:25 +0000</pubDate>
      <link>https://dev.to/pratap2210/useeffect-react-hooks-series-3agd</link>
      <guid>https://dev.to/pratap2210/useeffect-react-hooks-series-3agd</guid>
      <description>&lt;p&gt;Welcome back to the series of &lt;strong&gt;React Hooks&lt;/strong&gt;. In part one of the series, I wrote about the useEffect react hook.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Series path&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/use-state-react-hook"&gt;useState&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;useEffect&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;useEffect&lt;/code&gt; hook?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useEffect hook&lt;/code&gt; lets you perform side effects in functional components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Few examples of side effects
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Data fetching&lt;/li&gt;
&lt;li&gt;Setting up a subscription&lt;/li&gt;
&lt;li&gt;Manually changing the DOM in React&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are familiar with &lt;code&gt;React class lifecycle&lt;/code&gt; methods, you can think of &lt;code&gt;useEffect hook&lt;/code&gt; as &lt;code&gt;componentDidMount&lt;/code&gt;, &lt;code&gt;componentDidUpdate&lt;/code&gt; and &lt;code&gt;componentWillUnmount&lt;/code&gt; combined.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does &lt;code&gt;useEffect&lt;/code&gt; hook do?
&lt;/h2&gt;

&lt;p&gt;By using this &lt;code&gt;hook&lt;/code&gt;, you let React know that your component needs to perform something &lt;strong&gt;after rendering&lt;/strong&gt; of the component. React will remember the function which you passed and react call it &lt;strong&gt;after performing the DOM updates&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this &lt;em&gt;effect&lt;/em&gt; we can do many things like setting document title, fetch some data from an API, setting event listeners.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does useEffect run after every re-render?
&lt;/h2&gt;

&lt;p&gt;Yes! By default, the effect runs both after the first render and after every update of the component. Rather than thinking in terms of &lt;strong&gt;mounting&lt;/strong&gt; and &lt;strong&gt;updating&lt;/strong&gt;, you might find it simpler to think that &lt;strong&gt;effects&lt;/strong&gt; happen &lt;strong&gt;after render&lt;/strong&gt;. React &lt;strong&gt;guarantees&lt;/strong&gt; the DOM has been updated by the time it runs the effects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Let's change the document title for better understanding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using class component
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExampleComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//After rendering&lt;/span&gt;
  &lt;span class="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Updating the title in componentDidMount&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;//After updating&lt;/span&gt;
  &lt;span class="nf"&gt;componentDidUpdate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Updating the title in componentDidUpdate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Setting the title&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ExampleComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using functional component
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;useEffect&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ExampleComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Setting title using useEffect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Setting the title using useEffect hook&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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 &lt;strong&gt;function component&lt;/strong&gt;, you have to define one &lt;strong&gt;useEffect&lt;/strong&gt; function instead of &lt;strong&gt;componentDidMount&lt;/strong&gt; and &lt;strong&gt;componentDidUpdate&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting deeper
&lt;/h2&gt;

&lt;p&gt;Now we know what &lt;strong&gt;useEffect&lt;/strong&gt; is. Let us try to understand it deeper. &lt;strong&gt;useEffect&lt;/strong&gt; function accepts two-parameter. i) A function which gets called on every update/re-rendering. ii) An array of dependencies value on which the function has to get called.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; The useEffect below will always get called on rendering and updating of the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I will be called each time the component renders and re-renders&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;&lt;strong&gt;2.&lt;/strong&gt; The useEffect below will get called only once. i.e. the first time it renders. It is equivalent to &lt;strong&gt;componentDidMount&lt;/strong&gt;. The second parameter &lt;code&gt;[]&lt;/code&gt; is called the dependencies array. An empty array means no dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I will be called only once when the component is mounted&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; The useEffect below will get called each time the value of &lt;strong&gt;name&lt;/strong&gt; is changed. It is like &lt;strong&gt;componentDidUpdate&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I will be called first when the component is mounted and each time the name is changing&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="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;&lt;strong&gt;4.&lt;/strong&gt; If we want to do any clean-ups before the component is unmounted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&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="c1"&gt;// some tasks&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;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I do cleanups&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;will first run on component mount then, will run before useEffect and lastly before unmounting&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;I want to thank you for going through the part two of my React Hooks series, in case you missed part one please check it out here(hyperlink)&lt;/p&gt;

&lt;p&gt;If you have any questions, comments, corrections I would look forward to it. Thank you for making it this far.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Series path&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/use-state-react-hook"&gt;useState&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;useEffect&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💌 If you'd like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>useState - React Hooks Series</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Wed, 23 Jun 2021 02:38:40 +0000</pubDate>
      <link>https://dev.to/pratap2210/usestate-react-hooks-series-pm9</link>
      <guid>https://dev.to/pratap2210/usestate-react-hooks-series-pm9</guid>
      <description>&lt;p&gt;Welcome to the hooks series of react. In this series, I will go through the different hooks which were introduced in React 16.8.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Series path&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useState&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/use-effect-react-hook/"&gt;useEffect&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why do we prefer functional component over class component in react
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Functional component is much easier to read and test because they are plain JavaScript functions without state or lifecycle-hooks&lt;/li&gt;
&lt;li&gt;Functional component are written shorter and more straightforward, which makes it easier to develop, understand, and test.&lt;/li&gt;
&lt;li&gt;With the introduction of &lt;strong&gt;Hooks&lt;/strong&gt; in we are now able to manage state in functional components.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is Hooks?
&lt;/h2&gt;

&lt;p&gt;Hooks were a new addition in &lt;code&gt;React 16.8&lt;/code&gt;. They let you use state and other React features without writing a class. It means you don't need to use a class component to use a state.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is useState Hook?
&lt;/h2&gt;

&lt;p&gt;useState is a Hook which allows you to have state variables in functional components.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First of all, let us import &lt;code&gt;useState&lt;/code&gt; hook from react.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;How to use:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;setName&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="s2"&gt;Pratap&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;Let us try to understand what we just added:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: State Variable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setName&lt;/code&gt;: Function to change the state value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useState&lt;/code&gt;: useState react Hook.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Pratap&lt;/code&gt;: State initial value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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;setName&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="s2"&gt;Pratap&lt;/span&gt;&lt;span class="dl"&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Example&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let us add a button which will change the state value from &lt;code&gt;Pratap&lt;/code&gt; to &lt;code&gt;Prasar&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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;setName&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="s2"&gt;Pratap&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;changeState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//This will change the state value&lt;/span&gt;
    &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Prasar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;changeState&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Change Name&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Example&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In the next article in this React Hooks series, we will get to know and see about useEffect hooks.&lt;/p&gt;

&lt;p&gt;Thank you for reading my first part in the React Hooks Series!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Series path&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useState&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/use-effect-react-hook/"&gt;useEffect&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💌 If you'd like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>reacthooks</category>
    </item>
    <item>
      <title>Deep dive into Asynchronous Javascript</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Wed, 16 Jun 2021 00:54:29 +0000</pubDate>
      <link>https://dev.to/pratap2210/deep-dive-into-asynchronous-javascript-20ck</link>
      <guid>https://dev.to/pratap2210/deep-dive-into-asynchronous-javascript-20ck</guid>
      <description>&lt;p&gt;In this article we'll be covering callbacks, promises and async/await as these are the ways to deal with asynchronous data.&lt;/p&gt;

&lt;p&gt;To understand asynchronous, you'll first have to understand synchronous.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is synchronous?
&lt;/h4&gt;

&lt;p&gt;Synchronous is when something is done in sequence. In programming terms synchronous is when a bunch of code are executed one after the other.&lt;/p&gt;

&lt;p&gt;For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 3&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="nc"&gt;A&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above the code will run synchronously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;Task 1
Task 2
Task 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It prints &lt;code&gt;Task 1&lt;/code&gt;, &lt;code&gt;Task 2&lt;/code&gt;, and &lt;code&gt;Task 3&lt;/code&gt; one after the other. It will wait for every line to complete its execution.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is asynchronous?
&lt;/h4&gt;

&lt;p&gt;Asynchronous is nothing but just the opposite of synchronous. You keep doing things while others are getting completed.&lt;br&gt;
What do you think the code will print?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setTimeOut&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 3&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;well the above code will print&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;Task 1
Task 3
Task 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why does it print that way? It is because setTimeout function is not called immediately. It is called after 1 second. Till the time it waits the third console.log() gets printed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why Asynchronous Javascript?
&lt;/h4&gt;

&lt;p&gt;When a JavaScript code is executed, the synchronous code blocks further execution until it completes what it's doing. Code which takes long to complete can make the UI/server unresponsive until the function has returned, which result in a terrible user-experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For instance:&lt;/strong&gt; You want the latest comments of your blog from a server and render in your blog, and it is done synchronously, then a visitor who is in your blog site won't be able to do other things until those comments are loaded. Which indeed could cause a long delay before they could read the comments in your blog.&lt;br&gt;
To understand the above example, follow the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;comments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;loadCommentsFromDatabaseSync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nf"&gt;displayBlogInWebPage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, in order to display the blog on website, the website will first have to wait for &lt;code&gt;loadCommentsFromDatabaseSync()&lt;/code&gt; to get complete. Once &lt;code&gt;loadCommentsFromDatabaseSync()&lt;/code&gt; is completed then only it will display the blog in the web page.&lt;/p&gt;

&lt;p&gt;By using &lt;code&gt;async&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;comments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;loadCommentsFromDatabaseAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nf"&gt;displayBlogInWebPage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code &lt;code&gt;displayBlogInWebPage()&lt;/code&gt; will not wait for the completion of &lt;code&gt;loadCommentsFromDatabaseAsync()&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We should use asynchronous programming when performing expensive and time-consuming operations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Different ways of Asynchronous programming
&lt;/h2&gt;

&lt;p&gt;In javascript, there are three ways to accomplish asynchronous programming, namely &lt;code&gt;callback&lt;/code&gt;, &lt;code&gt;promises&lt;/code&gt; and &lt;code&gt;async/await&lt;/code&gt;.&lt;br&gt;
Let us get in detail with example.:&lt;/p&gt;
&lt;h3&gt;
  
  
  Callback
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What is a callback?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A callback is a function which is executed after a subsequent function has finished executing.&lt;/p&gt;

&lt;p&gt;As JavaScript functions are also a type of &lt;code&gt;object&lt;/code&gt; and they can be passed as an argument while calling a function so much like any other objects like &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt; etc.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Addition of two numbers: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;callback&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;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This must be printed after addition to the console&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;addTwoNumbers&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="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the above code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;Addition of two numbers: 5
This must be printed after addition to the console
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we have two functions:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;addTwoNumbers(a,b,callback):&lt;/strong&gt; This functions is called with there arguments &lt;code&gt;a&lt;/code&gt;,&lt;code&gt;b&lt;/code&gt; and &lt;code&gt;callback&lt;/code&gt;, where &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are &lt;code&gt;numbers&lt;/code&gt; while the third argument &lt;code&gt;callback&lt;/code&gt; is a function. The addTwoNumbers() prints the addition of the two numbers, and as soon as that completes its execution, the callback function is fired!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This type of function is also known as the &lt;code&gt;callback function&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;print():&lt;/strong&gt; As soon as addTwoNumbers() completes its execution and call the callback function this print() will get called and prints its output to the console.&lt;/p&gt;

&lt;p&gt;Callbacks are mainly used for handling asynchronous operations like – making an API request to the server, fetching/writing some data from/into a file, registering event listeners etc. Callbacks are used for the mentioned operations. Depeneding on the result of the operation the callback function will be executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Promises
&lt;/h3&gt;

&lt;p&gt;To understand the basic of promises please check-out &lt;a href="https://pratapsharma.in/fundamentals-of-javascript-every-developer-should-know/#5-promises"&gt;Basic of Promises&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This will be the continuation of the above link.&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  basic syntax of promise in javascript.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//resolve or reject is done here&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As discussed in the basics of Promises section promises have three states, and the states are self-explanatory:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Pending&lt;/strong&gt;: Pending is a state when the promise is neither resolved or rejected. It will continue to remain indefinitely &lt;code&gt;pending&lt;/code&gt; unless it gets resolved or rejected.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Resolved&lt;/strong&gt;: A promise is resolved when the resolve method is called from the promise. The resolved promise will be consumed in the &lt;code&gt;.then()&lt;/code&gt; section.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Rejected&lt;/strong&gt;: If the reject function was called from the promise, then the promise is rejected. If the promise is rejected, then it should be consumed in the &lt;code&gt;.catch()&lt;/code&gt; section.&lt;/p&gt;
&lt;h4&gt;
  
  
  Consuming a promise
&lt;/h4&gt;

&lt;p&gt;Please check the above link in order to see consuming promises.&lt;/p&gt;
&lt;h4&gt;
  
  
  Chaining of promises
&lt;/h4&gt;

&lt;p&gt;In order to understand the concepts of Async/await, one has to understand the core usage of promise, which includes &lt;code&gt;chaining of promises&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When a .then() method returns a promise, then the subsequent executions of the &lt;code&gt;.then()&lt;/code&gt; are suspended until the current promise block is resolved.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pratap&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;secondPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Prasar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;thirdromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error&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="nx"&gt;firstPromise&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;First Promise after 1 sec: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;x&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;secondPromise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Second Promise after 2 sec: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;x&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;thirdromise&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;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Third Promise after 3 sec: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the above code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;First promise after 1 sec:  Pratap
Second promise after 2 sec: Prasar
Third promise after 3 sec: Error
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt; of above code.&lt;/p&gt;

&lt;p&gt;Let us understand step by step:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have initialised 4 promises &lt;code&gt;firstPromise&lt;/code&gt;, &lt;code&gt;secondPromise&lt;/code&gt;, &lt;code&gt;thirdPromise&lt;/code&gt; and &lt;code&gt;fourthPromise&lt;/code&gt;. For the first instance, all four promises are racing toward resolve/reject.&lt;/li&gt;
&lt;li&gt;After 1 second the &lt;code&gt;firstPromise&lt;/code&gt; gets resolved as we are calling the &lt;code&gt;resolve&lt;/code&gt; method in the promise initialization and this gets printed in the console &lt;code&gt;First promise after 1 sec: Pratap&lt;/code&gt;. Then we return another promise &lt;code&gt;secondPromise&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After 2 seconds the &lt;code&gt;secondPromise&lt;/code&gt; also gets &lt;code&gt;resolved&lt;/code&gt; and &lt;code&gt;Second promise after 2 sec: Prasar&lt;/code&gt; gets printed to the console. We then return &lt;code&gt;thirdPromise&lt;/code&gt; from the &lt;code&gt;secondPromise&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;But, after 3 seconds the &lt;code&gt;thirdPromise&lt;/code&gt; gets rejected as we called &lt;code&gt;reject&lt;/code&gt; in the &lt;code&gt;thirdPromise&lt;/code&gt; initialization. &lt;code&gt;Third promise after 3 sec: Error&lt;/code&gt; gets printed to the console.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Promise.all
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Promise.all&lt;/code&gt; accepts an array of promises, and will attempt to fulfil all the promises. If any of the promises are rejected, then the promise will exit.&lt;/p&gt;

&lt;p&gt;In order to fulfil multiple promises, &lt;code&gt;promise.all&lt;/code&gt; was introduced. It is widely used in javascript frameworks where we want to get data from multiple &lt;code&gt;APIs&lt;/code&gt;. So rather then calling the promises individually you'd better want to use &lt;code&gt;Promise.all()&lt;/code&gt; method and handle the result based on the status of the promises.&lt;/p&gt;

&lt;p&gt;An example of &lt;code&gt;promise.all&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrayOfPromises&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promise1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promise2&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;runAllThePromises&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayOfPromises&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showSuccessMessage&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;showErrorMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;arrayOfPromises&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Promise.race
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;promise.race()&lt;/code&gt;method returns a promise which fulfills or rejects as soon as one of the promises in an array gets fulfills or rejects.&lt;/p&gt;

&lt;p&gt;We can understand &lt;code&gt;promise.race()&lt;/code&gt; as the realtime race.&lt;/p&gt;

&lt;p&gt;Suppose five people are participating in a race and any of them crosses the winning line then the race ends. Also, if any of them gets injured, then also the race gets end.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;First Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;secondPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Second Promise&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;race&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;firstPromise&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondPromise&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;Second Promise
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We passed &lt;code&gt;firstPromise&lt;/code&gt; and &lt;code&gt;secondPromise&lt;/code&gt; as an array in the argument of &lt;code&gt;Promise.race()&lt;/code&gt;. As &lt;code&gt;secondPromise&lt;/code&gt; gets resolved faster than &lt;code&gt;firstPromise&lt;/code&gt; so &lt;code&gt;Second Promise&lt;/code&gt; gets printed in the console.&lt;/p&gt;

&lt;h4&gt;
  
  
  Async/Await
&lt;/h4&gt;

&lt;p&gt;Async/Await is one of the most recent additions to the JavaScript language which is part of ES8.&lt;/p&gt;

&lt;p&gt;Async/Await is syntactic sugar on top of promises which makes asynchronous code easier to write and to read. The async code is written just like the synchronous code. It is just a special syntax to work with promises in a more synchronous-type manner.&lt;/p&gt;

&lt;p&gt;Basic function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getFruit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mango&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;The above snippet just a basic javascript code which returns &lt;code&gt;Mango&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  async
&lt;/h4&gt;

&lt;p&gt;Now, let us convert the above code to promise using &lt;code&gt;async&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getFruit&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;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mango&lt;/span&gt;&lt;span class="dl"&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;fruits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in the function above, just adding &lt;code&gt;async&lt;/code&gt; keyword in front of a function, not the function will return a &lt;code&gt;Promise&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;getFruit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// prints: Apple&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  await
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; keyword is used to wait for a Promise to get resolved. &lt;code&gt;await&lt;/code&gt; can be used inside an Async function block only. It makes JavaScript wait until the promise returns a result. It only makes the async function block wait and not the whole program execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;demonstrateAsync&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;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise is ressolved!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// wait until the promise returns us a value&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;demonstrateAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the above code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;Promise is ressolved!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;await&lt;/code&gt; should be used within an &lt;code&gt;async&lt;/code&gt; block only.&lt;br&gt;&lt;br&gt;
&lt;code&gt;await&lt;/code&gt; can't be used in top-level code&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Catching error in async/await
&lt;/h4&gt;

&lt;p&gt;Handling errors in async/await is pretty straight forward. In order to handle an error, we just have to wrap inside a &lt;code&gt;try..catch&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;thisThrowsError&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://invalidUrl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Invalid url&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;thisThrowsError&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any error in the scope of &lt;code&gt;try&lt;/code&gt; block will be handled in the &lt;code&gt;catch&lt;/code&gt; block.&lt;/p&gt;

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

&lt;p&gt;In this blog, we have completed the different ways of asynchronous programming in Javascript and their usage.&lt;/p&gt;

&lt;p&gt;Due to the addition of asynchronous programming in javascript, it makes it possible to express waiting for long-running actions without blocking the execution of code. In javascript, it is typically implemented using &lt;code&gt;callback&lt;/code&gt;, &lt;code&gt;promises&lt;/code&gt; and &lt;code&gt;async/await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Programming asynchronously in Javascript is made easier by using promises and async/await, which allow us to write an asynchronous program as if it were synchronous.&lt;/p&gt;

&lt;h3&gt;
  
  
  FURTHER READING
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/things-to-keep-in-mind-before-learning-javascript-framework/"&gt;Things to keep in mind before starting Javascript framework&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/introduction-to-strapi-a-nodejs-headless-cms/"&gt;Strapi.js - Open Source Node.js Headless CMS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.io/difference-between-var-let-const-in-javascript/"&gt;var, let, and const – Why to avoid var 😷 and how to put the other two to good use? - Javascript&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💌 If you’d like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>asynchronous</category>
      <category>webdev</category>
      <category>developers</category>
    </item>
    <item>
      <title>var, let, and const – Why to avoid var 😷 and how to put the other two to good use? - Javascript</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Sat, 12 Jun 2021 10:06:47 +0000</pubDate>
      <link>https://dev.to/pratap2210/var-let-and-const-why-to-avoid-var-and-how-to-put-the-other-two-to-good-use-javascript-1p0a</link>
      <guid>https://dev.to/pratap2210/var-let-and-const-why-to-avoid-var-and-how-to-put-the-other-two-to-good-use-javascript-1p0a</guid>
      <description>&lt;p&gt;Well, in this blog, we will illustrate the differences between &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;. We'll also discuss why we should avoid &lt;code&gt;var&lt;/code&gt; and prefer &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In ES5, we had only one way of declaring variables using &lt;code&gt;var&lt;/code&gt;. But with ES6 we now have three to declare a variable namely &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. var
&lt;/h3&gt;

&lt;p&gt;Firstly let us see how &lt;code&gt;var&lt;/code&gt; works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In most programming languages, the scope of &lt;code&gt;i&lt;/code&gt;will be inside the body of &lt;code&gt;loop&lt;/code&gt; only. The value of &lt;code&gt;i&lt;/code&gt; can be accessible within the &lt;strong&gt;body&lt;/strong&gt; of &lt;code&gt;for loop&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let us see what happens if we console the value of variable &lt;code&gt;i&lt;/code&gt; after the loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After loop &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//This prints 10. 😳 Why is that?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that we are able to access the variable &lt;code&gt;i&lt;/code&gt; even outside the &lt;code&gt;loop&lt;/code&gt;, which is very &lt;strong&gt;intuitive&lt;/strong&gt; when coming from another programming language. No one would expect the variable &lt;code&gt;i&lt;/code&gt; to &lt;strong&gt;survive&lt;/strong&gt; outside the &lt;code&gt;loop&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Why does that happen🤔?&lt;br&gt;
The reason is that there is only one type of variable scope in ES5, which is a function scope.&lt;/p&gt;

&lt;p&gt;To illustrate the above statement, let us wrap the above code inside a function and execute the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After loop &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//still prints 10 🤥&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us move the console.log() to outside of the loop and see what happens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After loop &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//It throws an error this time: Reference error: i is not defined. 🤤&lt;/span&gt;
&lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time it throws an error &lt;code&gt;Refrence error: i is not defined&lt;/code&gt; and it is because the scope of &lt;code&gt;var&lt;/code&gt; is within the f unction scope . The variable &lt;code&gt;i&lt;/code&gt; will be available only within the function counter and if called from outside the function it will throws and error.&lt;/p&gt;

&lt;p&gt;In order to avoid this type of error, Some developers used the concept of Immediately Invoked Function Expression (IIFE).&lt;/p&gt;

&lt;p&gt;This is how IIFE looks like.&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="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After loop &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Most of the Javascript developer does not know about IIFE.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;IIFE is a function that is declared and called immediately.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;People do this to keep the variables that are inside the IIFE to pollute the rest of the code. It creates lots of cohesive block of safe-heaven.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Javascript variables are hoisted
&lt;/h4&gt;

&lt;p&gt;What do &lt;code&gt;Javascript variables are hoisted&lt;/code&gt; means?&lt;br&gt;
Well, it means that they are hoisted to the top of their function.&lt;/p&gt;

&lt;p&gt;What Javascript compiler or interpreter does is it will go through the code, and it will find variable declaration and push them up to the top of the function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;70% of javascript developers do not know the concept of hoisting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Upon compiling the below code it still works. A the variable &lt;code&gt;i&lt;/code&gt; of &lt;code&gt;for loop&lt;/code&gt; is hoisted to top of the function.&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="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//It pushes the variable to top of the function.&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Keep note it doesn't matter how many lines of code are there in between, it will still move the variable declaration to the top of the function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is also a horrible aspect of hoisting. Let's say you forgot the variable declaration. as shown in below 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="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After loop &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This prints 10. What the heck just happened??😧&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why does it prints &lt;code&gt;10&lt;/code&gt;? This happened because the javascript interpreter has put the variable declaration out of the function this time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If there is no variable declaration inside a function the interpreter will keep going up the function chain all upto global/window object. And then if it finds it then it will place the variable in the global/window object.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After loop &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This is of course horrible because you can accidently declare a global variables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is why we are encouraged to always use &lt;code&gt;use strict&lt;/code&gt; statement, it prevents us from accidentally declaring a global variable.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After loop &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time the above code will throw an error &lt;code&gt;RefrenceError: i is not defined.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9999&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// this prints 1 2 ...10&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After loop &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This prints 9999&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though we are using the same variable name, it prints 9999 because the inner &lt;code&gt;var i = 0&lt;/code&gt; is scoped to the function only.&lt;/p&gt;

&lt;p&gt;This time let's remove var from inside the function and see what happens&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9999&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// this prints 1 2 ...10&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After loop &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This prints 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It prints 10, this is because &lt;code&gt;i&lt;/code&gt; inside function is reassigning the outside variable &lt;code&gt;i&lt;/code&gt;. As mentioned above it will check if variable &lt;code&gt;i&lt;/code&gt; is defined in function, if it doesn't found it then it will move up to the global/window. As it finds the variable &lt;code&gt;i&lt;/code&gt; then it will be re-assigned. That is the reason why it is logging out 10.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always remember the concept of hoisting. Hoisting is nothing but moving the variable declaration to the top of the function or global/window object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. let
&lt;/h3&gt;

&lt;p&gt;Now let us demonstrate the same example using &lt;code&gt;let&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9999&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After for loop&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This prints 9999. It works.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see from the example code above that &lt;code&gt;let&lt;/code&gt; is not behaving like &lt;code&gt;var&lt;/code&gt;. And the above code works as expected.&lt;br&gt;
The reason why it worked is that &lt;code&gt;let&lt;/code&gt; introduces &lt;code&gt;block scoping&lt;/code&gt;. The variable &lt;code&gt;let i = 0&lt;/code&gt; of &lt;code&gt;for loop&lt;/code&gt; will be available only inside the body of &lt;code&gt;for loop&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The block scoping of &lt;code&gt;let&lt;/code&gt; is also valid for if/else, switch etc.&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9999&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;After for and if&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This still prints 9999.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the code snippet we can see that the the scope of variable &lt;code&gt;i&lt;/code&gt; is scoped with in &lt;code&gt;for loop&lt;/code&gt; and &lt;code&gt;if&lt;/code&gt; respectively.&lt;/p&gt;

&lt;p&gt;Just like &lt;code&gt;var&lt;/code&gt; we can re-assign let.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;57&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints 57&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;let&lt;/code&gt; is block scope while &lt;code&gt;var&lt;/code&gt; is function scope.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A lot of people say that &lt;code&gt;let&lt;/code&gt; is the new &lt;code&gt;var&lt;/code&gt; and I agree with that.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. const
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;const&lt;/code&gt; is a variable just like &lt;code&gt;let&lt;/code&gt; which cannot be re-assigned. To demonstrate this, let me show you an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;57&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above throws an error &lt;code&gt;TypeError: Assignment to constant variable.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's look at an example below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints {y: 15}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that we are able to update the property of &lt;code&gt;x&lt;/code&gt;. Now let us try to assign a new object to constant variable &lt;code&gt;x&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;z&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This time it throws an error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code snippet above throws an error because we are re-assigning a constant variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  var vs let vs const
&lt;/h3&gt;

&lt;p&gt;Whole blog summarized in a table.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scopes&lt;/th&gt;
&lt;th&gt;var&lt;/th&gt;
&lt;th&gt;let&lt;/th&gt;
&lt;th&gt;const&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Stored in global scope&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Function Scope&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Block Scope&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can be re-assigned&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can be re-declared&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can be hoisted&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Always use &lt;code&gt;const&lt;/code&gt; if you do not want to change the variable.&lt;/li&gt;
&lt;li&gt;Whenever you are using &lt;code&gt;let&lt;/code&gt; first ask yourself if you are really changing the variable or do you really need to change it. If the answer is &lt;strong&gt;yes&lt;/strong&gt; then use &lt;code&gt;let&lt;/code&gt; else use &lt;code&gt;const&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Always use 'const'. If you REALLY need to change state, use 'let'. 'var' is dead.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; should always be avoided because of hoisting.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this blog we have learnt about&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why to avoid &lt;code&gt;var&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;What &lt;code&gt;hoisting&lt;/code&gt; in javascript is.&lt;/li&gt;
&lt;li&gt;What Immediately Invoked Function Expression (IIFE) is and how to use it.&lt;/li&gt;
&lt;li&gt;How to decide between using let and const.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;I hope this post saved your time and life. If you liked the post, feel free to share it to help others find it!&lt;/p&gt;

&lt;p&gt;You may also want to read &lt;a href="https://pratapsharma.in/getting-started-with-git/"&gt;Getting Started with Git - A beginner's guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may also follow me on &lt;a href="https://www.linkedin.com/in/prataps22/"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/Pratap2210"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;💌 If you’d like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>developer</category>
      <category>tips</category>
    </item>
    <item>
      <title>Building RESTful API using Nodejs and Express</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Fri, 11 Jun 2021 09:21:49 +0000</pubDate>
      <link>https://dev.to/pratap2210/building-restful-api-using-nodejs-and-express-4pi4</link>
      <guid>https://dev.to/pratap2210/building-restful-api-using-nodejs-and-express-4pi4</guid>
      <description>&lt;p&gt;Working on the front-end using HTML, CSS &amp;amp; Javascript and wondering how RESTful API works in the backend. Then you can start working on it using Nodejs.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Nodejs?
&lt;/h2&gt;

&lt;p&gt;Node.js is a server-side programming framework built on Chrome's JavaScript engine, which provides event-driven and non-blocking I/O. The coding is done in Javascript, and its latest version is v11.9.0. Read the &lt;a href="https://nodejs.org/en/docs/"&gt;docs&lt;/a&gt; for further information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nodejs installation guide:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Download the Nodejs application from the official Node.js official site: &lt;a href="https://nodejs.org/en/download/"&gt;https://nodejs.org/en/download/&lt;/a&gt; Follow the steps depending on your OS.&lt;/li&gt;
&lt;li&gt;After installation, check for the version of Node and NPM using the command in your terminal NPM is the Node Package Manager and is a tool for installing, uninstalling and maintaining package modules for your app. NPM is installed along with Node:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;-v&lt;/span&gt;
npm &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5pjb4jqslsd1ojfenhvl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5pjb4jqslsd1ojfenhvl.png" alt="Node - npm version" width="289" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After verifying the versions of both Node and NPM, now create a new folder for our server.&lt;br&gt;
Run the below commands in your terminal to create a new folder, and inside a folder, create one new file, index.js.&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;SampleServer
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;SampleServer
SampleServer&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After successfully creating folders and files, run the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;SampleServer&lt;span class="nv"&gt;$ &lt;/span&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the command npm will create a new file package.json.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;package.json&lt;/strong&gt; - It holds various metadata/dependencies relevant to the project. This file gives information to npm, which allows it to identify the project and handle the project's dependencies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbwjags3nfc07vavbv7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbwjags3nfc07vavbv7w.png" alt="package.json" width="554" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After configuring Node and npm, now it's time to install express. ExpressJS is a web framework for Node that provides you with a simple API to build websites, web apps and backends. With ExpressJS, you don't need to worry about low-level protocols, processes, etc. In order to install express, run the following command to add the framework.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;SampleServer&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will add all the dependency of express in the server application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6m70zwrllzvup1kdmw5z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6m70zwrllzvup1kdmw5z.png" alt="dependecy-structure" width="689" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it's finally time to write some codes. Add the following code to your index.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;req&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello world!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;App running in port 3000&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;A variable name 'express' is holding a new express application.&lt;br&gt;
app.listen(3000) implies that the server is listening local in port number 3000. Now run the server by using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;SampleServer&lt;span class="nv"&gt;$ &lt;/span&gt;node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjg5ozt1ja4qer9wpzxiv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjg5ozt1ja4qer9wpzxiv.png" alt="terminal output" width="617" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now request the server which you've just created from the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhh8y1lkr4yfht9n9k9qu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhh8y1lkr4yfht9n9k9qu.png" alt="web output" width="282" height="68"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You successfully created a sample server.&lt;/p&gt;

&lt;p&gt;Few important express methods to be familiar with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;app.listen()&lt;/strong&gt;: To listen to the server in a particular port.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;app.get()&lt;/strong&gt;: The GET method requests data from the server. Requests using the GET method should only retrieve data and should have no other effect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;app.post()&lt;/strong&gt;: The POST method requests the server to accept data that is enclosed in the request as a new object/entity of the resource identified by the URI.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;app.put()&lt;/strong&gt;: The PUT method requests the server to accept the data enclosed in the request as a modification to existing data identified by the URI. In case data does not exist, then the PUT method should create one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;app.delete()&lt;/strong&gt;: It is a delete method for express. The DELETE method requests the server to remove/delete the specified resource.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Did you enjoy this? Please give it a like so others can discover it as well.&lt;/p&gt;

&lt;p&gt;You may also like my other articles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.in/regex-with-javascript/"&gt;Console.log and other console methods to debug your code in JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.in/things-to-keep-in-mind-before-learning-javascript-framework/"&gt;Things to keep in mind before starting Javascript framework&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pratapsharma.in/difference-between-var-let-const-in-javascript/"&gt;var, let, and const – Why to avoid var 😷 and how to put the other two to good use? - Javascript&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you liked the article, feel free to share it to help others find it!&lt;/p&gt;

&lt;p&gt;You may also follow me on &lt;a href="https://www.linkedin.com/in/prataps22/"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/Pratap2210"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;💌 If you’d like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Useful Git commands that may save your life</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Fri, 11 Jun 2021 02:41:30 +0000</pubDate>
      <link>https://dev.to/pratap2210/useful-git-commands-that-may-save-your-life-3mg</link>
      <guid>https://dev.to/pratap2210/useful-git-commands-that-may-save-your-life-3mg</guid>
      <description>&lt;p&gt;Git commands aren't always easy. If they were, we would have these commands at our disposal. They would be super helpful for performing everyday tasks like creating or renaming a git branch, removing files, and undoing changes.&lt;/p&gt;

&lt;p&gt;In this post, I’m focusing on major git commands that gets all (almost) your work fulfilled.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to undo the most recent local commits in Git?
&lt;/h2&gt;

&lt;p&gt;You can run&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="nv"&gt;$ &lt;/span&gt;git reset HEAD~1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can even undo your commit but leave your files and your index:&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="nv"&gt;$ &lt;/span&gt;git reset &lt;span class="nt"&gt;--soft&lt;/span&gt; HEAD~1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to permanently undo the commit and you have cloned some repository:&lt;br&gt;
To get commit id:&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="nv"&gt;$ &lt;/span&gt;git log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git reset &lt;span class="nt"&gt;--hard&lt;/span&gt; &amp;lt;commit_id&amp;gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;git push origin &amp;lt;branch_name&amp;gt; &lt;span class="nt"&gt;-f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to undo a public commit?
&lt;/h2&gt;

&lt;p&gt;You can just run&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="nv"&gt;$ &lt;/span&gt;git revert HEAD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to delete a Git branch locally and remotely?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Deleting local branch
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git branch &lt;span class="nt"&gt;-d&lt;/span&gt; &amp;lt;branchname&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Deleting remote branch
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git push origin &lt;span class="nt"&gt;--delete&lt;/span&gt; &amp;lt;branch&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to revert to a particular commit?
&lt;/h2&gt;

&lt;p&gt;To revert to a particular commit you'll need a commit id. To get a commit id just run&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="nv"&gt;$ &lt;/span&gt;git log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then copy the commit id you want to rever back to. And then run:&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="nv"&gt;$ &lt;/span&gt;git revert &amp;lt;commit-id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How Undo a git stash?
&lt;/h2&gt;

&lt;p&gt;You can just run:&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="nv"&gt;$ &lt;/span&gt;git stash pop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it will unstash your changes.&lt;/p&gt;

&lt;p&gt;In case you want to preserve the state of files (staged vs. working), use&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="nv"&gt;$ &lt;/span&gt;git stash apply &lt;span class="nt"&gt;--index&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to undo 'git add' before commit?
&lt;/h2&gt;

&lt;p&gt;You can undo &lt;code&gt;git add&lt;/code&gt; before commit with&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="nv"&gt;$ &lt;/span&gt;git reset &amp;lt;file&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To unstage all due changes&lt;/strong&gt;&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="nv"&gt;$ &lt;/span&gt;git reset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to rename a local Git branch?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git branch &lt;span class="nt"&gt;-m&lt;/span&gt; &amp;lt;oldname&amp;gt; &amp;lt;newname&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To rename current branch
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git branch &lt;span class="nt"&gt;-m&lt;/span&gt; &amp;lt;newname&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to discard unstaged changes in Git?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;For all unstaged files
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;For a specific file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout &lt;span class="nt"&gt;--&lt;/span&gt; path/to/file/to/revert
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: -- here to remove argument ambiguation.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to delete all tags from a Git repository?
&lt;/h2&gt;

&lt;p&gt;To delete remote tags simply do:&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="nv"&gt;$ &lt;/span&gt;git tag &lt;span class="nt"&gt;-l&lt;/span&gt; | xargs &lt;span class="nt"&gt;-n&lt;/span&gt; 1 git push &lt;span class="nt"&gt;--delete&lt;/span&gt; origin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then delete the local copies of tag:&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="nv"&gt;$ &lt;/span&gt;git tag | xargs git tag &lt;span class="nt"&gt;-d&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;I hope this post saved your time and life. If you liked the post, feel free to share it to help others find it!&lt;/p&gt;

&lt;p&gt;You may also want to read &lt;a href="https://pratapsharma.io/getting-started-with-git/"&gt;Getting Started with Git - A beginner's guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may also follow me on &lt;a href="https://www.linkedin.com/in/prataps22/"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/Pratap2210"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;💌 If you’d like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>git</category>
      <category>command</category>
      <category>guthub</category>
      <category>useful</category>
    </item>
    <item>
      <title>10 effective tips for New Developers</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Wed, 09 Jun 2021 15:37:03 +0000</pubDate>
      <link>https://dev.to/pratap2210/10-effective-tips-for-new-developers-40n9</link>
      <guid>https://dev.to/pratap2210/10-effective-tips-for-new-developers-40n9</guid>
      <description>&lt;p&gt;Habits are the key to success. If you want to run a marathon, you better make it a habit to run every day.&lt;/p&gt;

&lt;p&gt;Same goes for programming. If you want to become a great programmer, then you need to code every day and implement at least these habits.&lt;/p&gt;

&lt;p&gt;Having good day-to-day habits are very crucial to reach your goal. These habits will skyrocket your efficiency. These are the habits which I follow in my every daily life.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Exercise/work-out daily
&lt;/h3&gt;

&lt;p&gt;Take out some time to play sports or work out as a healthy body and a healthy mind are essential to be able to code for longer hours. This will improve your productivity.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Do Projects
&lt;/h3&gt;

&lt;p&gt;If you do projects regularly, you will dramatically improve your skills as a developer.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Eat the frog first
&lt;/h3&gt;

&lt;p&gt;Always do the task you hate the most first. After finishing it, that will be your ahaa! moment. You'll have time to think it clearly for the rest of the day.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Stay Hydrated
&lt;/h3&gt;

&lt;p&gt;Always have a bottle of water next to you. Having so will help you keep hydrated and increase your productivity.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Understand the Code
&lt;/h3&gt;

&lt;p&gt;Next time you copy code from &lt;strong&gt;Stack Overflow&lt;/strong&gt; try to understand how the code you're copying works. Try to read each line of code as a story. Every line you copy or write has to have some story.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Invest in your self
&lt;/h3&gt;

&lt;p&gt;Always invest in yourself. Your brain is your biggest asset in life. Invest time in learning new technology, develop your skills. Invest in books, courses, mentors.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Solve online coding problems
&lt;/h3&gt;

&lt;p&gt;Solving online coding problems is the most crucial habit which will be very very useful to develop your logic and problem-solving skills. The more problem you solve, the more you'll build your skills.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Contribute to open-source
&lt;/h3&gt;

&lt;p&gt;The more you contribute to the open-source project, the more you learn. This is also the best way to learn to work with others.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Read books (most important)
&lt;/h3&gt;

&lt;p&gt;A good book will give you an overview of a specific field, and guide you through the author's angle from the start to the end of a topic. It's like keeping your feet in someone else shoes.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Communicate with others
&lt;/h3&gt;

&lt;p&gt;Communication comes into play when writing documentation for frameworks and libraries you've worked on, or when sending emails or slack messages to coworkers. Communication is an essential factor in how two or more people convey complex ideas and concepts to each other, which is core to collaborating as a software developer. As a developer, you will work as a group.&lt;/p&gt;

&lt;h3&gt;
  
  
  Teach other developers (Additional)
&lt;/h3&gt;

&lt;p&gt;Teaching is the best way to learn. If you teach or help others, the better you'll become in understanding the basics. I spend 2-3 hours of my day teaching/helping others.&lt;/p&gt;

&lt;h2&gt;
  
  
  To sum it up
&lt;/h2&gt;

&lt;p&gt;Habits are who you are. Improving your practices will drastically help you in becoming a better developer.&lt;/p&gt;

&lt;p&gt;I hope these tips will help you improve your habits.&lt;/p&gt;

&lt;p&gt;💌 If you'd like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You may also follow me on &lt;a href="https://www.linkedin.com/in/prataps22/"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/Pratap2210"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please don't hesitate to drop a comment here if you want to discuss any of these tips. I'll be happy to know what helps you become a better programmer.&lt;/p&gt;

</description>
      <category>developers</category>
      <category>effective</category>
      <category>tips</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Tips and Tricks in Javascript</title>
      <dc:creator>Pratap Sharma</dc:creator>
      <pubDate>Wed, 09 Jun 2021 03:31:05 +0000</pubDate>
      <link>https://dev.to/pratap2210/tips-and-tricks-in-javascript-4gak</link>
      <guid>https://dev.to/pratap2210/tips-and-tricks-in-javascript-4gak</guid>
      <description>&lt;p&gt;In this article, let us cover some tips and tricks of Javascript everyone should be aware of.&lt;/p&gt;

&lt;h2&gt;
  
  
  Convert to boolean
&lt;/h2&gt;

&lt;p&gt;All values in javascript are &lt;code&gt;truthy&lt;/code&gt; except for &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;""&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;,&lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;NaN&lt;/code&gt; and of course &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can easily switch between &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; by using the negative operator(&lt;code&gt;!&lt;/code&gt;), which also converts the type to &lt;strong&gt;boolean&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="c1"&gt;//true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="c1"&gt;//false&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="c1"&gt;//false&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Amazing right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Convert to Number
&lt;/h2&gt;

&lt;p&gt;Converting to number can be easily achieved using the &lt;strong&gt;addition operator&lt;/strong&gt; (&lt;code&gt;+&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "10"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "string"&lt;/span&gt;

&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "number"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How cool is that?&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick float to integer
&lt;/h2&gt;

&lt;p&gt;There are several ways to convert &lt;strong&gt;float to an integer&lt;/strong&gt; by using &lt;code&gt;Math.floor()&lt;/code&gt;, &lt;code&gt;Math.ceil()&lt;/code&gt; or &lt;code&gt;Math.round()&lt;/code&gt;, but there is also a quicker way to convert a &lt;strong&gt;float to an integer&lt;/strong&gt; by using the &lt;strong&gt;bitwise OR operator&lt;/strong&gt; (&lt;code&gt;|&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;11.5&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="c1"&gt;// 11&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;11.5&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="c1"&gt;// -11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Wow! Amazing right? I started using this more frequently now.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Short-Circuit Evaluation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Using &amp;amp;&amp;amp;
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; will return the first &lt;code&gt;false&lt;/code&gt; or &lt;code&gt;falsy&lt;/code&gt; value. If every operand is expected to be &lt;code&gt;true&lt;/code&gt;, then the last evaluated expression will be returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using ||
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;||&lt;/code&gt; will return the first &lt;code&gt;true&lt;/code&gt; of &lt;code&gt;truthy&lt;/code&gt; value. If every operand is expected to be &lt;code&gt;false&lt;/code&gt;, then the last evaluated expression will be returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Short-Circuit evaluation is widely used in any programming language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Filter unique values from an array
&lt;/h2&gt;

&lt;p&gt;You can easily find &lt;strong&gt;unique values&lt;/strong&gt; using the &lt;code&gt;Set&lt;/code&gt; data structure. The &lt;strong&gt;Set&lt;/strong&gt; was introduced in &lt;strong&gt;ES6&lt;/strong&gt; along with the spread operator (&lt;code&gt;...&lt;/code&gt;). Let us use both the new concept to filter unique values of an array.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I know you'll use this a lot. You can thank me later. 😄&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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;6&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;uniqueArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uniqueArr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [ 1, 2, 3, 4, 5, 6 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Self calling function or IIFE
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;IIFE - Immediately Invoked Function Expression&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is often called as &lt;strong&gt;Self-Invoked Annonymous Function&lt;/strong&gt; or &lt;strong&gt;IIFE&lt;/strong&gt;. It is a function that executes automatically when it gets created.&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="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am running automatically&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;//I am running automatically&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read more about &lt;strong&gt;IIFE&lt;/strong&gt; &lt;a href="https://pratapsharma.in/javascript-interview-questions/#explain-why-the-following-doesnt-work-as-an-iife-code-classlanguage-textfunction-foo-code-what-needs-to-be-changed-to-properly-make-it-an-iife"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get a random item from an array
&lt;/h2&gt;

&lt;p&gt;We can use &lt;code&gt;Math.random()&lt;/code&gt; and &lt;code&gt;Math.floor()&lt;/code&gt; methods to get a random item from an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pratap&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Prasar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sudip&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shiva&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;randomName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;names&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;It is also one of the most commonly used tricks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Primitive operations can be faster than function calls
&lt;/h2&gt;

&lt;p&gt;Keep in mind that primitive operations can be faster than function calls.&lt;/p&gt;

&lt;p&gt;Instead of using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;min&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;min&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Empty an array
&lt;/h2&gt;

&lt;p&gt;You can empty an array using the &lt;code&gt;array.length&lt;/code&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dumarray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;dumarray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dumarray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// []&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  To sum it up
&lt;/h2&gt;

&lt;p&gt;This is it from this article. I hope I'm able to help you with top Javascript tips and tricks.&lt;/p&gt;

&lt;p&gt;💌 If you'd like to receive more tutorials in your inbox, you can sign up for the newsletter &lt;a href="https://pratap.substack.com"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please don't hesitate to drop a comment here if I miss anything. Also, let me know if I can make the post better.&lt;/p&gt;

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