<?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: Sagar Medtiya</title>
    <description>The latest articles on DEV Community by Sagar Medtiya (@sagarmedtiya).</description>
    <link>https://dev.to/sagarmedtiya</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%2F925077%2F625af564-55fd-4f66-b301-fa639d3fc139.jpeg</url>
      <title>DEV Community: Sagar Medtiya</title>
      <link>https://dev.to/sagarmedtiya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagarmedtiya"/>
    <language>en</language>
    <item>
      <title>The Uses of DSA in Real Life🤓</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Thu, 15 Sep 2022 12:56:42 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/the-uses-of-dsa-in-real-life-5cjk</link>
      <guid>https://dev.to/sagarmedtiya/the-uses-of-dsa-in-real-life-5cjk</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GvrpimPZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3kal64yr2qry8v0go8k.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GvrpimPZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3kal64yr2qry8v0go8k.gif" alt="Image description" width="880" height="461"&gt;&lt;/a&gt;&lt;br&gt;
Ever wondered why DSA is so much important in implementing software and as well as hiring process. Why the &lt;strong&gt;companies&lt;/strong&gt; is primarily focused on taking DSA and not some specific &lt;strong&gt;language/framework questions&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Knowledge of DSA like &lt;strong&gt;Tree, graphs, array&lt;/strong&gt; and various other algorithm goes a long way in solving problem efficiently with proper tool. Just like a car mechanic needs the right tool to fix a &lt;strong&gt;car&lt;/strong&gt; and make it run properly, a programmer needs the right tool (algorithm and data structure) to make the software run properly. So the &lt;strong&gt;interviewer&lt;/strong&gt; wants to find a candidate who can apply the right set of tools to solve the given problem.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In this blog I'll share some examples of DSA used in real life. So, lets' get started with&lt;/em&gt; &lt;strong&gt;array&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Array
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;2D arrays, commonly known as, matrices, are used in image processing.&lt;/li&gt;
&lt;li&gt;Arrays can be used in speech processing where every speech signal is an array.&lt;/li&gt;
&lt;li&gt;In games like online chess, where the player can store his past moves as well as current moves. It indicates a hint of position. &lt;/li&gt;
&lt;li&gt;It is also used in record management.&lt;/li&gt;
&lt;li&gt;Contacts on a cell phone.&lt;/li&gt;
&lt;li&gt;For CPU scheduling in computer.&lt;/li&gt;
&lt;li&gt;Your viewing screen is also a multidimensional array of pixels.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yZYmtfsb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244404897/V9j-FjE_J.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yZYmtfsb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244404897/V9j-FjE_J.png" alt="Copy-of-arrays.png" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  String
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Spam email detection.&lt;/li&gt;
&lt;li&gt;Plagiarism detection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9bear23E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244461572/wKE2elVEV.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9bear23E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244461572/wKE2elVEV.jpg" alt="pp-raffia-string-roll-5.jpg" width="880" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Linked List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Linked lists are used to display image containers. Users can visit past, current, and next images.&lt;/li&gt;
&lt;li&gt;They are used to store the history of the visited page.&lt;/li&gt;
&lt;li&gt;They are used to perform undo operations.&lt;/li&gt;
&lt;li&gt;Linked are used in software development where they indicate the correct syntax of a tag.&lt;/li&gt;
&lt;li&gt;Linked lists are used to display social media feeds.&lt;/li&gt;
&lt;li&gt;In a music playlist, songs are linked to the previous and next.&lt;/li&gt;
&lt;li&gt;Used in switching between applications and programs (Alt + Tab) in the Operating system (implemented using Circular Linked List)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t7Q6rmQf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244514202/XrQ8eN0_a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t7Q6rmQf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244514202/XrQ8eN0_a.png" alt="1_Failms_Mv7tZn65KSV-68A.png" width="880" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Binary Search
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Finding Page Number in a book, e.g. Target page number is 35, you open at page no. 15, its less, you move ahead and open 43, it is greater, you again move backward.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GVRws9Ev--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244766864/TSlKl28m_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GVRws9Ev--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244766864/TSlKl28m_.png" alt="binary-search.png" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Stacks
&lt;/h2&gt;

&lt;p&gt;A queue is a data structure that uses LIFO order.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Undo/Redo button/operation in word processors.&lt;/li&gt;
&lt;li&gt;Wearing/Removing Bangles.&lt;/li&gt;
&lt;li&gt;Pile of Dinner Plates.&lt;/li&gt;
&lt;li&gt;Stacked chairs.&lt;/li&gt;
&lt;li&gt;Changing wearables on a cold evening, first in, comes out at last.&lt;/li&gt;
&lt;li&gt;Last Hired, First Fired which is typically utilized when a company reduces its workforce in an economic recession.&lt;/li&gt;
&lt;li&gt;Loading bullets into the magazine of a gun. The last one to go in is fired first. Bam!&lt;/li&gt;
&lt;li&gt;Scratch cards earned after Google pay transaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KmNb9G0G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244817228/fgXFrdK1G.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KmNb9G0G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244817228/fgXFrdK1G.png" alt="word-image.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Queues
&lt;/h2&gt;

&lt;p&gt;A queue is a data structure that uses FIFO order.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In Escalators.&lt;/li&gt;
&lt;li&gt;Printer spooler.&lt;/li&gt;
&lt;li&gt;Sending emails.&lt;/li&gt;
&lt;li&gt;Car washes queue.&lt;/li&gt;
&lt;li&gt;Server while responding to requests.&lt;/li&gt;
&lt;li&gt;Operating System uses queues for job/task scheduling.&lt;/li&gt;
&lt;li&gt;Data packets in communication are arranged in queue format.&lt;/li&gt;
&lt;li&gt;Stack/Queue is used in the back and forward buttons of the web browser.&lt;/li&gt;
&lt;li&gt;While switching multiple applications, windows uses a circular queue.&lt;/li&gt;
&lt;li&gt;A circular queue is used to maintain the playing sequence of multiple players in a game.&lt;/li&gt;
&lt;li&gt;A queue can be implemented in Linked List-based Queue, Array-based Queue, Stack-based Queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a0yVPlQL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244851943/WZhwKcO9v.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a0yVPlQL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244851943/WZhwKcO9v.jpg" alt="men-women-standing-long-queue-reception-3d-isometric-illustration_1284-55195.jpg" width="880" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Priority Queue
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Process scheduling in the kernel.&lt;/li&gt;
&lt;li&gt;Priority queues are used in file downloading operations in a browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tWoA8tJI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244921890/7DbUw1Hhs.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tWoA8tJI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244921890/7DbUw1Hhs.webp" alt="Introduction.webp" width="780" height="692"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sorting Algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Order things by their value.&lt;/li&gt;
&lt;li&gt;Backend Databases (Merge Sort).&lt;/li&gt;
&lt;li&gt;Playing Cards with your friends (Insertion Sort).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;sort()&lt;/strong&gt; uses IntroSort (a hybrid of Quicksort, Heapsort, and Insertion Sort), Faster than qsort().&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iNAt7U71--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244968337/LVGGtG7Ks.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iNAt7U71--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244968337/LVGGtG7Ks.jpg" alt="Sorting-Image.jpg" width="880" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Graph
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Facebook, Instagram, and all social media networking sites, every user is Node, use the graph to suggest friends.&lt;/li&gt;
&lt;li&gt;The GPS navigation system also uses shortest path APIs. Google Maps, Apple Maps, and Bing Maps.&lt;/li&gt;
&lt;li&gt;Reacts virtual DOM uses graph data structures.&lt;/li&gt;
&lt;li&gt;MS Excel uses DAG (Directed Acyclic Graphs).&lt;/li&gt;
&lt;li&gt;Path Optimization Algorithms, BFS, DFS.&lt;/li&gt;
&lt;li&gt;Recommendation Engines.&lt;/li&gt;
&lt;li&gt;Scientific Computations.&lt;/li&gt;
&lt;li&gt;Flight Networks.&lt;/li&gt;
&lt;li&gt;Page ranking.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ulbf9FJv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245177597/RvgLuj_ES.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ulbf9FJv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245177597/RvgLuj_ES.png" alt="1_IBtDZq0_4yWpXt0mhda0jw.png" width="880" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tree
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A decision-based algorithm is used in machine learning which works upon the algorithm of a tree.&lt;/li&gt;
&lt;li&gt;Databases also use B-Tree data structures for indexing.&lt;/li&gt;
&lt;li&gt;Domain Name Server (DNS) also uses tree structures.&lt;/li&gt;
&lt;li&gt;The file system of computer or mobile.&lt;/li&gt;
&lt;li&gt;Parsers(XML parser), Filesystem.&lt;/li&gt;
&lt;li&gt;Code Compression(zip).&lt;/li&gt;
&lt;li&gt;DOM in Html.&lt;/li&gt;
&lt;li&gt;Evaluate an expression (i.e., parse).&lt;/li&gt;
&lt;li&gt;Integral to compilers/automata theory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D9CMYTcF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244600679/VvZybKxgd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D9CMYTcF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663244600679/VvZybKxgd.jpg" alt="lllYzG60bDfxJ6CRQUI5FTjkqI7-acWR4vam2s901IU.jpg" width="880" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Binary Search Tree
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;3D Game Engine.&lt;/li&gt;
&lt;li&gt;Computer Graphics Rendering.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P6nV7m11--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245335990/VNt-rPJOX.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P6nV7m11--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245335990/VNt-rPJOX.webp" alt="bst-vs-not-bst.webp" width="880" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Trie
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Dictionary application.&lt;/li&gt;
&lt;li&gt;Autocomplete feature in searching.&lt;/li&gt;
&lt;li&gt;Auto-completing the text and spells checking.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gbM2nFdu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245852068/UM8hKF47y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gbM2nFdu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245852068/UM8hKF47y.jpg" alt="Inkedlearnersbucket.com-1-1_LI.jpg" width="768" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hash Table
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Data stored in databases is generally of the key-value format which is done through hash tables.&lt;/li&gt;
&lt;li&gt;Social network feeds.&lt;/li&gt;
&lt;li&gt;Every time we type something to be searched in google chrome or other browsers, it generates the desired output based on the principle of hashing.&lt;/li&gt;
&lt;li&gt;Message Digest, a function of cryptography also uses hashing for creating output in such a manner that reaching the original input from that generated output is almost next to impossible.&lt;/li&gt;
&lt;li&gt;In our computers we have various files stored in it, each file has two very crucial information that is, filename and file path, in order to make a connection between the filename to its corresponding file path hash tables are used.&lt;/li&gt;
&lt;li&gt;Password hashing.&lt;/li&gt;
&lt;li&gt;Used for fast data lookup symbol table for compilers, database indexing, caches, Unique data representation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5v18bXdw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245511574/P27Pkll0C.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5v18bXdw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245511574/P27Pkll0C.png" alt="hash-table.png" width="880" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Heap
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Systems concerned with security and embedded systems such as Linux Kernel uses Heap Sort because of the O( n log(n) ).&lt;/li&gt;
&lt;li&gt;If we are stuck in finding the K-th smallest (or largest) value of a number then heaps can solve the problem in an easy and fast manner.&lt;/li&gt;
&lt;li&gt;Used by JVM (Java Virtual Machine) to store Java objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8JXi49VG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245917682/V4cKMYlvM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8JXi49VG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663245917682/V4cKMYlvM.png" alt="1mghTRv.png" width="880" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Greedy Algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Dijkstra algorithm.&lt;/li&gt;
&lt;li&gt;Shopping on a tight budget but want to buy gifts for all family members.&lt;/li&gt;
&lt;li&gt;Prims and Kruskals algorithms are used for finding the minimum spanning trees.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hZx80eGy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663246084831/MRnKKMVXu.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hZx80eGy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663246084831/MRnKKMVXu.webp" alt="Thumbnail_Greedy_Problem.webp" width="880" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  DIJKSTRA Algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Used in applications like Google Maps to find the shortest path in a graph.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X3CiPcAH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663246205760/P_RHXvW6v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X3CiPcAH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663246205760/P_RHXvW6v.png" alt="90Qwu.png" width="880" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamic Programming
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In Google Maps to find the shortest path between the source and the series of destinations (one by one) out of the various available paths.&lt;/li&gt;
&lt;li&gt;In networking to transfer data from a sender to various receivers in a sequential manner.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z3T7YVaK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663246318723/fnwudfhGg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z3T7YVaK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663246318723/fnwudfhGg.png" alt="5647129569656832.png" width="880" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Backtracking
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Suppose we are coding a chess-playing algorithm and at a certain point, the algorithm finds that a set of steps fails to win. In this situation, the algorithm will reverse back to the safe state and try another possible set of steps.&lt;/li&gt;
&lt;li&gt;Sudoku solver&lt;/li&gt;
&lt;li&gt;2048 game&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8uVyr3Uc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663246355494/0WJIN21Vd.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8uVyr3Uc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663246355494/0WJIN21Vd.webp" alt="ba-state-space-tree.webp" width="844" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note I cannot claim 100% copyright on the content, some examples are taken from other sources over Quora(Igor Markov).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading.💗 Connect me on &lt;a href="https://www.linkedin.com/in/sagar-medtiya/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>✅𝐔𝐧𝐝𝐞𝐫𝐬𝐭𝐚𝐧𝐝𝐢𝐧𝐠 𝐄𝐯𝐞𝐧𝐭s 𝐢𝐧 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Tue, 13 Sep 2022 12:31:24 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/s-1b24</link>
      <guid>https://dev.to/sagarmedtiya/s-1b24</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wdi9PSfn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663051122838/GQfmRsgau.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wdi9PSfn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663051122838/GQfmRsgau.gif" alt="img" width="880" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What are events?
&lt;/h2&gt;

&lt;p&gt;Events allow you to write JavaScript code that reacts to certain situations. Examples of events include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user clicking the mouse button&lt;/li&gt;
&lt;li&gt;The Web page loading&lt;/li&gt;
&lt;li&gt;A form field being changed&lt;/li&gt;
&lt;li&gt;Playing video content&lt;/li&gt;
&lt;li&gt;Typing inside a search bar&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To react with those events we assign an event handler which is a callback function that runs when that event occurs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Handlers
&lt;/h2&gt;

&lt;p&gt;To allow you to run your bits of code when these events occur, JavaScript provides us with the &lt;strong&gt;event handler&lt;/strong&gt;. All the JavaScript handler starts with the word &lt;code&gt;on&lt;/code&gt;, and each event handler deals with the specific event. Some examples of the event handlers are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;onChange&lt;/strong&gt; - The data in the form element is changed by the user.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;onClick&lt;/strong&gt; - The object is clicked on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;onSubmit&lt;/strong&gt; - The user clicks the forms Submit button.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;onError&lt;/strong&gt; - A JavaScript error occurs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;onMove&lt;/strong&gt; - The user moves the browser window or frame.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Here is an example of using one event called &lt;code&gt;onclick&lt;/code&gt;.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button class="btn"&amp;gt;Click me!&amp;lt;/button&amp;gt;
const button = document.querySelector(".btn")

button.onclick = () =&amp;gt; {
  console.log("Button clicked.");
};

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;onclick&lt;/code&gt; event is triggered when the user clicks on the button. As a result &lt;strong&gt;Button clicked&lt;/strong&gt; is output into the console.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Listeners
&lt;/h2&gt;

