<?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: Tahir Ahmed Tapadhar</title>
    <description>The latest articles on Forem by Tahir Ahmed Tapadhar (@ahmedtahir).</description>
    <link>https://forem.com/ahmedtahir</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%2F435681%2Fab4e9abb-0927-46cb-b937-67c47826c55c.png</url>
      <title>Forem: Tahir Ahmed Tapadhar</title>
      <link>https://forem.com/ahmedtahir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ahmedtahir"/>
    <language>en</language>
    <item>
      <title>Earn a Microsoft Azure Certification for FREE !!!</title>
      <dc:creator>Tahir Ahmed Tapadhar</dc:creator>
      <pubDate>Wed, 01 Jun 2022 18:30:01 +0000</pubDate>
      <link>https://forem.com/ahmedtahir/earn-a-microsoft-azure-certification-for-free--79n</link>
      <guid>https://forem.com/ahmedtahir/earn-a-microsoft-azure-certification-for-free--79n</guid>
      <description>&lt;h2&gt;
  
  
  &lt;em&gt;Overview:&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;With the introduction of new technologies on a daily basis, cloud computing is one of the modern technologies that is growing fast. Cloud computing is one of the bigger industries that generate billions of dollars in revenue every year with hundreds and thousands of job opportunities. Many big names are offering unique cloud services to many customers all over the world, such as &lt;strong&gt;"Amazon Web Services (AWS), Google Cloud Platform (GCP), Oracle Cloud, IBM Cloud, Rackspace, and Microsoft Azure"&lt;/strong&gt; — with 29% application workload of the overall cloud market.&lt;/p&gt;

&lt;p&gt;Microsoft Azure, often referred to as Azure is a cloud computing service operated by Microsoft for application management via Microsoft-managed data centers. Azure is currently one of the leading public cloud service providers out there in the market.&lt;/p&gt;

&lt;p&gt;It provides a range of cloud services, including compute, analytics, storage and networking. Users can pick and choose from these services to develop and scale new applications, or run existing applications in the public cloud.&lt;/p&gt;

&lt;p&gt;To pursue a career in cloud computing, all major cloud services providers offer career paths and certifications, but Microsoft Azure is a single cloud service provider that offers role-based certifications that are specifically designed according to the market requirements and the job roles. Due to the large adoption of Microsoft Azure cloud services, Microsoft Azure certified professionals are in high demand for developing cloud applications and managing cloud infrastructures. Therefore from a developer perspective, gaining skills in cloud computing &amp;amp; specifically Azure is a win-win anytime.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Path:&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Certifications normally come with a price. But still, Microsoft has provided ways &amp;amp; means for developers to acquire the certification completely free of cost. There are various paths using which one can easily bag a free certification for Azure. Today I would be talking about one of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Microsoft Events:&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Microsoft has various global events &lt;a href="https://www.microsoft.com/en-us/events" rel="noopener noreferrer"&gt;Microsoft events&lt;/a&gt; of which there are &lt;strong&gt;2 events&lt;/strong&gt; that are major &amp;amp; takes place annually:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Microsoft Build&lt;/li&gt;
&lt;li&gt;Microsoft Ignite&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Like every year, &lt;strong&gt;"Microsoft Build 2022"&lt;/strong&gt; comes again which such an opportunity where if you register yourself for the event (24-25-26 May) &amp;amp; signup for the &lt;strong&gt;"Microsoft Build Cloud Skills Challenge 2022"&lt;/strong&gt;, you can earn a free certification for yourself.&lt;/p&gt;

&lt;p&gt;Registration Link: &lt;br&gt;
&lt;a href="https://www.microsoft.com/en-us/cloudskillschallenge/build/registration/2022" rel="noopener noreferrer"&gt;Microsoft Build Cloud Skills Challenge | May 2022&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hurry up !!! grab the opportunity before the clock ticks out&lt;/strong&gt; ⏱🔥&lt;/p&gt;

</description>
      <category>azure</category>
      <category>cloud</category>
      <category>cloudskills</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Microsoft Launches Q&amp;A for .NET</title>
      <dc:creator>Tahir Ahmed Tapadhar</dc:creator>
      <pubDate>Mon, 17 May 2021 13:12:14 +0000</pubDate>
      <link>https://forem.com/ahmedtahir/microsoft-launches-q-a-for-net-p6o</link>
      <guid>https://forem.com/ahmedtahir/microsoft-launches-q-a-for-net-p6o</guid>
      <description>&lt;p&gt;Microsoft, the American multinational trillion-dollar technology company never fails to astound us. The tech giants have launched their own Q&amp;amp;A forum for their .NET community where developers can unreluctantly ask all things .NET. You can also think of it as something like &lt;strong&gt;Stack Overflow&lt;/strong&gt;, but only for .NET-related technologies. For those who are thinking what is .NET, simply put, its a software framework put together, developed &amp;amp; maintained by Microsoft. &lt;/p&gt;

&lt;h4&gt;
  
  
  Overview:
&lt;/h4&gt;

&lt;p&gt;.NET is a free, cross-platform, open source developer platform for building many different types of applications. With .NET, you can use multiple languages, editors, and libraries to build for web, mobile, desktop, games, and IoT.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fx7d8el5uhxxtkbw6i9oe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fx7d8el5uhxxtkbw6i9oe.png" alt=".NET-Banner" width="800" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;.NET has come a long way from it's original implementation i.e. &lt;strong&gt;.NET framework&lt;/strong&gt; which supported running websites, services, desktop apps, and more &lt;strong&gt;only on Windows&lt;/strong&gt; operating system to &lt;strong&gt;.NET Core&lt;/strong&gt;, a cross-platform implementation for running websites, services, and console apps on &lt;strong&gt;Windows, Linux, and macOS&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Introducing .NET 5:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;.NET 5&lt;/strong&gt;, as Microsoft says, is the future of .NET. During the Microsoft Build 2019, the Microsoft Team announced that .NET 5 is on its way out, and it would be arriving in 2020. The announcement added that there would be .NET 5 that is Opensource and Cross-platform, which will replace &lt;strong&gt;.NET Framework, .NET Core and Xamarin&lt;/strong&gt; with a single unified platform called .NET 5 Framework.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmxusdo3vjer0kjaiecpt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmxusdo3vjer0kjaiecpt.png" alt=".NET5" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown above, the main goal of .NET 5 is to empower unified .NET Client Application projects to create deliverables for various platforms including Windows, UNIX, Linux, Legacy Windows, iOS, Android, HTML5, and Macintosh. &lt;strong&gt;.NET 5&lt;/strong&gt; will be powered with the best features of the .NET Framework, .NET Core, Mono, and Xamarin to provide APIs, libraries, and run-time to create apps for Web, Windows, Mobile &amp;amp; IoT devices.&lt;/p&gt;

&lt;h4&gt;
  
  
  .NET Q&amp;amp;A:
&lt;/h4&gt;

