<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Abhishek Jain</title>
    <description>The latest articles on Forem by Abhishek Jain (@abhishekjain35).</description>
    <link>https://forem.com/abhishekjain35</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%2F225044%2F88106697-2260-46b7-8924-dea2ea0267a8.jpeg</url>
      <title>Forem: Abhishek Jain</title>
      <link>https://forem.com/abhishekjain35</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abhishekjain35"/>
    <language>en</language>
    <item>
      <title>Introduction to Promises in JavaScript.</title>
      <dc:creator>Abhishek Jain</dc:creator>
      <pubDate>Fri, 28 May 2021 13:46:15 +0000</pubDate>
      <link>https://forem.com/abhishekjain35/introduction-to-promises-in-javascript-3cd4</link>
      <guid>https://forem.com/abhishekjain35/introduction-to-promises-in-javascript-3cd4</guid>
      <description>&lt;p&gt;I promise that you will have a good understanding of promises by the end of the article 😁.&lt;/p&gt;

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

&lt;p&gt;A promise in javascript is just like a promise we make in our lives. It refers to an event that will occur in the future.&lt;br&gt;
Promises in javascript are used to handle asynchronous operations. Promises have three possible states -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pending (Initial State)&lt;/li&gt;
&lt;li&gt;Fulfilled (Successful)&lt;/li&gt;
&lt;li&gt;Rejected (Failed)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgokowel8p73wuh407x87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgokowel8p73wuh407x87.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we make a promise, its state will be pending till either it is fulfilled or rejected. If fulfilled, its value will be the value it resolves with, and if it encounters any error, its value will be the value it rejects with (the error object).&lt;/p&gt;