&lt;p&gt;This is the &lt;em&gt;modern way&lt;/em&gt;. To listen the events on elements, we first need to fetch the element. We can then use the &lt;code&gt;addEventListener()&lt;/code&gt; method. The syntax to add a handler is this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;element.addEventListener(event, handler, [options]);&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;element&lt;/strong&gt; - specified element from &lt;strong&gt;HTML elements&lt;/strong&gt; , but also any other &lt;strong&gt;DOM&lt;/strong&gt; object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;event&lt;/strong&gt; - specified event type (eg: click, scroll, submit)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;handler&lt;/strong&gt; - the function to be called and executed when the event occurs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;options&lt;/strong&gt; - optional object[&lt;strong&gt;capture, once, passive&lt;/strong&gt;] specifies characteristics about the event listener. &lt;code&gt;{capture: false}&lt;/code&gt; is the default value and it watches an event in a bubbling phase. &lt;code&gt;{capture: true}&lt;/code&gt; watches an event in a capturing phase. &lt;code&gt;{once: true}&lt;/code&gt; is a useful option if you want to execute only once. So the attached event listener would be automatically removed when invoked. {passive: true} indicates that the function will never call the &lt;code&gt;preventDefault()&lt;/code&gt; method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can listen on &lt;code&gt;window&lt;/code&gt; to intercept &lt;strong&gt;global&lt;/strong&gt; events, like the usage of the &lt;strong&gt;keyboard&lt;/strong&gt; , and you can listen on specific elements to check events happening on them, like a &lt;strong&gt;mouse&lt;/strong&gt; click on a button.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;to learn more about options please refer to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener"&gt;MDN document&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To get how it works let's see some code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button class="myButton"&amp;gt;Click me!&amp;lt;/button&amp;gt;
const button = document.querySelector('.myButton'); // go get something
button.addEventListener('click', function(){ // listen for something
  console.log('button clicked'); // do something
});

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

&lt;/div&gt;



&lt;p&gt;Although the above code will work, but we can create a &lt;strong&gt;function&lt;/strong&gt; before the event listener.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handleClick() {
  console.log('button clicked');
}
button.addEventListener('click', handleClick);

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Event Object
&lt;/h3&gt;

&lt;p&gt;To properly use &lt;strong&gt;event&lt;/strong&gt; we dont just want to know if there was a &lt;strong&gt;clicked, scroll or keydown&lt;/strong&gt;. Wed like to know what was clicked? Where was the Y position when scrolled? Which key was pressed? Event Object is where we can get some answers.&lt;/p&gt;

&lt;p&gt;When an event happens, the browser creates an &lt;strong&gt;Event Object&lt;/strong&gt; that holds information on that event and passes it as an argument to the handler.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Properties of &lt;code&gt;event&lt;/code&gt; object:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;event.type&lt;/code&gt; - Event type, such as &lt;strong&gt;"click", "keydown"&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;event.currentTarget&lt;/code&gt; : Element that the event was attached to.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;event.target&lt;/code&gt;: Element where the event actually happened. ( this is useful in &lt;strong&gt;Event delegation&lt;/strong&gt; where you have a single handler for the repetitive event on a common parent.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;event.isTrusted&lt;/code&gt; : Boolean tells whether the event was generated by a user action or created or modified by a script.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's jump right into the code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button class="coupon"&amp;gt;Coupon code for &amp;lt;strong&amp;gt;shoes&amp;lt;/strong&amp;gt;&amp;lt;/button&amp;gt;
&amp;lt;button class="coupon"&amp;gt;Coupon code for &amp;lt;strong&amp;gt;clothes&amp;lt;/strong&amp;gt;&amp;lt;/button&amp;gt;
const couponButtons = document.querySelectorAll('button.coupon')
function handleCouponButtonClick(){
  console.log(event.target); // logs &amp;lt;strong&amp;gt;shoes&amp;lt;/strong&amp;gt;
  console.log(event.currentTarget); // logs &amp;lt;button class="coupon"&amp;gt;Coupon code for &amp;lt;strong&amp;gt;shoes&amp;lt;/strong&amp;gt;&amp;lt;/button&amp;gt;
  console.log('coupon button clicked');
}
couponButtons.forEach(function(couponButton){
  couponButton.addEventListener('click', handleCouponButtonClick);
});

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

&lt;/div&gt;



&lt;p&gt;The log of &lt;code&gt;event.target&lt;/code&gt; outputs the node that triggered the event, in this case its the strong tag and its contents.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;event.currentTarget&lt;/code&gt; logs the element that the event listener is attached to, so we get a log of the entire button markup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Event propagation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Event propagation&lt;/strong&gt; is a term used when talking about event capturing and event bubbling. It relates to the order in which &lt;strong&gt;event handlers&lt;/strong&gt; are called when an element is nested inside another element and both have event listeners registered for the same type of event, for example, a click.&lt;/p&gt;

&lt;p&gt;To understand &lt;strong&gt;propagation&lt;/strong&gt; we need to understand how events are dispatched in the DOM. Event objects are dispatched to an event target, but before the dispatch can happen a propagation path needs to be determined.&lt;/p&gt;

&lt;p&gt;The propagation path is a list of current event targets that the event object needs to pass through. This path reflects the DOM structure of the document and the elements in order. The last item in this list is called the event target. Anything before the event target is often referred to as a target ancestor, with the last item before the target being the targets parent.&lt;/p&gt;

&lt;p&gt;Once a propagation path has been calculated the event object passes through one or more event phases. The three phases are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Capture phase&lt;/strong&gt; - the event object trickles down through the DOM from the &lt;code&gt;window&lt;/code&gt;, through the targets ancestors until it reaches the parent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Target phase&lt;/strong&gt; - the event object arrives at the event target.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bubbling phase&lt;/strong&gt; - the event object propagates through the targets ancestors back up to the window.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A nice way of remembering this is that events trickle down and bubble up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qgi6XW57--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663070709808/GCybqwOV1.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qgi6XW57--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1663070709808/GCybqwOV1.jpeg" alt="1_ofDtCgRrzurDUuPrmoIrlg.jpeg" width="606" height="581"&gt;&lt;/a&gt;&lt;em&gt;Source: w3.org&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button class="myButton"&amp;gt;Click me!&amp;lt;/button&amp;gt;
const myButton = document.querySelector('.myButton');
function handleButtonClick() {
  console.log('Button clicked');
}
myButton.addEventListener('click', handleButtonClick);
window.addEventListener('click', function(event) {
  console.log('Window clicked');
  console.log(event.bubbles); // logs true
});

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

&lt;/div&gt;



&lt;p&gt;When running the above code, if I click anywhere in the browser window the &lt;code&gt;console.log&lt;/code&gt; returns &lt;strong&gt;Window clicked&lt;/strong&gt; , however, if I click on the button then the logs return both Button clicked and Window clicked, in that order. As a result of &lt;strong&gt;bubbling&lt;/strong&gt; , the event is captured and handled by the inner element first (the button) and then propagated to the outer element (the window).&lt;/p&gt;

&lt;p&gt;Its worth noting that event bubbling doesnt occur with every kind of event. During propagation it is possible for a listener to know if an event bubbles by reading the .bubbles boolean property of the event object. The above code contains the line &lt;code&gt;console.log(event.bubbles)&lt;/code&gt;; which in this instance logs true.&lt;/p&gt;

&lt;p&gt;We can stop event propagation within any listener to prevent an ancestor elements event handlers from being notified about an event. We do this with the &lt;code&gt;stopPropagation()&lt;/code&gt; method.&lt;/p&gt;

&lt;h3&gt;
  
  
  Features of Event Listener
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Some of the features of the event listener method include:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;addEventListener()&lt;/strong&gt; method attaches an event handler to the specified element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This method attaches an &lt;strong&gt;event handler&lt;/strong&gt; to an element without overwriting existing event handlers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can add many &lt;strong&gt;event handlers&lt;/strong&gt; to one element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can &lt;strong&gt;add many&lt;/strong&gt; event handlers of the same type to one element, i.e two click events.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Event listeners can be added to any DOM object not only HTML elements. i.e the window object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;addEventListener()&lt;/strong&gt; method makes it easier to control how the event reacts to bubbling.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Which Should I Use, event listener or event handler?
&lt;/h2&gt;

&lt;p&gt;Generally, it is advised to use &lt;strong&gt;event listeners&lt;/strong&gt; over &lt;strong&gt;event handlers&lt;/strong&gt; because you can add multiple &lt;strong&gt;event listeners&lt;/strong&gt; for the same event.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://javascript.info/event-delegation"&gt;Event Delegation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://javascript.info/bubbling-and-capturing"&gt;Bubbling&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy coding💗&lt;/p&gt;

</description>
    </item>
    <item>
      <title>𝐒𝐩𝐫𝐞𝐚𝐝 𝐚𝐧𝐝 𝐑𝐞𝐬𝐭 𝐎𝐩𝐞𝐫𝐚𝐭𝐨𝐫 101🚀</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Mon, 12 Sep 2022 17:52:56 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/101-561f</link>
      <guid>https://dev.to/sagarmedtiya/101-561f</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q99zj-2p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3palqw92do3tkfsnxuw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q99zj-2p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3palqw92do3tkfsnxuw.gif" alt="Image description" width="880" height="461"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;strong&gt;Prerequisites:&lt;/strong&gt; &lt;a href="https://dev.to/sagarmedtiya/know-your-javascript-3519-temp-slug-8221759"&gt;JavaScript ES6&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript uses three dots (...) for both the rest and spread operators. But these two operators are not the same. It was introduced in ECMAScript 2015 and they allow us to write more concise and flexible code like most of the other ES6 features do.&lt;/p&gt;
&lt;h2&gt;
  
  
  🍁Spread
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The spread operator allows us to expand the collected elements of an iterable data type like strings, arrays, or objects into individual elements in places. It helps to create a list of elements from a list by spreading out the values of an iterable where zero or more arguments (function calls) or elements (array literals) or key-value pairs (object literals) are expected.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt; &lt;code&gt;var var_name = [...iterable];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The use of the spread operator is explained below,&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  🍀Function calls
&lt;/h3&gt;

&lt;p&gt;The spread operator can be used to take elements of an array and pass them as a list of arguments into a function.&lt;/p&gt;

&lt;p&gt;Let's jump to the example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;printAnimals =(Animal1, Animal2, Animal3)=&amp;gt;{
    console.log(Animal1 + ',' + Animal2 + ',' +Animal3);
};
(Animal1, Animal2, Animal3)=&amp;gt;{
    console.log(Animal1 + ',' + Animal2 + ',' +Animal3);
}
let animals = ['tiger', 'elephant', 'lion'];
printAnimals(...animals);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q0sQt_0o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662980697225/IDy0y-5c-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q0sQt_0o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662980697225/IDy0y-5c-.png" alt="image.png" width="880" height="314"&gt;&lt;/a&gt;The function &lt;code&gt;printAnimals&lt;/code&gt; takes in three parameters and we want to pass three arguments stored in &lt;code&gt;animal&lt;/code&gt; array. When we call the function, the spread operator unpacks the passed array of arguments, tiger, elephant, and lion.&lt;/p&gt;

&lt;h3&gt;
  
  
  🍀Array Literals
&lt;/h3&gt;

&lt;p&gt;Although we have &lt;code&gt;concat&lt;/code&gt; and &lt;code&gt;slice&lt;/code&gt; method the spread operator provides us a new quick method for merging and copying arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr1 = ['coffee','tea', 'milk']
const arr2 = ['juice', 'smoothie']
var beverages = arr1.concat(arr2); //without spread

var beverages = [...arr1,...arr2]; // with spread

console.log(beverages)

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gsoFCXai--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662992109256/TIpteNBzG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gsoFCXai--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662992109256/TIpteNBzG.png" alt="image.png" width="880" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above example shown, the spread operator is used to add the elements of the &lt;code&gt;arr2&lt;/code&gt; to the end of the &lt;code&gt;arr1&lt;/code&gt;. This process creates a new array non-destructively by concatenating two existing arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr1 = ['coffee','tea', 'milk']
var arrCopy1 = arr1.slice() //without spread

var arrCopy2 = [...arr1] //with spread

console.log(arrCopy1);
console.log(arrCopy2);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1wogyl9S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662993106184/QoA7lRpgj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1wogyl9S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662993106184/QoA7lRpgj.png" alt="image.png" width="880" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The example above copies &lt;code&gt;arr1&lt;/code&gt; into &lt;code&gt;arrCopy2&lt;/code&gt;. The changes you will do on the &lt;code&gt;arrCopy2&lt;/code&gt; will not affect the &lt;code&gt;arr1&lt;/code&gt;, this process makes an exact copy of an existing array.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;There are other use cases like,&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remove duplicate entries from Array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr1 = ['coffee','tea','milk','coffee','milk'];

const arr1Copy = [...new Set(arr1)];
console.log(arr1Copy)

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xu1YeRjo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662994389741/Wm99QDOg5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xu1YeRjo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662994389741/Wm99QDOg5.png" alt="Screenshot 2022-09-12 201240.png" width="880" height="86"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Convert string to Array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myBeverage = 'tea';
var bevArr = [...myBeverage]
console.log(bevArr)

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uWNU02c3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662994526877/sdv7YEqLM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uWNU02c3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662994526877/sdv7YEqLM.png" alt="Screenshot 2022-09-12 201421.png" width="880" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Find min max&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var max1 = Math.max(3,2,1,5,-10); // without spread

var mynums = [3,2,1,5,-10];
var max2 = Math.max(...mynums) // with spread
console.log(max2)

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HPatHRV1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662994569863/FupVgMrwE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HPatHRV1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662994569863/FupVgMrwE.png" alt="Screenshot 2022-09-12 201634.png" width="880" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🍀Object Literals
&lt;/h3&gt;

&lt;p&gt;Another use case of the spread operator is to use it in &lt;strong&gt;object literals&lt;/strong&gt;. Since ES2018, it is possible to use the spread operator to &lt;strong&gt;copy&lt;/strong&gt; and &lt;strong&gt;merge&lt;/strong&gt; objects:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N03E03x2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662994176554/wN_kHtzZ2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N03E03x2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662994176554/wN_kHtzZ2.png" alt="1_GTS3KItyV3vuvNpJP6F94g.png" width="880" height="345"&gt;&lt;/a&gt;The spread operator only provides a &lt;strong&gt;shallow copy&lt;/strong&gt; of its own enumerable properties from a provided object and then returns the result in a new object by excluding the prototype.&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Rest Operator
&lt;/h2&gt;

&lt;p&gt;The Rest operator is the opposite of Spread operator. While spread operator expands elements of an iterable, rest operator compresses them. It collects several elements. In functions when we require to pass &lt;code&gt;arguments&lt;/code&gt; but were not sure how many we have to pass, the rest parameter makes it easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function function_name(...arguments) {
    statements;
}

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

&lt;/div&gt;



&lt;p&gt;The rest parameter, which is the same syntax as the spread operator, can be used for &lt;strong&gt;destructuring&lt;/strong&gt; arrays and &lt;strong&gt;objects&lt;/strong&gt; to unpack their &lt;strong&gt;data&lt;/strong&gt; into different individual variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function log(a,...numbersToLog){
    console.log(a);
    console.log(numbersToLog);
}