&lt;p&gt;Microsoft has always put its community first &amp;amp; have constantly upgraded their products based on the community feedback. Since we now know that &lt;strong&gt;.NET&lt;/strong&gt; has come here to stay &amp;amp; is not going to go any where for a long long time, Microsoft has come up with a platform where the community can interact with one another and get their questions answered. You can visit the platform here:&lt;br&gt;
&lt;a href="https://docs.microsoft.com/en-us/answers/products/dotnet" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/answers/products/dotnet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0h619ltkrpnaf91my8ay.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0h619ltkrpnaf91my8ay.PNG" alt=".NET.Q&amp;amp;A.1" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9vfvw1hcdbqcc0xnbkr0.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9vfvw1hcdbqcc0xnbkr0.PNG" alt=".NET.Q&amp;amp;A.2" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Microsoft Q&amp;amp;A is the home for technical questions and answers about products from Microsoft. If you are looking for help on any Microsoft product you are sure to find it on Microsoft Q&amp;amp;A. Join the community of growing experts who are here to help developers get the help and answers they need on their projects. When you head to Microsoft Q&amp;amp;A for .NET you will find a wide range of .NET topics including runtime, app &amp;amp; web development, languages, data, and more. Browse questions from the community, ask your own, and help out others!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Have you ever been working on some code and ran into an issue and needed to ask someone for help?"&lt;/strong&gt; said Microsoft's &lt;strong&gt;James Montemagno&lt;/strong&gt; in a Nov. 25 blog post &lt;a href="https://devblogs.microsoft.com/dotnet/announcing-microsoft-q-and-a-for-dotnet/" rel="noopener noreferrer"&gt;Announcing Microsoft Q&amp;amp;A for .NET&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;"Maybe you are looking for assistance on how to start your next app or perhaps you are seeking some architecture guidance? What if there were one place where you could go to get your technical questions answered by experts from Microsoft and the community? Well there is now with the launch of Microsoft Q&amp;amp;A for .NET!"&lt;/p&gt;

&lt;p&gt;The .NET community can breathe a sigh of relief now that we have our own dedicated Stack Overflow💪😅😅...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://visualstudiomagazine.com/articles/2020/12/02/net-q-a.aspx" rel="noopener noreferrer"&gt;https://visualstudiomagazine.com/articles/2020/12/02/net-q-a.aspx&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://devblogs.microsoft.com/dotnet/announcing-microsoft-q-and-a-for-dotnet/" rel="noopener noreferrer"&gt;https://devblogs.microsoft.com/dotnet/announcing-microsoft-q-and-a-for-dotnet/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>webdev</category>
      <category>news</category>
    </item>
    <item>
      <title>Arrow Functions in JavaScript, How is it different?</title>
      <dc:creator>Tahir Ahmed Tapadhar</dc:creator>
      <pubDate>Sun, 18 Oct 2020 19:25:57 +0000</pubDate>
      <link>https://forem.com/ahmedtahir/arrow-functions-in-javascript-how-is-it-different-476h</link>
      <guid>https://forem.com/ahmedtahir/arrow-functions-in-javascript-how-is-it-different-476h</guid>
      <description>&lt;p&gt;JavaScript has seen a lot of changes in the past few years making it robust, elegant &amp;amp; the primary choice language of the web. Ever since ES6 was released, JavaScript has become a different ballgame altogether with various drastic changes adding to it's elegance. With each successive ES release, it brought a lot of cool stuff to the table. One such major breakthrough came with the release of ES6 which introduced features like the &lt;code&gt;let&lt;/code&gt; &amp;amp; &lt;code&gt;const&lt;/code&gt; keywords, &lt;code&gt;template literals&lt;/code&gt;, the &lt;code&gt;class&lt;/code&gt; keyword, &lt;code&gt;modules&lt;/code&gt;, &lt;code&gt;destructuring&lt;/code&gt; and a lot more. Among these hallmark was the &lt;strong&gt;'Arrow Functions'&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Ever since it's inception, it has been widely acclaimed &amp;amp; adopted by the JS developers/community. It is been widely used in libraries &amp;amp; big frameworks like Angular, React, Vue, etc. It has completely changed the way we look &amp;amp; write functions in JS making it short and concise. Before diving in, let's talk briefly about traditional ways of writing functions in JavaScript.&lt;/p&gt;

&lt;h4&gt;
  
  
  Functions in JS:
&lt;/h4&gt;

&lt;p&gt;There are a few different ways to define a function in JavaScript.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function declaration:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A function declaration defines a named function. To create a function declaration you use the &lt;code&gt;function&lt;/code&gt; keyword followed by the name of the 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="c1"&gt;// function declaration&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Function expression:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A function expression is very similar to and has almost the same syntax as a function declaration. The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions. In simple words, a function expression can be defined as an anonymous function assigned to a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// function expression&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Function() Constructor:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;function&lt;/code&gt; statement is not the only way to define a new function, you can define your function dynamically using &lt;strong&gt;Function()&lt;/strong&gt; constructor along with the &lt;strong&gt;new&lt;/strong&gt; operator. This is how we can use the Function() constructor to create 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;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;return a+b;&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However creating functions using this method is not recommended. Calling the constructor directly can create functions dynamically but suffers from security and similar performance issues.&lt;/p&gt;

&lt;p&gt;Apart from the above mentioned methods, there is a new way to define functions in JavaScript post the release of ES6 and that is the &lt;strong&gt;Arrow Function&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Arrow Functions:
&lt;/h4&gt;

&lt;p&gt;The Arrow function is the latest way to define a function in JS. It is a shorter syntax for writing function expressions. An arrow function is a cleaner, concise &amp;amp; a compact alternative to a traditional function expression, but is limited and can't be used in all situations. Here is how we write the above function using the Arrow function syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// arrow function&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;In an arrow function , we don't require a &lt;code&gt;function&lt;/code&gt; keyword. Instead we use a fat arrow &lt;strong&gt;(=&amp;gt;)&lt;/strong&gt; between the parameters and the body of the function. The above arrow function can also be compacted further. If the body of our function includes only a single line, we can write the function like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;Also, if there is a single line of code in the function body &amp;amp; returns a value, we can eliminate both the &lt;code&gt;return&lt;/code&gt; keyword and the &lt;code&gt;{}&lt;/code&gt; braces completely like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are the 2 methods: The latter arrow function that we have is exactly equivalent to the former function expression shown above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// function expression&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// arrow function&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see, this syntax is cleaner &amp;amp; more compact and makes you write less lines of code.😃 &lt;/p&gt;

&lt;h4&gt;
  
  
  Note:
&lt;/h4&gt;

&lt;p&gt;If the arrow function has a single parameter, we can skip writing the parenthesis &lt;code&gt;()&lt;/code&gt; i.e. we need not require the parenthesis &lt;code&gt;()&lt;/code&gt; for enclosing the single parameter. But if the function has more than one parameter, parenthesis is required.&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;var&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tahir&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'Hi Tahir'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How are Arrow functions different?
&lt;/h4&gt;

&lt;p&gt;The obvious question you must be asking yourself is what was the need for introducing arrow functions and why should I use it. One good reason would be, that it would make your code look more readable &amp;amp; smaller by eliminating good amount of lines. &lt;br&gt;
Further more, I will replace all my existing functions that I have defined using function declaration/expression with Arrow functions and my code will work the same as before. Big deal? The answer to that is &lt;strong&gt;NO!!&lt;/strong&gt;. Don't do that.  &lt;/p&gt;

&lt;p&gt;Even though at first it might look like there is just a syntax difference between them, but that is not true. There are quite subtle differences between the Arrow functions &amp;amp; the traditional methods. Let's discuss what are they. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;'args'&lt;/strong&gt; object:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Arrow functions don't have access to &lt;code&gt;arguments&lt;/code&gt; object. Let's understand this with an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// function expression&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&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="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 1&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;arguments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;

