<?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: Swastik Upadhye</title>
    <description>The latest articles on DEV Community by Swastik Upadhye (@swasdev4511).</description>
    <link>https://dev.to/swasdev4511</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%2F422547%2F0503230a-b5fa-4de2-abe6-19a819e5d36d.jpg</url>
      <title>DEV Community: Swastik Upadhye</title>
      <link>https://dev.to/swasdev4511</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/swasdev4511"/>
    <language>en</language>
    <item>
      <title>Javascript : Interpretated or Compiled ?</title>
      <dc:creator>Swastik Upadhye</dc:creator>
      <pubDate>Mon, 24 Oct 2022 18:39:52 +0000</pubDate>
      <link>https://dev.to/swasdev4511/javascript-interpretated-or-compiled--19po</link>
      <guid>https://dev.to/swasdev4511/javascript-interpretated-or-compiled--19po</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is a long-debated question. The most of the community goes with the interpreted language. But what is interpreted language ? Does it fit entirely to the defination of interpreted language ? We are going to touch some of the key concepts : Compilation, Interpretation, Parsing, AST, etc. Let's find out one by one. &lt;/p&gt;

&lt;h2&gt;
  
  
  Compilation
&lt;/h2&gt;

&lt;p&gt;The computer understands only 0s and 1s which is called as machine code. So at the final stage every computer program must be converted into this machine code.&lt;br&gt;
The program or the source code written by the user goes through various stages and it is called either compilation or interpretation.&lt;/p&gt;

&lt;p&gt;In the compilation process, the entire source code is converted into a machine code at once. And it produces a binary code which is portable that can be executed later on.&lt;/p&gt;

&lt;p&gt;When it comes to Javscript we don't really see this kind of behaviour/mechanism right.&lt;/p&gt;

&lt;p&gt;But this is not the pefect base reason that we can go for final conclusion. &lt;/p&gt;

&lt;p&gt;Moreover, how Javascript handles the errors plays key role to conclude upon whether it is compiled or interpreted.&lt;/p&gt;


&lt;h2&gt;
  
  
  Interpretation
&lt;/h2&gt;

&lt;p&gt;In the Interpretaion, there is an interpreter which runs and  executes the code line by line. So program is running and executed at the same time.&lt;/p&gt;

&lt;p&gt;So to simply it. Let us consider that we have 5 lines of code.&lt;br&gt;
In the Interpretation, the code is being executed frome line no. 1 to 5 sequentially. &lt;br&gt;
Now if there's an error due to change in value at runtime that cannot be detected early.&lt;/p&gt;

&lt;p&gt;So does this conclude that JS is not an Interpreted language ?&lt;/p&gt;

&lt;p&gt;Now it sounds so confusing and really hard to conclude right. We should understand what is parsing and what are parsed languages ?&lt;/p&gt;


&lt;h2&gt;
  
  
  Parsing
&lt;/h2&gt;

&lt;p&gt;Some languages undergo through processing step before executing which is nothing but parsing.&lt;/p&gt;

&lt;p&gt;So unlike Interpreted languages if there's any error at line no.3 It can be detected in the parsing phase itself before execution.&lt;/p&gt;

&lt;p&gt;Now the big question is what is the difference between parsed language and compiled language.&lt;/p&gt;

&lt;p&gt;It is to be noted that : &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;All compiled languages are parsed&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When any program is parsed completely then after the parsing it outputs the program in a specific parsed form which is called as &lt;u&gt;Abstract Syntax Tree&lt;/u&gt; (AST)&lt;/p&gt;

&lt;p&gt;Let's consider 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;var a = 10;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Equivalent AST for above program :
&lt;/h2&gt;

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

&lt;p&gt;Note : We can check AST generated for our source code here : &lt;a href="https://astexplorer.net/"&gt;AST&lt;/a&gt;  &lt;/p&gt;




&lt;h2&gt;
  
  
  What happens in case of JS ?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can observe that there is an early error detection mechanism when we try to execute the Javascript program&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So this infers that Javascript program is being parsed before its execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thus, Javascript is a &lt;strong&gt;Parsed Language&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now this parsed Javascript code is converted to some kind of byte-code by Javascript engine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Furthermore this byte code is refined and optimized by JIT compiler and then Javscript VM executes it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Although Javascript looks like an interpreted language at first glance.&lt;br&gt;