log(1,2,3)

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--roeDaB1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662995525216/qmftyP4cr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--roeDaB1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662995525216/qmftyP4cr.png" alt="Screenshot 2022-09-12 203025.png" width="880" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, the rest parameter collected the rest of the elements of &lt;code&gt;log&lt;/code&gt; into a new array. Note that the rest parameter can only be the last parameter.&lt;/p&gt;

&lt;p&gt;The rest parameter can be very useful when we work with an indefinite number of &lt;strong&gt;parameters&lt;/strong&gt; in the functions. See the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add=(...numbers)=&amp;gt;{
    let answer = 0;
    for(let num of numbers){
        answer+= num; 
    }
    return answer;
}

add(1,23,4,5,6,7,8,9,4,5); //72
add(1,2,3) //6

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;add()&lt;/code&gt; function receives a rest parameter numbers and adds the arguments we pass into it when it is called. This makes functions more flexible and helps us to run them with as many arguments as we want. The first function call &lt;code&gt;add(1, 2, 3)&lt;/code&gt; returns 6 and the second function call &lt;code&gt;add(1,23,4,5,6,7,8,9,4,5)&lt;/code&gt; with more arguments returns 72. As shown in this example, the rest parameter can gather any number of arguments into an array.&lt;/p&gt;

&lt;p&gt;Remember that the arguments object is an array-like object but does not support array methods like &lt;code&gt;map()&lt;/code&gt; and &lt;code&gt;forEach()&lt;/code&gt; unless you convert it to a real array. Since the rest parameter gives us an array, those array methods can now be easily used.&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread%5C_syntax"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread\_syntax&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest%5C_parameters"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest\_parameters&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you have like this blog. Happy coding💗&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always remember, no one reached to the top in one shot. It took them a lot more struggle and hard work than you can imagine. So strive for knowledge, and keep moving forward. Thank you&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>10 𝐆𝐢𝐭𝐇𝐮𝐛 𝐑𝐞𝐩𝐨𝐬𝐢𝐭𝐨𝐫𝐢𝐞𝐬 𝐭𝐨 𝐌𝐚𝐤𝐞 𝐲𝐨𝐮 𝐚 10𝐱 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐞𝐫 🤩</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Mon, 12 Sep 2022 09:34:02 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/10-10-3ap7</link>
      <guid>https://dev.to/sagarmedtiya/10-10-3ap7</guid>
      <description>&lt;p&gt;These are the ten GitHub repository that can help you become a good developer.🚀&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--11YcjSCZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9m3cj5zubqdnes07mtr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--11YcjSCZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9m3cj5zubqdnes07mtr.jpg" alt="Image description" width="880" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Public APIs
&lt;/h2&gt;

&lt;p&gt;👉 The Public APIs repository is a collective list of free APIs for software and web development use. APIs are integral to development, and this repository finding useful APIs makes it easy for developers to find free and relevant APIs in the category. In addition, the list is constantly updated with newer APIs in the public domain.&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/public-apis/public-apis"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Developer Roadmaps
&lt;/h2&gt;

&lt;p&gt;👉 The most complete roadmap, that will help you to grasp the bigger picture of the development landscape, its main technologies, and the recommended sequence of learning things.&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/kamranahmedse/developer-roadmap/blob/master/readme.md"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁The Algorithms
&lt;/h2&gt;

&lt;p&gt;👉 The Algorithm is an open-source resource for learning data structures, data algorithms and their implementation in any programming language. Data structures are the core for programming and developing, and this repository explores more than 34 languages, including Python, Java, Go, Java Plus, Lua, Rust, C++ and more.&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/TheAlgorithms"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁30Days of Javascript
&lt;/h2&gt;

&lt;p&gt;👉 Starter Files + Completed solutions for the JavaScript 30 Day Challenge by Wes Bos.&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/wesbos/JavaScript30"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Developer Handbook
&lt;/h2&gt;

&lt;p&gt;👉 An opinionated guide on how to become a professional Web/Mobile App Developer.&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/apptension/developer-handbook"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Web Fundamentals
&lt;/h2&gt;

&lt;p&gt;👉 Some of the best practices for modern web development, are provided by Google developers.&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/google/WebFundamentals"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Clean Code Javascript
&lt;/h2&gt;

&lt;p&gt;👉 Software engineering principles, from Robert C. Martin's book "Clean Code", adapted for JavaScript. A guide to producing readable, reusable, and refactorable software in JavaScript.&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/ryanmcdermott/clean-code-javascript"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Real World
&lt;/h2&gt;

&lt;p&gt;👉 Realworld allows you to choose any frontend (React, Vue, &amp;amp; more) and any backend (Node, Django, &amp;amp; more) and see how they power real-world, beautifully designed full-stack apps.&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/gothinkster/realworld"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Build your own X
&lt;/h2&gt;

&lt;p&gt;👉Build your own (insert technology here).&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/codecrafters-io/build-your-own-x"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁EbookFoundation
&lt;/h2&gt;

&lt;p&gt;👉 Freely available programming books.&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/EbookFoundation"&gt;GitHub repository here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you have like this small tutorial. Happy coding💗&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always remember, no one reached to the top in one shot. It took them a lot more struggle and hard work than you can imagine. So strive for knowledge, and keep moving forward. Thank you&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Comment down which repository you liked the most. 😎&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Automate your workflow with GitHub Actions🤖</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Mon, 12 Sep 2022 07:24:34 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/automate-your-workflow-with-github-actions-5ckf</link>
      <guid>https://dev.to/sagarmedtiya/automate-your-workflow-with-github-actions-5ckf</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TuaGcpj7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662665532323/iF7tpzyeK.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TuaGcpj7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662665532323/iF7tpzyeK.gif" alt="image" width="880" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  GitHub Action
&lt;/h2&gt;

&lt;p&gt;Let's get the full picture of what &lt;strong&gt;GitHub Action&lt;/strong&gt; is and break it down into several components,&lt;/p&gt;

&lt;p&gt;In any software projects, there are repetitive tasks that can be benefited by GitHub Actions. Code reviews, branch management, pull request labelling, regression testing, issue triaging, release management, npm package publication, and cloud deployment are examples of such tasks. These are hard to do manually or consistently.&lt;/p&gt;

&lt;h3&gt;
  
  
  🍁Workflows
&lt;/h3&gt;

&lt;p&gt;A workflow is an automated🤖 process composed of a series📃 of jobs that gets executed when its triggered by an event. Workflows are defined in YAML files and are stored in a &lt;code&gt;.github/workflows&lt;/code&gt; at the root of the repository. A repository can also have multiple workflows.&lt;/p&gt;

&lt;h3&gt;
  
  
  🍁Events
&lt;/h3&gt;

&lt;p&gt;An event is anything that can happen in GitHub repository. This includes pushing a code, creating a branch🌿, opening a pull request, and even commenting on an issue. There's a hell lot of triggers, you can have a look👁 &lt;a href="https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#webhook-events"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🍁Jobs
&lt;/h3&gt;

&lt;p&gt;A job is a series📃 of tasks that gets executed in a workflow upon being triggered by an event. Each step is either a script or a Github action. A workflow can have multiple jobs that run in parallel.&lt;/p&gt;

&lt;h3&gt;
  
  
  🍁Runners
&lt;/h3&gt;

&lt;p&gt;Runners are processes on a server that run the workflow when its triggered. Each runner is responsible for executing a given job. Runners are hosted in the cloud but they can also be self-hosted in custom cloud environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  🍁Actions
&lt;/h3&gt;

&lt;p&gt;Actions are individual tasks: they are called inside a job. Actions are used to perform complex tasks that you may call multiple times and import into your workflows. Some examples of actions are: installing node packages, hosting the code, running a script, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building a Workflow file
&lt;/h2&gt;

&lt;p&gt;Let's jump🦘 right into making a workflow and see how it's work,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Sample
on: 
  push:
    branches:
      - main
  schedule: 0 12 * * *
jobs:
  print_hello:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - with: 
            app_name: "hello world!"
            api_key: "1212@#!@!@$32323"
      - run: | 
          echo "Hello World!"
          echo "Hey!"

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

&lt;/div&gt;