&lt;span class="c1"&gt;// arrow function&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// arguments is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Arrow functions don't have their own &lt;strong&gt;'this'&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The behavior of this inside of an arrow function differs considerably from the regular function’s this behavior. It does not have its own bindings to &lt;code&gt;this&lt;/code&gt; and should not be used as &lt;code&gt;methods&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;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;print&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Traditional functions&lt;/strong&gt; default &lt;code&gt;this&lt;/code&gt; to the window scope. &lt;strong&gt;Arrow functions&lt;/strong&gt; do not default &lt;code&gt;this&lt;/code&gt; to the window scope, rather they execute in the scope they are created.&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;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="s1"&gt;this&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// {Window Object}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Upon invoking &lt;strong&gt;person.eat()&lt;/strong&gt;, this prints a &lt;code&gt;window&lt;/code&gt; object. The reason this happened because here, the callback function inside &lt;strong&gt;setTimeout()&lt;/strong&gt; is not part of any object. It's a standalone function. When we call a function as a standalone function outside of an object, by default &lt;code&gt;this&lt;/code&gt; points to window object.&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;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="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="s1"&gt;this&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// {person Object}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be resolved using an Arrow function because they don't rebind &lt;code&gt;this&lt;/code&gt;. In other words, if we change the callback to an arrow function, it will inherit the &lt;code&gt;this&lt;/code&gt; keyword. In the callback function, because we have used the &lt;strong&gt;arrow function syntax&lt;/strong&gt;, the value of &lt;code&gt;this&lt;/code&gt; is not reset. Instead it inherits the value of &lt;code&gt;this&lt;/code&gt; in the context in which this code is defined.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrow functions cannot be used as a &lt;strong&gt;constructor&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are aware of constructor functions in JavaScript, then you should know that a regular function can easily construct objects. For example, the &lt;code&gt;Person()&lt;/code&gt; function creates instance of a person class&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;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tahir&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;person1&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The arrow function resolves &lt;code&gt;this&lt;/code&gt; lexically. In another words, the arrow function doesn’t define its own &lt;strong&gt;execution context&lt;/strong&gt;. A consequence of &lt;code&gt;this&lt;/code&gt; resolved lexically is that an arrow function &lt;strong&gt;cannot&lt;/strong&gt; be used as a constructor. If you try to invoke an arrow function prefixed with &lt;code&gt;new&lt;/code&gt; keyword, JavaScript throws an error:&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;var&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tahir&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// TypeError: Person is not a constructor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all there is to &lt;strong&gt;Arrow Functions&lt;/strong&gt;. In a nutshell, we simply cannot interchangeably use &lt;code&gt;Arrow functions&lt;/code&gt; over &lt;code&gt;Function expressions&lt;/code&gt; or vice versa. The right way to go would fully depend upon the requirement of how you want to build things as per need. Hope this was helpful. Happy Learning🙂🙂..&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is Type coercion in JavaScript ?</title>
      <dc:creator>Tahir Ahmed Tapadhar</dc:creator>
      <pubDate>Sat, 22 Aug 2020 20:42:39 +0000</pubDate>
      <link>https://forem.com/ahmedtahir/what-is-type-coercion-in-javascript-4l96</link>
      <guid>https://forem.com/ahmedtahir/what-is-type-coercion-in-javascript-4l96</guid>
      <description>&lt;p&gt;It will not be incorrect to say that javascript falsely looks like other programming languages. Unlike languages like C++/Java/C#, javascript is nothing like those languages. It's a different concept, a different mindset, a different paradigm. While many programmers learning javascript or using javascript find some of its elements as they dig into more advanced features odd or difficult to learn or weird. You're going to find that when you truly understand it, how javascript works &amp;amp; how it works the way it does you'll discover that javascript is a deceptively powerful &amp;amp; beautiful programming language. This is also the reason why it is one of the most popular programming language.&lt;/p&gt;

&lt;p&gt;We need to understand how javascript is functioning under the hood in order to be able to properly write javascript code; deep advanced javascript code. We're going beyond just the basics. So even though it may seem at moments during this post that we're covering that "Oh, you don't really need to know that!", you'll find as we progress that, fundamental foundational knowledge is going to brighten the path for you, because very quickly you'll see just how important they really are.&lt;/p&gt;

&lt;p&gt;What lures devs into learning javascript is understanding it's &lt;strong&gt;weird parts&lt;/strong&gt;. One such concept that we shall talk about today is the &lt;strong&gt;Type Coercion&lt;/strong&gt; in JavaScript. During your time of development using the javascript language, I'm sure you must have encountered something like the below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="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="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// '55'&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="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 0&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="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 4&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="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&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="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 0&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="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// '22' 😳huh??..&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="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// '2'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Type Coercion:
&lt;/h4&gt;

&lt;p&gt;Type coercion is the automatic or implicit conversion of values from one data type to another data type(eg. string to number &amp;amp; vice-versa). &lt;code&gt;Type conversion&lt;/code&gt; is similar to &lt;strong&gt;Type Coercion&lt;/strong&gt; because they both convert values from one data type to another with one key difference — type coercion is implicit whereas type conversion can be either implicit or explicit.&lt;br&gt;
In simple terms, we as a developer don't explicitly perform &lt;code&gt;type coercion&lt;/code&gt;. It is done internally by the javascript engine.&lt;/p&gt;
&lt;h4&gt;
  
  
  What causes type coercion?
&lt;/h4&gt;

&lt;p&gt;First of, if I'm not wrong, you cannot perform mathematical operations on two operands of different type in any(compiled) language. In a compiled language, the compiler itself will throw an error during compilation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In any compiled language like C#, this is not possible&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;operand1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// int&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;operand2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'5'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// char&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;operand1&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;operand2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But HEY!! this is JavaScript....it is dynamic, everything is possible here😛. So in javascript, when we perform some mathematical operations on two variables of the same type, the result would be similar to that of any other language. But when the operands are of &lt;strong&gt;different types&lt;/strong&gt;, javascript &lt;strong&gt;internally&lt;/strong&gt; tries to make things even by making both the operands of the same type. This is done by converting either one of the operand to match the type of the second operand.&lt;br&gt;
Considering the above example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// number 5 is coerced to string '5'&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="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//'55'&lt;/span&gt;

&lt;span class="c1"&gt;// string 5 is coerced to number 5&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="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, javascript has coerced the value 5 from &lt;code&gt;number&lt;/code&gt; to &lt;code&gt;string&lt;/code&gt; making both the operands &lt;strong&gt;string&lt;/strong&gt; type and then concatenates the values together resulting into &lt;code&gt;'5' + '5' =&amp;gt; '55'&lt;/code&gt;. Similarly,  in the second example, javascript coerces one of the operand to match the type of the other. But this time string &lt;code&gt;'5'&lt;/code&gt; is coerced to number &lt;code&gt;5&lt;/code&gt; evaluating to &lt;code&gt;5 - 5 =&amp;gt; 0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since JavaScript is a &lt;strong&gt;weakly-typed language&lt;/strong&gt;, values are converted between different types automatically or rather implicitly. It usually happens when you apply operators to values of different types. JavaScript types can be broadly categorized into 2 categories&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Primitive Types :
(number, string, boolean, null, undefined, symbol)&lt;/li&gt;
&lt;li&gt;Compound/Structural Types :
(Object, Array, Map, Set, Date, etc..) basically almost everything made with &lt;code&gt;new&lt;/code&gt; keyword&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Types of conversion
&lt;/h4&gt;