&lt;p&gt;For example, when we make API requests to the server, it immediately returns a promise with &lt;em&gt;pending&lt;/em&gt; state. If the API call is successful, the state of promise changes from &lt;strong&gt;pending&lt;/strong&gt; to &lt;strong&gt;fulfilled&lt;/strong&gt;, and if the API request fails, then its state changes from &lt;strong&gt;pending&lt;/strong&gt; to &lt;strong&gt;rejected&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating a Promise in Javascript
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promiseExample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Resolve with Any type of data (objects, arrays, strings, etc...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error description.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So, we can create a promise by using the &lt;code&gt;new Promise()&lt;/code&gt; constructor. It takes a function as an argument. This function takes two callback functions, &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt;. Whenever you want to fulfill the promise, you can call the &lt;code&gt;resolve&lt;/code&gt; callback function and pass the value to it. To reject a promise, call the &lt;code&gt;reject&lt;/code&gt; callback, providing some error message.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using the Promise
&lt;/h2&gt;

&lt;p&gt;We can use the above promise creation example.&lt;/p&gt;
&lt;h4&gt;
  
  
  .then()
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promiseExample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise Fulfilled.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise Rejected.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;promiseExample&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Promise Fulfilled.&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So, the &lt;code&gt;.then()&lt;/code&gt; method takes a callback function that executes whenever the promise resolves (or is fulfilled). The callback itself takes a parameter to store the actual result returned from the promise.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;.then&lt;/code&gt; also takes a second parameter, a callback function, to handle errors, but there's a better way.&lt;/p&gt;
&lt;h4&gt;
  
  
  .catch()
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;promiseExample&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Promise Rejected.&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;.catch()&lt;/code&gt; method also takes a callback that executes whenever the promise rejects (or fails). This callback takes an error parameter to catch the error information.&lt;/p&gt;
&lt;h2&gt;
  
  
  Chaining of Promises
&lt;/h2&gt;

&lt;p&gt;Suppose we have to perform multiple asynchronous tasks. In that case, we use promise chaining.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Resolve promise after 1 sec&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promiseExample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data of 1st Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;promiseExample&lt;/span&gt;
  &lt;span class="c1"&gt;// 1st .then()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;dataOfFirstPromise&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dataOfFirstPromise&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// data of 1st Promise&lt;/span&gt;

    &lt;span class="c1"&gt;// simulating API call which resolves after 1 sec.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data of 2nd Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="c1"&gt;// 2nd .then()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;dataOfSecondPromise&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dataOfSecondPromise&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// data of 2nd Promise&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Few things to note here - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;.then()&lt;/code&gt; and &lt;code&gt;.catch()&lt;/code&gt; methods always returns a promise so that we can again use &lt;code&gt;.then()&lt;/code&gt; and &lt;code&gt;.catch()&lt;/code&gt; on them and chain the promises.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the above example, we use two &lt;code&gt;.then()&lt;/code&gt; methods. So, to consume the result of the first &lt;code&gt;.then()&lt;/code&gt; method, we always need to return that value from it. In this case, we &lt;code&gt;return&lt;/code&gt; a promise from the first &lt;code&gt;.then()&lt;/code&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We use &lt;code&gt;.catch()&lt;/code&gt; to catch the error if it occurs in any of the promises. This is the main reason we use &lt;code&gt;.catch()&lt;/code&gt; instead of the second parameter of &lt;code&gt;.then()&lt;/code&gt;. The &lt;code&gt;.catch()&lt;/code&gt; method always catches the error either if it occurs in promise or the &lt;code&gt;.then()&lt;/code&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the above example, we first create a promise which resolves after 1 second. After that, we call &lt;code&gt;.then&lt;/code&gt; on the promise and get the result of the first promise in parameter &lt;code&gt;dataOfFirstPromise&lt;/code&gt;. Now, if we want to fire another API request only after the 1st promise resolves, we can do that here. So we simulate API request with 2nd promise that resolves after 1 second, and we can get the result of 2nd promise in the 2nd &lt;code&gt;.then()&lt;/code&gt; method. You can chain as many &lt;code&gt;.then() 's and&lt;/code&gt;.catch() 's as you want.&lt;/p&gt;

&lt;p&gt;That is all about Promise chaining.&lt;/p&gt;

&lt;p&gt;Well, this was a brief introduction to promises. Thanks for reading.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/13HgwGsXF0aiGY/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/13HgwGsXF0aiGY/source.gif"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Closures in Javascript.</title>
      <dc:creator>Abhishek Jain</dc:creator>
      <pubDate>Fri, 26 Mar 2021 13:19:59 +0000</pubDate>
      <link>https://forem.com/abhishekjain35/closures-in-javascript-2338</link>
      <guid>https://forem.com/abhishekjain35/closures-in-javascript-2338</guid>
      <description>&lt;p&gt;Closures in javascript is one of the many questions normally asked in javascript interviews.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a closure?
&lt;/h2&gt;

&lt;p&gt;whenever a function is created, it forms a closure with the outer functions' scope. Think of that closure as a pack of &lt;strong&gt;All the variables of the outer function scope.&lt;/strong&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Let's understand this with an example.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;createAdder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstNum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;secondNum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;   &lt;span class="c1"&gt;//The closure function&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;firstNum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;secondNum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;addOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createAdder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 11&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's understand what's happening in the above code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First we define a function createAdder that takes &lt;em&gt;firstNum&lt;/em&gt; as a parameter.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, we return an anonymous function that takes a &lt;em&gt;secondNum&lt;/em&gt; as a parameter.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, we declare &lt;em&gt;addOne&lt;/em&gt; variable, with a call to createAdder function, with &lt;em&gt;firstNum&lt;/em&gt; parameter value being 1.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;      &lt;span class="c1"&gt;// so now the value of addOne should be -&lt;/span&gt;

      &lt;span class="nx"&gt;addOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;secondNum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;   &lt;span class="c1"&gt;//The closure function&lt;/span&gt;
                 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;firstNum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;secondNum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;so now, when we call &lt;em&gt;addOne(10)&lt;/em&gt;. how does this &lt;em&gt;addOne&lt;/em&gt; get the value of &lt;em&gt;firstNum&lt;/em&gt;? It's because of &lt;strong&gt;closure&lt;/strong&gt;.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;so when the anonymous function (returned function) is created. It looks for parent variables. It finds &lt;em&gt;firstNum&lt;/em&gt; variable with value 1 and forms a closure with it (saves the variable) so that it can access the firstNum variable later.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;so the addOne(10) function returns 11. Because it already has the &lt;em&gt;firstNum&lt;/em&gt;'s value as 1.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, I think you understand closure, Let's look at a practical example of where we can use closure.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/abhishekjain35/embed/zYNvLyv?height=600&amp;amp;default-tab=js,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In the above example, the &lt;em&gt;&lt;strong&gt;updateClickCount&lt;/strong&gt;&lt;/em&gt; variable is assigned to a IIFE (immediately invoked function expression). So, the IIFE calls itself and returns an anonymous function which is finally assigned to &lt;em&gt;&lt;strong&gt;updateClickCount&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now, this anonymous function forms a closure with the parent function so that it has access to the parent function's variables i.e. &lt;em&gt;counter&lt;/em&gt; and &lt;em&gt;p&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;When we click, it calls the &lt;em&gt;&lt;strong&gt;updateClickCount&lt;/strong&gt;&lt;/em&gt; which references to the anonymous function, increments the counter and updates the textContent of &lt;em&gt;p&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Well, this is it from my side. See you soon 😁&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/hrRJ41JB2zlgZiYcCw/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/hrRJ41JB2zlgZiYcCw/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Debouncing in Javascript.</title>
      <dc:creator>Abhishek Jain</dc:creator>
      <pubDate>Tue, 16 Feb 2021 15:05:49 +0000</pubDate>
      <link>https://forem.com/abhishekjain35/debouncing-in-javascript-276j</link>
      <guid>https://forem.com/abhishekjain35/debouncing-in-javascript-276j</guid>
      <description>&lt;p&gt;Debouncing is a pattern that allows &lt;em&gt;&lt;strong&gt;delaying execution of some piece of code&lt;/strong&gt;&lt;/em&gt; until a specified time to &lt;strong&gt;avoid unnecessary CPU cycles, API calls and improve performance.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why debouncing?
&lt;/h2&gt;

&lt;p&gt;One word "&lt;strong&gt;Performance&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;Suppose, you are building an e-commerce application. There you have to implement a search bar for searching products And when the user types in a character, an API call is made.&lt;/p&gt;

&lt;p&gt;Look at the example below.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/abhishekjain35/embed/WNopLYV?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In the above example, we're having a simple searchBar and a count of API calls made. As we type in the searchBar, the count of API called increases with each character. But that's not what we want to happen. What we want is to wait for the user to stop typing. As soon as the user stops typing then we want to make the API call.&lt;/p&gt;

&lt;p&gt;so, how can we fix this? - here comes debouncing into play.&lt;/p&gt;

&lt;p&gt;Final version with debouncing.&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/abhishekjain35/embed/QWGpoeZ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Pretty cool huh?&lt;/p&gt;

&lt;h3&gt;
  
  
  The debouncing function
&lt;/h3&gt;

&lt;p&gt;Take a look at this debounce function we implemented above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;What we are doing here is, initialize a timer then return a &lt;code&gt;function&lt;/code&gt;. As soon as the user starts typing the &lt;code&gt;function&lt;/code&gt; executes -:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First it clears the timer if it's initialized.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;then it assigns the timer setTimeout function, which will run after 1 second if it is not cleared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if the user types any character within 1 second the &lt;code&gt;function&lt;/code&gt; will be called again. But in the above step, we already assigned the setTimeout function to the timer variable. So the clearTimeout will clear the function from the timer variable and also assign a new setTimeout function to the timer variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if the user didn't type and 1 second has passed, the function in setTimeout will execute and make the API call.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it.&lt;/p&gt;

&lt;p&gt;The full version of the debounce function with &lt;code&gt;this&lt;/code&gt; and argument binding is -:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Well, this is it from my side. See you soon 😁&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/hrRJ41JB2zlgZiYcCw/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/hrRJ41JB2zlgZiYcCw/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Introduction to Event delegation in JavaScript.</title>
      <dc:creator>Abhishek Jain</dc:creator>
      <pubDate>Sat, 19 Dec 2020 11:14:12 +0000</pubDate>
      <link>https://forem.com/abhishekjain35/introduction-to-event-delegation-in-javascript-3p7p</link>
      <guid>https://forem.com/abhishekjain35/introduction-to-event-delegation-in-javascript-3p7p</guid>
      <description>&lt;p&gt;Hello devs, let's understand event delegation.😁&lt;/p&gt;

&lt;p&gt;To understand event delegation, firstly, we'll need to get a hold of how the event listeners work.&lt;/p&gt;

&lt;p&gt;Look at the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sampleDiv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sampleDiv&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;sampleDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sample Div clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Woohooo, We've added an event listener.&lt;br&gt;
Things to note in the above code - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Browser always provides the event object as a parameter for the callback function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Event object provides many properties, But we're going to focus on the &lt;code&gt;event.target&lt;/code&gt; and &lt;code&gt;event.currentTarget&lt;/code&gt; properties.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Let's assume a situation
&lt;/h4&gt;

&lt;p&gt;Suppose a simple todo app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//HTML&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sampleDiv&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;AddBtn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Add&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="c1"&gt;//NOTE: some basic CSS is applied on these elements.&lt;/span&gt;

&lt;span class="c1"&gt;//JavaScript&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sampleDiv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sampleDiv&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AddButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;AddBtn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;AddButton&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todoToRemove&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Click me to remove&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;sampleDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the code above does this - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ZSifZ5G2qOjiOrwwuj/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ZSifZ5G2qOjiOrwwuj/giphy.gif" alt="Todo gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We want to click on the &lt;code&gt;todo(the paragraph)&lt;/code&gt; to remove the todo. so, I think that we can attach event listeners to each todo and remove todo when it is clicked like this-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementsByClassName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todoToRemove&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, It doesn't work.&lt;br&gt;
Since we are adding the todo dynamically through our javascript, the above code runs firstly, but there are no todos so that listeners are attached to any of them.&lt;/p&gt;
&lt;h3&gt;
  
  
  So, How can we fix this?
&lt;/h3&gt;

&lt;p&gt;Here comes the concept of event delegation. Theoretically, what happens in event delegation is we attach the event listener to a parent element. We check for the &lt;code&gt;event.target&lt;/code&gt; property, which results in the actual element on which the event has triggered.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So, the key takeaway is &lt;code&gt;event.currentTarget&lt;/code&gt; is the actual element on which the event listener is attached. And, &lt;code&gt;event.target&lt;/code&gt; is the element on which the actual event happened.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Fixing the problem with event delegation
&lt;/h3&gt;

&lt;p&gt;We can fix the todo removing problem with this simple code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;sampleDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;//NOTE: the remove function have less support on legacy browsers. so, you can also use this line inside if statement - &lt;/span&gt;
&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentTarget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this works perfectly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/rSw1ENoaCJgxLNoerE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/rSw1ENoaCJgxLNoerE/giphy.gif" alt="Working Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We added an event listener to the parent and checked if &lt;code&gt;event.target&lt;/code&gt; is a &lt;code&gt;p&lt;/code&gt; tag. If true, then we call the remove function on it to remove it from the dom tree.&lt;/p&gt;

&lt;p&gt;This whole concept of adding an event listener to a parent is called event delegation.&lt;/p&gt;

&lt;p&gt;That's it from my side. Till then 👋👋&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/7DzlajZNY5D0I/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/7DzlajZNY5D0I/giphy.gif" alt="BYE BYE"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Features you may not know about Chrome Dev-Tools</title>
      <dc:creator>Abhishek Jain</dc:creator>
      <pubDate>Sun, 20 Sep 2020 16:52:51 +0000</pubDate>
      <link>https://forem.com/abhishekjain35/features-you-may-not-know-about-chrome-dev-tools-4c0d</link>
      <guid>https://forem.com/abhishekjain35/features-you-may-not-know-about-chrome-dev-tools-4c0d</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;We all use chrome dev tools pretty much daily. But here are a few features I wish I knew earlier that could save me time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1: Change colors with color picker
&lt;/h3&gt;

&lt;p&gt;Did you know? Chrome has an inbuilt color picker&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/yXJJc7gm2h74yYg95A/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/yXJJc7gm2h74yYg95A/giphy.gif" alt="Color picker gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get this, open dev tools and click on the color you want to change - you will see the pen icon, click on it and move it then click on the color you want it to be changed with. &lt;strong&gt;You can also copy the color by clicking on it&lt;/strong&gt;(right side of the pen icon).&lt;/p&gt;

&lt;h3&gt;
  
  
  2: Simulate Slow internet
&lt;/h3&gt;

&lt;p&gt;You may or may not already know about it. So let me tell you. It can be very useful for testing purposes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fty1biiw6aa9zlg8ne0dy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fty1biiw6aa9zlg8ne0dy.png" alt="Slow internet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also add custom network speeds according to your needs in the custom tab.&lt;/p&gt;

&lt;h3&gt;
  
  
  3: Get Material colors
&lt;/h3&gt;

&lt;p&gt;Searching for material color palettes? Don't! It's already available in the dev tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3EmiYABbFI7xue7T4P/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3EmiYABbFI7xue7T4P/giphy.gif" alt="Material colors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also &lt;code&gt;long-press right click&lt;/code&gt; to view more available shades.&lt;/p&gt;

&lt;h3&gt;
  
  
  4: Check CPU usage by a website
&lt;/h3&gt;

&lt;p&gt;This feature is available in the performance monitor tab of dev tools. Open dev tools press &lt;code&gt;esc&lt;/code&gt; and follow this -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/IZZUXUgcqB6bbZx5lq/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/IZZUXUgcqB6bbZx5lq/giphy.gif" alt="performance"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see it tells real-time CPU usage. You can also check other features like js heap size etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  5: Pretty print API data.
&lt;/h3&gt;

&lt;p&gt;When working with lots of data with API, we have to log and view the data. That will yield JavaScript Object (JSON), which in the case of 2D arrays is going to be hard read and hard to find useful information in. But there's a simple fix for that:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.table()&lt;/code&gt;&lt;br&gt;
All it takes is to replace console.log with console.table. This function can easily display 1D and 2D arrays, but what makes this function extra useful is that it can also correctly displays column names and on top of that it also allows you to sort by each of these columns.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzc1kk4158gm3t2djshhw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzc1kk4158gm3t2djshhw.png" alt="pretty print table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  document.designMode = "on"
&lt;/h4&gt;

&lt;p&gt;This is a fun one. You can do almost anything with this feature. Just type &lt;code&gt;document.designMode = "on"&lt;/code&gt;&lt;br&gt;
and see the magic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/JdHLNc2y25EUgKArZe/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/JdHLNc2y25EUgKArZe/giphy.gif" alt="designMode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Change dev tools theme:
&lt;/h4&gt;

&lt;p&gt;We developers love dark themes, dev tools also got it. Just click on the settings icon in dev tools and the first option you'll see is the theme.&lt;/p&gt;

&lt;p&gt;This was it for today. See you soon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/jUwpNzg9IcyrK/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/jUwpNzg9IcyrK/giphy.gif" alt="peace"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>chrome</category>
      <category>devtools</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>List Comprehension and Creating lists from range function in Python🤙🤙</title>
      <dc:creator>Abhishek Jain</dc:creator>
      <pubDate>Sat, 04 Jan 2020 18:52:40 +0000</pubDate>
      <link>https://forem.com/abhishekjain35/list-comprehension-and-creating-lists-from-range-function-in-python-1a5i</link>
      <guid>https://forem.com/abhishekjain35/list-comprehension-and-creating-lists-from-range-function-in-python-1a5i</guid>
      <description>&lt;p&gt;The list is the most versatile python data structure and stores an ordered sequence of elements, just like the to-do-list.In Python, Lists are &lt;strong&gt;mutable&lt;/strong&gt;, meaning that the details can be altered, unlike tuples or even strings. These elements of a list are called items and can be any data type.&lt;/p&gt;

&lt;p&gt;Now for understanding this article, we need to understand the range() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;#my_list becomes [0, 2, 4, 6, 8]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the range() function creates a sequence of numbers. First parameter of range() function is the starting number, the second parameter is number to stop before it and the third parameter is steps between these numbers(By default it is 1).&lt;/p&gt;

&lt;p&gt;Now, we are going to look at some awesome tricks we can do on the lists in python ...&lt;/p&gt;

&lt;p&gt;So, let's create a list first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;#creates my_list with the values of [1, 3, 5, 7, 9]
&lt;/span&gt;    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;#prints the value at given index i.e. 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we know that we can get the desired value by index. Here comes the cool part, we can give a range in the [] brackets And get the values within that range. For example-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;#Prints [1, 3, 5, 7]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first parameter 0 means the starting point. The second parameter means how many numbers you want to get. We can give the 3rd parameter to get the interval also. &lt;/p&gt;

&lt;p&gt;Now, coming back to list comprehension in python. First, let me tell you the problem list comprehension is trying to solve.&lt;/p&gt;

&lt;p&gt;Suppose we have given a list of string.&lt;/p&gt;

&lt;p&gt;names = ["react", "angular", "ember"]&lt;/p&gt;

&lt;p&gt;and we need to use upper case on every string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;   &lt;span class="n"&gt;upper_case_names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

   &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
       &lt;span class="n"&gt;upper_case_word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
       &lt;span class="n"&gt;upper_case_names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upper_case_word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;#["REACT","ANGULAR","EMBER"]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if we could do the above code in one line only! So, here comes the list comprehension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;upper_case_names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;#["REACT","ANGULAR","EMBER"]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And Done!&lt;/p&gt;

&lt;p&gt;Explanation: We start with the empty list []. Then on the right-hand side, we put the iterator or conditional, and on the left side, we put statements that we need to execute. That's it.&lt;/p&gt;

&lt;p&gt;You can do so many cool stuff with the list comprehension. &lt;/p&gt;

&lt;p&gt;And that's it for this article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l1J3CbFgn5o7DGRuE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l1J3CbFgn5o7DGRuE/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>datastructures</category>
      <category>list</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Data Structure and Algorithms: Palindrome String.</title>
      <dc:creator>Abhishek Jain</dc:creator>
      <pubDate>Tue, 10 Dec 2019 09:58:11 +0000</pubDate>
      <link>https://forem.com/abhishekjain35/data-structure-and-algorithms-palindrome-string-45a0</link>
      <guid>https://forem.com/abhishekjain35/data-structure-and-algorithms-palindrome-string-45a0</guid>
      <description>&lt;p&gt;Hey there, today we are gonna check if a string is &lt;strong&gt;Palindrome&lt;/strong&gt; or not. Now, before doing that we need to understand what a palindrome is...&lt;/p&gt;

&lt;p&gt;Basically a palindrome is defined as a string that is written the same forward and backwards. For example -: abcdbca, if you read this from backwards it will the same.&lt;/p&gt;

&lt;p&gt;So, how can we implement this algorithm in python? &lt;br&gt;
Well, it's just a few lines of code in python...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;reversed_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;))):&lt;/span&gt;
        &lt;span class="n"&gt;reversed_string&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;reversed_string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we defined a function and it takes a parameter of string. In the second line, we define a variable &lt;code&gt;reversed_string&lt;/code&gt; and assign it to an empty string. In the third line of code, we start a for loop with a range of length of the string in the reversed order, Because we want the reversed string of the input string. In the fourth line of code, we assign the input order of string in reversed order to the &lt;code&gt;reversed_string&lt;/code&gt; variable. In the fifth line of code, we check if input &lt;code&gt;string&lt;/code&gt; is equal to the &lt;code&gt;reversed_string&lt;/code&gt; if it then returns True, else returns False.&lt;/p&gt;

&lt;p&gt;Hey, it's my first article. I hope you liked this article. I will be back with more amazing content until then enjoy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/jUwpNzg9IcyrK/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/jUwpNzg9IcyrK/giphy.gif" alt="bye"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