It is clear now it goes through various phases as stated above. &lt;br&gt;
Javascript is a mixed approach of  &lt;strong&gt;Compiled and Interpretated Language&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Thank you for stopping by!. If you find it useful consider sharing it. You can connect with me here : &lt;a href="https://www.linkedin.com/in/swasdev4511/"&gt;LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Understanding Slice Method In Javascript</title>
      <dc:creator>Swastik Upadhye</dc:creator>
      <pubDate>Tue, 23 Aug 2022 18:33:00 +0000</pubDate>
      <link>https://dev.to/swasdev4511/understanding-slice-method-in-javascript-24di</link>
      <guid>https://dev.to/swasdev4511/understanding-slice-method-in-javascript-24di</guid>
      <description>&lt;p&gt;Hey there 👋, I have explained &lt;a href="https://dev.to/swasdev4511/array-splice-method-3akg"&gt;Splice&lt;/a&gt; method in my previous post. &lt;br&gt;
Here I am going to explain &lt;strong&gt;slice&lt;/strong&gt; method with some examples.&lt;br&gt;
Although there is not much difference in their names ( just an extra p , right ) but they differ so much in their mechanism or how they work. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Slice method does not mutate the original array.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Yeah, you have read it right. Unlike splice method, slice method does not change the contents of the original array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rather it returns a copy of the segment / fraction / slice of an original array ( more precisely a &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy"&gt;shallow copy&lt;/a&gt; ).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Take a look at syntax :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;slice(start, end)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; : start and end both can be &lt;strong&gt;&lt;u&gt;optional&lt;/u&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To understand this we will directly jump into examples.&lt;/li&gt;
&lt;li&gt;And for that we will start with simple scenarios&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we can divide this in three parts : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;slice without arguments : slice()&lt;/li&gt;
&lt;li&gt;slice with start : slice(start)&lt;/li&gt;
&lt;li&gt;slice with both start and end : slice(start, end)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's take a look at them one by one.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;slice()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example - 1
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let resultedArray = numbers.slice();
console.log(' result - 1 ', resultedArray);   // [1, 2, 3, 4, 5, 6, 7, 8, 9];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;From the above example you might be wondering the original array and resulted array are exactly same.Then what the heck is the use of the slice method here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Well it returns a copy ( &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy"&gt;shallow copy&lt;/a&gt; ) of an original array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Following code block explains it.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let products = [ { id : 100 , productName : 'Mobile', manufacturer : { id : 1 , countryCode : 'USA'}} ,{ id : 101 , productName : 'TV'}, { id : 102 , productName : 'Washing Machine', manufacturer : { id : 2 , countryCode : 'CAN'}} ];

let resultedArray = products.slice();

console.log( ' products ', products);
console.log( ' resultedArray ', resultedArray);

// To check their equality 
console.log( ' Equality ', products === resultedArray ) // false

// To check the equality of child nodes ( nested keys )
console.log(' child node ', products[0].manufacturer === resultedArray[0].manufacturer );   // true

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As we check for the equality of parent object, it clearly gives us indication that both the objects are not same.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When we check for the equality of nested objects, it indicates that both are same. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;That means slice method does make a shallow copy.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;slice(start)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example - 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let resultedArray = numbers.slice(2);
console.log(' result - 1 ', resultedArray);   // [3, 4, 5, 6, 7, 8, 9];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Here in the above example slice method gives a slice or the portion of the array that starts with specified index to the end of the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Nothing fancier right, but wait there are several use cases start can be +ve , -ve , zero &amp;amp; index out of bound.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's look at them one by one.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example - 3
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let resultedArray = numbers.slice(-2);
console.log(' result - 1 ', resultedArray);   // [8, 9];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When the start is specified as -ve integer then it will point from the end of the array and slice / gives the portion of an array till the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example - 4
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let resultedArray = numbers.slice(0);
console.log(' result - 1 ', resultedArray);   // [1, 2, 3, 4, 5, 6, 7, 8, 9];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When the start is 0. It simply creates a copy. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;slice() &amp;lt;=&amp;gt; slice(0)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Example - 5
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let resultedArray = numbers.slice(20);
console.log(' result - 1 ', resultedArray);   // [];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When the start is index out of bound ( i.e greater than array length ) then it will return an empty array.&lt;/li&gt;
&lt;/ul&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;slice(start, end)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example - 6
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let resultedArray = numbers.slice(2, 4);
console.log(' result - 1 ', resultedArray);   // [3, 4];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the above example slice method gives us a portion of array starting from start index - 2 upto end index but &lt;strong&gt;&lt;u&gt;excludes the element at end index&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thus we get elements 3 and 4 at the index 2 and 3 respectively ( we did not get element present at index 4 );&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;When the end is specified then element at that position is excluded in the resulting array after slice&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Now another use case can be end can be -ve. I won't go in details for that as you might have got the idea if you have gone through above examples.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example - 7
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let resultedArray = numbers.slice(2, -4);
console.log(' result - 1 ', resultedArray);  // [3, 4, 5];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Thank you for stopping by!. If you find it useful consider sharing it. You can connect with me here : &lt;a href="https://www.linkedin.com/in/swasdev4511/"&gt;LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Most Powerful Array Method : Splice</title>
      <dc:creator>Swastik Upadhye</dc:creator>
      <pubDate>Thu, 11 Aug 2022 17:39:00 +0000</pubDate>
      <link>https://dev.to/swasdev4511/array-splice-method-3akg</link>
      <guid>https://dev.to/swasdev4511/array-splice-method-3akg</guid>
      <description>&lt;p&gt;Splice is one of the most powerful methods of Array. We can achieve following things using splice method :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove element/s from a specified index of an array&lt;/li&gt;
&lt;li&gt;Replace element/s from an array ( &lt;strong&gt;Remove + Insert&lt;/strong&gt; )&lt;/li&gt;
&lt;li&gt;Insert element/s to an array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short, splice method mutates the original array.&lt;/p&gt;

&lt;p&gt;Take a look at syntax :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;splice(start, deleteCount, item1, item2, itemN)

Note : Except start all others are optional )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To understand this we will directly jump into examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing Elements from an Array
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Example - 1&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 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.splice(2);
console.log(' result - 1 ', numbers);   // [1, 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What splice method does here is it removes all the other elements from the specified index ( i.e start as specified in syntax )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now there are four possibilities : start - index might be +ve , -ve , zero , index out of bound &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example - 2&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 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.splice(0);
console.log(' result - 1 ', numbers);   // []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Basically splice method removes all the elements from index 0.&lt;/li&gt;
&lt;li&gt;That means ultimately it removes all the elements from the array.&lt;/li&gt;
&lt;li&gt;We can conclude that we can &lt;strong&gt;&lt;u&gt;empty the array&lt;/u&gt;&lt;/strong&gt; using splice method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example - 3&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 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.splice(-2);
console.log(' result - 1 ', numbers);   // [1, 2, 3, 4, 5, 6, 7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When start index is specified -ve then splice method removes all the elements from the end of that array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example - 4&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 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.splice(20);
console.log(' result - 1 ', numbers);   // [1, 2, 3, 4, 5, 6, 7, 8, 9];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When start index is specified greater than or equal to length of the array then no element is removed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Removing N no. of Elements from an Array
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;splice(start, deleteCount)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now along with the index from where we can start removing elements, we can specify exactly how many elements can be removed with the &lt;strong&gt;deleteCount&lt;/strong&gt; as specified in the syntax.&lt;/li&gt;
&lt;li&gt;Note that it's an &lt;strong&gt;optional&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Now we will take a look at the examples so that we can get an idea of it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example - 5&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 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.splice(3,2);
console.log(' result - 1 ', numbers);   // [1, 2, 3, 6, 7, 8, 9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here in the above example splice method will start removing 2 elements (2 - deletecount) from the start index i.e 3&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Replacing Elements from an Array
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;splice(start, deleteCount, item1, item2, itemN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We can replace the elements from an array i.e inserting and removing the elements simultaneously.&lt;/li&gt;
&lt;li&gt;Take a look at following example you can clearly understand this&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example - 6&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 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.splice(3,2, "a", "b", "c");
console.log(' result - 1 ', numbers);   // [1, 2, 3, 'a', 'b', 'c', 6, 7, 8, 9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here, in the above example from index 3 ( i.e start - 3 ), 2 elements ( i.e delete count - 2 and elements : 3 &amp;amp; 4 ) are removed and "a", "b" and "c" are added. &lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Inserting Elements into an Array
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This can be easily achieved by keeping &lt;strong&gt;delete count 0&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example - 7&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 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.splice(3,0, "a", "b", "c");
console.log(' result - 1 ', numbers);   // [1, 2, 3, 'a', 'b', 'c', 4, 5, 6, 7, 8, 9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I suppose this example doesn't need an explanation if you have gone through previous example.&lt;/p&gt;




&lt;h2&gt;
  
  
  Return value
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sometimes we need to keep track of exactly which are the elements are removed.&lt;/li&gt;
&lt;li&gt;When we splice on an array then, it returns an array of deleted elements.&lt;/li&gt;
&lt;li&gt;Take a look at this example : &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example - 8&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 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let deletedArray = numbers.splice(3,2);
console.log( " -- deleted arr --- ", deletedArray); // [4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Take a note of it, if no element is removed from an array then &lt;strong&gt;empty array&lt;/strong&gt; is returned&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for stopping by!. If you find it useful consider sharing it. You can connect with me here : &lt;a href="https://www.linkedin.com/in/swasdev4511/"&gt;Linkedin&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Sorting In Javascript | How it works under the hood ?</title>
      <dc:creator>Swastik Upadhye</dc:creator>
      <pubDate>Sun, 24 Apr 2022 10:09:00 +0000</pubDate>
      <link>https://dev.to/swasdev4511/sorting-in-javascript-how-it-works--1cb</link>
      <guid>https://dev.to/swasdev4511/sorting-in-javascript-how-it-works--1cb</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;You might have guessed from the title what I'm going to talk about in this post. Sorting is one of the most popular and often used methods.&lt;/p&gt;

&lt;p&gt;Sort() method sorts the elements of an array and returns an array in a specific sorted format. &lt;/p&gt;

&lt;p&gt;Take a look at the following example.&lt;/p&gt;

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

let animals = [ "Zebra", "Bear", "Kangaroo", "Giraffe", "Lion", "Tiger", "Dog" ];
animals.sort();
console.log(animals);

// Output :  ['Bear', 'Dog', 'Giraffe', 'Kangaroo', 'Lion', 'Tiger', 'Zebra']


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

&lt;/div&gt;

&lt;p&gt;Oh! That's super easy 😄!&lt;/p&gt;

&lt;p&gt;Now take a look at the following example.&lt;/p&gt;

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

let numbers = [ 1 , 50, 101 , 4 , 90 , 40 , 1000, 78 , 9 ];
numbers.sort();
console.log(numbers);

// Expected  Output :   [1, 4, 9, 40, 50, 78, 90, 101, 1000]
// Actual Output :   [1, 1000, 101, 4, 40, 50, 78, 9, 90]


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Oh wait!😳 What did we get here?
&lt;/h2&gt;



&lt;p&gt;Let's find out!🧐&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;From observation, one thing is clear that if the array contains the elements with string data type only, they are being sorted in &lt;strong&gt;ascending&lt;/strong&gt; order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Basically, what happens behind the scene is each element in an array is converted into string and then UTF-16 code equivalent value of them are compared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just to make it simple, look at the following diagram : &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mjs809a80os3sfrubei.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%2F2mjs809a80os3sfrubei.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now we get to know that why in the second example the output was so wierd.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Because we were not aware about the things happening under the hood.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Sort with Compare function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Also, there is an another form wherein sort methods can accept a &lt;a href="https://dev.to/swasdev4511/dealing-with-asynchronous-data-in-javascript-episode-01-500a"&gt;callback function&lt;/a&gt; which is known as compare function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The compare function accepts two parameters ( e.g a &amp;amp; b ), and compare these two values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it is returning positive integer then it places b before a.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it is returning negative integer then it places a before b.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it is returning zero then it keeps the order as it is.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdosvga25m9zgvjrf4r96.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%2Fdosvga25m9zgvjrf4r96.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;ul&gt;
&lt;li&gt;Now we will try to write a function for sorting an array which contains &lt;strong&gt;numbers&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Using Inline function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzbcmrwqtbdt2eaqscvfj.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%2Fzbcmrwqtbdt2eaqscvfj.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Arrow function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffhmjsoi8aed38cx0395p.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%2Ffhmjsoi8aed38cx0395p.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using short-hand version&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff67v70cck8qyw7zu2fgg.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%2Ff67v70cck8qyw7zu2fgg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Return value :
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;sort() method works on the principle &lt;a href="https://en.wikipedia.org/wiki/In-place_algorithm" rel="noopener noreferrer"&gt;place-in&lt;/a&gt; algorithm.&lt;/li&gt;
&lt;li&gt;It returns the reference of the original array.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;That means if we try to change the returned array it will also change the original array as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Take a look at the following example :&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

let numbers = [1, 5, 6, 7, 2, 3, 4, 8, 9 ];
let sortedArray = numbers.sort((a, b) =&amp;gt; a - b);

console.log( " numbers : ",numbers); // [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log( " sortedArray : ",sortedArray); // [1, 2, 3, 4, 5, 6, 7, 8, 9];

// To check the same reference 
console.log( numbers === soretedArray); // true


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

&lt;/div&gt;




&lt;h2&gt;
  
  
  Application :
&lt;/h2&gt;

&lt;p&gt;Earlier, In one of my Interviews I have been asked to write a program and the problem was : &lt;/p&gt;

&lt;p&gt;//Given a list of non-negative integers, arrange them in such a manner that they form the largest number possible. The result is going to be very large, hence return the result in the form of a string.&lt;br&gt;
// Input:&lt;br&gt;
// 3 30 34 5 9&lt;br&gt;
// 546 54 60 548&lt;/p&gt;

&lt;p&gt;// Output: 9534330;&lt;br&gt;
// 6054854654;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk9mnty2dtgcx6tjnm8vt.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%2Fk9mnty2dtgcx6tjnm8vt.png" alt="Sorting_problem"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for stopping by!. If you find it useful consider sharing it. You can connect with me here : &lt;a href="https://www.linkedin.com/in/swasdev4511/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Array : Reduce method ( How it works and its applications )</title>
      <dc:creator>Swastik Upadhye</dc:creator>
      <pubDate>Sat, 19 Mar 2022 15:53:19 +0000</pubDate>
      <link>https://dev.to/swasdev4511/array-reduce-method-how-it-works-and-its-applications--2o92</link>
      <guid>https://dev.to/swasdev4511/array-reduce-method-how-it-works-and-its-applications--2o92</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As the title suggest we are going to discuss one of the most widely used array methods : &lt;strong&gt;reduce&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The word itself describes that it &lt;strong&gt;reduces&lt;/strong&gt; the input value to single output value. Let us see how it works!&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;It iterates over each element in an array from left to right &lt;/li&gt;
&lt;li&gt;&lt;p&gt;It accepts a &lt;a href="https://dev.to/swasdev4511/dealing-with-asynchronous-data-in-javascript-episode-01-500a"&gt;callback function&lt;/a&gt; which has following parameters accumulator , currentvalue , index ,array and an optional intial value. ( although it is optional but it's a best practice to use it )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Syntax :&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reduce(function(accumulator, currentValue, index, array) {
   /* function-body */
}, initialValue)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;accumulator&lt;/strong&gt; (a) - it is the return value in last iteration or initial value&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;currentvalue&lt;/strong&gt; (c) - it is the value of the current element in the current ongoing iteration&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index&lt;/strong&gt; - it is the index of the current iteration&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;array&lt;/strong&gt; - it is an input array on which we are providing applying the reduce function&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;initialValue&lt;/strong&gt; - it is the starting value that is to be returned to the accumulator in the very first iteration&lt;/p&gt;