&lt;p&gt;Irrespective of any type, be it primitive/structural, when type coercion is performed, it converts the operand type into one of these types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To String&lt;/li&gt;
&lt;li&gt;To Number&lt;/li&gt;
&lt;li&gt;To Boolean&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Conversion logic for primitives and objects works differently, but both primitives and objects can only be converted in these three ways.&lt;/p&gt;

&lt;h4&gt;
  
  
  Primitive to String coercion:
&lt;/h4&gt;

&lt;p&gt;When adding two operands with the &lt;code&gt;binary(+)&lt;/code&gt; operator, the JavaScript engine will try to add the values if both the values are of &lt;code&gt;number&lt;/code&gt; type.&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;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="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5&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="mi"&gt;20&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But when either of the operand is a &lt;code&gt;string&lt;/code&gt; type, javascript performs coercion on that operand &amp;amp; converts both of them to string. Hence the result is now a concatenation of 2 strings. The order of the operands doesn't matter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// number,string&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="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// '5hi'&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="s1"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// '55'&lt;/span&gt;

&lt;span class="c1"&gt;// boolean, string&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="s1"&gt;hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'hifalse'&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="s1"&gt;12&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// '12true';&lt;/span&gt;

&lt;span class="c1"&gt;// number, boolean, string&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;two&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// '1twofalse'&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="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// '2true3'&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="s1"&gt;Three&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'Three4false'&lt;/span&gt;

&lt;span class="c1"&gt;// null, string&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="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'null123'&lt;/span&gt;

&lt;span class="c1"&gt;// undefined, string&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="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// '123undefined'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens when both the operands are not number, or also if either of them is not a string as well while using the &lt;code&gt;binary(+)&lt;/code&gt; operator&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;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="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//NaN&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we try to add &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt;, the JavaScript engine tries to convert the values to integer resulting in &lt;strong&gt;NaN&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Primitive to Number coercion:
&lt;/h4&gt;

