<?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: Neha Gupta</title>
    <description>The latest articles on DEV Community by Neha Gupta (@nayyyhaa).</description>
    <link>https://dev.to/nayyyhaa</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%2F507966%2F976a147d-8d77-44fb-8b2e-b7de273469c0.jpeg</url>
      <title>DEV Community: Neha Gupta</title>
      <link>https://dev.to/nayyyhaa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nayyyhaa"/>
    <language>en</language>
    <item>
      <title>Let's talk about CSS Box Model</title>
      <dc:creator>Neha Gupta</dc:creator>
      <pubDate>Mon, 20 Sep 2021 12:48:11 +0000</pubDate>
      <link>https://dev.to/nayyyhaa/css-box-model-ibg</link>
      <guid>https://dev.to/nayyyhaa/css-box-model-ibg</guid>
      <description>&lt;p&gt;All the HTML elements like a div, a button, or text content are considered as &lt;em&gt;boxes&lt;/em&gt; in our CSS file. And, how these boxes are aligned in our web page is answered by &lt;strong&gt;CSS Box Model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These &lt;em&gt;boxes&lt;/em&gt; are of two types:&lt;br&gt;
1) &lt;strong&gt;Inline-box&lt;/strong&gt; - They all will get placed on a line and padding or margin will not cause other elements to move away from the box.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;eg: &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;, &lt;span class="nt"&gt;&amp;lt;a&amp;gt;&lt;/span&gt;, &lt;span class="nt"&gt;&amp;lt;em&amp;gt;&lt;/span&gt; and &lt;span class="nt"&gt;&amp;lt;strong&amp;gt;&lt;/span&gt; are the inline elements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) &lt;strong&gt;Block-box&lt;/strong&gt; - This will break onto the new line and will extend in the inline direction to fill space available in its container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;eg: &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt; and &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt; are block elements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CSS Box Model
&lt;/h2&gt;

&lt;p&gt;CSS Box Model entails how our web pages are rendered. It contains the following parts:&lt;/p&gt;

&lt;p&gt;1) &lt;strong&gt;Content&lt;/strong&gt;: Here all of our content like text/images are placed.&lt;br&gt;
2) &lt;strong&gt;Padding&lt;/strong&gt;: This is a clear space area between our content and border.&lt;br&gt;
3) &lt;strong&gt;Border&lt;/strong&gt;: This forms the boundary around our content &amp;amp; padding.&lt;br&gt;
4) &lt;strong&gt;Margin&lt;/strong&gt;: This is a clear area outside the border. &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%2Fy3mpe1nlyppbu2xx4c1z.jpeg" 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%2Fy3mpe1nlyppbu2xx4c1z.jpeg" alt="Box Model"&gt;&lt;/a&gt;&lt;/p&gt;
CSS Box Model


&lt;h3&gt;
  
  
  Let's learn this with an example:
&lt;/h3&gt;

&lt;p&gt;Suppose you have two &lt;code&gt;div&lt;/code&gt; tags that contain a paragraph with &lt;code&gt;p&lt;/code&gt; tag and a &lt;code&gt;button&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In HTML:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"box1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;box 1&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;button&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"box2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;box 2&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;button&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In CSS&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;By default, some &lt;code&gt;margin&lt;/code&gt; and &lt;code&gt;padding&lt;/code&gt; are present on our webpage. Let's set it to 0 for getting more clarity around the topic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, set &lt;code&gt;background-color&lt;/code&gt; to each &lt;code&gt;div&lt;/code&gt; tag. The height given to box 1 is &lt;code&gt;100px&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.box1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;143&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;143&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;243&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.box2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;159&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;204&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our page will look something like this.&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%2Fhi5b0k9kyu2lh3dy1qu1.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%2Fhi5b0k9kyu2lh3dy1qu1.png" alt="example snip1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, in our &lt;strong&gt;CSS&lt;/strong&gt; make the following changes in &lt;code&gt;.box1&lt;/code&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.box1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;black&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;143&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;143&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;243&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've added margin of &lt;code&gt;20px&lt;/code&gt;, border of &lt;code&gt;10px&lt;/code&gt; and padding of &lt;code&gt;20px&lt;/code&gt; to our box1.&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%2Fblvloj07612m1mfdbagk.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%2Fblvloj07612m1mfdbagk.PNG" alt="example snip2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the outer orange box depicts our margin. After that, we have our border of &lt;code&gt;10px&lt;/code&gt;. The green space between our content and border is the padding. Finally, in the innermost part, we have our content.&lt;/p&gt;