&lt;p&gt;Now enough theory let us take a look an example. In this example we are going to calculate the total of all the numbers given in an array of numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [ 1, 2 ,3,4,5,6,7,8,9,10];
const sum = arr.reduce((accumulator,currentvalue) =&amp;gt; accumulator + currentvalue,0);
console.log("Sum : ",sum);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the above example on the very first iteration, the value of accumulator is the initial value i.e 0 and current value is 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thus it returns 0 plus 1 resulting 1 and accumulator value becomes 1. ( as we have provided addition logic )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now in the next iteration, the value of accumulator is 1 and currentvalue is 2 so the next value of accumulator results into their addition as 3 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This goes on till last iteration and the accumulator values is returned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Take a note that we can provide any logic as per our need inside the function body. And also, we can provide any initial value.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Some of the practical use cases where we can use reduce function  are :
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt; create string from an array of characters.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Find max. value in a given array of numbers&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Find the array of names for the given array of employees whose age is &amp;gt; 40 &lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Find the frequency of same aged users from given array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcuvat3jume4tprbf279s.png" alt="Image description" width="800" height="589"&gt;
&lt;/h2&gt;

&lt;p&gt;Thank you for stopping by. If you find it useful consider sharing it. You can connect with me here : &lt;a href="https://www.linkedin.com/in/swasdev4511/"&gt;linkedin&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Array : Different ways to Remove Duplicates</title>
      <dc:creator>Swastik Upadhye</dc:creator>
      <pubDate>Sat, 19 Mar 2022 14:20:25 +0000</pubDate>
      <link>https://dev.to/swasdev4511/array-remove-duplicates-29c2</link>
      <guid>https://dev.to/swasdev4511/array-remove-duplicates-29c2</guid>
      <description>&lt;p&gt;This is a very short article which covers a common use case we come across in our day to day coding. We often want to remove duplicate values from an array. Let's see! &lt;/p&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

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

