<?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: dev-ashishsethi</title>
    <description>The latest articles on Forem by dev-ashishsethi (@devashishsethi).</description>
    <link>https://forem.com/devashishsethi</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%2F766797%2F556feb6f-0920-44cc-b033-feb7552d0b04.png</url>
      <title>Forem: dev-ashishsethi</title>
      <link>https://forem.com/devashishsethi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/devashishsethi"/>
    <language>en</language>
    <item>
      <title>Hoisting and Temporal Dead Zone (TDZ) in JavaScript?</title>
      <dc:creator>dev-ashishsethi</dc:creator>
      <pubDate>Thu, 12 May 2022 09:30:16 +0000</pubDate>
      <link>https://forem.com/devashishsethi/hoisting-and-temporal-dead-zone-tdz-in-javascript-fb3</link>
      <guid>https://forem.com/devashishsethi/hoisting-and-temporal-dead-zone-tdz-in-javascript-fb3</guid>
      <description>&lt;p&gt;Hoisting and Temporal dead zone can be difficult to understand in the first try. I will try to make you understand in one of the simplest ways possible please be with me till the end.&lt;/p&gt;

&lt;p&gt;First to understand hoisting we will have to understand how does a JS program runs.&lt;/p&gt;

&lt;p&gt;A JS program runs in 2 phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Memory allocation phase&lt;/li&gt;
&lt;li&gt;Code execution phase&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Memory Allocation Phase&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a JS program all the variables are allocated only memory not their values and all the functions are allocated with their body.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Code Execution Phase&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After memory allocation code execution is done. Code execution is done line by line&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Hoisting&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Meaning of hoisting in English dictionary: raise or haul up.&lt;/p&gt;

&lt;p&gt;Hoisting in JS is also done in same way. When a variable or function is called before it is defined, this is known as hoisting.&lt;/p&gt;

&lt;p&gt;Hoisting in case of let and const variables is different from hoisting with var variables&lt;/p&gt;

&lt;p&gt;When a var variable is hoisted it gives &lt;code&gt;undefined&lt;/code&gt; but when a let or const variable is hoisted it gives &lt;code&gt;ReferenceError&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;code Example:&lt;br&gt;
In case of var&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(bestFood); // returns undefined because bestFood’s TDZ does not exist here&lt;br&gt;
  var bestFood = "Vegetable Fried Rice"; // bestFood’s TDZ does not exist here&lt;br&gt;
  console.log(bestFood);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In case of let and const&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(bestFood); // returns ReferenceError because bestFood’s TDZ &lt;br&gt;
let bestFood = "Vegetable Fried Rice";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This happens because when memory allocation of var variable is done it allocates a placeholder "undefined" but in case of let and const no such thing happens&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Temporal Dead Zone or TDZ&lt;/u&gt;&lt;/strong&gt; occurs when a variable is not initialized and you attempt to access a variable before its complete initialization, then it gives a &lt;code&gt;ReferenceError&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;TDZ ends when you initialize that variable&lt;/p&gt;

&lt;p&gt;Functions can also be hoisted.&lt;/p&gt;