&lt;p&gt;There are multiple scenarios where a primitive value is coerced to &lt;code&gt;number&lt;/code&gt; type. Any primitive is coerced to a &lt;code&gt;number&lt;/code&gt; type when the following operators are involved&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Comparison operators(&lt;code&gt;'&amp;gt;','&amp;lt;','&amp;gt;=','&amp;lt;='&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Unary operators(&lt;code&gt;'+','-'&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Arithmetic operators(&lt;code&gt;'+','-','*','/','%'&lt;/code&gt;)&lt;strong&gt;except for '+' when one operand is a string&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Bitwise operators(&lt;code&gt;'|','&amp;amp;','^','~'&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Loose equality operator(&lt;code&gt;'=='&lt;/code&gt;)&lt;strong&gt;except for when both arguments are already the same primitive&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  1. Comparison operator
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="c1"&gt;// true becomes 1&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;

&lt;span class="c1"&gt;// false becomes 0&lt;/span&gt;
&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="c1"&gt;// string 'true' becomes NaN&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="kc"&gt;NaN&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;

&lt;span class="c1"&gt;// null becomes 0&lt;/span&gt;
&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="c1"&gt;// undefined becomes NaN&lt;/span&gt;
&lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; 
&lt;span class="kc"&gt;NaN&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  2. Unary operator
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="c1"&gt;// 2&lt;/span&gt;

&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="c1"&gt;// 123&lt;/span&gt;

&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="c1"&gt;// 1&lt;/span&gt;

&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="c1"&gt;// 0&lt;/span&gt;

&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;span class="c1"&gt;// NaN&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  3. Arithmetic operator
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;33&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="mi"&gt;33&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; 
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;

&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;9&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; 
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="c1"&gt;// false becomes 0&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;Infinity&lt;/span&gt;

&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; 
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="c1"&gt;// true becomes 1&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; 
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;

&lt;span class="c1"&gt;// undefined becomes NaN&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt; 
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  4. Bitwise operator
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  5. Loose equality operator
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*In JS, == operator is very common to compare values. It compares the values based on their values ignoring their types*/&lt;/span&gt;
&lt;span class="mi"&gt;54&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;54&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="mi"&gt;54&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;
&lt;span class="c1"&gt;//true&lt;/span&gt;

&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="c1"&gt;//true&lt;/span&gt;

&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="c1"&gt;//true&lt;/span&gt;

&lt;span class="cm"&gt;/* complex cases*/&lt;/span&gt;
&lt;span class="c1"&gt;//null can only be coerced to null and undefined&lt;/span&gt;
&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

&lt;span class="c1"&gt;// 'true' becomes NaN&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Primitive to Boolean coercion:
&lt;/h4&gt;

&lt;p&gt;Primitives are coerced to boolean when using &lt;code&gt;logical&lt;/code&gt; operators like &lt;code&gt;'&amp;amp;&amp;amp;', '||', '!'&lt;/code&gt;. The most important point to remember here is even though logical operators do boolean conversions internally, but actually return the value of original operands, even if they are not boolean. The last evaluated operand is returned from each expression.&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="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;NOT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;

&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AND&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;20&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;20&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="mi"&gt;45&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;OR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another interesting thing here is that a primitive type coercion to boolean can also be triggered by the surrounding context, where the type/value is coerced to boolean such as control flows like an &lt;code&gt;if(value)&lt;/code&gt; statement. Truthy &amp;amp; Falsy values are the best examples of boolean type coercion. If you don't know what they are you can have a quick read about them here &lt;a href="https://dev.to/ahmedtahir/truthy-and-falsy-values-in-js-4ac4"&gt;Truthy and Falsy values in JS.&lt;/a&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="c1"&gt;// undefined evaluates to false&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&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="s1"&gt;truthy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="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="s1"&gt;falsy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// falsy&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Special cases:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;NaN doesn't equal to anything, not even itself
&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="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="kc"&gt;NaN&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  SIDE NOTE:
&lt;/h1&gt;

&lt;p&gt;Ever wondered why NaN is not equal to anything, including itself?&lt;br&gt;
Well, there are 2 types of NaN - &lt;code&gt;Quiet NaN&lt;/code&gt; (silently assigned) and &lt;code&gt;Signalling NaN&lt;/code&gt; (throws an exception). Though, both of them are NaN values, their bits differ in internal representation, so it's possible you are comparing Quiet NaN with Signalling NaN. Hence, JavaScript refuses to compare NaN with anything, including itself.&lt;/p&gt;

&lt;p&gt;Take a look at V8 Engines internal function JSEqualTyper (C++).&lt;strong&gt;The second if statement clearly states that if either side of == is a NaN, straightaway return false.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="n"&gt;Typer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Visitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;JSEqualTyper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Typer&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNone&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;None&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;singleton_false_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NullOrUndefined&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NullOrUndefined&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;singleton_true_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Max&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Max&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;singleton_false_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsSingleton&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Types are equal and are inhabited only by a single semantic value,&lt;/span&gt;
    &lt;span class="c1"&gt;// which is not NaN due to the earlier check.&lt;/span&gt;
    &lt;span class="n"&gt;DCHECK&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;singleton_true_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When applying &lt;code&gt;==&lt;/code&gt; to &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, numeric conversion does not happen. &lt;code&gt;null&lt;/code&gt; equals only to &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, and does not equal to anything else.
&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="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;               &lt;span class="c1"&gt;// false, null is not converted to 0&lt;/span&gt;
&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;            &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;       &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because &lt;strong&gt;null&lt;/strong&gt; and &lt;strong&gt;undefined&lt;/strong&gt;, both are evaluated as &lt;strong&gt;false&lt;/strong&gt; in terms of boolean in JavaScript. Hence, we get the values implicitly converted to Booleans.&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="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Useful Tip:
&lt;/h4&gt;

&lt;p&gt;Type coercion can be confusing sometimes, since the decision of converting an operand to a particular type is entirely made by the javascript engine. In real-world applications, this can be scary. One operator that does not trigger implicit type coercion is &lt;code&gt;===&lt;/code&gt;, which is called the &lt;strong&gt;'strict equality operator'&lt;/strong&gt;.Hence always use the &lt;strong&gt;'==='&lt;/strong&gt; operator.Hope this article was useful. Let me know if you guys have any questions. Happy Learning 😊😊...&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is the Execution Context, Execution Stack &amp; Scope Chain in JS</title>
      <dc:creator>Tahir Ahmed Tapadhar</dc:creator>
      <pubDate>Sun, 09 Aug 2020 19:20:15 +0000</pubDate>
      <link>https://forem.com/ahmedtahir/what-is-the-execution-context-execution-stack-scope-chain-in-js-26nc</link>
      <guid>https://forem.com/ahmedtahir/what-is-the-execution-context-execution-stack-scope-chain-in-js-26nc</guid>
      <description>&lt;p&gt;If you are or want to be a JavaScript developer, or are learning javascript, then you must know how the JavaScript programs are executed internally.Understanding of the execution context and execution stack is vital in order to understand how and why javascript works the way it does. Every javascript piece of code internally uses these concepts and knowing them well will surely make you a much better JavaScript developer.&lt;/p&gt;

&lt;h4&gt;
  
  
  Execution Context:
&lt;/h4&gt;

&lt;p&gt;There is always a &lt;strong&gt;context&lt;/strong&gt; present with javascript in which your code is being executed. Every block of code will have its own context in which it is executing. Simply put, an &lt;code&gt;execution context&lt;/code&gt; is nothing but an environment where the Javascript code is evaluated and executed. Whenever any code is run in JavaScript, it’s run inside an execution context.&lt;/p&gt;

&lt;p&gt;There are 3 different types of execution context in JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Global Execution Context (GEC):&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
This is the default or base execution context. The code that is not inside any function is in the global execution context. It performs two things: it creates a global object which is a window object (in the case of browsers) and sets the value of &lt;code&gt;_this_&lt;/code&gt; to equal to the global object. There can only be one global execution context in a program.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Function Execution Context (FEC):&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Every time a function is invoked, an execution context is created for that function. The execution context is destroyed when the associated function has returned or execution is over. Each function has its own execution context, but it’s created when the function is invoked or called.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Eval Execution Context (EEC):&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
code inside an &lt;code&gt;eval&lt;/code&gt; function also has its own execution context. But since it is now deprecated &amp;amp; also not recommended, we shall not discuss it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is a sample code snippet demonstrating the execution context.&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="cm"&gt;/* global execution context */&lt;/span&gt;

&lt;span class="c1"&gt;// every execution context has it's own variable environment&lt;/span&gt;
&lt;span class="c1"&gt;// GEC: global execution context&lt;/span&gt;
&lt;span class="c1"&gt;// FEC: function execution context&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="c1"&gt;// FEC of baz&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//3&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;bar&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="c1"&gt;// FEC of bar&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//2&lt;/span&gt;
   &lt;span class="nf"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//GEC&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;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//1&lt;/span&gt;
&lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//1&lt;/span&gt;

&lt;span class="cm"&gt;/* global execution context */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When the program runs, the first thing the javascript engine creates is a &lt;code&gt;global execution context&lt;/code&gt;. Everything that is not inside a function is a part of the GEC. So the var &lt;code&gt;foo&lt;/code&gt; is in the GEC &amp;amp; is put into memory space.&lt;/li&gt;
&lt;li&gt;When the javascript interpreter comes across &lt;code&gt;bar()&lt;/code&gt;,the function is invoked, a new execution context is immediately created and everything inside of it starts executing.&lt;/li&gt;
&lt;li&gt;Now this is the most important point to remember: 
&lt;strong&gt;'Every execution context (FEC) has it's own variable environment'&lt;/strong&gt;. Therefore when the variable &lt;code&gt;foo&lt;/code&gt; is declared again with value 2,  it is created only within bar()'s execution context.&lt;/li&gt;
&lt;li&gt;Then again &lt;code&gt;baz()&lt;/code&gt; is invoked, and a new execution context with its own variable environment, it's own memory space for its variables is created. Hence, when the variable &lt;code&gt;foo&lt;/code&gt; is re-declared in &lt;code&gt;baz()&lt;/code&gt;, it is only limited to this environment, and is different altogether.&lt;/li&gt;
&lt;li&gt;In short, every time you call a function, you get your own execution context.&lt;/li&gt;
&lt;li&gt;So even though, &lt;code&gt;foo&lt;/code&gt; is declared 3 times, they are &lt;strong&gt;distinct&lt;/strong&gt;, they are &lt;strong&gt;unique&lt;/strong&gt;, they &lt;strong&gt;don't touch each other&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Therefore calling functions &lt;code&gt;bar()&lt;/code&gt; and &lt;code&gt;baz()&lt;/code&gt; wouldn't affect &lt;code&gt;foo = 1&lt;/code&gt; in the GEC. To prove that, we have consoled &lt;code&gt;foo&lt;/code&gt; again at the very end of the program after calling &lt;code&gt;bar()&lt;/code&gt;. Should it have an impact? The answer is NO!!.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is how it is executed internally from execution context's point of view.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foc3hxhjy9bwvo0cmhb0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foc3hxhjy9bwvo0cmhb0h.png" alt="Alt Text" width="800" height="744"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Execution Stack:
&lt;/h4&gt;

&lt;p&gt;In computer science, a call stack is a stack data structure that stores information about the active &lt;em&gt;subroutines&lt;/em&gt; of a computer program. This kind of stack is also known as an &lt;code&gt;execution stack&lt;/code&gt;, &lt;code&gt;program stack&lt;/code&gt;, &lt;code&gt;control stack&lt;/code&gt;, &lt;code&gt;run-time stack&lt;/code&gt;, or &lt;code&gt;machine stack&lt;/code&gt;, and is often shortened to just &lt;code&gt;"the stack&lt;/code&gt;". &lt;a href="https://en.wikipedia.org/wiki/Call_stack" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;subroutine&lt;/em&gt; in simple words is nothing but what we call a &lt;code&gt;function&lt;/code&gt;. Simply put, JavaScript has a single call stack in which it keeps track of what function we are currently executing &amp;amp; what function is to be executed after that. But first- what's a stack? A stack is an array-like data structure where you can add items(push) to the back and only remove(pop) the last item from it. &lt;/p&gt;

&lt;p&gt;Woof!! those are some big words. I know it is difficult to digest the above piece of information, but be patient. Trust me, a piece of code and some visual diagrams will definitely make the concept clearer. So let's examine a piece of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Global execution context&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;global execution context&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&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;foo is executing&lt;/span&gt;&lt;span class="dl"&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;foo has finished executing&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bar&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;bar is executing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;foo&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;bar has finished executing&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;baz&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;baz is executing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;bar&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;baz has finished executing&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;baz&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;program successfully executed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Global execution context&lt;/span&gt;

&lt;span class="c1"&gt;// global execution context&lt;/span&gt;
&lt;span class="c1"&gt;// baz is executing&lt;/span&gt;
&lt;span class="c1"&gt;// bar is executing&lt;/span&gt;
&lt;span class="c1"&gt;// foo is executing&lt;/span&gt;
&lt;span class="c1"&gt;// foo has finished executing&lt;/span&gt;
&lt;span class="c1"&gt;// bar has finished executing&lt;/span&gt;
&lt;span class="c1"&gt;// baz has finished executing&lt;/span&gt;
&lt;span class="c1"&gt;// program successfully executed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initially, before running any piece of javascript code, the &lt;code&gt;execution stack&lt;/code&gt; is empty. When we run a javascript program/file, the javascript engine creates a &lt;code&gt;global execution context&lt;/code&gt; and pushes it on top of the stack the moment your program starts executing. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;In simple words, it is the context of your entire javascript program/source code or the context within which your entire code runs(as shown in the code snippet).&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given below is a simple illustration of how it really looks from the execution stack's perspective.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fytzba9pdtaoaa3z7ycix.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fytzba9pdtaoaa3z7ycix.jpg" alt="Alt Text" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the javascript interpreter comes to &lt;code&gt;baz()&lt;/code&gt;, the point of time when &lt;code&gt;baz()&lt;/code&gt; is called, a new execution context is created for baz() and is put on top of the execution stack. &lt;/li&gt;
&lt;li&gt;Inside function baz(), statements are executed then (if any). The moment &lt;code&gt;bar()&lt;/code&gt; is called inside baz(), a new execution context of &lt;code&gt;bar()&lt;/code&gt; is created and is pushed to the top of the stack.&lt;/li&gt;
&lt;li&gt;The interpreter then executes &lt;code&gt;bar()&lt;/code&gt; and the statements inside of it until it encounters invoking function &lt;code&gt;foo()&lt;/code&gt; and a new execution context is created and pushed on top of the stack.&lt;/li&gt;
&lt;li&gt;In the above diagram, at any given point:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(i). The running execution context is always on top of the stack.&lt;br&gt;
(ii). There can be at most one execution context running on the stack at a time.&lt;br&gt;
(iii). The arrow(⇨) on the stack denotes the current execution context.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After &lt;code&gt;foo()&lt;/code&gt; has finished execution, it is popped off from the stack &amp;amp; the control comes to the next line from where it was invoked i.e from &lt;code&gt;bar()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Similarly, the process goes on until each and every execution context gets completed and is removed from the stack.&lt;/li&gt;
&lt;li&gt;Finally, the GEC remains on the stack and is eventually popped off when the program completely finishes executing.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Scope Chain:
&lt;/h4&gt;

&lt;p&gt;What if, an execution context has no definition of a variable which it wants to print? Consider the code snippet below. &lt;code&gt;baz()&lt;/code&gt; has no variable declared as &lt;code&gt;foo&lt;/code&gt; in its execution context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// every execution context has a reference to it's outer environment&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="cm"&gt;/* foo's value is taken from the outer environment i.e. global environment */&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;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//1&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;bar&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//2&lt;/span&gt;
   &lt;span class="nf"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;'Every execution context has a reference to it's outer environment'&lt;/strong&gt;, all the way up to the global scope. This hierarchy/chain of reference is what is termed as &lt;strong&gt;Scope Chain&lt;/strong&gt;. So if the current execution context has a variable definition missing, it accesses its outer environment to look out for the variable. In the above code, when the execution context of &lt;code&gt;baz()&lt;/code&gt; couldn't find a variable &lt;code&gt;foo&lt;/code&gt; inside its environment, it went searching for it in it's outer environment i.e. &lt;code&gt;Global execution context&lt;/code&gt; and prints out it's value. This is what happened when the current execution context couldn't find a variable &lt;code&gt;foo&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1hlva0d0no8usit0s1p1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1hlva0d0no8usit0s1p1.png" alt="Alt Text" width="800" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The outer environment of &lt;code&gt;baz()&lt;/code&gt; happens to be the global execution context in our example. This is not the case every time. Consider the code snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// every execution context has a reference to it's outer environment&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="cm"&gt;/* here baz's outer environment is the execution context of bar */&lt;/span&gt;
   &lt;span class="cm"&gt;/* and not the global execution context*/&lt;/span&gt;
   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//2&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="nf"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have moved &lt;code&gt;baz()&lt;/code&gt; inside the function &lt;code&gt;bar()&lt;/code&gt;, i.e. it is now nested inside bar(). In this case, it's outer environment has now changed from the global execution context to the execution context of function &lt;code&gt;bar()&lt;/code&gt;. So now instead of printing &lt;code&gt;foo&lt;/code&gt; as 1(from GEC) it prints &lt;code&gt;foo&lt;/code&gt; as 2(from bar's execution context).  &lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion:
&lt;/h4&gt;

&lt;p&gt;Execution context had always been confusing for developers at first, especially those who are new to JavaScript. Visualizing these concepts and how they work internally helps simplify things making it a piece of cake 🍰. Knowing these concepts will help you know how &amp;amp; why javascript works the way it does. Do let me know if you guys have any queries or feedback for improvement. Hope you like it. Happy learning...😊&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Truthy and Falsy values in JS</title>
      <dc:creator>Tahir Ahmed Tapadhar</dc:creator>
      <pubDate>Wed, 29 Jul 2020 21:23:23 +0000</pubDate>
      <link>https://forem.com/ahmedtahir/truthy-and-falsy-values-in-js-4ac4</link>
      <guid>https://forem.com/ahmedtahir/truthy-and-falsy-values-in-js-4ac4</guid>
      <description>&lt;p&gt;"Every value in JavaScript has an inherent boolean value. When that value is evaluated in the context of a boolean expression, the value will be transformed into that inherent boolean value."&lt;br&gt;
The paragraph above is pretty dense with information. You should probably re-read it again.&lt;/p&gt;

&lt;h4&gt;
  
  
  Falsy values
&lt;/h4&gt;

&lt;p&gt;A value is &lt;strong&gt;falsy&lt;/strong&gt; if it converts to &lt;code&gt;false&lt;/code&gt; when evaluated in a boolean context. For example, an empty String &lt;code&gt;""&lt;/code&gt; is falsy because, &lt;code&gt;""&lt;/code&gt; evaluates to &lt;code&gt;false&lt;/code&gt;. You already know if...else statements, so let's use them to test the truthy-ness of &lt;code&gt;""&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd3tnu0kwk28ldk4czv10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd3tnu0kwk28ldk4czv10.png" alt="Alt Text" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Here’s the list of all of the falsy values:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Boolean value &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;number &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;number negative &lt;code&gt;-0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;BigInt &lt;code&gt;0n&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;empty string &lt;code&gt;""&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;null&lt;/code&gt; type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;undefined&lt;/code&gt; type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;NaN&lt;/code&gt;(stands for &lt;strong&gt;Not a Number&lt;/strong&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are &lt;code&gt;eight&lt;/code&gt; falsy values in all of JavaScript! &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;br&gt;
&lt;em&gt;(at the time of writing this article)&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Truthy values
&lt;/h4&gt;

&lt;p&gt;A value is &lt;strong&gt;truthy&lt;/strong&gt; if it converts to &lt;code&gt;true&lt;/code&gt; when evaluated in a boolean context. For example, the number &lt;code&gt;1&lt;/code&gt; is truthy because, &lt;code&gt;1&lt;/code&gt; evaluates to &lt;code&gt;true&lt;/code&gt;. Let's use an if...else statement again to test this out:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgupijaq2kqs48sd0j3y8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgupijaq2kqs48sd0j3y8.png" alt="Alt Text" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are some other examples of truthy values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;true&lt;/li&gt;
&lt;li&gt;42&lt;/li&gt;
&lt;li&gt;"pizza"&lt;/li&gt;
&lt;li&gt;"0"&lt;/li&gt;
&lt;li&gt;"null"&lt;/li&gt;
&lt;li&gt;"undefined"&lt;/li&gt;
&lt;li&gt;{}&lt;/li&gt;
&lt;li&gt;[]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Simply put: if it's not in the list of &lt;code&gt;falsy&lt;/code&gt; values, then it's &lt;code&gt;truthy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Be a little extra careful when working with the same element value but different element type. For example, &lt;code&gt;0&lt;/code&gt; is a number while &lt;code&gt;"0"&lt;/code&gt; is a string. Similarly &lt;code&gt;null&lt;/code&gt; is different(falsy) from string &lt;code&gt;"null"&lt;/code&gt;(truthy) and &lt;code&gt;undefined&lt;/code&gt;(falsy) is different from string &lt;code&gt;"undefined"&lt;/code&gt;(truthy)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxjl0hjg5lvkfdc5qi9v2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxjl0hjg5lvkfdc5qi9v2.png" alt="Alt Text" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Comparison operator
&lt;/h4&gt;

&lt;p&gt;Even though as mentioned above that &lt;code&gt;[]&lt;/code&gt; is a truthy value but behaves differently when used in conjunction with a comparison operator like &lt;code&gt;==&lt;/code&gt;, it evaluates to false. Likewise &lt;code&gt;null&lt;/code&gt; is a falsy, but when used with a &lt;code&gt;==&lt;/code&gt; operator evaluates to true. Similary for &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;NaN&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffvddiv5tbf34bevs3law.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffvddiv5tbf34bevs3law.png" alt="Alt Text" width="800" height="589"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all there is to this topic. For some people truthy &amp;amp; falsy values might be not of a big deal, but knowing how they behave can save you some real time instead of just scratching your head.Let me know if you guys have any questions. Happy Learning..&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is an Object in JavaScript? : Part II</title>
      <dc:creator>Tahir Ahmed Tapadhar</dc:creator>
      <pubDate>Mon, 27 Jul 2020 20:49:59 +0000</pubDate>
      <link>https://forem.com/ahmedtahir/what-is-an-object-in-javascript-part-ii-n3</link>
      <guid>https://forem.com/ahmedtahir/what-is-an-object-in-javascript-part-ii-n3</guid>
      <description>&lt;p&gt;In this post we will go into a bit deeper understanding of how an object is structured, enumerating properties of an object, property descriptors, etc.So if you haven't checked out my previous post &lt;a href="https://dev.to/ahmedtahir/javascript-fundamentals-introduction-to-object-literals-40jn"&gt;What is an Object in JavaScript? : Part I&lt;/a&gt; where I have discussed the basics of objects, I highly recommend you check that out first.&lt;/p&gt;

&lt;p&gt;So let's go ahead and create an object &lt;code&gt;person&lt;/code&gt; which has 2 properties: &lt;code&gt;name&lt;/code&gt; &amp;amp; &lt;code&gt;age&lt;/code&gt;. If you notice here that both of these keys are of type &lt;code&gt;string&lt;/code&gt;. In javascript an object key can only be a string. What if we want the property name to be a number and not a string. It is not possible to have a key which is not a string. But what if we want a number as a key?&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsr9dls8po5gntpfuh2sx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsr9dls8po5gntpfuh2sx.png" alt="Alt Text" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Square bracket notation[]:
&lt;/h4&gt;

&lt;p&gt;At first square brackets may just seem as an alternative to the dot notation to access a property but in fact, it has some unique applications. Using the square bracket notation, you can actually create a &lt;code&gt;number&lt;/code&gt; as a key, define a key with a &lt;code&gt;white space&lt;/code&gt; in it, also define an &lt;code&gt;empty string&lt;/code&gt; as a key.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2wry19ds63dx8yvgftme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2wry19ds63dx8yvgftme.png" alt="Alt Text" width="800" height="662"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now this is really weird!! Lets take a moment to let that sink in. So what is happening internally? When we use the &lt;code&gt;[]&lt;/code&gt; bracket notation, anything inside it is &lt;strong&gt;stringified&lt;/strong&gt; by javascript. For example, in the above code, when the key &lt;code&gt;404&lt;/code&gt; was added to &lt;code&gt;person&lt;/code&gt;, javascript calls the &lt;code&gt;'404.toString()'&lt;/code&gt; method, and will use this result string as the new key. Well we just used a number as a key, I wonder is it possible to use a function as a key?&lt;/p&gt;

&lt;h4&gt;
  
  
  Function as Key of an object:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fer2d5a162du9gvz5o08y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fer2d5a162du9gvz5o08y.png" alt="Alt Text" width="800" height="588"&gt;&lt;/a&gt;&lt;br&gt;
Yes, we can use a function as a key using the &lt;code&gt;[]&lt;/code&gt; bracket notation. Remember that javascript calls the &lt;code&gt;toString()&lt;/code&gt; method implicitly if you use a key other than string type or while using the bracket notation. In the above example, when we write &lt;code&gt;obj[funny]&lt;/code&gt; javascript will call the &lt;code&gt;funny.toString()&lt;/code&gt; method i.e. it will convert the whole function into a string &amp;amp; will use it as a key.If you print out &lt;code&gt;obj&lt;/code&gt;, you will see something like this.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu52g3bgpnk4gatoa40u0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu52g3bgpnk4gatoa40u0.png" alt="Alt Text" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know, JavaScript is funny that way. The question now is, how can we invoke the &lt;code&gt;bunny&lt;/code&gt; function which is the value assigned to our &lt;code&gt;key(funny)&lt;/code&gt; which is also a function. Calling &lt;code&gt;obj.funny&lt;/code&gt; or &lt;code&gt;obj.funny()&lt;/code&gt; simply won't work. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3l4ob2fncz7e1i2o48fw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3l4ob2fncz7e1i2o48fw.png" alt="Alt Text" width="800" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Enumerating object properties:
&lt;/h4&gt;

&lt;p&gt;We can iterate through the properties of an object. There are three native ways to list all the object properties:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;for...in&lt;/code&gt;: This method traverses all enumerable properties of an object and its prototype chain.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Object.keys()&lt;/code&gt;: This method returns an array with all the own (not in the prototype chain) enumerable properties' names of an object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Object.getOwnPropertyNames()&lt;/code&gt;: This method returns an array containing all own properties' names (enumerable or not) of an object. 
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnwtggjs4t9vs340jqe0z.png" alt="Alt Text" width="800" height="620"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Property Descriptors:
&lt;/h4&gt;

&lt;p&gt;Every object property in javascript has its own property descriptors which hold additional information about that property. Further more each property descriptors have certain inbuilt attributes defined by javascript. They are mainly divided into 2 categories: &lt;code&gt;Data Descriptors&lt;/code&gt; and &lt;code&gt;Accessor Descriptors&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2ie3tr41ufkdtjodmnne.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2ie3tr41ufkdtjodmnne.png" alt="Alt Text" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this post, I'll be talking about data descriptors only. We can very easily in javascript access the property descriptors of an object property using the in-built method &lt;code&gt;Object.getOwnPropertyDescriptor(obj, 'name')&lt;/code&gt; where obj is the reference object and name is the property name.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnjnc7jjg1kvuxpcosxle.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnjnc7jjg1kvuxpcosxle.png" alt="Alt Text" width="800" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here the value of the property &lt;code&gt;name&lt;/code&gt; of person object is &lt;code&gt;Betsy&lt;/code&gt;, is writable(can be modified), is enumerable, is configurable. Likewise we can also change these default attribute values of a property descriptor as per our needs using the in-built &lt;code&gt;Object.defineProperty(obj, 'name', {descriptor})&lt;/code&gt; method. An example illustrating its usage is given below.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzasbcrd508g8ibor22it.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzasbcrd508g8ibor22it.png" alt="Alt Text" width="800" height="569"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that is all about objects in javascript that you need to know for a head start. The predefined &lt;code&gt;Object&lt;/code&gt; type of javascript however has a lot of other cool useful methods which can also be used to leverage performance of your application. Let me know if you guys have any questions. You could drop them in the comments section below. Happy learning !!....😊😊 &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>objects</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>What is an Object in JavaScript? : Part I</title>
      <dc:creator>Tahir Ahmed Tapadhar</dc:creator>
      <pubDate>Thu, 23 Jul 2020 20:35:34 +0000</pubDate>
      <link>https://forem.com/ahmedtahir/javascript-fundamentals-introduction-to-object-literals-40jn</link>
      <guid>https://forem.com/ahmedtahir/javascript-fundamentals-introduction-to-object-literals-40jn</guid>
      <description>&lt;p&gt;You may have quite often heard developers saying the phrase &lt;code&gt;"Everything in JavaScript is an Object !!!"&lt;/code&gt; I'm pretty sure you have. So what exactly do we mean by that? What is it really? Today I will try to explain you what an object really is in this article...&lt;/p&gt;

&lt;p&gt;Like every programming language, JavaScript also has building blocks which are known as &lt;strong&gt;"Objects"&lt;/strong&gt; or formally speaking &lt;strong&gt;"Object Literals"&lt;/strong&gt;. So lets go ahead and see how to create an object in JavaScript.&lt;/p&gt;

&lt;h4&gt;
  
  
  Object:
&lt;/h4&gt;

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

&lt;p&gt;Congratulations! you just created your first object in JavaScript.&lt;br&gt;
So let's go ahead and examine the above line of code. In JavaScript we declare a variable using the &lt;code&gt;var&lt;/code&gt; keyword followed by the name of the variable i.e. &lt;code&gt;obj&lt;/code&gt; in our case. Curly braces&lt;code&gt;{}&lt;/code&gt; here denotes an object in JavaScript. Since there is nothing inside of it, therefore we call it an &lt;strong&gt;empty object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But real-world objects aren't as simple as this one. An object is a collection of properties, and a property is an association between a name (or key) and a value. An &lt;code&gt;Object Literal&lt;/code&gt; or simply put &lt;code&gt;Object&lt;/code&gt; have properties in the form of &lt;strong&gt;key-value&lt;/strong&gt; pairs. So lets go ahead and define some properties of our object.&lt;/p&gt;

&lt;h4&gt;
  
  
  Object Properties:
&lt;/h4&gt;

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

&lt;p&gt;A property's value can be of any type. Our above object &lt;code&gt;person&lt;/code&gt; has 3 properties namely: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;name&lt;/strong&gt; which is of type &lt;code&gt;string&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;age&lt;/strong&gt; which is of type &lt;code&gt;number&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;isEmployed&lt;/strong&gt; which is of type &lt;code&gt;boolean&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Methods:
&lt;/h4&gt;

&lt;p&gt;A method is a &lt;em&gt;function&lt;/em&gt; associated with an object. We can also have functions as a property of an object. So if an object has a property which is a function, that property is known as a &lt;code&gt;method&lt;/code&gt;. Lets see that in action.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8hsnn75sjiu1adbcflg8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8hsnn75sjiu1adbcflg8.png" alt="Alt Text" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Accessing Properties:
&lt;/h4&gt;

&lt;p&gt;You see how easy it is to define properties of an object. Likewise, it is also easy to access the properties of an object. We do that using the &lt;code&gt;dot(.)&lt;/code&gt; notation in JavaScript.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftqtpjluy4zx6wmd1kyh9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftqtpjluy4zx6wmd1kyh9.png" alt="Alt Text" width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; One very important thing to note here is that both JavaScript objects &amp;amp; properties are case sensitive. So if you do something like &lt;code&gt;person.Name&lt;/code&gt; it will give you &lt;code&gt;undefined&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fguctqr2k9e8k7t7f441u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fguctqr2k9e8k7t7f441u.png" alt="Alt Text" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is another way using which we can access the properties of an object. You simply need to enclose the name of the property in a &lt;strong&gt;square bracket&lt;/strong&gt; &lt;code&gt;[]&lt;/code&gt; and need to put them inside single/double quotes &lt;code&gt;'' / ""&lt;/code&gt;. However accessing a property using this method is not recommended. Below is an example demonstrating the above method.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxueaxhy1txoy4vriqbtm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxueaxhy1txoy4vriqbtm.png" alt="Alt Text" width="800" height="515"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One advantage of using the bracket notation is that you can define a property with a &lt;code&gt;white space&lt;/code&gt; between them which you cannot do using the &lt;code&gt;dot&lt;/code&gt; notation. You can also define an empty property using the bracket notation.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk8wj3n52rdie9wnax7lw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk8wj3n52rdie9wnax7lw.png" alt="Alt Text" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Adding Properties Dynamically:
&lt;/h4&gt;

&lt;p&gt;In JavaScript, we can easily add properties to an object dynamically at any instance of time.For instance, I could do something like this.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fra84hzenwxt9dc3jvwm0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fra84hzenwxt9dc3jvwm0.png" alt="Alt Text" width="800" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here,we have added a property &lt;code&gt;country&lt;/code&gt; to our person object dynamically(at run-time). Now if we print our person object again we could see a new property.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fveeqr1l5a57t8d7jro58.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fveeqr1l5a57t8d7jro58.png" alt="Alt Text" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Deleting properties:
&lt;/h4&gt;

&lt;p&gt;Similarly, javascript allows us to &lt;code&gt;remove/delete&lt;/code&gt; a property of an object when ever we want. To remove a property we use the &lt;code&gt;delete&lt;/code&gt; keyword followed by the &lt;code&gt;object.property&lt;/code&gt; name as shown below.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe3lwwnrf5udk93b6pd9z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe3lwwnrf5udk93b6pd9z.png" alt="Alt Text" width="800" height="515"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Nested Objects:
&lt;/h4&gt;

&lt;p&gt;It is possible that an object can be nested inside of another object. Since, the value of a property of an object can be anything from a string, number to a function...it can also be another complete new object. In the below example, the value of the property &lt;code&gt;address&lt;/code&gt; is a complete different object.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe5cww801i0c8kjg5q1xv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe5cww801i0c8kjg5q1xv.png" alt="Alt Text" width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So now I hope you guys are familiar as to what are objects in JavaScript and how to use them. I've demonstrated the very basics here. The second edition to this article will have a high level overview or more in-depth understanding of what JavaScript objects are &amp;amp; how are they structured internally. Please let me know if you guys have any questions. You could drop them in the comments section below. Happy learning !!....😊😊&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>objects</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