&lt;p&gt;Thank you for stopping by. Let me know any other way to deal with it. Let's connect on : &lt;a href="https://www.linkedin.com/in/swasdev4511"&gt;Linkedin&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Dealing with Asynchronous data : Async - await</title>
      <dc:creator>Swastik Upadhye</dc:creator>
      <pubDate>Sun, 23 Jan 2022 20:12:32 +0000</pubDate>
      <link>https://dev.to/swasdev4511/dealing-with-asynchronous-data-async-await-23d7</link>
      <guid>https://dev.to/swasdev4511/dealing-with-asynchronous-data-async-await-23d7</guid>
      <description>&lt;p&gt;Welcome to the world of JS! As we have discussed in my previous post about how we can handle asynchronous data using Promises. If you are new to promises then I will highly recommend to check my previous post here : &lt;a href="https://dev.to/swasdev4511/asynchronous-javascript-promises-472f"&gt;Promises&lt;/a&gt;. We are going to continue this discussion further a next step. ( Hopefully a last one on handling async data 😊 )&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the need ?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Consider the example shown in the code snippet below.&lt;/li&gt;
&lt;li&gt;Now open your code editor or console window in browser and try to write the code as below and hit enter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjm307ce0rnufqfmre1no.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%2Fjm307ce0rnufqfmre1no.png" alt="An example showing the need of async-await"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are you surprised to see the logs on the screen ? &lt;/li&gt;
&lt;li&gt;Well don't worry I am going to cover it briefly here why this happens. ( For those who cannot access editor right now or as lazy as I am 😉 , take a look at screenshot below)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdh1yvvdcv4iip3ag5bqw.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%2Fdh1yvvdcv4iip3ag5bqw.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can see that the order of the logs printed is not in the order that we are expecting. &lt;/li&gt;
&lt;li&gt;But why this happens ? To answer this question we should have known that how Javascript handles settimeouts and promises behind the scene and a concept called &lt;strong&gt;event loop&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;You can skip this part 👇 if you are a beginner&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Brief Explaination for those who are curious 🤔
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;For those who are well aware of event loop, just note that event loop continuously monitors the callstack and checks the macrotask queue as well as microtask queue.&lt;/li&gt;
&lt;li&gt;settimeouts come under &lt;strong&gt;macrotask queue&lt;/strong&gt; while promises come under &lt;strong&gt;microtask queue&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;When there is nothing to execute in the main thread and call stack is empty then &lt;strong&gt;microtask queue gets the priority&lt;/strong&gt; and gets executed and after that macrotask queue gets executed.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I know this is just too much in 2-3 lines to digest. If I try to explain it in details then this post will become too lengthy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So it is clear from the logs that sequential execution isn't possible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This problem is resolved by async functions with the use of await.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Async Functions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We can add &lt;strong&gt;async&lt;/strong&gt; keyword to a normal function sothat it can &lt;strong&gt;return implicitly the promise&lt;/strong&gt; rather than value.&lt;/li&gt;
&lt;li&gt;Check out the following code snippet : &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fplhue5hfzi7cs8fby5q8.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%2Fplhue5hfzi7cs8fby5q8.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can handle promises by chaining with then method if resolved or by chaining with catch method if rejected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now our goal is to wait till response come from the promises either resolved or rejected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To achieve this await keyword has introduced.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using await
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When we add &lt;strong&gt;await&lt;/strong&gt; keyword before a promise then the &lt;strong&gt;execution pauses&lt;/strong&gt; here till the promise gets resolved or rejected.&lt;/li&gt;
&lt;li&gt;Let us see an example shown in the code snippet below : &lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;In the above code example, there are three tasks : task1 , task2 &amp;amp; task3 which take 10sec,3sec &amp;amp; 1sec respectively to complete.&lt;/li&gt;
&lt;li&gt;When the async function getTaskData is invoked then it encounters task1 and waits(10sec) to resolve the promise and then after moves to task2.&lt;/li&gt;
&lt;li&gt;Again it waits( 3 sec ),after resolving task2 it is moved to task3.&lt;/li&gt;
&lt;li&gt;And at last, task3 gets resolved after 1sec&lt;/li&gt;
&lt;li&gt;This way synchronous behaviour is achieved.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Handling Errors
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We can make use of &lt;strong&gt;try-catch&lt;/strong&gt; to handle the errors &lt;/li&gt;
&lt;li&gt;Consider the following example for the sake of understanding&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9bzw80ig8miog4og277.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%2Fd9bzw80ig8miog4og277.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Takeouts
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;It's an &lt;strong&gt;extension of promises&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;They make asynchrounous code &lt;strong&gt;look like synchronous&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;async function &lt;strong&gt;always returns a promise&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;We can make use of &lt;strong&gt;try-catch&lt;/strong&gt; to handle the errors.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks for reading the article!!!.Feel free to drop me here if you have any doubts or suggestions! You can connect with me here : &lt;a href="https://twitter.com/swasdev4511" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &amp;amp; &lt;a href="https://www.linkedin.com/in/swasdev4511" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>promise</category>
      <category>await</category>
    </item>
    <item>
      <title>Dealing with asynchronous data in Javascript : Promises</title>
      <dc:creator>Swastik Upadhye</dc:creator>
      <pubDate>Sun, 16 Jan 2022 20:33:00 +0000</pubDate>
      <link>https://dev.to/swasdev4511/asynchronous-javascript-promises-472f</link>
      <guid>https://dev.to/swasdev4511/asynchronous-javascript-promises-472f</guid>
      <description>&lt;h2&gt;
  
  
  What is Promise ?