&lt;p&gt;code example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;catName("Tiger");&lt;br&gt;
function catName(name) {&lt;br&gt;
  console.log("My cat's name is " + name);&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;output: My cat's name is Tiger&lt;/p&gt;

&lt;p&gt;As you already know that when functions are hoisted then whole body of function is copied in memory allocation phase. So, In case function hoisting it does not give any error.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Nullish coalescing operator (??) &amp; Optional chaining operator (?.)</title>
      <dc:creator>dev-ashishsethi</dc:creator>
      <pubDate>Wed, 11 May 2022 18:14:11 +0000</pubDate>
      <link>https://forem.com/devashishsethi/nullish-coalescing-operator-optional-chaining-o4e</link>
      <guid>https://forem.com/devashishsethi/nullish-coalescing-operator-optional-chaining-o4e</guid>
      <description>&lt;p&gt;As a beginner when writing your JS code you may thought about how to handle null or undefined values especially if it is in a condition. Here Nullish coalescing comes into play.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lTm3Bc0E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gu3y2hdo7qxs5cq4sw5l.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lTm3Bc0E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gu3y2hdo7qxs5cq4sw5l.gif" alt="Didn't understand anything?" width="498" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Nullish coalescing operator (??)&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nullish coalescing operator (??) is a logical operator just like the OR(||) operator. If operation on left hand side returns null or undefined then right hand side operation is executed. If left hand side operation is not null or undefined then only left hand side operation is performed.&lt;/p&gt;

&lt;p&gt;Code example:&lt;br&gt;
example 1:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const testVariable= null ?? 'default string';&lt;br&gt;
console.log(testVariable);&lt;br&gt;
// output: "default string"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;example 2:&lt;br&gt;
&lt;code&gt;const testVariable2= 0 ?? 42;&lt;br&gt;
console.log(testVariable2);&lt;br&gt;
// output: 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This operator cannot be used with OR or AND operator, if you use it then you will get a syntax error.&lt;/p&gt;

&lt;p&gt;If you still want to use OR and AND operator with nullish coalescing operator then use parentheses around OR or AND operator &lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
(null || undefined) ?? "hello world!";&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Optional chaining operator(?.)&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When there are chances that the value of an object can be &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; then this operator is used.&lt;/p&gt;

&lt;p&gt;code example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let obj = { prop: "hi" };&lt;br&gt;
console.log(obj.prop?.toUpperCase());&lt;/code&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Promises in JS and its functions</title>
      <dc:creator>dev-ashishsethi</dc:creator>
      <pubDate>Wed, 11 May 2022 07:34:57 +0000</pubDate>
      <link>https://forem.com/devashishsethi/promises-in-js-and-its-functions-1fbg</link>
      <guid>https://forem.com/devashishsethi/promises-in-js-and-its-functions-1fbg</guid>
      <description>&lt;p&gt;While learning JavaScript everyone comes across this topic of "Promises", as a beginner it can be difficult to understand promises&lt;br&gt;
I will try my best to make you understand about promises, just stay with me on this journey till the end.&lt;/p&gt;

&lt;p&gt;Promises in JS are just like promises in real life, for example if some one has promised you something that they will perform some action upon your request then there is some probability that they will perform that and there is some probability that they will not perform that action.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_e7n_KRw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6h0rsg2il5jzs5tr9e9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_e7n_KRw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6h0rsg2il5jzs5tr9e9.gif" alt="Pinky Promise GIF" width="498" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Promises in JS are also similar. They have 3 states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pending&lt;/li&gt;
&lt;li&gt;Rejected&lt;/li&gt;
&lt;li&gt;Resolved a.k.a Fulfilled&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let's understand about these 3 states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Pending: It is initial state of the promise when you don't know whether an action will be done or not 🤔.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rejected: It is state after pending when it confirmed that the action is not performed or cannot be performed 🙁. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Resolved: It is state after pending when it confirmed that the action is performed 😎.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Promises are performed asynchronously but JS is a synchronous language, if didn't know about it before I would suggest you to read about this (Read about: Call stack, Web APIs, event loop, microtask queue).&lt;/p&gt;

&lt;p&gt;Promise Chaining&lt;br&gt;
Promise chaining is a way of executing two or more aysnc tasks which are related to previous  output received&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch(myRequest)&lt;br&gt;
  .then((response) =&amp;gt; {&lt;br&gt;
    if (!response.ok) {&lt;br&gt;
      throw new Error(HTTP error! Status: ${ response.status });&lt;br&gt;
    }&lt;br&gt;
    return response.blob();&lt;br&gt;
  })&lt;br&gt;
  .then((response) =&amp;gt; {&lt;br&gt;
    myImage.src = URL.createObjectURL(response);&lt;br&gt;
  })&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above example you can see there are 2 then() method being used here the last then() is working on previous then() response&lt;/p&gt;

&lt;p&gt;There are a few methods by which you can  perform some actions from any output. Some of the methods are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;then()&lt;/li&gt;
&lt;li&gt;catch()&lt;/li&gt;
&lt;li&gt;finally()&lt;/li&gt;
&lt;li&gt;all()&lt;/li&gt;
&lt;li&gt;allsettled()&lt;/li&gt;
&lt;li&gt;any()&lt;/li&gt;
&lt;li&gt;race()&lt;/li&gt;
&lt;li&gt;reject()&lt;/li&gt;
&lt;li&gt;resolve()&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;then(): This method is used in promise chaining. It takes 2 callbacks as arguments first is for resolve and second one is for reject. If immediate action on rejection is not needed then we can omit second callback and instead of second callback catch() can be used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;catch(): This method is for handling rejections occurred in a promise. It is similar to then() but the only difference is that it does not accept any callback for resolve. It only accepts callback for rejection&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;finally(): This method works when a promise is rejected or resolved. For this method it does not matter whether a promise is rejected or resolved this will run when a promise is settled( a state when a promise is rejected or resolved )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;all(): This method works on any iterable of promises(for example an array). It method resolves when all of the promises given are resolved. It rejects when any of the promises is rejected. In case of rejection it will give error related to the first promise that got rejected and will not check further.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;allsettled(): This method also works on iterable promises. this method waits for all the promises for being resolved or rejected, it checks every promise for the same. It is appropriate to use when all of the promises in the iterable is independent of each other but if there are dependent on each other all() method would be appropriate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;any(): This method also works on iterable promises. It returns a single promise that resolves as soon as any of the promises is resolved. if &lt;strong&gt;no&lt;/strong&gt; promises is resolved then AggregateError(group of errors) is thrown&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;race(): This method also works on iterable promises. It returns a promise as soon as a any of the promise is rejected or resolved whereas any() would check all the promises for rejection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;reject(): It returns a new promise that is rejected with a given reason&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;resolve(): It returns a promise that is resolved with a given value&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to use Margin in CSS?</title>
      <dc:creator>dev-ashishsethi</dc:creator>
      <pubDate>Fri, 03 Dec 2021 08:41:21 +0000</pubDate>
      <link>https://forem.com/devashishsethi/how-to-use-margin-in-css-501i</link>
      <guid>https://forem.com/devashishsethi/how-to-use-margin-in-css-501i</guid>
      <description>&lt;p&gt;According to box model in CSS margin is the outer most edge of any element, it can be set on all the four sides of the element. It can be written in shorthand or assigning different values to individual margin properties for example, margin-top:10px, margin-right:20px, margin-left:2px, margin-bottom:8px.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;u&gt;Writing margin in shorthand&lt;/u&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;For writing margin in shorthand there are some things to learn first, let me tell what those are.&lt;/p&gt;

&lt;p&gt;In shorthand you can specify only &lt;strong&gt;one value&lt;/strong&gt; or &lt;strong&gt;two values&lt;/strong&gt; or &lt;strong&gt;three values&lt;/strong&gt; or &lt;strong&gt;four values&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  One Value
&lt;/h2&gt;

&lt;p&gt;example-&amp;gt; margin: 2rem;&lt;br&gt;
 It applies same measurement(2rem here) to all the four sides.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Values
&lt;/h2&gt;

&lt;p&gt;example-&amp;gt; margin: 2rem 4rem&lt;br&gt;
 First value gets applied to top and bottom and second value gets &lt;br&gt;
 applied to left and right of element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three Values
&lt;/h2&gt;

&lt;p&gt;example-&amp;gt; margin: 2rem 4rem 1rem;&lt;br&gt;
 First value applies to top, second value applies to right and &lt;br&gt;
 left and third value applies to bottom.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four Values
&lt;/h2&gt;

&lt;p&gt;example-&amp;gt; margin: 2rem 4rem 3rem 1rem;&lt;br&gt;
 First value gets assigned to top, second values is assigned to &lt;br&gt;
 right, third values gets assigned to bottom and fourth value gets &lt;br&gt;
 assigned to left.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>css</category>
      <category>html</category>
    </item>
    <item>
      <title>What is CSS box model?</title>
      <dc:creator>dev-ashishsethi</dc:creator>
      <pubDate>Fri, 03 Dec 2021 06:10:30 +0000</pubDate>
      <link>https://forem.com/devashishsethi/what-is-css-box-model-2214</link>
      <guid>https://forem.com/devashishsethi/what-is-css-box-model-2214</guid>
      <description>&lt;p&gt;Each element in a document is represented by a rectangular box. Its has four parts:- &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Content Area&lt;/li&gt;
&lt;li&gt;Padding Area&lt;/li&gt;
&lt;li&gt;Border Area&lt;/li&gt;
&lt;li&gt;Margin Area&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ufeKUSaX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bb1ksetncaf2bslknku.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ufeKUSaX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bb1ksetncaf2bslknku.png" alt="Image description" width="194" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Content Area&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Represented by content edge(blue colored area). It is the area that is actually taken by the content for example any text or image or any video. Its has its width and height.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Padding Area&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Represented by padding edge(green colored area). It is actually the space between content's area and border. Using shorthand padding property of CSS or  padding-top, padding-right, padding-bottom, padding-left of CSS its width and height can be set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Border Area&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Represented by border edge(yellow colored area). After Padding area border area is present is holds border of the element, if any. border-width property of CSS can determine thickness of border&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Margin Area&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Represented by margin edge(dark orange colored area). The outer most area of any element is margin area, as you may know margin can be used to keep distance between two different elements. Its sizing can be done using margin property for shorthand or  margin-top, margin-right, margin-bottom, margin-left CSS properties&lt;/p&gt;

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