&lt;p&gt;The workflow is defined in a &lt;strong&gt;YML&lt;/strong&gt; file inside the &lt;code&gt;workflows&lt;/code&gt; folders📂. Remember the proper indentation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;on: 
  push:
    branches:
      - main
  schedule: 0 12 * * *

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Inside the &lt;code&gt;on&lt;/code&gt; section, you have to add the events (&lt;code&gt;push , pull_request&lt;/code&gt;) as subsections. Then, each event is linked to a specific branch🌿 (&lt;code&gt;main , dev&lt;/code&gt; ).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;schedule&lt;/code&gt; this will be triggered on 12pm (UTC) each day.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jobs:
  print_hello:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - with: 
            app_name: "hello world!"
            api_key: "1212@#!@!@$32323"
      - run: | 
          echo "Hello World!"
          echo "Hey!"

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Tasks are defined inside &lt;code&gt;jobs&lt;/code&gt; section.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;print_hello&lt;/code&gt; is the name of the job. &lt;/li&gt;
&lt;li&gt;`&lt;code&gt;runs-on&lt;/code&gt;, this is where the job will run. Possible values are ubuntu-latest, windows-latest, ubuntu-16.04. There are many more versions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;steps&lt;/code&gt; contains list📃 of step. If one fails, the next steps are skipped. There are ways to trigger it to.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;uses&lt;/code&gt; use other's actions and publish yours. Here &lt;code&gt;actions&lt;/code&gt; is the username, &lt;code&gt;checkout&lt;/code&gt; is the repo name. &lt;code&gt;v1&lt;/code&gt; is the tag.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;with&lt;/code&gt; takes the input from outside to act on, you can pass this using &lt;code&gt;with&lt;/code&gt; keywords. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;run&lt;/code&gt; , this | enables us to use multiple commands, good for formatting. You can a list of commands to run.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why to use GitHub actions?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;GitHub Actions&lt;/strong&gt; enable you to use others actions in your workflow which just make your workflow file small and speed💨 up your workflow building steps. Also you can write your own actions and publish it to actions marketplace in GitHub that can be used by others.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some examples of useful workflows and actions
&lt;/h2&gt;

&lt;p&gt;Now that you've understand all the workings of the &lt;strong&gt;GitHub Actions&lt;/strong&gt;. Let's see some example and go through typical use cases,&lt;/p&gt;

&lt;h3&gt;
  
  
  🍁Updating latest blog post on README.md of GitHub
&lt;/h3&gt;

&lt;p&gt;If you need to update your blog post on GitHub's &lt;code&gt;README.md&lt;/code&gt; file, you can use this workflow by &lt;strong&gt;gautamkrishnar&lt;/strong&gt; and add it in &lt;code&gt;.github/workflows&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
name: Latest blog post workflow&lt;br&gt;
on:&lt;br&gt;
  schedule:&lt;br&gt;
    # Runs every hour&lt;br&gt;
    - cron: "0 */1 * * *"&lt;br&gt;
jobs:&lt;br&gt;
  update-readme-with-blog:&lt;br&gt;
    name: Update this repo's README with latest blog posts&lt;br&gt;
    runs-on: ubuntu-latest&lt;br&gt;
    steps:&lt;br&gt;
      - uses: actions/checkout@v2&lt;br&gt;
      - uses: gautamkrishnar/blog-post-workflow@master&lt;br&gt;
        with:&lt;br&gt;
          feed_list: "URL" #type your RSS URL&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;/p&gt;

&lt;h3&gt;
  
  
  🍁Deploy code on &lt;strong&gt;Heroku&lt;/strong&gt; whenever you push it to &lt;strong&gt;GitHub&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To deploy your code whenever you push it to GitHub, you can use this workflow to simply your hectic work. There are many more options like using docker etc, you can check it out &lt;a href="https://github.com/AkhileshNS/heroku-deploy"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
name: Deploy&lt;/p&gt;

&lt;p&gt;on:&lt;br&gt;
  push:&lt;br&gt;
    branches:&lt;br&gt;
      - master&lt;/p&gt;

&lt;p&gt;jobs:&lt;br&gt;
  build:&lt;br&gt;
    runs-on: ubuntu-latest&lt;br&gt;
    steps:&lt;br&gt;
      - uses: actions/checkout@v2&lt;br&gt;
      - uses: akhileshns/&lt;a href="mailto:heroku-deploy@v3.6.8"&gt;heroku-deploy@v3.6.8&lt;/a&gt; # This is the action&lt;br&gt;
        with:&lt;br&gt;
          heroku_api_key: "HEROKU_API_KEY"&lt;br&gt;
          heroku_app_name: "APP_NAME" #Must be unique in Heroku&lt;br&gt;
          heroku_email: "YOUR_EMAIL"&lt;br&gt;
          appdir: "FOLDER_NAME"&lt;br&gt;
          procfile: "web: npm start"&lt;br&gt;
          justlogin: true&lt;br&gt;
          working-directory: "FOLDER_DIRECTORY"&lt;br&gt;
          run: |&lt;br&gt;
            heroku container:login&lt;br&gt;
            heroku container:push --recursive -a "APP_NAME"&lt;br&gt;
            heroku container:release -a "APP_NAME" web&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;/p&gt;

&lt;p&gt;If youre curious about what you can do, you can check the following resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References📚:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.github.com/en/actions"&gt;https://docs.github.com/en/actions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/AkhileshNS/heroku-deploy"&gt;https://github.com/AkhileshNS/heroku-deploy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you have like this small tutorial. Happy coding💗&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always remember, no one reached to the top in one shot. It took them a lot more struggle and hard work than you can imagine. So strive for knowledge, and keep moving forward. Thank you&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>Meet my latest project, I built User Authentication using Passport.js and Bcrypt🎉</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Thu, 08 Sep 2022 19:10:13 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/meet-my-latest-project-i-built-user-authentication-using-passportjs-and-bcrypt-252p</link>
      <guid>https://dev.to/sagarmedtiya/meet-my-latest-project-i-built-user-authentication-using-passportjs-and-bcrypt-252p</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--slUxS_IR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662623226786/C2z7H51we.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--slUxS_IR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662623226786/C2z7H51we.jpg" alt="image" width="880" height="462"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;strong&gt;Prerequisites&lt;/strong&gt; : &lt;a href="https://dev.to/sagarmedtiya/know-your-javascript-3519-temp-slug-8221759"&gt;Javascript ES6&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  🍁Passport.js
&lt;/h2&gt;

&lt;p&gt;It is an authentication middleware for Express-based app using Node.js. Being a middleware, it has access to request and response object and can manipulate them into request-response cycle. The passport.js library consist of two separate libraries. The first is the main &lt;strong&gt;Passport JS&lt;/strong&gt; library, and the second is the relevant &lt;strong&gt;strategy&lt;/strong&gt; library.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The primary &lt;strong&gt;Passport JS&lt;/strong&gt; library is always required, and is used to maintain session information for authenticated users (i.e. you will import this library irrespective of the type of &lt;strong&gt;Strategy&lt;/strong&gt; that you will use to authenticate the user).&lt;/li&gt;
&lt;li&gt;The secondary &lt;strong&gt;strategy&lt;/strong&gt; library is dependent on the methodology you plan use to authenticate a user. eg. &lt;strong&gt;passport-local, passport-facebook, passport-oauth-google&lt;/strong&gt; etc. Users could be authenticated against a username/password saved in a database that you created either locally or on a cloud (called Local Strategy), OR users could be authenticated by having them log into their Google account (Google Authentication Strategy), or Facebook, or Github account.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Passport JS framework abstracts the Login process into 2 separate parts, the &lt;strong&gt;session management&lt;/strong&gt; (done by the &lt;strong&gt;Passport JS library&lt;/strong&gt; ), and the authentication (done by the secondary Strategy library eg. passport-local or passport-facebook or passport-oauth-google etc.)&lt;/p&gt;

&lt;p&gt;Here comes the express.js,&lt;/p&gt;

&lt;p&gt;The Passport JS library connects with the express-session library, and forms the basic scaffolding to attach the (authenticated) user information to the req.session object. The main Passport JS library deals with already authenticated users, and does not play any part in actually authenticating the users. Its sole purpose is to maintain (attach) the (already authenticated) user to sessions.&lt;/p&gt;
&lt;h2&gt;
  
  
  🍁Bcrypt
&lt;/h2&gt;

&lt;p&gt;It is very much important to have a secure system to save the password on the internet business. Saving password directly on the database is not recommended. There can be a security breach. In order to save the password without any security breach, and providing the users with the correct privacy of their personal data, we need to follow some protocols when building an app.&lt;/p&gt;

&lt;p&gt;There comes &lt;strong&gt;hashing&lt;/strong&gt; , developers usually run a function to convert this password into something that looks completely different from its original form through a mathematical algorithm. This process is called hashing and the mathematical algorithm is called hashing algorithm. The famous package of node.js is &lt;strong&gt;"bcrypt"&lt;/strong&gt; and it is downloaded on an average of 590,489 times a week (oh yeah, thats a lot 💰).&lt;/p&gt;

&lt;p&gt;Bcrypt also introduces salt technique. In order to increase the complexity of password security and protect users passwords from the attacks, an additional step called password salting is taken. &lt;strong&gt;Salting&lt;/strong&gt; a hash, in the field of cryptography, actually means to add an additional string of 32 or more characters to the password before it gets hashed.&lt;/p&gt;

&lt;p&gt;Lets demonstrate it with an example.&lt;/p&gt;

&lt;p&gt;Michael and Bob both use the same password &lt;code&gt;s@1t3dH@shBrown&lt;/code&gt; by coincidence, they will also have the same hash: &lt;code&gt;$2a$12$xdWgQ5mhv8rSaUK3qdusTO4XdMFbQi6TD/1VvOZjvGm10RXnhZZa2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However, if Michaels password is salted with &lt;code&gt;Iwx2ZE&lt;/code&gt;and Bobs password is salted with &lt;code&gt;0DoVej&lt;/code&gt;, they will have completely different salted hashes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Michael&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Password: &lt;code&gt;s@1t3dH@shBrown&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Salt: &lt;code&gt;Iwx2ZE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Salted Input: &lt;code&gt;Iwx2ZEs@1t3dH@shBrown&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Salted Hash Output: &lt;code&gt;$2a$12$TGRg8FCZvnDm.f4WPNtWQucwRv5zsi4D9Qy/gYgpfFfYx9XpXdE6a&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bob&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Password: &lt;code&gt;s@1t3dH@shBrown&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Salt: &lt;code&gt;0DoVej&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Salted Input: &lt;code&gt;0DoVejs@1t3dH@shBrown&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Salted Hash Output: &lt;code&gt;$2a$12$VtpXTHf69x1db/71bGHl3eMiEDAkgQe/Gq6UeNOKuHvdg.WnIXEHa&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As you can see, their salted hash outputs are quite different even though they share the same password.&lt;/p&gt;
&lt;h2&gt;
  
  
  🍁Let's build User Authentication
&lt;/h2&gt;
&lt;h3&gt;
  
  
  🍀Install the required package and run the server
&lt;/h3&gt;

&lt;p&gt;Type &lt;code&gt;npm init&lt;/code&gt; and install the below packages,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;express&lt;/li&gt;
&lt;li&gt;express-session&lt;/li&gt;
&lt;li&gt;passport&lt;/li&gt;
&lt;li&gt;passport-local&lt;/li&gt;
&lt;li&gt;bcryptjs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now create a file, &lt;code&gt;app.js&lt;/code&gt;, and add the following code to create the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const app = express();
const PORT = 3000;
app.listen(PORT, () =&amp;gt; {
console.log(`Listening on port ${PORT}`);
});

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

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;You can use &lt;code&gt;dotenv&lt;/code&gt; to store the PORT number.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🍀Set up the Database
&lt;/h3&gt;

&lt;p&gt;We'll use mongoDB, so let's install it&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i mongoose&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;create db.js file and configure the database&lt;br&gt;
&lt;/p&gt;

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

function Dbconnect(){

    const url = mongodb://localhost:27017/user;
    mongoose.connect(url,{ useNewUrlParser: true, useUnifiedTopology: true
        }).then(()=&amp;gt;{
            console.log('Connection Successful');
        }).catch((error)=&amp;gt;{     
            console.log('Something went wrong', error)
        });
}

module.exports = Dbconnect

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

&lt;/div&gt;



&lt;p&gt;Now, create UserModel.js and add the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose')
const {Schema} = mongoose
const UserSchema = new Schema ({
 email: {
 type: String,
 required: true
 },
 password: {
 type: String,
 required: true
 }
})
const UserModel = mongoose.model('user', UserSchema);
module.exports = UserModel;

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

&lt;/div&gt;



&lt;p&gt;Before storing the password, you need to encrypt it for security purposes. You will use &lt;strong&gt;bcrypt&lt;/strong&gt; , a very useful npm package that makes working with encrypted passwords easy.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i bcrypt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Modify UserModel.js to encrypt the password before saving it to the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose')
const bcrypt = require('bcryptjs');
const {Schema} = mongoose

const UserSchema = new Schema ({
...
})
UserSchema.pre('save', async function(next) {
 try {
 // check method of registration
 const user = this;
 if (!user.isModified('password')) next();
 // generate salt
 const salt = await bcrypt.genSalt(10);
 // hash the password
 const hashedPassword = await bcrypt.hash(this.password, salt);
 // replace plain text password with hashed password
 this.password = hashedPassword;
 next();
 } catch (error) {
 return next(error);
 }
 });
...
const User = mongoose.model('User', UserSchema);

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

&lt;/div&gt;



&lt;p&gt;Here we are using a pre save hook to modify the password before it is saved. The idea is to store the hash version of the password instead of the plain text password. A hash is a long complex string generated from a plain text string.&lt;/p&gt;

&lt;p&gt;Use &lt;strong&gt;isModified&lt;/strong&gt; to check whether the password is changing since you only need to hash new passwords. Next, generate a salt and pass it with the plain text password to the hash method to generate the hashed password. Finally, replace the plain text password with the hashed password in the database.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;app.js&lt;/strong&gt; , connect to the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// connect to db
const db = require('./db');
db.connect();

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍀Set up Passport
&lt;/h3&gt;

&lt;p&gt;Install &lt;strong&gt;Passport&lt;/strong&gt; and &lt;strong&gt;passport-local&lt;/strong&gt;. You will use these packages to register and login users.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i passport
npm i passport-local

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

&lt;/div&gt;



&lt;p&gt;Create a new file, passportConfig.js, and import passport-local and the UserModel.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const LocalStraregy = require("passport-local").Strategy;
const User = require("./userModel");

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

&lt;/div&gt;



&lt;p&gt;Configure Passport to handle user registration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const LocalStrategy = require("passport-local");
const User = require("./userModel");
module.exports = (passport) =&amp;gt; {
 passport.use(
 "local-signup",
 new LocalStrategy(
 {
 usernameField: "email",
 passwordField: "password",
 },
 async (email, password, done) =&amp;gt; {
 try {
 // check if user exists
 const userExists = await User.findOne({ "email": email });
 if (userExists) {
 return done(null, false)
 }
 // Create a new user with the user data provided
 const user = await User.create({ email, password });
 return done(null, user);
 } catch (error) {
 done(error);
 }
 }
 )
 );
}

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

&lt;/div&gt;



&lt;p&gt;In the above code, you are checking if the email is already in use. If the email does not exist, register the user. Note that you are also setting the username field to accept an email. By default, &lt;strong&gt;passport-local&lt;/strong&gt; expects a username, so you need to tell it you are passing in an email instead.&lt;/p&gt;

&lt;p&gt;Use &lt;strong&gt;passport-local&lt;/strong&gt; to also handle user login.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = (passport) =&amp;gt; {
 passport.use(
 "local-signup",
 new localStrategy(
 ...
 )
 );
passport.use(
 "local-login",
 new LocalStrategy(
 {
 usernameField: "email",
 passwordField: "password",
 },
 async (email, password, done) =&amp;gt; {
 try {
 const user = await User.findOne({ email: email });
 if (!user) return done(null, false);
 const isMatch = await user.matchPassword(password);
 if (!isMatch)
 return done(null, false);
 // if passwords match return user
 return done(null, user);
 } catch (error) {
 console.log(error)
 return done(error, false);
 }
 }
 )
 );
};

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

&lt;/div&gt;



&lt;p&gt;Here, check whether the user exists in the database, and if they do, check if the password provided matches the one in the database. Note you also call the &lt;strong&gt;matchPassword()&lt;/strong&gt; method on the user model so go to &lt;strong&gt;UserModel.js&lt;/strong&gt; file and add it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UserSchema.methods.matchPassword = async function (password) {
 try {
 return await bcrypt.compare(password, this.password);
 } catch (error) {
 throw new Error(error);
 }
};

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

&lt;/div&gt;



&lt;p&gt;This method compares the password from the user and the one in the database and returns true if they match.&lt;/p&gt;

&lt;h3&gt;
  
  
  🍀Set Up Authentication Routes
&lt;/h3&gt;

&lt;p&gt;You now need to create the endpoints to which users will send data. First up is the signup route which will accept the email and password of a new user.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;app.js&lt;/strong&gt; , use the passport authentication middleware you just created to register the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post("/auth/signup",
 passport.authenticate(&amp;amp;apos;local-signup&amp;amp;apos;, { session: false }),
 (req, res, next) =&amp;gt; {
 // sign up
 res.json({
 user: req.user,
 });
 }
);

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

&lt;/div&gt;



&lt;p&gt;If successful, the signup route should return the created user.&lt;/p&gt;

&lt;p&gt;Next, create the login route.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post(
 "/auth/login",
 passport.authenticate(&amp;amp;apos;local-login&amp;amp;apos;, { session: false }),
 (req, res, next) =&amp;gt; {
 // login
 res.json({
 user: req.user,
 });
 }
);

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍀Add Protected Routes
&lt;/h3&gt;

&lt;p&gt;So far, you have used Passport to create a middleware that registers a user in the database and another that allows a registered user to sign in. Next, you will create an authorization middleware to protect sensitive routes using a JSON web token(JWT). To implement JWT authorization, you need to:&lt;/p&gt;

&lt;p&gt;Generate JWT token. Pass the token to the user. The user will send it back in authorization requests. Verify the token sent back by the user.&lt;/p&gt;

&lt;p&gt;You will use the &lt;strong&gt;jsonwebtoken&lt;/strong&gt; package to handle JWTs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i jsonwebtoken&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, generate a &lt;strong&gt;token&lt;/strong&gt; for each user that successfully logs in.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;app.js&lt;/strong&gt; , import &lt;strong&gt;jsonwebtoken&lt;/strong&gt; and modify the login route like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post(
 "/auth/login",
 passport.authenticate(&amp;amp;apos;local-login&amp;amp;apos;, { session: false }),
 (req, res, next) =&amp;gt; {
 // login
 jwt.sign({user: req.user}, 'secretKey', {expiresIn: '1h'}, (err, token) =&amp;gt; {
 if(err) {
 return res.json({
 message: "Failed to login",
 token: null,
 });
 }
 res.json({
 token
 });
 })
 }
);

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

&lt;/div&gt;



&lt;p&gt;In a real-life application, you would use a more complicated secret key and store it in a configuration file.&lt;/p&gt;

&lt;p&gt;The login route returns a token if successful.&lt;/p&gt;

&lt;p&gt;Use &lt;strong&gt;passport-jwt&lt;/strong&gt; to access protected routes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i passport-jwt&lt;/code&gt; In &lt;strong&gt;passportConfig.js&lt;/strong&gt; , configure the &lt;strong&gt;passport-jwt&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const JwtStrategy = require("passport-jwt").Strategy;
const { ExtractJwt } = require("passport-jwt")
module.exports = (passport) =&amp;gt; {
passport.use(
 "local-login",
 new LocalStrategy(
 ...
 );
passport.use(
 new JwtStrategy(
 {
 jwtFromRequest: ExtractJwt.fromHeader("authorization"),
 secretOrKey: "secretKey",
 },
 async (jwtPayload, done) =&amp;gt; {
 try {
 // Extract user
 const user = jwtPayload.user;
 done(null, user);
 } catch (error) {
 done(error, false);
 }
 }
 )
 );
};

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

&lt;/div&gt;



&lt;p&gt;Notice you are extracting the JWT from the authorization header instead of the request body. This prevents hackers from intercepting a request and grabbing the token.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Now You're Ready to Take Your User Authentication to the Next Level&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I hope you have like this small tutorial. Happy coding💗&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always remember, no one reached to the top in one shot. It took them a lot more struggle and hard work than you can imagine. So strive for knowledge, and keep moving forward. Thank you&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>Understanding React🚀-Redux and using Redux Toolkit🚀</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Mon, 05 Sep 2022 19:48:37 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/understanding-react-redux-and-using-redux-toolkit-pbf</link>
      <guid>https://dev.to/sagarmedtiya/understanding-react-redux-and-using-redux-toolkit-pbf</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1As6iO5V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662357347401/wVltWLfy3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1As6iO5V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662357347401/wVltWLfy3.gif" alt="image" width="880" height="461"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;strong&gt;Prerequisites&lt;/strong&gt; : &lt;a href="https://dev.to/sagarmedtiya/understanding-the-basics-of-react-hooks-ao8-temp-slug-6127684"&gt;React&lt;/a&gt;, &lt;a href="https://dev.to/sagarmedtiya/know-your-javascript-3519-temp-slug-8221759"&gt;Javascript ES6&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  🍁What is Redux?
&lt;/h2&gt;

&lt;p&gt;Redux is a state container for JavaScript app. It is mostly paired with redux, where it takes control of state away from React components and gives it to a centralised place called a store.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FPO3FWAt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662359474019/xZohws0Q2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FPO3FWAt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662359474019/xZohws0Q2.png" alt="1_A4aekTEykxIdS9oLPtLH_w.png" width="880" height="587"&gt;&lt;/a&gt; &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eF2G4xvC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662359480492/vTLB8xYBx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eF2G4xvC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662359480492/vTLB8xYBx.png" alt="1_2r3KY9vBtUBXXEQMl0Iiog.png" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's understand how redux works,&lt;/p&gt;

&lt;p&gt;The upper image represents React without Redux. Each circle represents component. When a component initiates a change (the blue circle), this change is communicated to the other components one step at a time. This may seem simple enough when we only have 10 components, but what about an app with 20, 50 or 100 components? As an app becomes larger, debugging can quickly becomes harder, and we lose sight of how information is passed from one component to another.&lt;/p&gt;

&lt;p&gt;On the lower is the same React app with Redux. In this, when a component initiates a change, that information goes straight from the blue circle to our store (the green circle). From there, the change is then communicated directly to all the components that need to update.&lt;/p&gt;
&lt;h2&gt;
  
  
  🍁Understand State management
&lt;/h2&gt;

&lt;p&gt;State management is a way to present the data you use in your application in a form of data structure. Therefore, it will be easier to exchange this data between components. It facilitates the communication between the different components and lets them know about any updated data in an easy way.&lt;/p&gt;

&lt;p&gt;In React, there are many different libraries for state management : Redux, Recoil, context api etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7USg5cGy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662364767102/8RIr6Omz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7USg5cGy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662364767102/8RIr6Omz3.png" alt="0_aCC5mlKd6KnY9Lm1.png" width="700" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above picture depicts the process which is involved during the state management process. Now the blue box represents boilerplate and not every developer looks forward to use Redux. But don't worry, I'll give simple analogy to make you understand properly.&lt;/p&gt;
&lt;h3&gt;
  
  
  🍀View
&lt;/h3&gt;

&lt;p&gt;It is nothing but the front-end of the application, what ever changes happens, it should reflect the same on the application. For analogy, consider there is a store of Jewellery in the front. You are standing in front of it and looking at it, if somebody purchase, or if the store gets some Jewellery, you see the changes being done in the shop. This is a View.&lt;/p&gt;
&lt;h3&gt;
  
  
  🍀Store
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Store basically keeps all your state management data&lt;/em&gt; in one place, so to understand in an analogy, a store which we talked about in the View keeps all the jewellery in its store. Some other store keeps other stuffs. Hence stores.&lt;/p&gt;
&lt;h3&gt;
  
  
  🍀Action
&lt;/h3&gt;

&lt;p&gt;In Redux term, it has to dispatch an action to the Reducer, so that the state management can be done according to the action sent, which will eventually be reflected in the View of our application. Now, we have seen two new things, which we need to understand, and these are: &lt;strong&gt;Action and Dispatch&lt;/strong&gt;. So to understand both of them, and how are they linked with each other. Assume Action as a thought which comes into your mind, when you enter inside the shop, for example, I want to buy a jewellery. And Dispatch is something which comes out of your mouth, basically your voice/command/request, which sends a message to the Shopkeeper on why have you come to the shop.&lt;/p&gt;
&lt;h3&gt;
  
  
  🍀Reducer
&lt;/h3&gt;

&lt;p&gt;In Redux language, it is something which makes changes to the the State object in the Store, to be reflected in the View. Sophisticated, right? Now let us understand it in the analogical way, now you have entered into the store, and asked (Dispatched) for buying a jewellery (Action). The request you have raised to a person who would be standing on the other side of the store, we call him/her a shopkeeper, but he/she also is a Reducer. Your next question would be how? Then let me tell you how, as soon as you raise a request, the shopkeeper picks up the desired item for you and gives it to you in exchange of money. Now let us neglect the money part for a while, and focus on the jewellery reduction. So what happened actually in between the process? The shopkeeper (Reducer), made changes in the store by reducing the number of jewellery from the store, and hence the View is also changed, that is, the number of jewellery in the shop is changed now.&lt;/p&gt;
&lt;h2&gt;
  
  
  🍁Redux-toolkit
&lt;/h2&gt;

&lt;p&gt;The Redux Toolkit package is intended to be the standard way to write Redux logic. To simply the things it is recommend to use Redux Toolkit. It was developed in order to resolve three common Redux concerns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configuring a Redux store is too complicated&lt;/li&gt;
&lt;li&gt;I have to add a lot of packages to get Redux to do anything useful&lt;/li&gt;
&lt;li&gt;Redux requires too much boilerplate code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's use Redux Toolkit to make To-Do app.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  🍀Setting up Redux Toolkit
&lt;/h3&gt;

&lt;p&gt;Now well set up Redux Toolkit and React-Redux. Start by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# NPM
npm install @reduxjs/toolkit
npm install react-redux

# Yarn
yarn add @reduxjs/toolkit
yarn add react-redux

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍀Set up the store
&lt;/h3&gt;

&lt;p&gt;Create the store.js file. Import the configureStore API from Redux Toolkit. We'll start by creating an empty Redux store, and exporting it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from '@reduxjs/toolkit'

export const store = configureStore({
  reducer: {},
})

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍀Configure Redux Store to React
&lt;/h3&gt;

&lt;p&gt;Once the store is created, we can make it available to our React components by putting a React-Redux around our application in src/store.js. Import the Redux store we just created, put a Provider around your , and pass the store as a prop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

import store from './store';
import { Provider } from 'react-redux'

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
    &amp;lt;Provider store={store}&amp;gt;
        &amp;lt;App /&amp;gt;
    &amp;lt;/Provider&amp;gt;
);

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍀Create a Redux Slicer
&lt;/h3&gt;

&lt;p&gt;Create file name Reducers/todoSlider.js and import &lt;code&gt;createSlice&lt;/code&gt; from Redux Toolkit Api.&lt;/p&gt;

&lt;p&gt;Slicer requires a string name to identify the slice, an initial state value, and one or more reducer functions to define how the state can be updated. Slicer allows us define initial state , create action and reducer at same place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice } from '@reduxjs/toolkit'
export const toDoSlider = createSlice({
 name: 'toDo',
 initialState: {
   todoList: [
     { id: 1, content: "Hit the gym" },
     { id: 2, content: "Meet George"}
   ]
 },
 reducers: {
   addToDo: (state, action) =&amp;gt; {
     let newTodoList = {
       id: Math.random(),
       content: action.payload.newContent
     }
     state.todoList.push(newTodoList);
   },
   deleteToDo: (state, action) =&amp;gt; {
     let { todoList } = state;
     state.todoList = todoList.filter((item) =&amp;gt; 
         item.id !==action.payload.id);
   },
   editTodo: (state, action) =&amp;gt; {
     let { todoList } = state;
     state.todoList = todoList.map((item) =&amp;gt; 
       item.id === action.payload.id ? action.payload : item);
   }
  },
})
// Action creators are generated for each case reducer function
export const { addToDo, deleteToDo, editTodo } = toDoSlider.actions
export default toDoSlider.reducer;

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

&lt;/div&gt;



&lt;p&gt;From above code you can observe initial state is define and reducers functions are created to perform add, update and delete operation.&lt;/p&gt;

&lt;h3&gt;
  
  
  🍀Add Slice Reducers to the Store
&lt;/h3&gt;

&lt;p&gt;Next, we need to import the reducer function from the counter slice and add it to our store inside src/store.js . By defining a field inside the reducer parameter, we tell the store to use this slice reducer function to handle all updates to that state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from '@reduxjs/toolkit'

export const store = configureStore({
  reducer: {
        toDo: toDoReducer;
    },
})

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍀Create components and import into app.js
&lt;/h3&gt;

&lt;p&gt;Create a file named src/Components/AddTodo.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { addToDo } from '../Reducers/toDoSlider';
const AddTodo = () =&amp;gt; {
  const dispatch = useDispatch();
  const [state, setState] = useState({
     content: '',
     contentError: null
  });
  const handleChange = (e) =&amp;gt;{
    setState({...state, 
          [e.target.name]: e.target.value,
          [`${e.target.name}Error`]: null });
  }
  const add = () =&amp;gt;{
    if(content === ''){
      setState({...state, 
         contentError: 'You must write something!'});
       return;
    }
    dispatch(addToDo({newContent: content}));
    setState({...state, content: ''});
  }
  const { content, contentError } = state;
   return &amp;lt;div className='form'&amp;gt;
      &amp;lt;h2&amp;gt;What's your plan for today&amp;lt;/h2&amp;gt;
      &amp;lt;input type='text' value={content} 
        name='content' 
        onChange={handleChange}&amp;gt;
      &amp;lt;/input&amp;gt;
      &amp;lt;button type='button' className='button' 
        onClick={add}&amp;gt;Add
      &amp;lt;/button&amp;gt;
      {contentError ? 
         &amp;lt;div className='error'&amp;gt;{contentError}&amp;lt;/div&amp;gt;: null}
    &amp;lt;/div&amp;gt;;
};
export default AddTodo;

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

&lt;/div&gt;



&lt;p&gt;From above code you can observe Redux hook useDispatch is use to dispatch addToDo action to update state.&lt;/p&gt;

&lt;p&gt;Create a file named src/Components/ListTodo.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import { AiFillEdit, AiOutlineCloseCircle } from "react-icons/ai";
import { useDispatch, useSelector } from 'react-redux';
import { deleteToDo, editTodo } from '../Reducers/toDoSlider';
const ListTodo = () =&amp;gt; {
  const { todoList } = useSelector((state) =&amp;gt; state.toDo);
  const dispatch = useDispatch();
  const [isEditing, setEditing] = useState(false);
  const [state, setState] = useState({
   id: '', content: '', contentError: null
  });
  const onEditToggle = ( id, content) =&amp;gt; {
   setEditing(true);
   setState({ ...state, id, content});
  }
  const handleChange = (e) =&amp;gt;{
   setState({...state, [e.target.name]: e.target.value,  
      [`${e.target.name}Error`]: null });
  }
  const { content, contentError, id } = state;
  const edit = () =&amp;gt;{
   if(content === ''){
    setState({...state, contentError: 'You must write something!'});
    return;
   }
   dispatch((editTodo({content, id})));
   setEditing(false);
  }
return &amp;lt;div&amp;gt;
 {
   isEditing ?
    &amp;lt;div className='form'&amp;gt;
      &amp;lt;h2&amp;gt;Update your plan for today&amp;lt;/h2&amp;gt;
      &amp;lt;input type='text' value={content} name='content' 
         onChange={handleChange}&amp;gt;
      &amp;lt;/input&amp;gt;
      &amp;lt;button type='button' className='button' 
        onClick={edit}&amp;gt;Edit
     &amp;lt;/button&amp;gt;
     {contentError ? 
       &amp;lt;div className='error'&amp;gt;{contentError}&amp;lt;/div&amp;gt;: null
     }
   &amp;lt;/div&amp;gt; :
   &amp;lt;ul className='todos'&amp;gt;
    {
      todoList.map(({id, content})=&amp;gt; {
        return &amp;lt;li className='grid' key={id}&amp;gt;
          &amp;lt;span className='content'&amp;gt;{content}&amp;lt;/span&amp;gt;
          &amp;lt;span className='todo-action'&amp;gt;  
            &amp;lt;AiOutlineCloseCircle className="close" 
              onClick={() =&amp;gt; dispatch(deleteToDo({id}))}
            /&amp;gt;
            &amp;lt;AiFillEdit className="edit" 
              onClick={() =&amp;gt;onEditToggle(id, content)} 
            /&amp;gt;
          &amp;lt;/span&amp;gt;
       &amp;lt;/li&amp;gt;
     })
    }
  &amp;lt;/ul&amp;gt;
  }
&amp;lt;/div&amp;gt;;
};
export default ListTodo;

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

&lt;/div&gt;



&lt;p&gt;In above component Redux hook useDispatch is use to dispatch deleteToDo, editTodo action and useSelector to get state from reducer.&lt;/p&gt;

&lt;p&gt;Import Components into App.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './App.css';
import AddTodo from './Components/AddTodo';
import ListTodo from './Components/ListTodo';
function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;AddTodo /&amp;gt;
      &amp;lt;ListTodo /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
export default App;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍀Finally, add styles inside app.css
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* variables */
:root{
  --primary: #FFC636;
  --secondary: #0A0B5B;
}
/* reset */
body,p,a,ul,li{
 margin: 0;
 padding: 0;
 text-decoration: none;
}
li{
 list-style-type: none;
}
/* base styles */
body{
 background: var(--secondary);
 overflow-x: hidden;
}
.button{
 background: none;
 border: 2px solid var(--primary);
 color: var(--primary);
 padding: 6px 12px;
 border-radius: 10px;
 text-transform: uppercase;
 box-shadow: 1px 2px 3px rgba(0,0,0,0.6);
 display: inline-block;
 font-size: 1em;
 margin-left: 5px;
}
.button:hover{
 color: #222;
 background: var(--primary);
}
input{
 background: rgba(255,255,255,0.05);
 padding: 10px 16px;
 border-radius: 10px;
 border: 2px solid #9893D8;
 color: #f2f2f2;
 font-size: 1em;
}
.error{
 color: rgb(187, 30, 30);
 text-align: left;
 margin-left: 2px;
}
/* fonts */
body{
 color: #f2f2f2;
}
/* mobile styles */
.App{
 margin: 5%;
}
.form{
 text-align: center;
}
.grid{
 display: grid;
 grid-template-columns: repeat(8, 1fr);
 gap: 10px;
 box-sizing: border-box;
}
.todos{
 margin-top: 20px;
}
.todos li{
 margin-top: 10px;
 background-color: #6767ab;
 padding: 3%;
 box-shadow: 1px 2px 3px honeydew;
 cursor: pointer;
}
.content{
 grid-column: 1/8;
}
.todo-action{
 display: flex;
 font-size: 1.5em
}
/* small tablet styles */
@media screen and (min-width: 620px){
 input{
  width: 300px;
 }
 .todos li{
   margin: 10px 20%;
   padding: 2%;
  }
 }
/* large tablet &amp;amp; laptop styles */
@media screen and (min-width: 960px){
.todos li{
  margin: 10px 25%;
 }
}
@media screen and (min-width: 1200px){
 .todos li{
  margin: 10px 33%;
  padding: 1%;
 }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍀Output
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RBGMwLbI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662406904386/wwnTqYEMt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RBGMwLbI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662406904386/wwnTqYEMt.png" alt="1_ROHEKdVW3EzM8ibTwPGv9Q.png" width="531" height="661"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you have liked this small tutorial. Happy coding💗&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always remember, no one reached to the top in one shot. It took them a lot more struggle and hard work than you can imagine. So strive for knowledge, and keep moving forward. Thank you&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>Learn to use Git and GitHub Efficiently!!!🚀🚀</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Thu, 01 Sep 2022 18:49:06 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/learn-to-use-git-and-github-efficiently-5174</link>
      <guid>https://dev.to/sagarmedtiya/learn-to-use-git-and-github-efficiently-5174</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U7OyA1GT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662027057890/uPor0KR21O.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U7OyA1GT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1662027057890/uPor0KR21O.png" alt="image" width="880" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁What is a Git?
&lt;/h2&gt;

&lt;p&gt;Git is an open-source distributed &lt;em&gt;version control system&lt;/em&gt;(DVCS). Especially, it is a system used to record changes to files over time, so you can see what all changes you have made later on.&lt;/p&gt;

&lt;p&gt;A version control system simply tracks the history of changes whenever people or team collaborate to do project together. As the project evolves team can test, run and contribute to the code with confidence that any version can be recovered anytime if something happens. Developers can review the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which changes were made?&lt;/li&gt;
&lt;li&gt;Who made the changes?&lt;/li&gt;
&lt;li&gt;When were the changes made?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🍁What is a GitHub?
&lt;/h2&gt;

&lt;p&gt;GitHub is a website and cloud-based service that helps developers store and manage their code, as well as track and control changes to their code. GitHub's interface is user-friendly enough so that the novice coders can take advantage of Git. Without GitHub, using Git generally requires a bit more technical savvy and use of the command line. With addition to that, anyone can SignIn and host a public repository freely which makes GitHub especially popular with open-source projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁How to install Git?
&lt;/h2&gt;

&lt;p&gt;You can download your Git version according to your OS. Here is the &lt;a href="https://git-scm.com/downloads"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Git Commands
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git init&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt; : &lt;code&gt;git init &amp;lt;directory&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git clone&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt; : &lt;code&gt;git clone &amp;lt;repo&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git add&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt; : &lt;code&gt;git add &amp;lt;directory&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git commit -m&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt; : &lt;code&gt;git commit -m &amp;lt;message&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git status&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt; : &lt;code&gt;git status&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git branch&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt; : &lt;code&gt;git branch&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git merge&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt; : &lt;code&gt;git merge &amp;lt;branch&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git pull&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It updates the local line of development with updates from its remote counterpart. Developers use this command if a teammate has made commits to a branch on a remote, and they would like to reflect those changes in their local environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt; : &lt;code&gt;git pull --rebase &amp;lt;remote&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git push&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It updates the remote repository with any commits made locally to a branch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt; : &lt;code&gt;git push &amp;lt;remote-repo&amp;gt; --all&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Move backward to the previous state
&lt;/h2&gt;

&lt;p&gt;If we found a bug, we must trace from the beginning of the code, looking into line per line of codes. Git simplifies our work with the move backward feature. Instead of looking from the beginning of works, we can track based on the previous commit before a bug was found. There are three commands to move back on git. It is used for our needs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout&lt;/code&gt;: it is like a time machine, we can restore the condition of the project file to the designated time. However, this is temporary. These are not stored in the git database&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git reset&lt;/code&gt;: this command makes us unable to go back to the future. We will lose our commits. After the git logs were reset, we need to write a new commit&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git revert&lt;/code&gt;: will take the existing file condition in the past, then merge them with the last commit&lt;/p&gt;

&lt;h2&gt;
  
  
  🍁Git Lifecycle
&lt;/h2&gt;

&lt;p&gt;The git lifecycle is divided into four states namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modified&lt;/strong&gt; : any changes didnt been marked yet. We can do anything here, manipulate files, create or delete a new folder, and other things.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Staged&lt;/strong&gt; : a condition when our changes have been marked but didnt been recorded yet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Committed&lt;/strong&gt; : folders or files successfully are been recorded into our .git folder.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🍁Start a new repository and publish it to GitHub
&lt;/h2&gt;

&lt;p&gt;Let's create a new repository and publish it to GitHub. Do not initialise the repository with a README, .gitignore or License. This empty repository will await your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# create a new directory, and initialize it with git-specific functions
git init my-repo

# change into the `my-repo` directory
cd my-repo

# create the first file in the project
touch README.md

# git isn't aware of the file, stage it
git add README.md

# take a snapshot of the staging area
git commit -m "add README to initial commit"

# provide the path for the repository you created on github
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git

# push changes to github
git push --set-upstream origin master
git push -u origin master (Alternative)

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

&lt;/div&gt;



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

&lt;p&gt;This is the end of our tutorial, coders! Learn more about the &lt;a href="https://education.github.com/git-cheat-sheet-education.pdf"&gt;git cheat sheet!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading this blog. Hope you have liked it!💗&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Some best techniques to Optimize your React App🐱‍💻</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Wed, 31 Aug 2022 11:17:12 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/some-best-techniques-to-optimize-your-react-app-2klg</link>
      <guid>https://dev.to/sagarmedtiya/some-best-techniques-to-optimize-your-react-app-2klg</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O9rwPyN7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661751226264/hhHEVM6R2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O9rwPyN7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661751226264/hhHEVM6R2.gif" alt="image" width="880" height="461"&gt;&lt;/a&gt;&lt;br&gt;
Improving the web🌐 application performance plays an important role to improve User Experience and keep users engaged. However, it can be challenging to optimize your React App for performance. This post will cover some techniques to improve your React code.&lt;/p&gt;

&lt;p&gt;Lets see what all important best optimization techniques we can apply to the React code.&lt;/p&gt;
&lt;h2&gt;
  
  
  🍀shouldComponentUpdate
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;shouldComponentUpdate&lt;/code&gt; is a lifecycle method in react. This method makes the react component to re-render whenever there are changes in state or props and that change will affect the output.&lt;/p&gt;

&lt;p&gt;For more detailed blog, you can have a look to this &lt;a href="https://blog.sagarmedtiya.me/component-lifecycle-in-reactjs#heading-shouldcomponentupdate"&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It helps in checking whether re-rendering is required or not. If re-rendering is not required then shouldComponentUpdate does not render the code. For example, if you want certain code to not re-render then this method can be of very handy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; It is encouraged to use functional components over class components. With functional component you can use &lt;code&gt;useMemo()&lt;/code&gt; and &lt;code&gt;React.memo&lt;/code&gt; hooks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function shouldComponentUpdate(nextProps, nextState){
    return nextProps.id !== this.props.id;
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  🍀Lazy Loading
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Thn-LCEM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661931273580/HEUf-aSWy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Thn-LCEM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661931273580/HEUf-aSWy.gif" alt="Lazy Loading.gif" width="880" height="461"&gt;&lt;/a&gt;Lazy loading is a great technique to improve your app's performance. The idea of lazy loading is to load a component only when it is needed. React comes bundled with &lt;code&gt;React.lazy&lt;/code&gt; API so that you can import a dynamic import to regular component.&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 plaintext"&gt;&lt;code&gt;const LazyComponent = React.lazy(() =&amp;gt; import('./LazyComponent'));

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

&lt;/div&gt;



&lt;p&gt;React.lazy takes a function that must call a dynamic import(). This will then return a Promise which resolves to a module with a default export containing a React component.&lt;/p&gt;

&lt;p&gt;Another way is to load components only when they are in the viewport. You can use this awesome library &lt;code&gt;react-loadable-visibility&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import LoadableVisibilty from 'react-loadable-visibility/react-loadable';
import MyLoader from './my-loader-component'

const LoadableComponent = LoadableVisibilty({
    loader:() =&amp;gt; import('./my-component'),
    loading:MyLoader
});

export default function APP(){
    return &amp;lt;LoadableComponent/&amp;gt;
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍁Suspense
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cb-H7ZUy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661931600965/IWjNifttU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cb-H7ZUy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661931600965/IWjNifttU.png" alt="Lazy Loading.png" width="880" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Suspense is an experimental feature at this time. Experimental features may change significantly and without a warning before they become a part of React.&lt;/p&gt;

&lt;p&gt;Suspense is an another technique used for lazy loading. It lets you "wait" for some code to load and lets you declaratively specify a loading state while waiting. Multiple lazy components can be wrapped with the suspense component. It takes a fallback property that accepts the react elements you want to render as the lazy component is being loaded.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// install the experimental version
npm install react@experimental react-dom@experimental

import React,{Component, lazy, Suspense} from 'react'
const MyComp = lazy(()=&amp;gt;import('../mycomp'))
&amp;lt;Suspense fallback={&amp;lt;div&amp;gt;Loading...&amp;lt;/div&amp;gt;}&amp;gt;
&amp;lt;div&amp;gt;
    &amp;lt;MyComp&amp;gt;&amp;lt;/MyComp&amp;gt;    
&amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Suspense is most popularly used for waiting for the data&lt;/li&gt;
&lt;li&gt;But it can also be used to wait for images, script or any asynchronous code.&lt;/li&gt;
&lt;li&gt;It helps you avoid race conditions 

&lt;ul&gt;
&lt;li&gt;Race conditions are bug that happen due to incorrect assumptions about the order in which our code can run.&lt;/li&gt;
&lt;li&gt;Suspense feels more like reading data synchronously -- as if it was already loaded.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h3&gt;
  
  
  🍁Suspense - Data Fetching
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It is used to wait for rendering component until the required data is fetched.

&lt;ul&gt;
&lt;li&gt;That means no need to add conditional code anymore!&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;You need to enable &lt;code&gt;concurrent mode&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;So the rendering is not blocked.&lt;/li&gt;
&lt;li&gt;This gives better user experience.
&lt;/li&gt;
&lt;/ul&gt;


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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// enable concurrent mode 
const rootEl = document.getElementById('root')

//ReactDOM.render(&amp;lt;APP/&amp;gt;, rootEl)
const root = ReactDom.createRoot(rootEl) //you'll use a new createRoot API

root.render(&amp;lt;APP/&amp;gt;)

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍁Render-as-you-Fetch
&lt;/h3&gt;

&lt;p&gt;This approach doesn't wait for the response to come back before we start rendering. We start rendering pretty much immediately after kicking off the network request.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start fetching&lt;/li&gt;
&lt;li&gt;Start rendering&lt;/li&gt;
&lt;li&gt;finish fetching
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const resource = fetchCartData();

function CartPage(){
    return (
        &amp;lt;Suspense fallback={&amp;lt;h1&amp;gt;Loading Cart...&amp;lt;/h1&amp;gt;}&amp;gt;
            &amp;lt;CartDetails/&amp;gt;
        &amp;lt;/Suspense&amp;gt;
    );
}
function CartDetails(){
    //Try to read product info, although it might not have loaded yet
    const product = resource.product.read();
    return &amp;lt;h1&amp;gt;{product.name}&amp;lt;/h1&amp;gt;
}

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;CartPage()&lt;/code&gt; is loaded. It tries to load &lt;code&gt;CartDetails()&lt;/code&gt;, but &lt;code&gt;CartDetails()&lt;/code&gt; makes call to &lt;code&gt;resource.product.read()&lt;/code&gt; so this component &lt;code&gt;suspenses&lt;/code&gt;. React shows the fallback loader and keep fetching the data in the background. When all the data is retrieved the fallback loader is replaced by &lt;code&gt;CartDetails()&lt;/code&gt; children.&lt;/p&gt;

&lt;h2&gt;
  
  
  🍀Memoization
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RK-KkfJD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661931796640/vcq4IPbeK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RK-KkfJD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661931796640/vcq4IPbeK.png" alt="Lazy Loading (1).png" width="880" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Memoizing in React is a performance feature of the framework that aims to speed up the render process of component. It is a well know concept in programming, aiming to speed up the program by caching results of the expensive function and re-using those cached result as to avoid rendering of expensive function again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MW8gaMR9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661935070366/ImRjAmClU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MW8gaMR9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661935070366/ImRjAmClU.png" alt="Lazy Loading (2).png" width="880" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Memoization in React is not guarantee that your function will be cached&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🍁When to use it?
&lt;/h3&gt;

&lt;p&gt;Suppose your state object is updating but your value is not really changing, you can use &lt;code&gt;memo&lt;/code&gt; to not re-render your functional component. If your component just takes primitive value as props, just wraps it in &lt;code&gt;memo()&lt;/code&gt; to prevent an unwanted re-render.&lt;/p&gt;

&lt;p&gt;React.memo() by default just compares the top-level props. It is not that simple for nested objects. For nested objects, you can pass custom comparer function to check if prop values are same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default React.memo((props)=&amp;gt;{
    return (&amp;lt;div&amp;gt;{props.val}&amp;lt;/div&amp;gt;)
})

//for nested object
const MemoedElement = React.memo(Element, areEqual) 

export function areEqual(prevProps: props, nextProps:props){
    const cardId = nextProps.id
    const newActiveCardId = nextProps.newActiveCardId
    const isActive = newActiveCardId == cardId

    return !some([
        isActive,
    ])
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🍁useMemo
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;React.memo&lt;/code&gt; is used to memoize components. You can use &lt;code&gt;useMemo&lt;/code&gt; to memoize inner variable. If there's CPU intensive operation going on to calculate those variables, and the variables does not really change that often then you can use &lt;code&gt;useMemo&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const allItems = getItems()
//CPU intensive logic
const itemCategories = useMemo(()=&amp;gt; getUniqueCategories(allItems),[allItems])

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  🍀Points to remember
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Measure performance using these tools

&lt;ul&gt;
&lt;li&gt;Chrome dev tools
&lt;/li&gt;
&lt;/ul&gt;


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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Play with the throttle feature
- Check out the performance timeline and flame charts

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Chrome's Lighthouse tool

&lt;ul&gt;
&lt;li&gt;Minimise unnecessary component re-renders&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;use shouldComponentUpdate where applicable&lt;/li&gt;
&lt;li&gt;use PureComponent&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;React.memo&lt;/code&gt; for functional components
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- along with the ```