&lt;/h2&gt;

&lt;p&gt;Well, we use this word in our daily life so many times. We often make promises to ourselves , sometimes we keep them and sometimes break them 😉. Does it have the exact same meaning when it comes to Programming ? &lt;/p&gt;

&lt;p&gt;Well, sort of!. To make this understand we should recall what is the purpose of handling asynchronous data. In an asynchronous manner, what we are doing, first we complete the primary task and then only we try to accomplish susequent task.Here, primary task is a kind of a promise. Now, let us find out how are they exactly behave in programming context.&lt;/p&gt;




&lt;h2&gt;
  
  
  Promise
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Promise is an object that produces a single value either &lt;strong&gt;resolved&lt;/strong&gt; or &lt;strong&gt;rejected&lt;/strong&gt; ( make them / break them )&lt;/li&gt;
&lt;li&gt;Promise has three states : &lt;strong&gt;Pending&lt;/strong&gt; , &lt;strong&gt;fulfilled&lt;/strong&gt; &amp;amp; &lt;strong&gt;rejected&lt;/strong&gt;.
They are &lt;strong&gt;eager&lt;/strong&gt; i.e as soon as we call promise constructor, it starts the given task.&lt;/li&gt;
&lt;li&gt;It accepts two functions : &lt;strong&gt;resolve&lt;/strong&gt; &amp;amp; &lt;strong&gt;reject&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Promises are thennable i.e we can execute the subsequent task using &lt;strong&gt;then&lt;/strong&gt; method.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;In the above code example, we are going to perform one of the two actions depending on the lock down status. &lt;/li&gt;
&lt;li&gt;If there is no lockdonwn then we can go for a trip otherwise we have to sit in our home.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, take a practical example. A user is trying to sign in a website. Here, authentication api is called if it is going to return a token then only user is allowed to see dashboard.&lt;/p&gt;

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




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