&lt;p&gt;You can also check this out in our developer tools in our browser.&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%2F70s7v4n8dvn5z0c8cxbr.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%2F70s7v4n8dvn5z0c8cxbr.png" alt="dev-tool"&gt;&lt;/a&gt;&lt;/p&gt;
Content Box Model



&lt;p&gt;Notice that the height that we've mentioned is &lt;code&gt;100px&lt;/code&gt; but, in our developer's tool and the UI, it's getting computed to a lot &lt;code&gt;160px&lt;/code&gt;. To do such calculations we have two types of box-models.&lt;br&gt;
1) Content Box Model&lt;br&gt;
2) Border Box Model&lt;/p&gt;

&lt;h2&gt;
  
  
  1) Content Box Model
&lt;/h2&gt;

&lt;p&gt;That's because by default, the &lt;code&gt;box-sizing&lt;/code&gt; property is set to &lt;code&gt;content-box&lt;/code&gt;. Here, only the content of the box is taken in effect when calculating the width of the box. This adds up all the height and width to our content with the following formula:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Total element's width = &lt;code&gt;width&lt;/code&gt; + &lt;code&gt;padding-left&lt;/code&gt; + &lt;code&gt;padding-right&lt;/code&gt; + &lt;code&gt;border-left&lt;/code&gt; + &lt;code&gt;border-right&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Total element's height = &lt;code&gt;height&lt;/code&gt; + &lt;code&gt;padding-top&lt;/code&gt; + &lt;code&gt;padding-bottom&lt;/code&gt; + &lt;code&gt;border-top&lt;/code&gt; + &lt;code&gt;border-bottom&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Hence, we were getting &lt;code&gt;160px&lt;/code&gt; of total height = &lt;code&gt;100px&lt;/code&gt;+ &lt;code&gt;10px&lt;/code&gt; + &lt;code&gt;10px&lt;/code&gt; + &lt;code&gt;20px&lt;/code&gt; + &lt;code&gt;20px&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;:&lt;br&gt;
&lt;code&gt;box-sizing: content-box&lt;/code&gt; this is present by default.&lt;/p&gt;

&lt;h2&gt;
  
  
  2) Border Box Model
&lt;/h2&gt;

&lt;p&gt;Here, the width of the element as being the combined width of the border, padding, and content area of the box.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;:&lt;br&gt;
&lt;code&gt;box-sizing: border-box&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After applying this in our code, we'll observe the following results:&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%2Fgxi96jdw5q6hlxsl4fvl.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%2Fgxi96jdw5q6hlxsl4fvl.png" alt="dev-box model"&gt;&lt;/a&gt;&lt;/p&gt;
Border Box Model



&lt;p&gt;The &lt;code&gt;height&lt;/code&gt; of the content is reduced from &lt;code&gt;100px&lt;/code&gt; to &lt;code&gt;40px&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Illustrative difference between &lt;code&gt;content-box&lt;/code&gt; and &lt;code&gt;border-box&lt;/code&gt;&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%2Fngs6xxr52xykdw71cr0l.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%2Fngs6xxr52xykdw71cr0l.png" alt="content and border box"&gt;&lt;/a&gt;&lt;/p&gt;
Difference between &lt;b&gt;content-box&lt;/b&gt; and &lt;b&gt;border-box&lt;/b&gt;



&lt;h2&gt;
  
  
  Wrap Up
&lt;/h2&gt;

&lt;p&gt;Great! Now we know what CSS Box Model is and why it is so useful for aligning HTML elements in our web pages. ❤️&lt;br&gt;
We got a good grasp of the differences between the Content Box Model &amp;amp; Border Box Model.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ux</category>
      <category>uiweekly</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Everything you need to know about Execution Context in JavaScript</title>
      <dc:creator>Neha Gupta</dc:creator>
      <pubDate>Fri, 17 Sep 2021 19:38:43 +0000</pubDate>
      <link>https://dev.to/nayyyhaa/everything-you-need-to-know-about-execution-context-in-javascript-2jha</link>
      <guid>https://dev.to/nayyyhaa/everything-you-need-to-know-about-execution-context-in-javascript-2jha</guid>
      <description>&lt;p&gt;Okay! You may be writing your code in JavaScript for ages, you know what logic to use when but, have you ever wondered how variable or function created holds information about its environment?&lt;br&gt;
