<?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: Sushil Choudhari</title>
    <description>The latest articles on DEV Community by Sushil Choudhari (@sushilmod).</description>
    <link>https://dev.to/sushilmod</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%2F860492%2F0a02bd66-7305-411d-83d2-9020a1e25c43.jpeg</url>
      <title>DEV Community: Sushil Choudhari</title>
      <link>https://dev.to/sushilmod</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sushilmod"/>
    <language>en</language>
    <item>
      <title>Difference between let , const and var in Javascript</title>
      <dc:creator>Sushil Choudhari</dc:creator>
      <pubDate>Fri, 22 Jul 2022 17:57:35 +0000</pubDate>
      <link>https://dev.to/sushilmod/difference-between-let-const-and-var-in-javascript-2mka</link>
      <guid>https://dev.to/sushilmod/difference-between-let-const-and-var-in-javascript-2mka</guid>
      <description>&lt;p&gt;In this blog, we will discuss the javascript keywords var, let and const.&lt;br&gt;
After ES6 it's a bit confusing to understand this keyword and how they work inside the javascript.&lt;/p&gt;

&lt;p&gt;Let's start the scope of let, var and const &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scope&lt;/strong&gt;&lt;br&gt;
let:- variables declared in let's are in the block scope&lt;/p&gt;

&lt;p&gt;const:- variables declared in const are also in the block scope &lt;/p&gt;

&lt;p&gt;var:- variable declared with var is in the functional scope&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function abc () {
var a=10
}
console.log(a) // gives an error
for(let i = 0;i&amp;lt;2;i++){
    console.log( i ) // print 1 2 
}
console.log(i) // gives an error
{
const b=1;
console.log(b)// print 1
}
console.log(b)// gives an error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;for var, it shows the error because its is functional scope so it cant accessible outside the function&lt;/li&gt;
&lt;li&gt;for let inside the for loop block, it prints the output but outside the block, it gives an error as let is blocked scope&lt;/li&gt;
&lt;li&gt;for const inside the curly braces, it prints the value but outside braces give an error as it is also a block scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's check Reassigning values to let, const and var&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;var:- reassigning is allowed&lt;/li&gt;
&lt;li&gt;let:- reassigning is allowed&lt;/li&gt;
&lt;li&gt;const:- reassigning is not allowed
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 1 ;
a = 2;
console.log(a) // print 2
let l = 1; 
l = 2 ;
console.log(l) // print 2
const c = 1 ;
c=2
consol.log(c) // give an error 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;for var reassigning of value shows most recent value 2&lt;/li&gt;
&lt;li&gt;for let also reassigning of value shows most recent value 2&lt;/li&gt;
&lt;li&gt;for const reassigning of value gives an error as reassigning is not allowed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's check for Redeclaration of let, const and var&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;let:- Redeclaration is not allowed &lt;/li&gt;
&lt;li&gt;var:- Redeclaration is allowed&lt;/li&gt;
&lt;li&gt;const:- Redeclaration is not allowed
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var v = 1;
var v = 20;
console.log(v);// prints 20
let i = 1;
let i = 20;
console.log(i); //  gives an error
const c = 1;
const c = 20;
console.log(c) // gives an error 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;for var, there is no error and prints the most recent declared value&lt;/li&gt;
&lt;li&gt;for let and const there is an error as it does not allow the redeclaration of variable &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are some of the differences between let , const and var, hope you like it&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Promise in Javascript</title>
      <dc:creator>Sushil Choudhari</dc:creator>
      <pubDate>Fri, 22 Jul 2022 17:12:00 +0000</pubDate>
      <link>https://dev.to/sushilmod/promise-in-javascript-1co</link>
      <guid>https://dev.to/sushilmod/promise-in-javascript-1co</guid>
      <description>&lt;p&gt;In this blog, I will try to cover the basics of promises in javascript.&lt;br&gt;
Let's talk about the idea before we get into the promise in javascript code.&lt;/p&gt;