&lt;ul&gt;
&lt;li&gt;If we are having multiple asynchronous tasks which depend on one another then we can chain them using &lt;strong&gt;then&lt;/strong&gt; method&lt;/li&gt;
&lt;li&gt;As promises are thennable we can chain n number of promises one after another.&lt;/li&gt;
&lt;/ul&gt;

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




&lt;h2&gt;
  
  
  Handling Errors
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Errors can be occured because of multiple factors. So it is neccessary to handle them rather than providing a broken page.&lt;/li&gt;
&lt;li&gt;Promises do have a &lt;strong&gt;catch&lt;/strong&gt; method. We can chain it to promise object.&lt;/li&gt;
&lt;li&gt;The code example shown below illustrates that if there's an error then he/she will redirected to the signup page.&lt;/li&gt;
&lt;/ul&gt;

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




&lt;h2&gt;
  
  
  Advantages &amp;amp; Drawbacks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We don't need to nest the functions one into another, as it is done in the callbacks.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It improves the code readability and provides a better way of handling errors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As promises are eager, once fired we cannot cancel them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The programming execution doesn't stop at the moment until the promise is received. The program continues to run further line by line.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also, still the code doesn't look synchronous.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;In one of my Interviews I have been asked to write a promise.&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Problem statement&lt;/u&gt;&lt;/strong&gt; :&lt;br&gt;