Well, there's something called &lt;strong&gt;Execution Context&lt;/strong&gt; which gets created by our JS Engine which does all the magic in the background. Let's demystify it in this article.&lt;/p&gt;
&lt;h1&gt;
  
  
  What is Execution Context?
&lt;/h1&gt;

&lt;p&gt;By the name we can decode it as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execution&lt;/strong&gt; = to execute out code,&lt;br&gt;
&lt;strong&gt;Context&lt;/strong&gt; = specific environment&lt;/p&gt;

&lt;p&gt;Hence, Execution Context provides information about our &lt;em&gt;environment&lt;/em&gt; where our &lt;em&gt;specific&lt;/em&gt; code is &lt;em&gt;stored and executed&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Whenever a script is executed by the JS engine, a new execution context is created. By default &lt;strong&gt;Global Execution Context&lt;/strong&gt; is created.&lt;/p&gt;
&lt;h2&gt;
  
  
  Global/Default Execution Context
&lt;/h2&gt;

&lt;p&gt;It is the first thing that is created when we write JavaScript code. Hence referred to as Default Context. Since JS is a single-threaded language, only one Global Execution Context(GEC) is created for executing the code.&lt;/p&gt;

&lt;p&gt;It has two phases:&lt;/p&gt;

&lt;p&gt;1) Creation phase&lt;br&gt;
2) Execution phase&lt;/p&gt;

&lt;p&gt;Let's dive deep into it!&lt;/p&gt;
&lt;h3&gt;
  
  
  1) Creation Phase
&lt;/h3&gt;

&lt;p&gt;In this phase, the compilation of JS code is done but doesn't involve the execution of code. Let's consider the following program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello Kitty!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;printHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we debug this program with our developer's tool of our browser, we could notice that the value of &lt;code&gt;x&lt;/code&gt; variable is coming as &lt;code&gt;undefined&lt;/code&gt; in our scripts and &lt;code&gt;"Hello Kitty!"&lt;/code&gt; has not been printed in our console. Also, there's something called &lt;code&gt;window&lt;/code&gt; present in our global scope.&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%2Fk31d8k0hzy4jebqouweg.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%2Fk31d8k0hzy4jebqouweg.png" alt="globalScope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means in Creation Phase following this happens:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;variables&lt;/strong&gt; - initialized with &lt;em&gt;undefined&lt;/em&gt; value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;functions&lt;/strong&gt; - declared and initialized but are NOT executed yet&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;window/global&lt;/strong&gt; object - gets created (holds information about function arguments, variables as well as inner functions declaration)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;this&lt;/strong&gt; - created which points to the global object created above&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To sum it up,&lt;br&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%2Faocmxql0t3eiu60ssur9.jpeg" 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%2Faocmxql0t3eiu60ssur9.jpeg" alt="Creation Phase"&gt;&lt;/a&gt;&lt;/p&gt;
Image src: https://www.instagram.com/nehacode/



&lt;h3&gt;
  
  
  2) Execution Phase
&lt;/h3&gt;

&lt;p&gt;Finally! Our code gets executed in this phase. JS engine executes the code line-by-line where all the variables are finally initialized with their value and functions get invoked. For each function invocation, &lt;strong&gt;Functional Execution Context&lt;/strong&gt; gets created. Let's learn about this.&lt;/p&gt;

&lt;h4&gt;
  
  
  Functional/Local Execution Context
&lt;/h4&gt;