useMemo()

``` hook

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;use &lt;code&gt;React.lazy&lt;/code&gt; if you not doing server-side rendering&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;service worker&lt;/code&gt; to cache files that are worth caching&lt;/li&gt;
&lt;li&gt;use libraries like &lt;code&gt;react-snap&lt;/code&gt; to pre-render components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading. Like for more such blogs. Happy coding💗&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Component Lifecycle🌀 in ReactJS🚀</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Fri, 26 Aug 2022 18:57:31 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/component-lifecycle-in-reactjs-3i7f</link>
      <guid>https://dev.to/sagarmedtiya/component-lifecycle-in-reactjs-3i7f</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---KdcQxn2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661493137137/P7DgU8_4I.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---KdcQxn2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661493137137/P7DgU8_4I.gif" alt="image" width="880" height="461"&gt;&lt;/a&gt;&lt;br&gt;
In React, component are designed to follow the natural cycle🚲 of life. As everyone goes through a cycle of born, grow and die. Each component in react goes through &lt;strong&gt;Mounting&lt;/strong&gt; , &lt;strong&gt;Updating&lt;/strong&gt; and &lt;strong&gt;Unmounting&lt;/strong&gt;. Below is a good illustration of how react lifecycle works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WTqAGISF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661499700211/OehGybRp1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WTqAGISF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661499700211/OehGybRp1.png" alt="image.png" width="880" height="493"&gt;&lt;/a&gt;&lt;em&gt;Image Source: &lt;a href="https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/"&gt;wojtekmaj&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt; provides access to certain built-in events/methods called lifecycle🌀 hooks or lifecycle methods. These methods give you opportunities to control and manipulate how a component reacts to changes in the application.&lt;/p&gt;

&lt;p&gt;Lets have a look at each phase in a component lifecycle:&lt;/p&gt;
&lt;h2&gt;
  
  
  🍀Initialization
&lt;/h2&gt;

&lt;p&gt;This is the first stage in react lifecycle, in this stage🪜 the component is constructed with provided props and default states. This is where we define defaults and initial values for &lt;code&gt;this.props&lt;/code&gt; and &lt;code&gt;this.state&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  🍀Mounting
&lt;/h2&gt;

&lt;p&gt;This is the next state🪜 in React Lifecycle. After you have prepared the basic code requirement, states and props, you need to mount on the browser. This is done via browser DOM. React uses virtual DOM to put all the elements into the memory. Note: Child component is mounted before the parent component.&lt;/p&gt;
&lt;h3&gt;
  
  
  🌿constructor
&lt;/h3&gt;

&lt;p&gt;constructor()👷 method is called when the component is initiated and its the best place to initialize our state. The constructor method takes props as an argument and starts by calling &lt;code&gt;super(props)&lt;/code&gt; before anything else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Todo extends React.Component {
  //Setting the initial state of the Component
  constructor() {
    super();
    this.state = {
      task: "",
      isCompleted: false,
      dateCreated: ""
    }
  }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  🌿componentDidMount
&lt;/h3&gt;

&lt;p&gt;This is the final ReactJS hook🪝 method that is executed after the component mounts the DOM. This method is executed after first render and is executed only on client side. This is a great place to set up initial data. Child component's &lt;code&gt;componentDidMount&lt;/code&gt; runs before parent's &lt;code&gt;componentDidMount&lt;/code&gt;. It runs🏃 only once. You can also make the right API calls under this method so that you can retrieve the data the right way.&lt;/p&gt;

&lt;h3&gt;
  
  
  🌿static getDerivedStateFromProps
&lt;/h3&gt;

&lt;p&gt;This method is called (or invoked) before the component is rendered to the DOM on initial mount. It allows a component to update its internal state in response to a change in props. To update the state -&amp;gt; return object with new values.&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 plaintext"&gt;&lt;code&gt;static getDerivedStateFromProps(props, state){
    return{
          points: 200 // update state with this
   }
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  🍀Updating
&lt;/h2&gt;

&lt;p&gt;The third stage🪜 starts when the component has been adopted on the browser. During this phase the component is already inserted into DOM. A component is updated when there is a change in &lt;code&gt;state&lt;/code&gt; and &lt;code&gt;props&lt;/code&gt; React basically has three built-in methods that are called while updating the components.&lt;/p&gt;

&lt;h3&gt;
  
  
  🌿shouldComponentUpdate
&lt;/h3&gt;

&lt;p&gt;The method tells the program🐱💻 about the state of rendering when it is updated. If new props or rules are being updated, then a rendering can be done or skipped. This is important to code in properly as there are evolving states in the program as well. Updating the method as true/false is the proper approach. The default here is true, which can be changed as per the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  🌿getSnapshotBeforeUpdate
&lt;/h3&gt;

&lt;p&gt;This hook🪝 is executed right after the render method is called. It has access to &lt;code&gt;props&lt;/code&gt; and &lt;code&gt;state&lt;/code&gt; before the update. It is handy when you want some DOM info or want to change DOM just after an update is made. For example, getting information about the scroll position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getSnapshotBeforeUpdate(prevProps, prevState){
    //capture the scroll position so we can adjust scroll later
    if(prevProps.list.length &amp;lt; this.props.list.length){
        const list = this.listRef.current;
        return list.scrollHeight - list.scrollTop;
    }
    return null;
}

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

&lt;/div&gt;



&lt;p&gt;Value queried from the DOM in &lt;code&gt;getSnapshotBeforeUpdate&lt;/code&gt; will refer to the value just before the DOM is updated. It doesn't work on its own and used in conjunction with the &lt;code&gt;componentDidUpdate&lt;/code&gt; lifecycle🌀 method.&lt;/p&gt;

&lt;h3&gt;
  
  
  🌿componentDidUpdate
&lt;/h3&gt;

&lt;p&gt;This method is called just after the re-rendering of the component. This hook🪝 has &lt;code&gt;prevProps&lt;/code&gt;and &lt;code&gt;prevState&lt;/code&gt; available. This lifecycle method is invoked after the &lt;code&gt;getSnapshotBeforeUpdate&lt;/code&gt; is invoked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidupdate(prevProps, prevState, snapshot){
    if(condition){
        this.setState({..})
    }
    else{
        //do something else o loop
    }
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  🍀Unmounting
&lt;/h2&gt;

&lt;p&gt;The final stage🪜 of unmounting is essential as it doesnt require the component and gets unmounted from the DOM. As the final state🪜, it is designed to produce the outcome via unmounting.&lt;/p&gt;

&lt;h3&gt;
  
  
  🌿componentWillUnmount
&lt;/h3&gt;

&lt;p&gt;If there are any cleanup actions like canceling API calls or clearing any caches in storage you can perform that in the componentWillUnmount method. You cannot use &lt;code&gt;setState&lt;/code&gt; inside this method as the component will never be re-rendered.&lt;/p&gt;

&lt;p&gt;These are the methods that you are most likely come across. I've omitted some deprecated methods or are set to be deprecated in the very near future.&lt;/p&gt;

&lt;p&gt;Thanks for reading💗&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.cuelogic.com/blog/reactjs-lifecycle"&gt;https://www.cuelogic.com/blog/reactjs-lifecycle&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/react-component.html"&gt;https://reactjs.org/docs/react-component.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Know Your JavaScript🤌</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Wed, 24 Aug 2022 12:20:02 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/know-your-javascript-1nk7</link>
      <guid>https://dev.to/sagarmedtiya/know-your-javascript-1nk7</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BkfbTqCt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezf6mymb62ijra2xcj5o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BkfbTqCt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezf6mymb62ijra2xcj5o.gif" alt="Image description" width="880" height="385"&gt;&lt;/a&gt;&lt;br&gt;
This blog covers the basics of JavaScript. So, lets begin&lt;/p&gt;
&lt;h2&gt;
  
  
  🔭Scope
&lt;/h2&gt;

&lt;p&gt;Scope in JavaScript defines accessibility of variables, objects and functions. There are three types of scope:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global Scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the JavaScript engine executes a script, it creates a global execution context. When we define global-scoped variable then that variable can be accessible through out the script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var message = 'Hi';

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DVAywEqb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660903195045/8Defyz2cj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DVAywEqb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660903195045/8Defyz2cj.png" alt="JavaScript-Global-Variables.png" width="556" height="232"&gt;&lt;/a&gt;&lt;em&gt;Image Source: javascripttutorial.net/&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The variable message is global-scoped. It can be accessible everywhere in the script.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Local Scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The variable which is defined inside the function is called local variable and can only be accessible inside the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function animal(){
    let animal1 = 'tiger';
    console.log(animal1);
}
animal();
console.log(animal1);

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aYBNS_of--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660906122874/YTOZJb9XU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aYBNS_of--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660906122874/YTOZJb9XU.png" alt="Screenshot 2022-08-19 160323.png" width="880" height="30"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, the variable &lt;code&gt;animal1&lt;/code&gt;, when calling outside the function is returning referenceError as the variable is defined inside the function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Block Scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Generally, whenever you see curly brackets {}, it is a block. It can be the area within the &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else&lt;/code&gt;, &lt;code&gt;switch&lt;/code&gt; conditions or &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;do while&lt;/code&gt;, and &lt;code&gt;while&lt;/code&gt; loops.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hoisting
&lt;/h2&gt;

&lt;p&gt;By the definition of MDN,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It allows functions to be safely used in code before they are declared.&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 plaintext"&gt;&lt;code&gt;console.log(Hoist);
var Hoist = 'The variable Has been hoisted'; //Output: undefined

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

&lt;/div&gt;



&lt;p&gt;JavaScript only hoists declarations, not initialization. Functions are hoisted first and then variables. Variables and constants declared with let and const are not hoisted. Function declaration are hoisted but function expressions are not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polymorphism
&lt;/h2&gt;

&lt;p&gt;Polymoriphism in javascript refers to the concept of reusing a single piece of code multiple times.&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 plaintext"&gt;&lt;code&gt;class Car{
    constructor(vehicle){
        this._vehicle = vehicle;
    }
    move(){
        console.log('drive', this._vehicle);
    }
}
class Bike{
    constructor(vehicle){
        this._vehicle = vehicle;
    }
    move(){
        console.log('ride', this._vehicle);
    }
}
function getVehicle(vehicle){
    switch(vehicle.type){
        case"bike":
            return new Bike(vehicle);
        case "car":
            return new Car(vehicle);
        default:
            break;
    }
}

let vehicle = getVehicle({
    type: "bike",
});
vehicle.move() // ride {type:'bike'}
vehicle = getVehicle({
    type: 'car',
})
vehicle.move(); // drive {type:'car}

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Iq_iWKxD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660915241300/ZOKF_3b-8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Iq_iWKxD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660915241300/ZOKF_3b-8.png" alt="Screenshot 2022-08-19 185027.png" width="880" height="33"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, both are 🚜vehicles and both vehicles move. But depending on the type of the vehicle, they move differently. But from the user's point of view they just have to call &lt;code&gt;move()&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closures
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In laymen terms, whenever you see a function keyword within another function, the inner function has access to variables in the outer function, that is a closures.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 42;
function js(){
  return a;
}
js()

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

&lt;/div&gt;



&lt;p&gt;This is the example of closure because &lt;code&gt;a&lt;/code&gt; is outside the scope of &lt;code&gt;function js()&lt;/code&gt;. Closures are just using variables that come from a higher scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Closure remembers the environment
&lt;/h3&gt;

&lt;p&gt;The function defined in the closure 'remembers' the environment in which it was created. Closure happens when an inner function is defined in outer function and is made accessible to be called later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fruit(){
    var apple = 'Hello apple';
    var log = ()=&amp;gt;{
        console.log(apple);
    }
    return log;
}
var closure = fruit();
closure();

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3yxAVsjh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661108676075/3TG1IdoZd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3yxAVsjh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661108676075/3TG1IdoZd.png" alt="Screenshot 2022-08-22 003346.png" width="880" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, &lt;code&gt;closure&lt;/code&gt;variable is now pointing to the &lt;code&gt;log()&lt;/code&gt; function. Meaning calling &lt;code&gt;closure()&lt;/code&gt; function is actually invoking the &lt;code&gt;log()&lt;/code&gt; function from the &lt;code&gt;fruit()&lt;/code&gt; function. If you see result &lt;code&gt;log()&lt;/code&gt; functions accurately logs the value of &lt;code&gt;hello&lt;/code&gt; variable which was originally declared in the parent function &lt;code&gt;fruit()&lt;/code&gt;. It means the &lt;code&gt;log()&lt;/code&gt; function has accurately "remembered" the value of the &lt;code&gt;hello&lt;/code&gt; variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  IIFE
&lt;/h2&gt;

&lt;p&gt;IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.&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 plaintext"&gt;&lt;code&gt;(function fruit(){
    //your code here
})()

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

&lt;/div&gt;



&lt;p&gt;It is a function expression. It is moreover a self-executing function and useful in closure. It also prevents from polluting the global scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var sum = (function(){
    var foo = 20;
    function bar(){
        foo = foo +10;
        console.log(foo);
    }
    return bar;
})()
sum()   
sum()
sum()

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S6f0E0rR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661145229324/9mF0oUOe0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S6f0E0rR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661145229324/9mF0oUOe0.png" alt="Screenshot 2022-08-22 103403.png" width="880" height="146"&gt;&lt;/a&gt;The primary reason to use an IIFE is to obtain data privacy. Because JavaScripts var scopes variables to their containing function, any variables declared within the IIFE cannot be accessed by the outside world.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;this&lt;/code&gt; keywords
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; operator will create the instance of an object. If the &lt;code&gt;new&lt;/code&gt; keyword is called then the following actions are taken:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It links the newly created object to another object. It does it by setting its constructor to another object. The object type is set to its constructor function.&lt;/li&gt;
&lt;li&gt;It makes the &lt;code&gt;this&lt;/code&gt; variable point to the newly created object.&lt;/li&gt;
&lt;li&gt;It invokes the constructor function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;object.prototype&lt;/code&gt; property is set to the object's prototype. For example,
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Car(name){
    console.log(this); //this points to myCar
    this.name = name;
}

var myCar = new Car('honda')
console.log(myCar)

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S_rbp_fM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661236909086/24yC4OzRk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S_rbp_fM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661236909086/24yC4OzRk.png" alt="image.png" width="880" height="140"&gt;&lt;/a&gt;For more insights,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Car&lt;/code&gt; is a constructor function because it is invoked using &lt;code&gt;new&lt;/code&gt; keyword. &lt;code&gt;Car&lt;/code&gt; function has a field called &lt;code&gt;name&lt;/code&gt;. &lt;code&gt;myCar&lt;/code&gt; object is created from the &lt;code&gt;Car&lt;/code&gt; function using &lt;code&gt;new&lt;/code&gt; keyword. It makes &lt;code&gt;Car&lt;/code&gt; the prototype/constructor of &lt;code&gt;myCar&lt;/code&gt;. It sets the &lt;code&gt;name&lt;/code&gt; field to &lt;code&gt;Honda&lt;/code&gt;. The value of &lt;code&gt;myCar&lt;/code&gt; becomes &lt;code&gt;{name: "Honda"}&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prototype
&lt;/h2&gt;

&lt;p&gt;Prototype is a link to another object. In JavaScript, objects are chained together by prototype chain. JS objects inherit properties and methods from a prototype. Prototype property allows you to add properties and methods to any object dynamically.&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 plaintext"&gt;&lt;code&gt;function Animal(name){
    this.name = name;
}
Animal.prototype.age =10;
var Cat = new Animal('cat');
console.log(Cat)
console.log(Cat.name)
console.log(Cat.age)

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KV3Reamo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661243396455/BAs4Rxd-d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KV3Reamo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661243396455/BAs4Rxd-d.png" alt="Screenshot 2022-08-23 135918.png" width="880" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When object &lt;code&gt;Cat&lt;/code&gt; is inherited from object &lt;code&gt;Animal&lt;/code&gt;, then &lt;code&gt;Animal&lt;/code&gt; is the prototype object or the constructor of the &lt;code&gt;Cat&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prototypal Inheritance
&lt;/h2&gt;

&lt;p&gt;Prototypal Inheritance means that if the property is not found in the original object itself then the property will be searched for in the object's parent prototype object.&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 plaintext"&gt;&lt;code&gt;function Animal(name){
    this.name = name;
}
Animal.prototype.move=function(){
    console.log('move');
}
Cat.prototype = Object.create(Animal.prototype)
function Cat(name){
    Animal.call(this,name);
}

Cat.prototype.meow=function(){
    console.log('meow');
}

var misty = new Cat('misty');
console.log(misty);
console.log(misty.name);
console.log( misty.meow());
console.log( misty.move())

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cWGrnliw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661264968281/oB_Q6iyjx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cWGrnliw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661264968281/oB_Q6iyjx.png" alt="Screenshot 2022-08-23 170716.png" width="880" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Animal&lt;/code&gt; object is at the top of the inheritance. It has a &lt;code&gt;Animal.prototype&lt;/code&gt; property on it. We also have &lt;code&gt;Cat&lt;/code&gt; object and to execute prototypal inheritance we have to link their &lt;code&gt;prototype&lt;/code&gt;. &lt;code&gt;Cat.prototype&lt;/code&gt; is linked with &lt;code&gt;Animal.prototype&lt;/code&gt;. Then we have created &lt;code&gt;misty&lt;/code&gt; object from &lt;code&gt;Cat&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why prototypal inheritance is better?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It is simple, just create and extend objects. You don't worry about classes, interfaces, abstract classes, virtual base classes, constructor etc.&lt;/li&gt;
&lt;li&gt;It is more powerful, you can 'mimic' multiple inheritance by extending object from multiple objects. Just handpick properties and methods from the prototypes you want.&lt;/li&gt;
&lt;li&gt;It is dynamic, you can add new properties to prototypes after they are created. This also auto-adds those properties and methods to those object which are inherited from this prototype.&lt;/li&gt;
&lt;li&gt;It is less verbose than class-based inheritance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Memoization
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Memoization is a technique for speeding up the application by caching its previously computed results. If the data is not cached, then the function is executed, and the result is added to the cache.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The function is an integral part of the programming, they help to modularity and reusable to our code, as per above definition memoization is an optimizing our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoizedAdd = () =&amp;gt; {
    let cache = {};
    return (value) =&amp;gt; {
        if (value in cache) {
            console.log('Fetching from cache');
            return cache[value];
        } else {
            console.log('Calculating result');
            let result = value + 10;
            cache[value] = result;
            return result;
        }
    }
}
// returned function from memoizedAdd
const newAdd = memoizedAdd();
console.log(newAdd(9)); //output: 19 calculated
console.log(newAdd(9)); //output: 19 cached

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c1PrRquV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661283807560/VZxeoMMyg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c1PrRquV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661283807560/VZxeoMMyg.png" alt="image.png" width="880" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  apply, call and bind methods
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;apply&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt; are used to attach a correct &lt;code&gt;this&lt;/code&gt; to the function and invoke it. The difference is the way of function invocation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;bind()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It returns a function. This returned function can later be called with a certain context set for calling the original function. The returned function needs to be invoked separately.&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 plaintext"&gt;&lt;code&gt;var person ={
    hello: function(message){
        console.log(this.name + " says hello "+ message)
    }
}
var ngNinja = {
    name: "NgNinja Academy"
}
var sayHello = person.hello.bind(ngNinja)
sayHello("world");

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UBnw66ap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661325158637/hc1TCKpjG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UBnw66ap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661325158637/hc1TCKpjG.png" alt="image.png" width="880" height="113"&gt;&lt;/a&gt;Here, &lt;code&gt;person&lt;/code&gt; object has a method called &lt;code&gt;hello()&lt;/code&gt;. &lt;code&gt;ngNinja&lt;/code&gt; object does not have it. You can bind &lt;code&gt;hello()&lt;/code&gt; to &lt;code&gt;ngNinja&lt;/code&gt; object and call it later in the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;call()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;call()&lt;/code&gt; attaches this to function and invokes the function immediately. The owner object is sent as an argument. With &lt;code&gt;call()&lt;/code&gt;, an object can use a method belonging to another object.&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 plaintext"&gt;&lt;code&gt;var person ={
    hello: function(message){
        console.log(this.name + " says hello "+ message)
    }
}
var ngNinja = {
    name: "NgNinja Academy"
}
person.hello.call(ngNinja,"world");

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eOC3-mtU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661325534799/2bTb7KPvD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eOC3-mtU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661325534799/2bTb7KPvD.png" alt="image.png" width="880" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example &lt;code&gt;this&lt;/code&gt; is set to the &lt;code&gt;ngNinja&lt;/code&gt; object. You can send arguments to the function as a comma-separated list following the owner object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;apply()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;apply()&lt;/code&gt; also attaches this to a function and invokes the function immediately. &lt;code&gt;apply()&lt;/code&gt; is similar to &lt;code&gt;call()&lt;/code&gt; except it takes an array of arguments instead of the comma-separated list.&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 plaintext"&gt;&lt;code&gt;var person ={
    hello: function(message){
        console.log(this.name + " says hello "+ message)
    }
}
var ngNinja = {
    name: "NgNinja Academy"
}
person.hello.apply(ngNinja,["world"]);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8p97KTyW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661326027288/pFcyYVm8C.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8p97KTyW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661326027288/pFcyYVm8C.png" alt="image.png" width="880" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, this is set to the ngNinja object. You can send arguments to the function as a comma-separated list following the owner object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Callback Function
&lt;/h2&gt;

&lt;p&gt;Callback functions are executed "later". Later it can be any action that you'd want to be completed before calling the callback function. Callback functions are passed as arguments to the outer function.&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 plaintext"&gt;&lt;code&gt;function getName(){
    return "viewers";
}
function greet(getName){
    const name = getName();
    return "Hello " + name;
}
greet(getName);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fzueWIYC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661327711852/sjRTkYER8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fzueWIYC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661327711852/sjRTkYER8.png" alt="image.png" width="880" height="95"&gt;&lt;/a&gt;In the above code, we can see that we are passing the function which we want to execute later on. &lt;code&gt;greet()&lt;/code&gt; is the outer function. And &lt;code&gt;getName()&lt;/code&gt; is the callback function. We pass &lt;code&gt;getName()&lt;/code&gt; function to the outer &lt;code&gt;greet()&lt;/code&gt; function as a function argument. The value from &lt;code&gt;getName()&lt;/code&gt; callback function is then used in the outer function &lt;code&gt;greet()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promises
&lt;/h2&gt;

&lt;p&gt;For detailed explanation of the promise, you can read the other blog. &lt;a href="https://dev.to/sagarmedtiya/understanding-promises-in-javascript-11id-temp-slug-9874661"&gt;Promises&lt;/a&gt;. Thank you 🧡&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous Js
&lt;/h2&gt;

&lt;p&gt;In JavaScript Synchronous and asynchronous are code execution Pattern,&lt;/p&gt;

&lt;p&gt;In JavaScript Code Execution done By two separate ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser JS Engine (popular V8 JS Engine)&lt;/li&gt;
&lt;li&gt;NodeJs V8 Engine&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Browser JS&lt;/strong&gt; Engine parse Html file and executes JavaScript by three patterns,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;synchronous&lt;/li&gt;
&lt;li&gt;Asynchronous&lt;/li&gt;
&lt;li&gt;defer
&lt;/li&gt;
&lt;/ol&gt;

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

&amp;lt;script src='index.js'&amp;gt; //default Synchronous

&amp;lt;script async src='index.js'&amp;gt; //parse as Asynchronously

&amp;lt;script defer src='index.js'&amp;gt; //parse as deferred

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NodeJS V8 Engine:&lt;/strong&gt; NodeJS V8 engine executes its JavaScript Code as single-threaded based on the Event loop!&lt;/p&gt;

&lt;h2&gt;
  
  
  async and await
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; makes a function return a Promise and &lt;code&gt;await&lt;/code&gt; makes a function wait for a Promise. Similar to callback and promises, we have another paradigm for handling async programming. Every async function you write will return a promise, and every single thing you await will ordinarily be a promise.&lt;/p&gt;

&lt;p&gt;In JavaScript Asynchronous pattern handled in various versions,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ES5 -&amp;gt; Callback&lt;/p&gt;

&lt;p&gt;ES6 -&amp;gt; Promise&lt;/p&gt;

&lt;p&gt;ES7 -&amp;gt; async &amp;amp; await For example,&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let promise1 = new Promise((resolve, reject)=&amp;gt;{
    console.log("Hello");
})
let promise2 = new Promise((resolve,reject)=&amp;gt;{
    resolve(setTimeout(()=&amp;gt;{
        console.log('viewers')
    },5000))
})

async function display() {
    let data = await Promise.all([promise1,promise2]);
    console.log(data);
  }

display()

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gHD4ROul--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661340215605/jQbaC3P2_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gHD4ROul--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661340215605/jQbaC3P2_.png" alt="Screenshot 2022-08-24 163246.png" width="880" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, we have used two promises and used &lt;code&gt;display()&lt;/code&gt; function with &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;. In Async &amp;amp; Await lot more stuff there, Just play around it. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Map9cAA0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.google.com/uc%3Fexport%3Ddownload%26id%3D166Ecq6uBl61U14OUlkHOHIBv2ArKoumJ" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Map9cAA0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.google.com/uc%3Fexport%3Ddownload%26id%3D166Ecq6uBl61U14OUlkHOHIBv2ArKoumJ" alt="Thank you" width="64" height="64"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here JavaScript API cheatsheet for front end developer &lt;a href="http://overapi.com/javascript"&gt;http://overapi.com/javascript&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TbU-5w69--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661341225019/k9Z5JX1Yx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TbU-5w69--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661341225019/k9Z5JX1Yx.png" alt="Screenshot 2022-08-24 165509.png" width="880" height="410"&gt;&lt;/a&gt;&lt;em&gt;Image source: &lt;a href="http://overapi.com/javascript"&gt;http://overapi.com/javascript&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading, if you like it you can subscribe to my blog. 🧡&lt;/p&gt;

&lt;p&gt;References from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/"&gt;https://developer.mozilla.org/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hackernoon.com/"&gt;https://hackernoon.com/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>🤞Understanding Promises in JavaScript🚀</title>
      <dc:creator>Sagar Medtiya</dc:creator>
      <pubDate>Thu, 18 Aug 2022 12:03:55 +0000</pubDate>
      <link>https://dev.to/sagarmedtiya/understanding-promises-in-javascript-h6j</link>
      <guid>https://dev.to/sagarmedtiya/understanding-promises-in-javascript-h6j</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wv_Tlobj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661946186871/-9NO2hO-z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wv_Tlobj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1661946186871/-9NO2hO-z.gif" alt="image" width="880" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Promise🤞?
&lt;/h2&gt;

&lt;p&gt;By the definition of MDN,&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The &lt;strong&gt;Promise&lt;/strong&gt; object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We know that JavaScript is a synchronous programming language but callback function makes it Asynchronous.&lt;/p&gt;

&lt;p&gt;Let's understand promise by simple example, suppose you made a promise🤞 to yourself that you will wake up at sharp 5AM. But eventually, you don't know whether you will make it or not, that means either you are going to complete that promise or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works?
&lt;/h2&gt;

&lt;p&gt;The promises returns one of the 3 possible states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fullfilled:&lt;/strong&gt; You woke up at sharp 5AM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rejected:&lt;/strong&gt; You didn't wake up at 5AM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pending:&lt;/strong&gt; You don't know if you'll wake up or not.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A promise is &lt;strong&gt;settled&lt;/strong&gt; if it's not pending means it has been resolved or rejected. After it has been settled there is no use of calling &lt;code&gt;resolve()&lt;/code&gt; or &lt;code&gt;reject()&lt;/code&gt;.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;new Promise( (resolve, reject)=&amp;gt; { ... } );

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For example📃&lt;/strong&gt; ,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let wakePromise = true;
let wakeUp = new Promise((resolve, reject)=&amp;gt; {
    setTimeout(() =&amp;gt; {
        if (wakePromise) {
        resolve('I woke up');
    } 
    else {
        reject('I didnt wake up'); }
    }, 5000);
});
console.log(wakeUp);

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qMZU4ytj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660803125791/08uwsZYG_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qMZU4ytj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660803125791/08uwsZYG_.png" alt="Screenshot 2022-08-18 112212.png" width="880" height="212"&gt;&lt;/a&gt;After completion of 5 seconds the promise gets fulfilled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--p4xjwj9c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660803579001/4kDbX2CVl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p4xjwj9c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660803579001/4kDbX2CVl.png" alt="Screenshot 2022-08-18 113014.png" width="880" height="60"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we change &lt;code&gt;wakePromise = false&lt;/code&gt; then it returns,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J_3-JV0e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660803859852/P09Ew57as.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J_3-JV0e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660803859852/P09Ew57as.png" alt="Screenshot 2022-08-18 113414.png" width="880" height="191"&gt;&lt;/a&gt;So the reject method moved the promises to the rejected() state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Static types and prototype method:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Promise.prototype.then(onFulfilled, onRejected)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;.then&lt;/code&gt; method is used to schedule📅 a callback to be executed when the promise is successfully resolved.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Promise.prototype.catch(onRejected)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;.catch&lt;/code&gt; method is used to schedule📅 a callback to be executed when the promise is rejected.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Promise.prototype.finally(onFinally)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;.finally&lt;/code&gt; is used to execute the same piece of code whether the promise is fulfilled or rejected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example📃&lt;/strong&gt; ,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function makePromise(wakeUp) {
    return new Promise(function (resolve, reject) {
       setTimeout(() =&amp;gt; {
           if (wakeUp) {
               resolve("I woke up at 5AM.");
           } else {
               reject("I didn't wake up at 5Am.");
           }
       }, 5 * 1000);
   });
   }
   let wakeUp = makePromise(true);
   wakeUp
       .then(success =&amp;gt; console.log(success))
       .catch(reason =&amp;gt; console.log(reason))
       .finally(() =&amp;gt; console.log("Let's sleep"));

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I-gu5vhm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660808551458/_-LFTKNcG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I-gu5vhm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660808551458/_-LFTKNcG.png" alt="Screenshot 2022-08-18 131219.png" width="880" height="192"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Promise Chaining is a concept by which we may initialize another promise inside our .then() method and accordingly we may execute our results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example📃&lt;/strong&gt; ,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let promise = new Promise((resolve, reject) =&amp;gt; {
    resolve("Test");
  });
  promise
    .then(
      new Promise((resolve, reject) =&amp;gt; {
        resolve("exams");
      })
      .then((result1) =&amp;gt; {
        console.log(result1);
      })
    )
    .then((result2) =&amp;gt; {
      console.log(result2);
    });

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FVosgZtG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660819744624/a5FPly52M.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FVosgZtG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660819744624/a5FPly52M.png" alt="Screenshot 2022-08-18 161853.png" width="880" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use &lt;code&gt;promise.all()&lt;/code&gt; for the nested &lt;code&gt;.then&lt;/code&gt; method. It can simply the messy code easily. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example📃&lt;/strong&gt; ,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Promise1 = new Promise((resolve, reject)=&amp;gt;{
    setTimeout(() =&amp;gt; {
        console.log('The first promise has resolved');
        resolve(10);
      }, 1 * 1000);
})
const Promise2 = new Promise((resolve, reject)=&amp;gt;{
    setTimeout(() =&amp;gt; {
        console.log('The second promise has resolved');
        resolve(10);
      }, 1 * 1000);})
Promise.all([Promise1, Promise2])
    .then((data)=&amp;gt;{
        console.log(data);
    })
    .catch((error)=&amp;gt;{
        console.log(error)
    })

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nESFsoC7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660822852808/HZ1fWRob8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nESFsoC7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1660822852808/HZ1fWRob8.png" alt="Screenshot 2022-08-18 161853.png" width="880" height="59"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Important points
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use promises whenever you are using async or blocking code.&lt;/li&gt;
&lt;li&gt;A promise is an object that returns a value in the future.&lt;/li&gt;
&lt;li&gt;A promise starts in the pending state and ends in either a fulfilled state or a rejected state.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;resolve&lt;/code&gt; maps to &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt; maps to &lt;code&gt;catch&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If something needs to be done in both cases use &lt;code&gt;.finally&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading my post. I hope you've liked it. Happy coding😊&lt;/p&gt;

&lt;p&gt;Some more blogs by me💗&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.sagarmedtiya.me/some-best-techniques-to-optimize-your-react-app"&gt;Some best techniques to Optimize your React App🐱💻&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.sagarmedtiya.me/component-lifecycle-in-reactjs"&gt;Component Lifecycle🌀 in ReactJS🚀&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.sagarmedtiya.me/know-your-javascript"&gt;Know Your JavaScript🤌&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