Write a promise if it is having number then it will resolve as number else reject as NOT  a number&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getPromise(value) {
  return new Promise((resolve, reject) =&amp;gt; {
    if (typeof value === 'number') {
      resolve('I am number!');
    } else {
      reject(' I am NOT a number!');
    }
  });
}

getPromise("abc").then((res) =&amp;gt; {
  console.log('Resolved -------', res);
}, (err) =&amp;gt; {
    console.log(" Rejected -----",err);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So to overcome the drawbacks, async-await is introduced&lt;br&gt;
We will see what are they and how they work in the next blog.&lt;/p&gt;

&lt;p&gt;Thanks for reading the article!!!.Feel free to drop me here if you have any doubts and suggestions!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Dealing with asynchronous data in Javascript : Callbacks</title>
      <dc:creator>Swastik Upadhye</dc:creator>
      <pubDate>Sun, 02 Jan 2022 17:22:52 +0000</pubDate>
      <link>https://dev.to/swasdev4511/dealing-with-asynchronous-data-in-javascript-episode-01-500a</link>
      <guid>https://dev.to/swasdev4511/dealing-with-asynchronous-data-in-javascript-episode-01-500a</guid>
      <description>&lt;p&gt;First of all, let us understand what is the meaning of word asynchronous in programming context.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;It is a behaviour when the program is being executed and the thread will not wait till the execution of the previous line is completed is asynchronous.
( synchronous meaning procedural one task is done then and then only another is taken)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Different ways to handle async data :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Callback&lt;/li&gt;
&lt;li&gt;Promise&lt;/li&gt;
&lt;li&gt;Async-await&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  In this article, I am going to discuss callbacks briefly.
&lt;/h2&gt;

&lt;p&gt;Consider the below example, in which user is going to make a request on server to create a student and then trying to get all students.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Normal Approach&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(For the sake of simplicity, I have mimicked the server request by adding setTimeout.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let stdList = [];
function getStudents(){
   setTimeout( () =&amp;gt; {
    return stdList;
   },3000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, the user is making a request to get the data from a server which is going to take some time ( 3 sec ) for completing  it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createStudent(stdObj){
   setTimeout( () =&amp;gt; {
     stdList.push(stdObj);
   },5000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, the user is trying to create a student i.e writing on the server which is going to take more time ( 5 sec ) for completing  it. ( Generally, this much time doesn't take in reality but for the sake of understanding I am considering this )&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let stdObj = { name : "Ayush" , age : 16 , nationality : "Indian" }
createStudent(stdObj);
getStudents();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To achieve this goal, if we go through conventional method by making one by one requests as shown in above code snippet. &lt;br&gt;
After analyzing the result, the chances are very low that you will get exact output as expected.&lt;br&gt;
Because write operation takes time than get operation. When we make request to the server for write operation, Javscript doesn't wait and it moves to next line and tries to execute.&lt;/p&gt;

&lt;p&gt;So, to avoid this, callbacks plays an importantant role.&lt;br&gt;
Let us understand what the heck callback is ? To understand this we need to have some basic knowledge of so called &lt;del&gt;functions&lt;/del&gt; in Javascript ( which is the ❤️ of Javascript )&lt;/p&gt;


&lt;h2&gt;
  
  
  Callback (Callback Functions)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In JavaScript, functions are objects.&lt;/li&gt;
&lt;li&gt;Functions are the first class citizens.&lt;/li&gt;
&lt;li&gt;That means &lt;strong&gt;we can assign a function to a variable, we can return a function from a function, we can pass a function as an 
argument while calling a function, we can pass function as a 
parameter.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;That means we can assign them to variables or can pass them to 
another function as a parameter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Callback function&lt;/strong&gt; : Function which is passed as an argument to another function and executed later on is called a Callback Function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Higher Order Function&lt;/strong&gt; : The function which takes another function as a parameter ( or a function that returns another function ) is called the Higher Order Function.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;With Callback&lt;/u&gt;&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 createStudent(stdObj,callback){
   setTimeout( () =&amp;gt; {
     stdList.push(stdObj);
     callback();
   },5000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;createStudent(stdObj,getStudents);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are passing a function to another function : function getStudent is passed to the function createStudent which is totally valid in Javascript.&lt;br&gt;
Now after the execution of the createStudent when we get response we are calling the getStudents ( i.e &lt;strong&gt;callback&lt;/strong&gt;);&lt;br&gt;
By this way, we get exact data as we expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Drawbacks
&lt;/h2&gt;

&lt;p&gt;But wait! What if we have scenario wherein we are making multiple requests one after another. Is it feasible to go with this approach using callbacks?&lt;br&gt;
The answer is big &lt;strong&gt;NO&lt;/strong&gt;. Because we have to nest functions one into another which is not handy.&lt;br&gt;
It is called as &lt;strong&gt;callback hell&lt;/strong&gt; ( Pyramid of doom )&lt;br&gt;
Moreover, it is not easy to debug the code and it's a very bad practice.&lt;br&gt;
As complexity increases this can create problems.&lt;/p&gt;

&lt;p&gt;So to overcome the drawbacks of callbacks, Promises are born.&lt;br&gt;
We will see what are they and how they resolve the problems caused with callbacks in the next blog.&lt;/p&gt;

&lt;p&gt;Thanks for reading the article, hope this will help you clear your doubts.Still if you have doubts or suggestions feel free to drop me here : &lt;a href="https://www.linkedin.com/in/swasdev4511"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

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