&lt;p&gt;Whenever a new function gets called, a new execution context is created with basic two phases: The creation phase and the execution phase. This new execution context is known as &lt;strong&gt;Local/Functional Execution Context (FEC)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Hence in the above code, while parsing the function invocation we could notice our function being present in the local scope.&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%2Fuu4uk62h9onqefgcrbt2.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%2Fuu4uk62h9onqefgcrbt2.png" alt="local scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This FEC is similar to GEC, the difference is that it creates the &lt;code&gt;arguments object&lt;/code&gt; instead of creating the &lt;code&gt;global object&lt;/code&gt; where &lt;code&gt;this&lt;/code&gt; points to the current object.&lt;/p&gt;

&lt;p&gt;Hence, In Execution Phase:&lt;br&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%2Fdsz44n1syexithh1u6o4.jpeg" 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%2Fdsz44n1syexithh1u6o4.jpeg" alt="Execution phase"&gt;&lt;/a&gt;&lt;/p&gt;
Image src: https://www.instagram.com/nehacode/



&lt;h3&gt;
  
  
  Visual Representation
&lt;/h3&gt;

&lt;p&gt;Since, for each function invocation =&amp;gt; new execution context is created. Let's visualize this whole concept:&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%2Fg8k0llsgxo40s3ysn2vf.jpeg" 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%2Fg8k0llsgxo40s3ysn2vf.jpeg" alt="Execution Contexts"&gt;&lt;/a&gt;&lt;/p&gt;
Image src: https://www.instagram.com/nehacode/


 

&lt;p&gt;and, this feels trippy! 😵‍💫&lt;/p&gt;

&lt;p&gt;For ease of storing Global Execution Context and all Local Execution Context, we have a data structure called &lt;strong&gt;Call Stack&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whenever a new execution context is created, it gets stacked above the previous execution context, and so on. The JS engine takes care of keeping track of this stack's execution so that one execution context gets executed at a time to maintain the Single-threaded nature of JS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;Great! Now we know what execution context it and why is it so useful for our beloved JS engine ❤️ We got a good grasp of the differences between Global Execution Context &amp;amp; Local Execution Context, along with the phases required in their creation.&lt;/p&gt;

&lt;p&gt;👏&lt;em&gt;Thanks for reading!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>computerscience</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Build your own Shakespeare Translation Web App with JavaScript Fetch API</title>
      <dc:creator>Neha Gupta</dc:creator>
      <pubDate>Sat, 10 Apr 2021 20:50:54 +0000</pubDate>
      <link>https://dev.to/nayyyhaa/build-your-own-shakespeare-translation-web-app-with-javascript-fetch-api-loo</link>
      <guid>https://dev.to/nayyyhaa/build-your-own-shakespeare-translation-web-app-with-javascript-fetch-api-loo</guid>
      <description>&lt;p&gt;Shakespeare may have been a genius, but one thing's for sure: he wasn't speaking our language. His ever-popular works (dramas and poems) make his unique language style live even today.&lt;br&gt;
I've always been curious about how Shakespeare would've expressed my thoughts in his words. &lt;em&gt;Have you been too??&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Then you've come to the right place, my friend!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a vanillaJS project which uses API from &lt;a href="https://funtranslations.com/" rel="noopener noreferrer"&gt;https://funtranslations.com/&lt;/a&gt; to &lt;strong&gt;translate English text into Shakespeare English.&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Prerequisites&lt;/th&gt;
&lt;th&gt;Basic understanding of HTML and CSS, an understanding of what JavaScript is.&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is what we'll build:&lt;br&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%2F85qojwxr74iiajwgrumb.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%2F85qojwxr74iiajwgrumb.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Try out the app &lt;a href="https://iamshakespeare.netlify.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Source Code
&lt;/h2&gt;

&lt;p&gt;In case you get lost while following along, you can grab the &lt;a href="https://github.com/nayyyhaa/shakespeare-translator" rel="noopener noreferrer"&gt;source code from here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's begin!&lt;/p&gt;
&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;To get started, we'll be using VSCode for writing our code. Create your first file with the name &lt;strong&gt;index.html&lt;/strong&gt; for writing out HTML code.&lt;br&gt;
In our Application, we have 3 basic elements:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Input Field - to accept the user's input&lt;/li&gt;
&lt;li&gt;Translate Button - to create an event when the user clicks on the translate button.&lt;/li&gt;
&lt;li&gt;Output Field - to preview the translated text.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These 3 elements can be created as follows in HTML:&lt;/p&gt;
&lt;h6&gt;
  
  
  HTML code snippet - index.html