&lt;p&gt;A promise in javascript is just like a promise in a real life, for example, a father promises his daughter to bring chocolates while returning from the office and then the promise has to redeem either it gets completed means the father brings the chocolates or is rejected father doesn't bring the chocolates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Promise? how we can create Promise&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A promise is an object which gets produced after a response to an asynchronous call in javascript code. this lets asynchronous calls work and return as synchronous values like success or failure of promise.&lt;/p&gt;

&lt;p&gt;A Promise has a three state... &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pending state: Initial State&lt;/li&gt;
&lt;li&gt;fulfilled state: Promise completed&lt;/li&gt;
&lt;li&gt;rejected state: Promise Failed
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's create a Promise in javascript&lt;br&gt;
The promise is created by the Promise function which takes a callback function having two arguments resolve and reject.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now inside of this promise function, we need to define the actual promise so, let's take an example of a resolved promise i.e when the flag value is true the promise gets resolved and gives the message mentioned in resolve and if the value is false it gives the message mentioned in rejected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let createdPromise = new Promise((resolve ,reject)=&amp;gt;{
let flag = true ;
    if(flag){
        resolve("Promise Created")
    }else{
        reject("Promise Not Created")
    }
})
 Promise Created // for flag = true
 Promise Not Created // for flag =false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so, how to get those values and print them on the console? , we need to interact with promise therefore we use .then() anything inside in it will going to run for resolve. To get the reject value we use .catch()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let flag = true ;
    if(flag){
        resolve("Promise Created")
    }else{
        reject("Promise Not Created")
    }
}).then((message) =&amp;gt;{
    console.log(message) //  prints Promise Created  ( if flag=true )
}).catch((message)=&amp;gt;{
    console.log(message) //  prints Promise Not Created  ( if flag=false )
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is similar to the callbacks but has a more organised and cleaner way.&lt;br&gt;
Promises are very great to use when you need something that will take a long time to get some data from APIs and do something with data inside .then() or get an error inside .catch() if the promise is rejected.&lt;/p&gt;

&lt;p&gt;how Promise replaces callback?&lt;br&gt;
let's take an example of a callback and how it works as shown in the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var flag = true
function getUserName(callback,errorCallback){
    if(flag){
        callback("UserName")
    }else{
        errorCallback(" failed to get UserName ")
    }
}
getUserName(  ("message")=&amp;gt;{
    console.log("success"+message) // print success Username (if flag = true) 
},(error)=&amp;gt;{
    console.log("rejected"+message) // print rejected failed to get Username (if flag = false)
} )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's implement this into Promise&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var flag = true
function getUserName(){
    return new Promise((resovle , reject )=&amp;gt;{
    if(flag){
        resolve("UserName")
    }else{
        reject(" failed to get UserName ")
    }
})  
}
getUserName().then(  ("message")=&amp;gt;{
    console.log("success"+message) // print success Username (if flag = true) 
}).catch((error)=&amp;gt;{
    console.log("rejected"+message) // print rejected failed to get Username (if flag = false)
} )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;its a lot similar to callback but more readable and we can do Channing easily on a promise by using the .then() function &lt;/p&gt;

&lt;p&gt;That's it for Promise hope you liked it &lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is "This" keyword ? in Javascript</title>
      <dc:creator>Sushil Choudhari</dc:creator>
      <pubDate>Fri, 22 Jul 2022 15:55:21 +0000</pubDate>
      <link>https://dev.to/sushilmod/what-is-this-keyword-in-javascript-52e9</link>
      <guid>https://dev.to/sushilmod/what-is-this-keyword-in-javascript-52e9</guid>
      <description>&lt;p&gt;If you are a beginner in javascript you may encounter the "THIS" keyword.  Initially, it is confusing to understand as it behaves differently for javascript language concerning other languages like java, c# etc. &lt;br&gt;
In this blog, I will cover the basics of this keyword and how its works concerning different scenarios in javascript.&lt;/p&gt;

&lt;p&gt;Before we get into "THIS", here is some prerequisite with which you have to be familiar with it.&lt;/p&gt;

&lt;p&gt;As you know browser does not understand the deep level of javascript code so its needs to be converted into code which the browser will execute. Therefore, the browser's JS engine creates a special environment to execute the Advanced Javascript code called Execution Context.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Execution context:- The scope in which the lines of code are executed. The javascript has a stack of these execution contexts and the one on top of the stack is currently being executed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two types of Execution Context &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global Execution Context:- It is the default Execution Context which means all Javascript code which is outside the function executed in this Context so, for each javascript file there can only be one Global Execution Context &lt;/li&gt;
&lt;li&gt;Functional Execution Context:-It is a different type of Execution Context created when a function calls happen in Javascript code. There can be one or more Functional calls so, for each call different Functional Execution Contexts will get created.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we know how Code is executed, let's start with "THIS"&lt;br&gt;
This simply means how the function has been called &lt;/p&gt;

&lt;p&gt;By, the default execution context is global - which led to this referring to a global context which contains a window object means this points to a window object as you can see in the below code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function abc(){
       console.log(this)
}
abc() /* which will gives you 
      window object:-Window {window: Window, self: 
       Window, document: document,...} */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, If strict mode is enabled for any function, then the value of this will be marked as undefined as in strict mode as you can see in the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function abc(){
    'use strict';
       console.log(this)
}
abc()// which gives of "undefined" as a value of this  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;"this" with constructor function&lt;/strong&gt; &lt;br&gt;
when the function is called with a new keyword it creates a new instance every time it gets called, thus the value of this will refer to the most recent created instance as you can see in the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function capitalOfCountry(country,capital){
    this.country = country ;
    this.capital = capital ;

    console.log(`${this.capital} is a capital of ${this.country } `)
}
new capitalOfCountry("India","New Delhi")// New Delhi is a capital of India 
new capitalOfCountry("Dhaka", "Bangladesh")//Bangladesh is a capital of Dhaka 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;"this" with the call, apply and bind&lt;/strong&gt; &lt;br&gt;
To set the custom value of this we use to call, apply and bind methods which come with every javascript function.&lt;br&gt;
let's see Call and Apply first as the only difference between them are the second argument of apply is an array which contains arguments while in call arguments are passed individually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function capitalOfCountry(country,capital){
    this.country = country ;
    this.capital = capital ;
    this.dispalyCapitalOfCountry = function() {
    console.log(`${this.capital} is a capital of ${this.country } `)
    }
}

let firstCountry = new capitalOfCountry("India","New Delhi")
firstCountry.displayCapitalOfCountry()//New Delhi is a capital of India
let secondCountry = new capitalOfCountry("Bangladesh","Dhaka")
secondCountry.displayCapitalOfCountry()//Dhaka is a capital of Bangladesh 

firstCountry.dispalyCapitalOfCountry.call(secondCountry)//Dhaka is a capital of Bangladesh

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

&lt;/div&gt;



&lt;p&gt;And with the bind method, will create the new function bind with the object specified in the bind( second country ) so this refers to secondCountry&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function capitalOfCountry(country,capital){
    this.country = country ;
    this.capital = capital ;
    this.dispalyCapitalOfCountry = function() {
    console.log(`${this.capital} is a capital of ${this.country }`);
    }
}
let firstCountry = new capitalOfCountry("India","New Delhi")
firstCountry.dispalyCapitalOfCountry()//New Delhi is a capital of India
let secondCountry = new capitalOfCountry("dhaka","bangladesh")
secondCountry.dispalyCapitalOfCountry()//bangladesh is a capital of dhaka
let displaySecond = firstCountry.dispalyCapitalOfCountry.bind(secondCountry)
displaySecond()//bangladesh is a capital of dhaka
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"this" simply means how the function is called in the javascript code&lt;/li&gt;
&lt;li&gt;By default "this" refers to the window object which is present in a global context&lt;/li&gt;
&lt;li&gt;with constructor function, it always refers to the newly created object&lt;/li&gt;
&lt;li&gt;with the call, apply and Bind this will always refer to the values passed in that function.&lt;/li&gt;
&lt;/ul&gt;

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