&lt;/h6&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"textarea"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"inputTxt"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"insert your text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"translateButton"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Translate!&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"outputTxt"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'/scripts/app.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note: &amp;lt; script &amp;gt; tag is being used to bind this HTML file with the JavaScript file &lt;strong&gt;app.js&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Initialising variables to store our data
&lt;/h2&gt;

&lt;p&gt;This section of the code sets up the variables we need to store the data our program will use.&lt;br&gt;
In your &lt;strong&gt;app.js&lt;/strong&gt; file, create the following variables:&lt;/p&gt;
&lt;h6&gt;
  
  
  JS code snippet - app.js
&lt;/h6&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;inputElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#inputTxt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// input element&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;translateBtnElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#translateButton&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// button element&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;outputElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#outputTxt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output element&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://shakespeare.p.mashape.com/shakespeare.json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//API URL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The first three variables &lt;code&gt;inputElement&lt;/code&gt;, &lt;code&gt;translateBtnElement&lt;/code&gt;, &lt;code&gt;outputElement&lt;/code&gt; are each made to store a reference to the form text input, translate button and output element in our HTML.&lt;br&gt;
Our final variable &lt;code&gt;url&lt;/code&gt; is used to store the server's API call URL from where we obtain the translated data.&lt;/p&gt;

&lt;p&gt;Here, we've used &lt;code&gt;.querySelector()&lt;/code&gt; function for selecting the particular &lt;strong&gt;id&lt;/strong&gt; that we've already set in our index.html file.&lt;/p&gt;

&lt;p&gt;To listen to the button click event we need to define an event handler function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;translateBtnElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;translateFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;   &lt;code&gt;click&lt;/code&gt; - is the event&lt;/li&gt;
&lt;li&gt;   &lt;code&gt;translateBtnElement&lt;/code&gt; - is the event listener&lt;/li&gt;
&lt;li&gt;   &lt;code&gt;translateFunction&lt;/code&gt; - is the event handler/callback function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After &lt;code&gt;click&lt;/code&gt; event has been fired on &lt;code&gt;translateBtnElement&lt;/code&gt;, the  &lt;code&gt;addEventListener()&lt;/code&gt; method handles by calling &lt;code&gt;translateFunction()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before defining the &lt;code&gt;translateFunction()&lt;/code&gt; we need to get some basic knowledge about APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an API?
&lt;/h3&gt;

&lt;p&gt;API stands for &lt;strong&gt;Application Programming Interface&lt;/strong&gt;, is a set of functions that allows applications to access data and interact with external software components, operating systems, or microservices.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;WOAH! What?!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;OK! Let me explain this to you in easy words. Suppose you are in a restaurant and you are dying to have that chocolate cake. You don't go straight to the chef for placing the order, right? The waiter does that for you. That's what API is. &lt;strong&gt;It's an interface that communicates between applications.&lt;/strong&gt;&lt;br&gt;
Here,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;   You/Customer: Client&lt;/li&gt;
&lt;li&gt;   Waiter: API&lt;/li&gt;
&lt;li&gt;   Chef: Server
Hence, in order to get the data from the web servers, we need APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our example, we are using &lt;a href="https://funtranslations.com/shakespeare" rel="noopener noreferrer"&gt;FunTranslationAPI&lt;/a&gt; to fetch the data in JSON format(key - value pair).&lt;/p&gt;

&lt;p&gt;Let's call the API then!&lt;/p&gt;
&lt;h2&gt;
  
  
  Fetch API
&lt;/h2&gt;

&lt;p&gt;The Fetch API is a modern interface that allows you to make HTTP requests to servers from web browsers to given URL.&lt;/p&gt;

&lt;p&gt;Basic syntax involved:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// handle the response&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// handle the error&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here in the &lt;code&gt;fetch()&lt;/code&gt; function we pass the URL of the resource from where we are requesting the data. This will pass the data as a &lt;code&gt;response&lt;/code&gt; object. The &lt;code&gt;response&lt;/code&gt; object is the API wrapper for the fetched resource with a number of useful properties and methods to inspect the response. This will then passed to the &lt;code&gt;data&lt;/code&gt; variable (you can give any name to this) for printing output.&lt;/p&gt;

&lt;p&gt;Now, it's time to define the functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining Functions() &lt;em&gt;for some action&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;To get our code into some action, we need to define some functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;translateFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;inputValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;inputElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;//fetching input value &lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                             &lt;span class="c1"&gt;//Fetch API call&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;outputElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shakespeare(Server) is busy! Try after sometime&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's break it down:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We'll extract &lt;code&gt;inputElement&lt;/code&gt; value into &lt;code&gt;inputValue&lt;/code&gt; variable. &lt;/li&gt;
&lt;li&gt;Making &lt;code&gt;fetch&lt;/code&gt; API call using the given &lt;code&gt;url&lt;/code&gt; and then extracting &lt;code&gt;response&lt;/code&gt; object. This is just an HTTP response, not the actual JSON. To extract the JSON body content from the response, we use the &lt;code&gt;json()&lt;/code&gt; method via using an &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions" rel="noopener noreferrer"&gt;arrow&lt;/a&gt; function.&lt;/li&gt;
&lt;li&gt;Setting &lt;code&gt;data&lt;/code&gt; variable's value to the &lt;code&gt;outputElement&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;Finally, error handling with &lt;code&gt;catch()&lt;/code&gt; function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's try our application. Go to the browser, input your text &amp;amp; click on the translate button. You'll get the following output. &lt;/p&gt;

&lt;h6&gt;
  
  
  In console
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Bad Request: text is missing."&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's not the output that we were expecting. That's because we've to pass the text to our URL. For that we'll define another function &lt;code&gt;translatedURL()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;translatedURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputValue&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ?text= &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;inputValue&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try our app with sample text &lt;em&gt;Hi. How are you?&lt;/em&gt; and calling in fetch() function as &lt;code&gt;fetch(translatedURL(inputValue))&lt;/code&gt; instead of previous &lt;code&gt;fetch(url)&lt;/code&gt; to concatenate the text message to our server API's URL. We'll get output like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"success"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"total"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"contents"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"translated"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Good morrow. How art thee?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
        &lt;/span&gt;&lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Hi. How are you?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;     
        &lt;/span&gt;&lt;span class="nl"&gt;"translation"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"shakespeare"&lt;/span&gt;&lt;span class="w"&gt;         
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Success! Not so much. Notice that the output text doesn't look pretty. This output is JSON data and we need to extract the translated value from it.&lt;/p&gt;

&lt;p&gt;Here,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;   translated: translated text&lt;/li&gt;
&lt;li&gt;   text: input text&lt;/li&gt;
&lt;li&gt;   translation: language of translation being used from FunTranslation API
We refer it by &lt;code&gt;json.contents.translated&lt;/code&gt;. Now our code should look something like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;translatedURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputValue&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;?text=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;inputValue&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;translateFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;inputValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;inputElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;finalURL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;translatedURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;finalURL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;outputElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;translated&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shakespeare(Server) is busy! Try after sometime&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and we get the following output:&lt;br&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%2Fus63l9tbhxnt004el08i.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%2Fus63l9tbhxnt004el08i.PNG" alt="output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Voila!&lt;/strong&gt; We've built our very own Shakespeare Translation Web App with JavaScript Fetch API.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Funtranslation APIs are free to use, hence they have a limitation of 5 calls/hour. Once it exceeds this limit, it would result in a failure with an error we've mentioned in the &lt;code&gt;catch()&lt;/code&gt; block.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Finished for now...
&lt;/h2&gt;

&lt;p&gt;Congrats on making it this far! We've got the basic understanding of DOM scripting i.e. JS in the browser, calling servers, and getting data from there, taking user input and showing user output, and many more things.&lt;/p&gt;

&lt;p&gt;Now all that's left for you is to design your own styling with CSS. You can also check out funtranslation &lt;a href="https://funtranslations.com" rel="noopener noreferrer"&gt;site&lt;/a&gt; for a similar app with different translation languages.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://iamshakespeare.netlify.app/" rel="noopener noreferrer"&gt;Click here&lt;/a&gt; to check out the live project.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Give it a try, create your version of the same and share your experience and feedback on the comments section.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;Thanks for reading!&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
