<?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: Sam Abaasi</title>
    <description>The latest articles on Forem by Sam Abaasi (@samabaasi).</description>
    <link>https://forem.com/samabaasi</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%2F754819%2Fc82a966a-4078-4e2b-aebe-2b7427a0abd0.jpg</url>
      <title>Forem: Sam Abaasi</title>
      <link>https://forem.com/samabaasi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/samabaasi"/>
    <language>en</language>
    <item>
      <title>Avoiding Unsafe Calls in JavaScript and React Projects with ESLint</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Sun, 29 Dec 2024 17:10:23 +0000</pubDate>
      <link>https://forem.com/samabaasi/avoiding-unsafe-calls-in-javascript-and-react-projects-with-eslint-4gko</link>
      <guid>https://forem.com/samabaasi/avoiding-unsafe-calls-in-javascript-and-react-projects-with-eslint-4gko</guid>
      <description>&lt;h2&gt;
  
  
  Avoiding Unsafe Calls in JavaScript and React Projects with ESLint
&lt;/h2&gt;

&lt;p&gt;📜✨ In modern JavaScript and React applications, it's common to encounter runtime errors caused by accessing properties on &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt; values, or calling methods on undefined arrays or objects. These issues can disrupt user experience and make debugging a nightmare. In this article, we'll identify the common issues and provide an ESLint configuration to mitigate them effectively. 🌟💻&lt;/p&gt;

&lt;p&gt;🌟✨ In our React or React Native projects, because we are not using TypeScript, we sometimes forget to write safe code. These unsafe codes can lead to many issues in production, like crashing React Native apps, frustrating users, and complicating maintenance. Let's dive into these common issues and how to solve them. 🚨🚀💔&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Issues with Unsafe Calls
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Accessing Properties on &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt; 🌟🔍✨
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Problem:
&lt;/h4&gt;

&lt;p&gt;Accessing a property on an object that is &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt; causes a runtime 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;const&lt;/span&gt; &lt;span class="nx"&gt;user&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="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;user&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="c1"&gt;// ❌ Runtime Error: Cannot read property 'name' of undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 1:&lt;/strong&gt; Use optional chaining (&lt;code&gt;?.&lt;/code&gt;).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&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;user&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 2:&lt;/strong&gt; Employ a default fallback.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&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;user&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;user&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Default Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 3:&lt;/strong&gt; Ensure initialization before access.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Default Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Calling Methods on &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt; ✋📉📋
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Problem:
&lt;/h4&gt;

&lt;p&gt;Calling methods like &lt;code&gt;.map()&lt;/code&gt; or &lt;code&gt;.filter()&lt;/code&gt; on an undefined array 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;const&lt;/span&gt; &lt;span class="nx"&gt;items&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="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&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;item&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// ❌ Runtime Error: Cannot read property 'map' of undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 1:&lt;/strong&gt; Verify that the variable is an array.
&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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;item&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;
&lt;strong&gt;Solution 2:&lt;/strong&gt; Provide a default array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;someValue&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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;item&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;
&lt;strong&gt;Solution 3:&lt;/strong&gt; Use the nullish coalescing operator (&lt;code&gt;??&lt;/code&gt;).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;possibleItems&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Invoking Undefined Functions ⚙️⚠️🚫
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Problem:
&lt;/h4&gt;

&lt;p&gt;Trying to call a function that might be undefined:&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;const&lt;/span&gt; &lt;span class="nx"&gt;handler&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="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ❌ Runtime Error: handler is not a function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 1:&lt;/strong&gt; Check existence before invocation.
&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&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;handler&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;
&lt;strong&gt;Solution 2:&lt;/strong&gt; Assign a no-op default function.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;passedHandler&lt;/span&gt; &lt;span class="o"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Destructuring Undefined or Null Objects 📦❌🔓
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Problem:
&lt;/h4&gt;

&lt;p&gt;Destructuring properties from an undefined object results in 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;const&lt;/span&gt; &lt;span class="nx"&gt;obj&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="kd"&gt;const&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;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ❌ Runtime Error: Cannot destructure property 'name' of undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 1:&lt;/strong&gt; Use optional chaining with defaults.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Default Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&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;
&lt;strong&gt;Solution 2:&lt;/strong&gt; Validate before destructuring.
&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&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;=&lt;/span&gt; &lt;span class="nx"&gt;user&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;h4&gt;
  
  
  5. Accessing Non-Existent Array Elements 🚫📋📊
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Problem:
&lt;/h4&gt;

&lt;p&gt;Accessing elements of an undefined array causes 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;const&lt;/span&gt; &lt;span class="nx"&gt;arr&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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;// ❌ Runtime Error: Cannot read property '0' of undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 1:&lt;/strong&gt; Provide a default fallback.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;arr&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="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Default Value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 2:&lt;/strong&gt; Initialize arrays properly.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  6. Invalid Array/Function Usage 🛑📝
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Problem:
&lt;/h4&gt;

&lt;p&gt;Using array methods like &lt;code&gt;.map()&lt;/code&gt; or &lt;code&gt;.filter()&lt;/code&gt; on undefined values or objects:&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;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&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="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;n&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;n&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="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ Runtime Error: Cannot read property 'filter' of undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 1:&lt;/strong&gt; Always validate inputs for array functions.
&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&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="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;
&lt;strong&gt;Solution 2:&lt;/strong&gt; Return a safe result if input isn't valid.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  7. Insufficient Conditional Checks 💡🔒
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Problem:
&lt;/h4&gt;

&lt;p&gt;Failing to validate conditions strictly can lead to bugs, such as relying on falsy values. For example, &lt;code&gt;if&lt;/code&gt; conditions expecting a boolean might incorrectly evaluate other types like &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;0&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;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&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="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Executes even if obj.prop is `undefined`, leading to unexpected behavior&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 1:&lt;/strong&gt; Use strict equality comparisons.
&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prop&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="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;Do something&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 2:&lt;/strong&gt; Coerce values explicitly for intended behavior.
&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prop&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="s1"&gt;Do something&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution 3:&lt;/strong&gt; Define explicit conditions in your code.
&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;boolean&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prop&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="s1"&gt;Safe conditional execution&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Using ESLint to Avoid Unsafe Calls 🚀🔒✅
&lt;/h2&gt;

&lt;p&gt;To catch these issues during development, we can leverage ESLint with specific rules. Below is an ESLint configuration that will flag unsafe calls and suggest fixes. 🛠️🔍🌟&lt;/p&gt;

&lt;h3&gt;
  
  
  ESLint Configuration 📜💻🛡️
&lt;/h3&gt;

&lt;p&gt;Add the following rules to your &lt;code&gt;.eslintrc.js&lt;/code&gt; or ESLint configuration file:&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;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@typescript-eslint/parser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// If using TypeScript&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&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;@typescript-eslint&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;extends&lt;/span&gt;&lt;span class="p"&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;eslint:recommended&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;plugin:@typescript-eslint/recommended&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;plugin:@typescript-eslint/recommended-requiring-type-checking&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="na"&gt;parserOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;ecmaVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2020&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// or later&lt;/span&gt;
    &lt;span class="na"&gt;sourceType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;project&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tsconfig.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Type-aware linting&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Disallow accessing properties on undefined/null&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@typescript-eslint/no-unnecessary-condition&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;warn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Enforce optional chaining where needed&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;no-unused-expressions&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;warn&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="na"&gt;allowShortCircuit&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;// 3. Disallow unsafe calls (functions on undefined/null)&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@typescript-eslint/no-unsafe-call&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;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="c1"&gt;// 4. Disallow unsafe member access (accessing props on undefined/null)&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@typescript-eslint/no-unsafe-member-access&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;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="c1"&gt;// 5. Catch invalid destructuring of undefined or null&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@typescript-eslint/no-unnecessary-condition&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;warn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="c1"&gt;// 6. Catch invalid array/function usage (e.g., map/filter on undefined)&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;consistent-return&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;warn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="c1"&gt;// 7. Enforce stricter conditional checks&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@typescript-eslint/strict-boolean-expressions&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&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="na"&gt;allowNullableObject&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="na"&gt;allowNullableBoolean&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="na"&gt;allowNullableString&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="na"&gt;allowNumber&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="na"&gt;allowAny&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="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;

    &lt;span class="c1"&gt;// Additional rules for clarity and safety:&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;no-implicit-coercion&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;warn&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="na"&gt;allow&lt;/span&gt;&lt;span class="p"&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;!!&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;no-unreachable&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;error&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;@typescript-eslint/no-non-null-assertion&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;error&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation of Rules 💡⚙️🌐
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@typescript-eslint/no-unnecessary-condition&lt;/code&gt;&lt;/strong&gt;: Flags unnecessary conditions or unhandled potential &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt; values. ⚠️&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;no-unused-expressions&lt;/code&gt;&lt;/strong&gt;: Ensures that short-circuited logic like &lt;code&gt;someObject &amp;amp;&amp;amp; someObject.doSomething&lt;/code&gt; is avoided unless explicitly necessary. 🚀&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@typescript-eslint/no-unsafe-call&lt;/code&gt;&lt;/strong&gt;: Prevents unsafe function calls on non-functions. ❌&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@typescript-eslint/no-unsafe-member-access&lt;/code&gt;&lt;/strong&gt;: Flags attempts to access properties on potentially &lt;code&gt;undefined&lt;/code&gt; values. ✋&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;consistent-return&lt;/code&gt;&lt;/strong&gt;: Enforces consistent return types in functions to avoid returning invalid or undefined values. 💾&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@typescript-eslint/strict-boolean-expressions&lt;/code&gt;&lt;/strong&gt;: Strengthens conditional expressions by preventing implicit coercion. 🔒&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@typescript-eslint/no-non-null-assertion&lt;/code&gt;&lt;/strong&gt;: Disallows the unsafe &lt;code&gt;!&lt;/code&gt; operator used to bypass null/undefined checks. 🚫&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Installing Required Dependencies 📦🔧🔍
&lt;/h2&gt;

&lt;p&gt;To enable these rules, ensure you have the necessary ESLint plugins and parsers installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Integrating ESLint with VSCode 🖥️🚦🌈
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Install the ESLint Extension:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Search for "ESLint" in the VSCode marketplace and install it. ✨&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Enable Auto-Fixing:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Add the following to your &lt;code&gt;settings.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"editor.codeActionsOnSave"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"source.fixAll.eslint"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"eslint.validate"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"javascript"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"javascriptreact"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"typescript"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"typescriptreact"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;strong&gt;Run ESLint:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Add an npm script to run ESLint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lint"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"eslint . --ext .js,.jsx,.ts,.tsx"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, run &lt;code&gt;npm run lint&lt;/code&gt; to catch issues. 🚀&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion 🌟🚀📘
&lt;/h2&gt;

&lt;p&gt;By implementing the above ESLint rules and practices, you can catch and fix unsafe calls before they become runtime errors. 🎉🌈 This approach will improve the reliability and maintainability of your JavaScript and React projects. 🌍⚙️✨&lt;/p&gt;




</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 10)? Class</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Thu, 26 Oct 2023 10:29:23 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-10-class-17oi</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-10-class-17oi</guid>
      <description>&lt;p&gt;JavaScript classes have become a fundamental part of modern JavaScript development. Introduced in ES6, they provide a convenient way to create and manage objects and their behaviors. However, understanding the full potential of JavaScript classes, beyond their syntactic sugar, is essential for writing efficient and maintainable code. In this article, we'll delve into JavaScript classes. We'll explore the syntax, inheritance, and the nuanced aspects of working with classes. We'll also discuss best practices, including how to avoid common pitfalls. Additionally, we'll take a closer look at the concept of auto-binding methods, which aims to address the challenge of maintaining the correct this context when working with class methods. Let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The Class Syntax&lt;/li&gt;
&lt;li&gt;Extending Classes&lt;/li&gt;
&lt;li&gt;Relative Polymorphism&lt;/li&gt;
&lt;li&gt;
Beyond Syntactic Sugar: A Closer Look

&lt;ul&gt;
&lt;li&gt;Preserving &lt;code&gt;this&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Understanding Prototype&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The Challenge of Maintaining this Context&lt;/li&gt;
&lt;li&gt;The Concept of Auto-Binding&lt;/li&gt;
&lt;li&gt;
The Problem with Auto-Binding Methods

&lt;ul&gt;
&lt;li&gt;The Hacky Solution&lt;/li&gt;
&lt;li&gt;Violating JavaScript's DNA&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The Danger of Permanent Hacks&lt;/li&gt;
&lt;li&gt;Best Practices: Embracing the Full Power of Classes&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Sources&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Class Syntax
&lt;/h2&gt;

&lt;p&gt;In JavaScript, classes are defined using the class keyword. They can have names, but it's worth noting that classes can also be expressions and even anonymous. The class syntax supports the declaration of constructors and methods without the need for commas between them. For 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="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;topic&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;topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome to the &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;topic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workshop! &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;question&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deepJS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;deepJS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What happened to 'this'?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Extending Classes
&lt;/h2&gt;

&lt;p&gt;JavaScript classes support inheritance through the extends clause. When a class extends another class, it inherits the methods and properties of the parent class. You can also define additional methods in the child class. Here's 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="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ChildWorkshop&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;speakUp&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Speaking up in the &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;topic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workshop!`&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;childJS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ChildWorkshop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Advanced JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;childJS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What's the superclass?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;childJS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speakUp&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Relative Polymorphism
&lt;/h2&gt;

&lt;p&gt;Relative polymorphism is a concept that allows child classes to override methods defined in the parent &lt;code&gt;class&lt;/code&gt;. JavaScript's &lt;code&gt;class&lt;/code&gt; system supports this through the &lt;code&gt;super&lt;/code&gt; keyword. It enables child &lt;code&gt;class&lt;/code&gt;es to refer to methods in the parent &lt;code&gt;class&lt;/code&gt; with the same name. This is particularly useful for customizing behavior. For instance:&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;class&lt;/span&gt; &lt;span class="nx"&gt;ChildWorkshop&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;speakUp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Can I ask questions?&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Speaking up in the &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;topic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workshop!`&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Beyond Syntactic Sugar: A Closer Look
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Preserving &lt;code&gt;this&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;One of the significant points is the behavior of &lt;code&gt;this&lt;/code&gt; within &lt;code&gt;class&lt;/code&gt; methods. In JavaScript, the &lt;code&gt;this&lt;/code&gt; keyword is not auto-bound to &lt;code&gt;class&lt;/code&gt; methods, and they behave just like regular functions. This means that if you pass a &lt;code&gt;class&lt;/code&gt; method to a function like &lt;code&gt;setTimeout&lt;/code&gt;, it will lose its this binding. To preserve this, developers often use hardbound methods or arrow functions, which can lead to unnecessary complexity and performance overhead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Prototype
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;class system&lt;/strong&gt; in JavaScript is built upon the concept of prototypes. Methods and properties are defined on the prototype, not on instances. However, when you assign a function directly to an instance, it no longer exists on the prototype. This practice can lead to creating separate copies of functions for each instance, which is inefficient and diverges from the core principles of class-based JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Challenge of Maintaining this Context
&lt;/h2&gt;

&lt;p&gt;One of the common challenges developers face when working with JavaScript classes is ensuring that the &lt;code&gt;this&lt;/code&gt; context remains consistent when calling class methods. This is particularly crucial when you pass class methods as callbacks or use them in asynchronous operations like &lt;code&gt;setTimeout&lt;/code&gt;. Without proper binding, the &lt;code&gt;this&lt;/code&gt; context can become unpredictable.&lt;/p&gt;

&lt;p&gt;Here's a simple example that illustrates this issue:&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;class&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;teacher&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;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; asked: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;question&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deepJS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kyle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;askFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;deepJS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;askFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What is auto-binding?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example, when we call &lt;code&gt;askFunction&lt;/code&gt;, the &lt;code&gt;this&lt;/code&gt; context inside the &lt;code&gt;ask&lt;/code&gt; method is no longer bound to the &lt;code&gt;deepJS&lt;/code&gt; instance. It results in an error because &lt;code&gt;this.teacher&lt;/code&gt;is &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Concept of Auto-Binding &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Auto-binding methods refer to a mechanism that automatically maintains the correct this context for class methods, without the need for explicit binding. While JavaScript doesn't provide native auto-binding, developers often come up with solutions to achieve it.&lt;/p&gt;

&lt;p&gt;Here's an example of how you can implement a basic auto-binding utility for class methods:&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="nx"&gt;autoBindMethods&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;classInstance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;classInstance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;methodNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getOwnPropertyNames&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;methodNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;methodName&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;classInstance&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;methodName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&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="nx"&gt;classInstance&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;methodName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;classInstance&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;methodName&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;classInstance&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;teacher&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;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;autoBindMethods&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="c1"&gt;// Automatically bind class methods&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; asked: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;question&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deepJS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kyle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;askFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;deepJS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;askFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What is auto-binding?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this modified example, we use the &lt;code&gt;autoBindMethods&lt;/code&gt; function to automatically bind all &lt;code&gt;class&lt;/code&gt; methods when an instance is created. This way, the &lt;code&gt;ask&lt;/code&gt; method maintains the correct &lt;code&gt;this&lt;/code&gt; context even when used as &lt;code&gt;askFunction&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem with Auto-Binding Methods &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Hacky Solution
&lt;/h3&gt;

&lt;p&gt;In discussions about auto-bound methods, a potential solution has been suggested. The idea is to replace actual methods on class prototypes with getters. These getters would dynamically create hard-bound versions of the methods on the fly and cache them in a WeakMap. When you access a method through the getter, you automatically get a hard-bound version. This is a complex and unconventional approach that fundamentally changes the behavior of JavaScript functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Violating JavaScript's DNA
&lt;/h3&gt;

&lt;p&gt;JavaScript functions are known for their dynamic nature. Auto-binding methods, while seemingly convenient, contradict the fundamental principles of JavaScript's functions. Attempting to force JavaScript into the mold of classes from other languages leads to complex and hacky solutions like the one described above. It doesn't align with JavaScript's core philosophy of flexibility and dynamic behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Danger of Permanent Hacks
&lt;/h2&gt;

&lt;p&gt;As has been wisely noted, "There's nothing more permanent than a temporary hacker." The danger of introducing such hacky solutions is that they might become permanent parts of the language. Once developers start using them, it's challenging to reverse the trend, even if they contradict the core principles of the language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices: Embracing the Full Power of Classes
&lt;/h2&gt;

&lt;p&gt;To make the most of JavaScript classes, it's crucial to embrace their full capabilities. Here are some best practices to consider:&lt;/p&gt;

&lt;p&gt;Leverage Prototypes: Embrace the prototype chain and avoid assigning functions directly to instances. This ensures efficient memory usage and maintains the dynamic nature of classes.&lt;/p&gt;

&lt;p&gt;Avoid Hardbound Methods: While hardbound methods and arrow functions can preserve this, they might not be necessary. Embrace the dynamic nature of JavaScript and leverage this without overcomplicating your code.&lt;/p&gt;

&lt;p&gt;Keep Classes Dynamic: Don't limit the class system's dynamic flexibility. If you need dynamic, flexible structures, consider alternatives like the module pattern, which has been available for over two decades and provides a robust solution for many use cases.&lt;/p&gt;

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

&lt;p&gt;JavaScript classes are a powerful tool for creating and managing objects in your code. While they provide a convenient syntax, it's essential to understand their inner workings and nuances fully. In this article, we explored the syntax of JavaScript classes, their inheritance mechanism, and the concept of relative polymorphism using the super keyword. We also delved into the discussion surrounding auto-binding methods, which aim to address the challenge of maintaining the correct this context in class methods.&lt;/p&gt;

&lt;p&gt;It's important to note that JavaScript's class system is built upon the concept of prototypes, where methods and properties are defined on the prototype rather than directly on instances. To make the most of JavaScript classes, we recommend leveraging prototypes, avoiding hardbound methods or arrow functions when they are unnecessary, and keeping classes dynamic.&lt;/p&gt;

&lt;p&gt;While the idea of auto-binding methods has been considered, it's important to avoid overly complex solutions that contradict the core principles of JavaScript's dynamic behavior. By embracing the dynamic nature of JavaScript's class system and maintaining the flexibility it offers, you can write more efficient, maintainable, and robust JavaScript code.&lt;/p&gt;

&lt;p&gt;Understanding JavaScript classes beyond their syntactic sugar empowers developers to make the most of this fundamental feature, creating better software and enhancing the JavaScript ecosystem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 9)? Arrow functions and lexical this</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Thu, 26 Oct 2023 10:25:36 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-9-arrow-functions-and-lexical-this-38jo</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-9-arrow-functions-and-lexical-this-38jo</guid>
      <description>&lt;p&gt;The advent of &lt;code&gt;ES6&lt;/code&gt; brought us many exciting features, and among them, arrow functions stood out as a concise and elegant way to write functions. However, while arrow functions have been embraced for their brevity, they come with certain quirks that can be a double-edged sword for developers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt;The Anonymous Nature of Arrow Functions&lt;br&gt;
The Inferred Purpose&lt;br&gt;
The Power of Descriptive Function Names&lt;br&gt;
The Readability Factor&lt;br&gt;
Arrow Functions in the Context of this&lt;br&gt;
The Essence of Lexical this&lt;br&gt;
Arrow Functions and the new Keyword&lt;br&gt;
The Curly Brace Confusion&lt;br&gt;
The Parent Lexical Scope&lt;br&gt;
The Right Tool for Lexical this Behavior&lt;br&gt;
Conclusion&lt;br&gt;
Sources&lt;/p&gt;
&lt;h2&gt;
  
  
  The Anonymous Nature of Arrow Functions
&lt;/h2&gt;

&lt;p&gt;Arrow functions are inherently anonymous, unlike traditional functions with a distinct name. This anonymity can impact code readability, making it challenging for developers to quickly discern the purpose of a function. Consider this arrow 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;As Kyle Simpson aptly put it, **"The only way to figure out what an arrow function is doing is to read its function body." **This lack of a name can obscure the function's intention, leading to potential confusion for you and other developers working on the code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The Inferred Purpose
&lt;/h3&gt;

&lt;p&gt;Proponents of arrow functions argue that their purpose is self-evident. However, it's essential to remember that the purpose of an arrow function is inferred rather than explicitly expressed through a well-chosen name. You are required to deduce the function's intent by examining its code, which can sometimes lead to ambiguity.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Power of Descriptive Function Names
&lt;/h3&gt;

&lt;p&gt;In contrast, when you use a named function, you provide a clear and explicit label for the function's purpose. For example, consider the alternative using a function declaration:&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="nx"&gt;getIdFromPerson&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getIdFromPerson&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this revised code, the function &lt;code&gt;getIdFromPerson&lt;/code&gt; conveys its purpose without ambiguity. It's evident that this function retrieves the &lt;code&gt;id&lt;/code&gt; property from a &lt;code&gt;person&lt;/code&gt; object. Descriptive names enhance code readability and maintainability, making it easier for you and your team to understand and work with the codebase.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Readability Factor
&lt;/h3&gt;

&lt;p&gt;While arrow functions offer brevity, they can sometimes compromise readability. Self-explanatory code, where the purpose of a function is evident from its name, often proves invaluable. Arrow functions' shorter syntax may initially appear more straightforward, but the brevity can come at the cost of understanding and maintainability. Moreover, arrow functions come in various syntax variations, leading to potential confusion in certain contexts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arrow Functions in the Context of this
&lt;/h2&gt;

&lt;p&gt;However, it's crucial to acknowledge that arrow functions bring a unique characteristic called "lexical this behaviour." This behaviour can be immensely valuable in specific situations.&lt;br&gt;
Consider this 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workshop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;ask&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="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome to the &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;topic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workshop!`&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;workshop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Welcome to the JavaScript workshop!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the &lt;code&gt;ask&lt;/code&gt; method of the workshop object contains an arrow function passed to &lt;code&gt;setTimeout&lt;/code&gt;. Surprisingly, within the arrow function, the &lt;code&gt;this&lt;/code&gt; keyword correctly points to the &lt;code&gt;workshop&lt;/code&gt; object. This phenomenon is the essence of &lt;strong&gt;"lexical this behavior."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's dive deeper into what &lt;strong&gt;"lexical this"&lt;/strong&gt; means:&lt;/p&gt;

&lt;p&gt;An arrow function does not define a &lt;code&gt;this&lt;/code&gt; keyword. In fact, &lt;strong&gt;it doesn't have a &lt;code&gt;this&lt;/code&gt; keyword at all&lt;/strong&gt;; it treats this like any other variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Essence of Lexical this
&lt;/h2&gt;

&lt;p&gt;In essence, &lt;strong&gt;lexical this means that an arrow function will keep climbing up the scope chain until it locates a function with a defined &lt;code&gt;this&lt;/code&gt; keyword&lt;/strong&gt;. The &lt;code&gt;this&lt;/code&gt; keyword in the arrow function is determined by the function containing it. In the example, it looks up one level in scope and finds the ask function.&lt;/p&gt;

&lt;p&gt;This understanding is crucial because it helps you avoid incorrect thinking, which can lead to bugs in your code. By thinking in alignment with JavaScript's design and the language specification (the spec), you can ensure that your code behaves as expected.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The JavaScript language specification confirms that an arrow function does not define local bindings for &lt;code&gt;arguments&lt;/code&gt;, &lt;code&gt;super&lt;/code&gt;, &lt;code&gt;this&lt;/code&gt;, or &lt;code&gt;new.target&lt;/code&gt;. This specification is a key point of reference, and adhering to it eliminates misconceptions and potential issues in your code.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Arrow Functions and the new Keyword
&lt;/h2&gt;

&lt;p&gt;One fascinating aspect of arrow functions is their behavior concerning the &lt;code&gt;new&lt;/code&gt; keyword. If you recall from earlier in this series, the &lt;code&gt;new&lt;/code&gt; keyword takes precedence over a hardbound function. That means, for some unusual reason, if you call &lt;code&gt;new&lt;/code&gt; on a hardbound function, it can override the hard binding and become the new &lt;code&gt;object&lt;/code&gt;. However, this is not the case with arrow functions. Calling &lt;code&gt;new&lt;/code&gt; on an arrow function results in an exception, as arrow functions are not hardbound functions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;"TypeError: function is not a constructor"&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Understanding this distinction is vital for maintaining code clarity and preventing unexpected behavior in edge cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Curly Brace Confusion
&lt;/h2&gt;

&lt;p&gt;One of the persistent frustrations among developers is the assumption that curly braces &lt;code&gt;{}&lt;/code&gt; imply a scope. We often associate them with &lt;code&gt;blocks&lt;/code&gt;, &lt;code&gt;function&lt;/code&gt; bodies, or scopes. However, it's essential to understand that not every set of curly braces denotes a scope.&lt;br&gt;
Consider this 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workshop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kyle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&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="nx"&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;teacher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;question&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, it appears that this function is enclosed in a scope. Still, this is not the case. These curly braces merely define the function body; they don't create a scope. Arrow functions don't introduce a new scope like regular functions do.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Parent Lexical Scope
&lt;/h2&gt;

&lt;p&gt;One common misconception is that arrow functions should inherit the scope in which they are defined. For example, you might expect an arrow function defined within an object to capture that object as its &lt;code&gt;this&lt;/code&gt; context. However, this isn't the case.&lt;/p&gt;

&lt;p&gt;Arrow functions are not context-aware; they don't possess their own &lt;code&gt;this&lt;/code&gt;. Instead, they resolve &lt;code&gt;this&lt;/code&gt; lexically, which means they inherit the &lt;code&gt;this&lt;/code&gt; from their parent lexical scope. In the global scope, this would typically be the global object.&lt;/p&gt;

&lt;p&gt;For example, in the workshop 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="nx"&gt;workshop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What happened to 'this'?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// undefined What happened to 'this'?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; inside the arrow function does not point to the &lt;code&gt;workshop&lt;/code&gt; object but inherits the &lt;code&gt;this&lt;/code&gt; from the global scope. So, it logs &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Right Tool for Lexical this Behavior
&lt;/h2&gt;

&lt;p&gt;Arrow functions are not a one-size-fits-all solution; they are the right tool for maintaining the &lt;code&gt;this&lt;/code&gt; context from their parent scope. For instance, when you need to pass a function as a callback to methods like &lt;code&gt;setTimeout&lt;/code&gt;, using an arrow function ensures that &lt;code&gt;this&lt;/code&gt; remains consistent with the &lt;code&gt;context&lt;/code&gt; in which it's defined.&lt;br&gt;
Let's consider a scenario:&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;workshop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;workshop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Still no 'this'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// undefined Still no 'this'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we explicitly use &lt;code&gt;call&lt;/code&gt; to set the &lt;code&gt;this&lt;/code&gt; context to the &lt;code&gt;workshop&lt;/code&gt; object. However, the arrow function still resolves &lt;code&gt;this&lt;/code&gt; lexically, ignoring the provided context. It logs &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;The key takeaway here is that arrow functions are not a one-size-fits-all solution. While they excel in certain situations, such as when you require &lt;strong&gt;lexical this&lt;/strong&gt; behaviour, they may not be the best choice for every use case. It's crucial to understand their behaviour and limitations fully. By doing so, you can wield arrow functions effectively, leveraging their strengths while avoiding their potential pitfalls. Ultimately, mastering this powerful feature will help you become a more proficient and versatile JavaScript developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 8)? Binding</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Thu, 26 Oct 2023 10:24:02 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-8-binding-4kn</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-8-binding-4kn</guid>
      <description>&lt;p&gt;In JavaScript, the &lt;code&gt;this&lt;/code&gt; keyword is a powerful, yet sometimes perplexing, element. Understanding how it behaves is crucial for writing effective and maintainable code. We will discuss four essential ways to invoke a function and how each method affects the binding of &lt;code&gt;this&lt;/code&gt;. We will explore these methods, understand their implications, and provide code examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;p&gt;Function Invocation and the "this" Keyword&lt;br&gt;
Implicit Binding&lt;br&gt;
Explicit Binding with .call and .apply&lt;br&gt;
The Challenge of Losing this Binding&lt;br&gt;
Hard Binding&lt;br&gt;
Striking a Balance&lt;br&gt;
The new Keyword&lt;br&gt;
Default Binding&lt;br&gt;
Binding Precedence&lt;br&gt;
Conclusion&lt;br&gt;
Sources&lt;/p&gt;
&lt;h2&gt;
  
  
  Function Invocation and the "this" Keyword &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;When you invoke a function in JavaScript, the behavior of the &lt;code&gt;this&lt;/code&gt; keyword depends on the context in which the function is called. Let's explore the four primary methods of function invocation.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Implicit Binding &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Implicit binding is one of the most common and intuitive ways to handle the &lt;code&gt;this&lt;/code&gt; keyword. It relies on the call site, which is the object used to invoke a 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workshop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;ask&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="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome to the &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;topic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workshop!`&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;workshop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Welcome to the JavaScript workshop!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;this&lt;/code&gt; keyword within the &lt;code&gt;ask&lt;/code&gt; function refers to the &lt;code&gt;workshop&lt;/code&gt; object. Implicit binding associates &lt;code&gt;this&lt;/code&gt; with the object that triggers the function.&lt;/p&gt;

&lt;p&gt;Implicit binding is a powerful mechanism for sharing behavior among different contexts. You can define a function once and use it across multiple objects, allowing flexibility and code reuse.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Explicit Binding with .call and .apply &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Explicit binding provides control over the this context of a function using the .call and .apply methods. These methods take the desired this context as their first argument.&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;const&lt;/span&gt; &lt;span class="nx"&gt;workshop1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workshop2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;React&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="nx"&gt;ask&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome to the &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;topic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workshop!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;workshop1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Welcome to the JavaScript workshop!&lt;/span&gt;
&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;workshop2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Welcome to the React workshop!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With explicit binding, you explicitly specify the this context, allowing you to share functions across different objects but with precise control.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implicit Sharing of Behavior &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Implicit binding enables developers to share behavior across different contexts by referencing a single function. For 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workshop1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workshop2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;React&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="nx"&gt;ask&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome to the &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;topic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workshop!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;workshop1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;workshop2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;workshop1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Welcome to the JavaScript workshop!&lt;/span&gt;
&lt;span class="nx"&gt;workshop2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Welcome to the React workshop!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario, you share a single &lt;code&gt;ask&lt;/code&gt; function between &lt;code&gt;workshop1&lt;/code&gt; and &lt;code&gt;workshop2&lt;/code&gt;, invoking it with distinct contexts.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Challenge of Losing &lt;code&gt;this&lt;/code&gt; Binding &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;One challenge in JavaScript is losing the &lt;code&gt;this&lt;/code&gt; binding when you pass a function around. Consider the following 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;delayedAsk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&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="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome to the &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;topic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workshop!`&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workshop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;delayedAsk&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Welcome to undefined workshop!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; context inside the setTimeout callback function is not bound to the workshop object. Instead, it defaults to the global context, resulting in undefined.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introducing Hard Binding &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To address the issue of losing the &lt;code&gt;this&lt;/code&gt; binding, developers often use a technique called "hard binding". This technique ensures that a function's &lt;code&gt;this&lt;/code&gt; context remains fixed, no matter how it is called. The &lt;code&gt;.bind&lt;/code&gt; method allows you to create a new function with a specific &lt;code&gt;this&lt;/code&gt; 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;delayedAsk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&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="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome to the &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;topic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workshop!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nx"&gt;bind&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="c1"&gt;// Hard binding to the workshop object&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workshop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;delayedAsk&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Welcome to the JavaScript workshop!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using &lt;code&gt;.bind(this)&lt;/code&gt;, you force the &lt;code&gt;this&lt;/code&gt; context within the &lt;code&gt;setTimeout&lt;/code&gt; callback to always reference the &lt;code&gt;workshop&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;This approach provides predictability but sacrifices some of the flexibility inherent in JavaScript.&lt;/p&gt;

&lt;p&gt;Hard binding is valuable when you need consistent behaviour across function invocations, but it should be used thoughtfully to balance predictability and flexibility.&lt;/p&gt;

&lt;h3&gt;
  
  
  Striking a Balance &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The choice between flexible and predictable &lt;code&gt;this&lt;/code&gt; binding is not accidental; it is intentional. Implicit binding offers flexibility, allowing you to share behavior among different contexts. However, there are scenarios where predictability is essential. The key is to strike a balance:&lt;/p&gt;

&lt;p&gt;Use implicit or explicit binding when flexibility is required and different contexts are beneficial.&lt;br&gt;
Utilize hard binding when you need predictability, ensuring that the &lt;code&gt;this&lt;/code&gt; context remains fixed.&lt;br&gt;
However, overusing hard binding may lead to a less maintainable and rigid codebase. Strive for a balance that aligns with your project's requirements, offering the right mix of flexibility and predictability for your specific use cases.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. The &lt;code&gt;new&lt;/code&gt; Keyword &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword is commonly misunderstood as a &lt;code&gt;class&lt;/code&gt; instantiation operator. However, it's not inherently tied to &lt;code&gt;class&lt;/code&gt;es. Its true role is to invoke a function with specific behaviours. When you use &lt;code&gt;new&lt;/code&gt; with a function, it carries out four distinct tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Creating a New Empty &lt;code&gt;Object&lt;/code&gt;:&lt;/strong&gt; &lt;code&gt;new&lt;/code&gt; instantly generates a new empty &lt;code&gt;object&lt;/code&gt;. This is the object that the function will operate on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Linking to Another Object:&lt;/strong&gt; The &lt;code&gt;new&lt;/code&gt; keyword connects the newly created object to another object (to the prototype).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Invoking the Function:&lt;/strong&gt; The function specified after &lt;code&gt;new&lt;/code&gt; is called. However, the function is executed with its &lt;code&gt;this&lt;/code&gt; keyword bound to the new object, not the linked one. This is a crucial distinction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Handling Return Values:&lt;/strong&gt; After executing the function, if it doesn't return its own object explicitly, the &lt;code&gt;new&lt;/code&gt; keyword assumes that the function intends to return &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Deconstructing the new Keyword
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword is deceptively simple, as it essentially hijacks the constructor function to accomplish these four critical tasks. The function's implementation is secondary to the actions carried out by new. Even if you use new with an entirely empty function, it still executes these four tasks. In essence, it's the new keyword that does the heavy lifting, not the function itself.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example of Using the &lt;code&gt;new&lt;/code&gt; Keyword
&lt;/h3&gt;

&lt;p&gt;Here's an illustrative example of how the &lt;code&gt;new&lt;/code&gt; keyword works with a constructor 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;topic&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;topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jsWorkshop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Workshop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jsWorkshop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: JavaScript&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;new&lt;/code&gt; keyword creates a new object, links it to &lt;code&gt;Workshop&lt;/code&gt;, invokes the &lt;code&gt;Workshop&lt;/code&gt; function with the &lt;code&gt;this&lt;/code&gt; context set to the new object, and implicitly returns the new object.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Default Binding &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The fourth way to bind &lt;code&gt;this&lt;/code&gt; is the fallback, called default binding. If none of the other three methods apply, the &lt;code&gt;this&lt;/code&gt; keyword can default to either the &lt;code&gt;global object&lt;/code&gt; (in &lt;code&gt;non-strict mode&lt;/code&gt;) or &lt;code&gt;undefined&lt;/code&gt; (in &lt;code&gt;strict mode&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;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kyle&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="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&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="nx"&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;teacher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;question&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="nx"&gt;askAgain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&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="nx"&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;teacher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;question&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Whats the non-strict-mode default?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Kyle Whats the non-strict-mode default?&lt;/span&gt;

&lt;span class="nx"&gt;askAgain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Whats the strict-mode default?&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&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In &lt;code&gt;strict mode&lt;/code&gt;, the default behaviour is to leave &lt;code&gt;this&lt;/code&gt; &lt;code&gt;undefined&lt;/code&gt;, which can cause &lt;code&gt;TypeError&lt;/code&gt; if you try to access properties on it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Binding Precedence
&lt;/h2&gt;

&lt;p&gt;In JavaScript, understanding the binding precedence of the &lt;code&gt;this&lt;/code&gt; keyword is essential to determine what it references when a function is invoked. Whether you're working with complex call sites or trying to unravel the intricacies of a particular code snippet, knowing the order of precedence for binding rules can be incredibly helpful. Consider this 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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;workshop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;kyle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&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="nx"&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;teacher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;question&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;workshop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;workshop&lt;/span&gt;&lt;span class="p"&gt;))(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What does this do?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// undefined What does this do?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A straightforward guide helps establish this order of precedence when determining the value of &lt;code&gt;this&lt;/code&gt; in a function. Let's break down these rules step by step:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. New Keyword&lt;/strong&gt;&lt;br&gt;
The first rule of precedence asks one simple question: Was the function called with the &lt;code&gt;new&lt;/code&gt; keyword? If the answer is yes, the &lt;code&gt;this&lt;/code&gt; keyword will point to the newly created object. This rule takes precedence over all others, making the &lt;code&gt;new&lt;/code&gt; keyword a powerful way to explicitly define the context for a function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Call or Apply&lt;/strong&gt;&lt;br&gt;
The second rule comes into play when the function is invoked using &lt;code&gt;call&lt;/code&gt; or &lt;code&gt;apply&lt;/code&gt;. It's important to note that &lt;code&gt;bind&lt;/code&gt; is a sub-rule of this second rule, as it internally relies on &lt;code&gt;apply&lt;/code&gt;. When &lt;code&gt;call&lt;/code&gt; or &lt;code&gt;apply&lt;/code&gt; is used, the &lt;code&gt;context&lt;/code&gt; object specified as the first argument becomes the value of &lt;code&gt;this&lt;/code&gt;. This rule allows you to directly control the context of a function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Context Object&lt;/strong&gt;&lt;br&gt;
The third rule considers whether the function is called on a &lt;code&gt;context&lt;/code&gt; object, like &lt;code&gt;workshop.ask&lt;/code&gt; in the example. If the function is invoked within a specific object, the &lt;code&gt;this&lt;/code&gt; keyword will reference that object. This rule provides a simple and intuitive way to manage &lt;code&gt;context&lt;/code&gt; within object methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Default Binding&lt;/strong&gt;&lt;br&gt;
When none of the previous three rules apply, the fourth and final rule comes into effect. By default, the &lt;code&gt;this&lt;/code&gt; keyword references the &lt;code&gt;global object&lt;/code&gt;, except in &lt;code&gt;strict mode&lt;/code&gt;. In &lt;code&gt;strict mode&lt;/code&gt;, it defaults to &lt;code&gt;undefined&lt;/code&gt;. This rule serves as a fallback when other binding rules don't match.&lt;/p&gt;

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

&lt;p&gt;Understanding the behaviour of the &lt;code&gt;this&lt;/code&gt; keyword and the four ways to invoke functions in JavaScript is fundamental to writing efficient and maintainable code. By choosing the right method for your specific use case, you can harness the full power of JavaScript's dynamic and flexible nature while ensuring predictable behaviour where needed.&lt;/p&gt;

&lt;p&gt;Ultimately, JavaScript offers a variety of tools for function invocation and this binding, allowing you to tailor your code to the demands of your projects. The key is to strike the right balance between predictability and flexibility, enabling you to create robust and versatile JavaScript applications.&lt;/p&gt;

&lt;p&gt;But let's reset our minds back. The question that we set out to ask is, &lt;strong&gt;if I have a &lt;code&gt;this&lt;/code&gt;-aware function, how do I know what its &lt;code&gt;this&lt;/code&gt; keyword points at?&lt;/strong&gt; And our strong temptation is, we want to assume that we can just answer that by looking at the function. What we've now seen is that &lt;strong&gt;there's no way to look at the function to answer that question&lt;/strong&gt;. &lt;strong&gt;You have to look at the call site&lt;/strong&gt;. &lt;strong&gt;You have to look at how the function's being called&lt;/strong&gt;. Because every time it gets called, the how of the call controls what the &lt;code&gt;this&lt;/code&gt; keyword will point at.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 7)? `this`</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Thu, 26 Oct 2023 10:21:19 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-7-this-a3b</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-7-this-a3b</guid>
      <description>&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword in JavaScript has long been a source of confusion and frustration for many developers. It often behaves in ways that seem unintuitive, leading to a sense of bewilderment. However, this is an incredibly powerful mechanism that allows for dynamic context switching in your code. To master it, we need to understand how it works in JavaScript, free from preconceived notions based on other programming languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt;Lexical Scope vs. Dynamic Scope&lt;br&gt;
The Pitfall of Comparisons&lt;br&gt;
The Basics: &lt;code&gt;this&lt;/code&gt; Represents the Execution Context&lt;br&gt;
Flexibility and Reusability through &lt;code&gt;this&lt;/code&gt;&lt;br&gt;
Explicitly Setting the Context&lt;br&gt;
The Four Ways to Invoke Functions&lt;br&gt;
Conceptualizing Different Buildings&lt;br&gt;
Conclusion&lt;br&gt;
Sources&lt;/p&gt;
&lt;h2&gt;
  
  
  Lexical Scope vs. Dynamic Scope
&lt;/h2&gt;

&lt;p&gt;Before diving into function invocation, it's crucial to understand two fundamental concepts related to JavaScript's scoping: lexical scope and dynamic scope.&lt;/p&gt;
&lt;h3&gt;
  
  
  Defining Lexical Scope:
&lt;/h3&gt;

&lt;p&gt;Lexical scope, also known as static scope, refers to the idea that scopes are nested within each other. In this model, a compiler, parser, or processor determines all scopes during the compilation phase, well before the code is executed. This means that the structure of scopes and variable access is decided at author time, during the development of the code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compile-Time Decisions:&lt;/strong&gt;&lt;br&gt;
Lexical scope is closely related to compile time. When we talk about lexical scope, it means that decisions about variable access, function nesting, and scope hierarchy are made at compile time. These decisions are not influenced by what happens at runtime during program execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fixed and Predictable Scope:&lt;/strong&gt;&lt;br&gt;
The variable references in lexical scope are determined during compilation and do not change during runtime. This means that, at compile time, it's known which variables belong to which scopes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimizability:&lt;/strong&gt;&lt;br&gt;
Lexical scope is highly optimizable because variable access is determined at compile time. There's no need for the runtime environment to continually look up variable references. Even in cases where variables may not be known at compile time, they can be resolved during the initial execution and remain consistent thereafter.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's important to note that, despite some nuances, JavaScript fundamentally operates with lexical scope.&lt;/p&gt;
&lt;h2&gt;
  
  
  Dynamic Scope
&lt;/h2&gt;

&lt;p&gt;Dynamic scope, in contrast, allows runtime conditions to affect scope and variable access. While this model is rare in JavaScript, it's more common in languages like Bash scripting. Dynamic scope offers flexibility, but it can be challenging for developers accustomed to lexical scope.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility of Dynamic Scope:&lt;/strong&gt;
Dynamic scope allows a function to have different variable resolutions when called from different places. This flexibility can be seen as both powerful and potentially chaotic. While it may seem unfamiliar to developers accustomed to lexical scope, it offers the potential for extensive code reuse.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In JavaScript, you can achieve a level of flexibility similar to dynamic scope using various &lt;code&gt;this&lt;/code&gt; binding techniques, as we'll see shortly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;They are simply different models for scoping. Lexical scope is fixed and predictable, determined at author time during compilation, while dynamic scope is runtime-dependent and flexible.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  The Pitfall of Comparisons
&lt;/h2&gt;

&lt;p&gt;One common mistake is trying to relate JavaScript's &lt;code&gt;this&lt;/code&gt; to the behavior of this in other languages. This can lead to confusion and hinder your ability to understand and utilize JavaScript effectively. It's essential to approach &lt;code&gt;this&lt;/code&gt; as a unique JavaScript feature.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Basics: &lt;code&gt;this&lt;/code&gt; Represents the Execution Context &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword in JavaScript represents the execution context for a function. Unlike many other languages where &lt;code&gt;this&lt;/code&gt; refers to the context of the function's definition, in JavaScript, it's &lt;strong&gt;all about how the function is called&lt;/strong&gt;. It's not determined by where a function is defined but by how it's called This means that this is dynamic and is determined by the context of the function call.&lt;/p&gt;

&lt;p&gt;Here's a simple example to illustrate this point:&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="nx"&gt;sayHello&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &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="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Hello, Alice!&lt;/span&gt;
&lt;span class="nx"&gt;person2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Hello, Bob!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;sayHello&lt;/code&gt; function's &lt;code&gt;this&lt;/code&gt; context dynamically changes based on the object from which it is called.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flexibility and Reusability through &lt;code&gt;this&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript's &lt;code&gt;this&lt;/code&gt; behavior provides a level of flexibility and reusability in your code that might seem counterintuitive at first. By changing the context of a function when invoking it, you can reuse the same function with different objects or settings.&lt;/p&gt;

&lt;p&gt;For instance:&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;const&lt;/span&gt; &lt;span class="nx"&gt;teacher1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;teacher2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&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="nx"&gt;introduce&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`I am &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="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, your teacher.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;introduce&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;teacher1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: I am Alice, your teacher.&lt;/span&gt;
&lt;span class="nx"&gt;introduce&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;teacher2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: I am Bob, your teacher.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;call&lt;/code&gt; method allows you to alter the context in which the &lt;code&gt;introduce&lt;/code&gt; function operates. This dynamic flexibility can be incredibly powerful in real-world JavaScript applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explicitly Setting the Context
&lt;/h2&gt;

&lt;p&gt;In JavaScript, you can explicitly set the context in which a function operates using methods like &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, and &lt;code&gt;bind&lt;/code&gt;. Each of these methods allows you to change the &lt;code&gt;this&lt;/code&gt; context for a given function.&lt;/p&gt;

&lt;p&gt;Consider these examples:&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;const&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&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="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="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="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Hello, Alice!&lt;/span&gt;
&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&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="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;// Outputs: Hi, Alice!&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greetToAlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&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="nx"&gt;greetToAlice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hey&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Outputs: Hey, Alice!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; are used for immediate function invocation, and you can pass &lt;code&gt;arguments&lt;/code&gt; directly.&lt;br&gt;
&lt;code&gt;bind&lt;/code&gt; creates a new function with the specified context, which can be invoked later.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Four Ways to Invoke Functions
&lt;/h2&gt;

&lt;p&gt;In JavaScript, there are four primary ways to invoke functions, each influencing how the &lt;code&gt;this&lt;/code&gt; keyword behaves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function Invocation:&lt;/strong&gt; Invoking a function as &lt;code&gt;someFunction()&lt;/code&gt; uses the global context.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method Invocation:&lt;/strong&gt; Invoking a method as &lt;code&gt;object.someMethod()&lt;/code&gt; sets &lt;code&gt;this&lt;/code&gt; to the &lt;code&gt;object&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constructor Invocation:&lt;/strong&gt; Using &lt;code&gt;new SomeConstructor()&lt;/code&gt; assigns &lt;code&gt;this&lt;/code&gt; to the new instance created by the &lt;code&gt;constructor&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Indirect Invocation:&lt;/strong&gt; Employing &lt;code&gt;someFunction.call(context, args)&lt;/code&gt; specifies the context explicitly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conceptualizing Different Buildings
&lt;/h2&gt;

&lt;p&gt;A useful metaphor for understanding this in JavaScript is to think of it as deciding which building to enter. Just as you'd ask which building contains the desired office, you must identify the specific context to determine how this will behave.&lt;/p&gt;

&lt;h2&gt;
  
  
  conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in JavaScript is a dynamic and powerful concept, designed to enable flexibility and reusability in your code. While it can be a source of confusion, a deeper understanding of how it works and some practical experience with different function invocations will empower you to harness its capabilities effectively. Shedding preconceived notions about &lt;code&gt;this&lt;/code&gt; from other programming languages is the first step towards mastering it in the world of JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 6)? Module</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Thu, 26 Oct 2023 10:19:43 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-6-module-48h0</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-6-module-48h0</guid>
      <description>&lt;p&gt;The module pattern is a versatile and powerful tool in JavaScript for organizing and structuring your code. At its core, it allows you to create self-contained units called modules, which bundle related data and functions. These modules are fundamental to writing clean, maintainable, and secure code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Lexical Scope and Closure&lt;/li&gt;
&lt;li&gt;The Namespace Pattern Explained&lt;/li&gt;
&lt;li&gt;The Namespace Pattern's Shortcomings&lt;/li&gt;
&lt;li&gt;What is Encapsulation?&lt;/li&gt;
&lt;li&gt;Encapsulation in the Module Pattern&lt;/li&gt;
&lt;li&gt;Classic/Revealing Module Pattern&lt;/li&gt;
&lt;li&gt;IIFE&lt;/li&gt;
&lt;li&gt;The Relationship with Encapsulation&lt;/li&gt;
&lt;li&gt;Encapsulation and Closure&lt;/li&gt;
&lt;li&gt;A Module's Purpose&lt;/li&gt;
&lt;li&gt;Factory Functions for Versatile Modules&lt;/li&gt;
&lt;li&gt;The Significance of the Module Pattern: A Modern Necessity&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Sources&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Lexical Scope and Closure
&lt;/h2&gt;

&lt;p&gt;Before we dive into modules, it's crucial to understand two foundational concepts: &lt;strong&gt;lexical scope&lt;/strong&gt; and &lt;strong&gt;closure&lt;/strong&gt;. Lexical scope deals with how variable names are resolved in nested functions, and closure is the mechanism that allows inner functions to capture and maintain closed-over variables from their containing scope. Here's a detailed 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outerVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am from the outer function&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="nx"&gt;inner&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outerVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Accessing outerVar due to closure&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;inner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;innerFn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;innerFn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: "I am from the outer function"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the inner function &lt;code&gt;inner&lt;/code&gt; can still access &lt;code&gt;outerVar&lt;/code&gt; even after &lt;code&gt;outer&lt;/code&gt; has completed execution, thanks to closure.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Namespace Pattern Explained
&lt;/h2&gt;

&lt;p&gt;In the Namespace Pattern, developers collect functions, variables, and objects within an object, often with a specific name relevant to the domain of the code. For example, in a web application, you might use a namespace like &lt;code&gt;MyApp&lt;/code&gt; to group various functionalities related to your application.&lt;/p&gt;

&lt;p&gt;Here's a basic example of the Namespace Pattern:&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;MyApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;formatDate&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;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Format the date&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="c1"&gt;// More utility functions&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Various modules&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 this example, &lt;code&gt;MyApp&lt;/code&gt; is a namespace that encapsulates both utility functions and application modules.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Namespace Pattern's Shortcomings
&lt;/h2&gt;

&lt;p&gt;While the Namespace Pattern is effective in preventing global scope pollution, it lacks several characteristics that define a true module:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why the Namespace Pattern Is Not a Module&lt;/strong&gt;&lt;br&gt;
The primary reason the Namespace Pattern is not considered a module is that it fails to achieve the level of encapsulation and data hiding that modules provide. Modules are designed to offer a clear distinction between public and private components, which the Namespace Pattern lacks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lack of Encapsulation:&lt;/strong&gt;&lt;br&gt;
In the Namespace Pattern, there is no clear distinction between public and private components. Everything placed within the namespace is accessible, which makes it challenging to achieve proper encapsulation. In a true module, you can hide certain parts of the code, allowing only specific functions and variables to be exposed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Data Hiding:&lt;/strong&gt;&lt;br&gt;
Modules emphasize data hiding, meaning certain data is hidden from the outside world. The Namespace Pattern doesn't inherently provide this level of data protection, as everything within the namespace is exposed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limited Control Over Visibility:&lt;/strong&gt;&lt;br&gt;
In a true module, you have explicit control over what parts of your code are publicly accessible and what remains private. The Namespace Pattern lacks the concept of private and public members.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Risk of Interference:&lt;/strong&gt; Without proper encapsulation, different parts of the code may inadvertently interfere with each other, leading to unexpected side effects. Modules aim to isolate different sections of your application to minimize interference.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limited Reusability:&lt;/strong&gt;&lt;br&gt;
The Namespace Pattern does not inherently promote reusability of code since there is no strict isolation of code components. In a module, you can easily reuse the same module in different parts of your application.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What is Encapsulation?
&lt;/h2&gt;

&lt;p&gt;Encapsulation is the practice of bundling data (variables) and the methods (functions) that operate on that data into a single unit. This unit is typically referred to as an object in object-oriented programming (OOP) or a module in JavaScript. Encapsulation involves two fundamental principles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Hiding:&lt;/strong&gt; Encapsulation allows you to hide the internal details of an object or module from the external world. It restricts direct access to the object's internal state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Public and Private Components:&lt;/strong&gt; Encapsulation distinguishes between what is public and what is private. Public components are accessible from the outside, whereas private components are hidden and can only be accessed from within the object or module.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Encapsulation in the Module Pattern
&lt;/h2&gt;

&lt;p&gt;In the module pattern, encapsulation is a core concept. It enables you to structure your code in a way that provides controlled access to your data and functions. Here's how encapsulation is achieved in the module pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Public Interface:&lt;/strong&gt; You define a public interface, which consists of functions and variables that you want to expose to the outside world. These are the parts of your module that other parts of your code can interact with.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Private Members:&lt;/strong&gt; Within the module, you can have variables and functions that are not part of the public interface. These are considered private members, and they are only accessible from within the module itself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Closure:&lt;/strong&gt; Closure is the mechanism through which private members are encapsulated. When you create an inner function inside the module, it has access to the module's scope and can capture and preserve private variables. This means that even after the outer module function has executed, the inner functions maintain access to the private data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's illustrate encapsulation in action with a simple 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;workshopModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Kyle Simpson&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;participants&lt;/span&gt; &lt;span class="o"&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;// Private variable&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addParticipant&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Private function&lt;/span&gt;
    &lt;span class="nx"&gt;participants&lt;/span&gt;&lt;span class="o"&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="nx"&gt;getParticipantCount&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="nx"&gt;participants&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;addParticipant&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Public function&lt;/span&gt;
    &lt;span class="nx"&gt;getParticipantCount&lt;/span&gt; &lt;span class="c1"&gt;// Public function&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workshop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;workshopModule&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;workshop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addParticipant&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;workshop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getParticipantCount&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: 1&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;workshop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;participants&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;teacher&lt;/code&gt;, &lt;code&gt;participants&lt;/code&gt;, and &lt;code&gt;addParticipant&lt;/code&gt; are encapsulated within the module. &lt;code&gt;addParticipant&lt;/code&gt; and &lt;code&gt;getParticipantCount&lt;/code&gt; are part of the public interface and can be accessed from outside the module, while &lt;code&gt;teacher&lt;/code&gt; and &lt;code&gt;participants&lt;/code&gt; remain hidden.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classic/Revealing Module Pattern
&lt;/h2&gt;

&lt;p&gt;The Classic or Revealing Module Pattern is a fundamental technique for creating modules in JavaScript. It builds on the concepts of encapsulation and closure. The pattern allows you to structure your code in a way that exposes only the necessary functions and variables while keeping others hidden. Here's 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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;MyModule&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;privateVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am private&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="nx"&gt;privateFunction&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="nx"&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 is a private function.&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="nx"&gt;publicFunction&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="nx"&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 is a public function.&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="c1"&gt;// Revealing module pattern&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;publicFunction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;publicFunction&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;

&lt;span class="nx"&gt;MyModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publicFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Accessing a public function&lt;/span&gt;
&lt;span class="nx"&gt;MyModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;privateVar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This is undefined as privateVar is hidden&lt;/span&gt;
&lt;span class="nx"&gt;MyModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;privateFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// This will result in an error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this pattern, an &lt;strong&gt;IIFE&lt;/strong&gt; is used to create a private scope where you can define both private and public members. The public members are explicitly returned, exposing only the desired functionality to the outside world.&lt;/p&gt;

&lt;h2&gt;
  
  
  IIFE
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;IIFE&lt;/strong&gt;, or Immediately-Invoked Function Expression, is a JavaScript function that is executed as soon as it is defined. It's commonly used in the module pattern to create a private scope for encapsulating variables and functions. Here's 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="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="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am private&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;function&lt;/code&gt; is invoked immediately after its declaration, and it can be used to create a private environment for encapsulating &lt;code&gt;data&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Relationship with Encapsulation:
&lt;/h2&gt;

&lt;p&gt;The driving force behind both the Classic/Revealing Module Pattern and IIFE is encapsulation. Encapsulation involves bundling data and functions into a single unit and distinguishing between what's public and private. This practice not only promotes clean code but also enhances security and data integrity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encapsulation and Closure
&lt;/h2&gt;

&lt;p&gt;In the context of modules, encapsulation and closure work together to create self-contained units with private and public members. Encapsulation ensures that data and functions are organized within a module, while closure allows private data to be accessible only within the module, even after the outer module function has executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Module's Purpose
&lt;/h2&gt;

&lt;p&gt;One of the primary purposes of a module is to manage shifting state over time. In many applications, data changes and evolves, and modules provide a structured way to handle these changes. By encapsulating state and behavior within a module, you can track the state and ensure that it changes in a controlled manner.&lt;/p&gt;

&lt;p&gt;Consider a module that tracks the state of an online workshop's participants. Through encapsulation, you can hide the internal details and provide functions to add and retrieve participants. The module maintains the state, and the outside world interacts with it through a defined public interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Factory Functions for Versatile Modules
&lt;/h2&gt;

&lt;p&gt;Unlike the revealing module pattern or object literals, factory functions can be used to produce multiple instances of a module, each with its own isolated state. This versatility makes factory functions an excellent choice when you need to create multiple modules with distinct data. Here's 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;createPersonModule&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="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="c1"&gt;// Private variables&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;privateName&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;privateAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Public functions&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getFullName&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="nx"&gt;privateName&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="nx"&gt;getAge&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="nx"&gt;privateAge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Public interface&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;getFullName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;getAge&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createPersonModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createPersonModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getFullName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: "Alice"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getFullName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: "Bob"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAge&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: 30&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAge&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: 25&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;createPersonModule&lt;/code&gt; factory function is used to create two distinct person modules, each with its private state. This approach is incredibly versatile when you need to manage multiple instances of a module.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Significance of the Module Pattern: A Modern Necessity
&lt;/h2&gt;

&lt;p&gt;In modern JavaScript development, the module pattern has become more than just a convenient way to structure code. It has evolved into a necessity for several reasons:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Organization:&lt;/strong&gt; As applications become more complex, organizing code becomes paramount. Modules provide a structured way to group related data and functions together, enhancing code maintainability and readability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Preventing Global Scope Pollution:&lt;/strong&gt; The global scope is a shared space where variables can collide and cause conflicts. Modules encapsulate their code, reducing the risk of global scope pollution and naming conflicts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Modules can be reused across different parts of an application or in entirely different projects. This reusability reduces code duplication and accelerates development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State Management:&lt;/strong&gt; Modules are excellent for managing state, especially in web applications. They allow you to keep track of application state changes and interactions with user interfaces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security:&lt;/strong&gt; By limiting access to specific variables and functions, modules enhance code security. They prevent unauthorized access and modifications of sensitive data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interference Mitigation:&lt;/strong&gt; Modules minimize interference between different parts of an application. Each module operates within its own scope, reducing the risk of unintended side effects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maintenance and Collaboration:&lt;/strong&gt; In a team environment, modules make code maintenance and collaboration more manageable. Developers can work on individual modules without affecting other parts of the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt; As applications grow, modules provide a scalable approach to code management. You can add or update modules without extensive changes to the entire codebase.&lt;/p&gt;

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

&lt;p&gt;In modern JavaScript development, the module pattern has become essential. It enhances code organization, prevents global scope pollution, promotes reusability, and improves state management and security. Modules are invaluable for mitigating interference, simplifying maintenance and collaboration, and ensuring code scalability. Whether you choose the Classic/Revealing Module Pattern, use IIFE, or implement factory functions, understanding encapsulation and closure is vital for mastering JavaScript modules. Incorporate these techniques into your codebase, and you'll be well-equipped to write clean, organized, and maintainable JavaScript code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 5)? Closure</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Wed, 25 Oct 2023 18:11:13 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-5-closure-2-jbi</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-5-closure-2-jbi</guid>
      <description>&lt;p&gt;Closures are a fundamental and powerful concept in JavaScript. They play a crucial role in creating maintainable and efficient code. Understanding closures is essential for every JavaScript developer. In this comprehensive guide, we will delve into the world of closures, from their origins to practical examples and best practices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction to Closure&lt;/li&gt;
&lt;li&gt;Origin of Closures&lt;/li&gt;
&lt;li&gt;Definition of Closure&lt;/li&gt;
&lt;li&gt;What Closure Is&lt;/li&gt;
&lt;li&gt;What Closure Is Not&lt;/li&gt;
&lt;li&gt;
Examples of Closure in JavaScript

&lt;ul&gt;
&lt;li&gt;Private Data with Closure&lt;/li&gt;
&lt;li&gt;Callback Functions&lt;/li&gt;
&lt;li&gt;Timers with Closure&lt;/li&gt;
&lt;li&gt;Looping with Closure&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Closed Over Variables&lt;/li&gt;
&lt;li&gt;Best Practices for Using Closures&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Sources&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction to Closure
&lt;/h2&gt;

&lt;p&gt;Imagine you have a function in JavaScript that references variables outside its own scope. These variables may belong to a different function or even the global scope. When you use this function, it retains access to those external variables. This phenomenon is known as a closure. Closures allow you to create self-contained and reusable functions that can encapsulate data and behavior effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Origin of Closures
&lt;/h2&gt;

&lt;p&gt;The concept of closure is not unique to JavaScript. It has its roots in lambda calculus, a mathematical concept predating the creation of programming languages. Closures have been present in functional programming languages like Lisp for decades. However, their widespread adoption in more general-purpose languages, such as JavaScript, is relatively recent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition of Closure
&lt;/h2&gt;

&lt;p&gt;Defining closure from an academic perspective can be complex. Instead, let's focus on an operational definition that explains the practical use of closures in JavaScript:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Closure is when a function is able to remember and access its lexical scope, including the variables outside of itself, even when that function executes in a different scope.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This definition consists of two essential parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The ability of a function to access its lexical scope.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The preservation of this access even when the function executes in a different scope.&lt;/strong&gt;
These two components collectively form what we refer to as a closure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Closure Is
&lt;/h2&gt;

&lt;p&gt;Closures allow JavaScript developers to create powerful and flexible code. Here are some key attributes of closures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Encapsulation:&lt;/strong&gt; Closures enable the encapsulation of data within functions, leading to cleaner and more organized code.
&lt;strong&gt;Data Privacy:&lt;/strong&gt; Closures protect variables from being directly accessed and modified from outside the function. This privacy enhances security and avoids unintended interference with data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Controlled Access:&lt;/strong&gt; Closures provide controlled access to variables by exposing only the necessary functions, preventing unauthorized changes to the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusability:&lt;/strong&gt; Closures can create self-contained modules that are reusable across different parts of your codebase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Callback Functions:&lt;/strong&gt; Closures are fundamental in handling callback functions, such as event handlers and asynchronous operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Closure Is Not
&lt;/h2&gt;

&lt;p&gt;To understand closures fully, it's crucial to know what they are not:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Capturing Values:&lt;/strong&gt; &lt;strong&gt;Closures do not capture specific values at a particular moment. Instead, they retain access to variables.&lt;/strong&gt; This means the value of a variable is dynamic and can change over time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Duplicates of Variables:&lt;/strong&gt; &lt;strong&gt;Closures do not create copies of variables&lt;/strong&gt;. They maintain a reference to the same variable, which can be modified directly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Object-Oriented Encapsulation:&lt;/strong&gt; While closures offer encapsulation, they do not provide class-based object-oriented encapsulation. JavaScript uses prototypes and classes for that purpose.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Examples of Closure in JavaScript
&lt;/h2&gt;

&lt;p&gt;Let's explore practical examples of how closures are used in JavaScript:&lt;/p&gt;

&lt;h3&gt;
  
  
  Private Data with Closure
&lt;/h3&gt;

&lt;p&gt;Closures can be used to create private data within objects:&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="nx"&gt;createPerson&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;privateName&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;getName&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="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;privateName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;setName&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;newName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;privateName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This would cause an error!&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createPerson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&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="nx"&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="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Alice"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the createPerson function returns an object representing a person. It contains two methods, getName and setName. The privateName variable is not directly accessible from outside the function. The closures within the returned object give us controlled access to the privateName variable while protecting it from direct modification outside the function. Attempting to modify privateName directly from outside the closure would result in an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Callback Functions
&lt;/h3&gt;

&lt;p&gt;Closures are frequently used in callback functions, such as event handlers and asynchronous operations. Here's a simple 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="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="c1"&gt;// Here, the `url` variable is closed over by the arrow functions&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Data fetched from &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Error fetching data from &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://api.example.com/data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the arrow functions inside the &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;catch&lt;/code&gt; methods of the promise close over the &lt;code&gt;url&lt;/code&gt; variable. This allows us to access the &lt;code&gt;url&lt;/code&gt; variable even though the callback functions are executed in a different context, such as when the &lt;code&gt;fetch&lt;/code&gt; operation is complete.&lt;/p&gt;

&lt;h3&gt;
  
  
  Timers with Closure
&lt;/h3&gt;

&lt;p&gt;A common use case for closure is when you set timers using &lt;code&gt;setTimeout&lt;/code&gt; or &lt;code&gt;setInterval&lt;/code&gt;. These functions involve callbacks that reference variables outside the function. Closure allows these callbacks to access the variables even when the outer function has finished execution.&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="nx"&gt;waitASec&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;question&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What is closure?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&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="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&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="nx"&gt;waitASec&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: "What is closure?"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Looping with Closure
&lt;/h3&gt;

&lt;p&gt;One classic example where developers often encounter issues related to closure is in loop structures. Consider this example with a for loop:&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="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&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;i&lt;/span&gt;&lt;span class="o"&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;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="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="nx"&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;i: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;i&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Surprisingly, this code logs &lt;code&gt;"i: 4"&lt;/code&gt; three times. Why? Because there's only one variable &lt;code&gt;i&lt;/code&gt;, and the closures preserve access to that single variable, resulting in the value &lt;code&gt;4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To create separate variables for each iteration, you can use block-scoped variables with &lt;code&gt;let&lt;/code&gt; or rely on JavaScript's automatic variable creation for each iteration (&lt;code&gt;ES6&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Closed Over Variables
&lt;/h3&gt;

&lt;p&gt;Understanding how closures handle variables is crucial. In JavaScript, a closure preserves a reference to the entire scope, not just individual variables. This means that even if you only reference one variable within a closure, the entire scope is retained. As a result, variables that you might not explicitly use can still consume memory. It's important to be aware of this when working with closures to avoid unintended memory leaks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Using Closures
&lt;/h2&gt;

&lt;p&gt;While closures are a powerful tool, they should be used with care. Here are some best practices for working with closures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Keep Closures Simple:&lt;/strong&gt; Avoid creating overly complex closures. They should be easy to understand and maintain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mind Memory Usage:&lt;/strong&gt; Be mindful of memory usage, especially when closures reference large objects. Always release references when they're no longer needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoid Overuse:&lt;/strong&gt; Not every function needs to be a closure. Use them when they genuinely offer advantages in terms of data encapsulation and controlled access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Block Scoping:&lt;/strong&gt; When appropriate, use &lt;code&gt;let&lt;/code&gt; and block-scoped variables to avoid issues related to loop closures.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;In this guide, we've explored the concept of closure in JavaScript, from its origins to practical applications. Closures empower developers to create self-contained functions, encapsulate data, and build reusable code modules. By understanding closures and their best practices, you can write cleaner, more secure, and more efficient JavaScript code. Closures are a powerful tool in the JavaScript developer's toolbox, and mastering them is essential for becoming a proficient JavaScript developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 4)? Hoisting, var, let, const, TDZ</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Wed, 25 Oct 2023 18:05:47 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-4-hoisting-var-let-const-tdz-2d6</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-4-hoisting-var-let-const-tdz-2d6</guid>
      <description>&lt;p&gt;JavaScript, often perceived as an interpreted language due to its runtime execution, is, in fact, a language with a &lt;strong&gt;two-step&lt;/strong&gt; process, including a critical &lt;a href="https://dev.to/samanabbasi/how-javascript-works-part-1-look-at-js-engine-1kkb"&gt;compilation stage&lt;/a&gt;. The compilation process comprises &lt;strong&gt;tokenization/lexing&lt;/strong&gt;, &lt;strong&gt;parsing&lt;/strong&gt;, and actual code compilation, making JavaScript more than just a simple scripting language. This compilation stage is essential to understand the concept of &lt;strong&gt;hoisting&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The Compilation Stage&lt;/li&gt;
&lt;li&gt;The Role of Parsing, AST, and Scope Analysis for Hoisting&lt;/li&gt;
&lt;li&gt;How Does Hoisting Fit into This Process?&lt;/li&gt;
&lt;li&gt;Hoisting with Variable Declarations&lt;/li&gt;
&lt;li&gt;Hoisting with Function Declarations&lt;/li&gt;
&lt;li&gt;
Variable Declarations and Function Declarations

&lt;ul&gt;
&lt;li&gt;Function Declarations&lt;/li&gt;
&lt;li&gt;Variable Declarations&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; Doesn't Hoist?&lt;/li&gt;
&lt;li&gt;Temporal Dead Zone and &lt;strong&gt;TDZ&lt;/strong&gt; Error&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TDZ&lt;/strong&gt; and &lt;code&gt;const&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Difference Between &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;/&lt;code&gt;const&lt;/code&gt; Hoisting&lt;/li&gt;
&lt;li&gt;Function Expressions&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Sources&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Compilation Stage
&lt;/h2&gt;

&lt;p&gt;Before a JavaScript program executes, it goes through a series of phases during the &lt;a href="https://dev.to/samanabbasi/how-javascript-works-part-1-look-at-js-engine-1kkb"&gt;compilation stage&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tokenization/Lexing:&lt;/strong&gt;&lt;br&gt;
The engine breaks the source code into chunks called &lt;code&gt;tokens&lt;/code&gt;. Tokens are the smallest units of a program, like words in natural language.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Parsing:&lt;/strong&gt; The engine analyzes the syntax of the &lt;code&gt;token&lt;/code&gt; stream to create an abstract syntax tree (&lt;code&gt;AST&lt;/code&gt;). The &lt;code&gt;AST&lt;/code&gt; represents the grammatical structure of the code and helps the engine understand its meaning.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compilation:&lt;/strong&gt; During this phase, the engine translates the &lt;code&gt;AST&lt;/code&gt; into executable code, often in the form of bytecode or machine code. This compiled code is optimized for execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Role of Parsing, AST, and Scope Analysis for Hoisting
&lt;/h2&gt;

&lt;p&gt;When a JavaScript program is executed, the first crucial step is the parsing of the source code. Parsing is the process of taking raw JavaScript code and transforming it into an Abstract Syntax Tree (&lt;code&gt;AST&lt;/code&gt;). This tree-like structure represents the grammatical and structural elements of the code.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;AST&lt;/code&gt;, in combination with &lt;strong&gt;&lt;a href="https://dev.to/samanabbasi/how-javascript-works-part-2-the-lexical-scope-mechanism-of-javascript-52jb"&gt;scope analysis&lt;/a&gt;&lt;/strong&gt;, plays a pivotal role in the process of &lt;strong&gt;hoisting&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Scope analysis involves identifying where in the code certain variables and functions are valid or accessible. It is during this analysis that JavaScript engines determine the boundaries of various scopes in your code.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How Does Hoisting Fit into This Process?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Hoisting&lt;/strong&gt; is not some mystical mechanism but rather a direct consequence of this parsing and scope analysis. It is essentially the act of re-arranging variable and function declarations and attaching them to the top of their appropriate scopes.&lt;/p&gt;

&lt;p&gt;Let's break it down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Variable Declarations:&lt;/strong&gt;&lt;br&gt;
When you declare a variable using &lt;code&gt;var&lt;/code&gt;, for instance, the JavaScript engine identifies it during the &lt;strong&gt;parsing phase&lt;/strong&gt;. It then hoists this declaration to the top of the current scope, ensuring that the variable can be accessed anywhere within that scope. This means that you can use a variable before it's declared in your code without causing an error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Function Declarations:&lt;/strong&gt; Similarly, &lt;code&gt;function&lt;/code&gt; declarations are also hoisted. The engine recognizes them during parsing, moves them to the top of the current scope, and assigns a reference to the &lt;code&gt;function&lt;/code&gt;. This allows you to call a &lt;code&gt;function&lt;/code&gt; before it's formally defined in your code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By performing this hoisting process based on the information gathered from parsing and scope analysis, JavaScript ensures that variables and functions are accessible where they should be. This is why hoisting is often described as just a matter of re-arranging these declarations to the top of their appropriate scopes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hoisting with Variable Declarations
&lt;/h2&gt;

&lt;p&gt;Let's start with variable declarations. Consider this 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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="c1"&gt;// Outputs: undefined&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="c1"&gt;// Outputs: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The magic happens here! Despite trying to &lt;code&gt;console.log&lt;/code&gt; the variable &lt;code&gt;a&lt;/code&gt; before it's declared, JavaScript doesn't throw an error. Instead, it hoists the variable declaration to the top of the current scope, making it available but &lt;code&gt;uninitialized&lt;/code&gt;. The code is effectively treated as if written 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;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Declaration is hoisted&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="c1"&gt;// Outputs: undefined&lt;/span&gt;
&lt;span class="nx"&gt;a&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;// Initialization still happens where you originally defined it&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="c1"&gt;// Outputs: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Hoisting with Function Declarations
&lt;/h2&gt;

&lt;p&gt;Now, let's see how function declarations are hoisted:&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;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: "Hello, World!"&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sayHello&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, we're calling the &lt;code&gt;sayHello&lt;/code&gt; function before its actual declaration. JavaScript hoists the function declaration to the top of the current scope, making it accessible before its appearance in the code. The code behaves as if it were written 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;function&lt;/span&gt; &lt;span class="nx"&gt;sayHello&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, World!&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="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: "Hello, World!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This demonstrates how &lt;code&gt;function&lt;/code&gt; declarations are hoisted in JavaScript, allowing them to be used before they are formally defined in the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variable Declarations and Function Declarations
&lt;/h2&gt;

&lt;p&gt;To grasp the nuances of hoisting, it's essential to differentiate between variable declarations and function declarations. The behavior differs significantly between these two constructs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Declarations
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Function declarations are hoisted entirely, encompassing both the declaration and initialization phases&lt;/strong&gt;. This means that when a function is declared using the &lt;code&gt;function&lt;/code&gt; keyword, it is effectively recognized as a function declaration. As a unique feature, function declarations don't require an assignment operator (&lt;code&gt;=&lt;/code&gt;) since the JavaScript engine comprehends that they are meant to create a function. This hoisting behavior of function declarations aligns with the rules set forth by the ECMAScript language standard.&lt;/p&gt;

&lt;p&gt;The rationale behind hoisting function declarations in their entirety is grounded in the behavior specified in the ECMAScript standard. In JavaScript, functions are considered &lt;strong&gt;"first-class citizens,"&lt;/strong&gt; signifying that they are treated as objects and can be referenced much like any other variable or value. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Benefits of Function Hoisting:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This full hoisting enables the calling of a function before its formal declaration within the code, fostering flexibility in code organization. It also contributes to more readable code, particularly in situations where functions invoke one another.&lt;/p&gt;

&lt;p&gt;This consistency in hoisting function declarations is a fundamental aspect of JavaScript's design and execution model. It ensures reliable and predictable behavior across various JavaScript engines and environments, aligning with the ECMAScript language standard.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variable Declarations
&lt;/h3&gt;

&lt;p&gt;On the other hand, variable declarations exhibit a contrasting hoisting behavior. While they are indeed hoisted, only the declaration part is elevated to the top of the current scope. The initialization of variables, however, remains at its original position in the code. This variance in hoisting behavior distinguishes variable declarations from function declarations.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;let&lt;/code&gt; Doesn't Hoist? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Contrary to common belief, &lt;code&gt;let&lt;/code&gt; does hoist, but it exhibits a distinct behavior compared to &lt;code&gt;var&lt;/code&gt;. In the case of &lt;code&gt;var&lt;/code&gt;, variables are hoisted to the entire function scope and are initialized to &lt;code&gt;undefined&lt;/code&gt; at the start of the function's execution. However, when it comes to &lt;code&gt;let&lt;/code&gt;, it does hoist, but it doesn't get &lt;code&gt;initialized&lt;/code&gt; during the hoisting phase. Instead, it enters what is known as the Temporal Dead Zone (&lt;code&gt;TDZ&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Temporal Dead Zone and &lt;strong&gt;TDZ&lt;/strong&gt; Error
&lt;/h2&gt;

&lt;p&gt;The Temporal Dead Zone (&lt;code&gt;TDZ&lt;/code&gt;) is a state where &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables exist but remain &lt;code&gt;uninitialized&lt;/code&gt;. If you attempt to access a &lt;code&gt;let&lt;/code&gt; variable before it's explicitly initialized in your code, JavaScript will throw a &lt;code&gt;TDZ&lt;/code&gt; error. This error is a safeguard to ensure that you don't access variables in an unpredictable or incomplete state.&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="nx"&gt;log&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="c1"&gt;// Throws a ReferenceError: Cannot access 'a' // before initialization&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;TDZ&lt;/strong&gt; and &lt;code&gt;const&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The concept of &lt;code&gt;TDZ&lt;/code&gt; primarily arose because of the behavior of &lt;code&gt;const&lt;/code&gt; declarations. If &lt;code&gt;const&lt;/code&gt; variables were allowed to be accessed before they were assigned a value, it would contradict the core principle of &lt;code&gt;const&lt;/code&gt;, which promises immutability and a single assigned value throughout a variable's lifetime. To prevent this, the &lt;code&gt;TDZ&lt;/code&gt; mechanism was introduced.&lt;/p&gt;

&lt;h2&gt;
  
  
  Difference Between &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;/&lt;code&gt;const&lt;/code&gt; Hoisting &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The critical difference in hoisting behavior is that &lt;code&gt;var&lt;/code&gt; variables are initialized to &lt;code&gt;undefined&lt;/code&gt; during hoisting. This means that you can access them right away, even if their assignment happens later in the code. On the other hand, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables don't receive an initial value during hoisting. They enter the &lt;code&gt;TDZ&lt;/code&gt;, and the only way to bring them out of this state is to explicitly initialize them using the &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword within the block scope. This behavior ensures a clear and consistent approach to variable initialization in JavaScript.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;const&lt;/code&gt; keyword works similarly to &lt;code&gt;let&lt;/code&gt; in terms of block scoping but with one crucial difference: it declares variables that cannot be reassigned after they are initialized. This can be particularly useful when you want to ensure that a variable retains its value within a block.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Expressions
&lt;/h2&gt;

&lt;p&gt;In contrast, function expressions do not exhibit the same behavior. They are not hoisted in the same way as function declarations. When a function is defined through an expression (typically by assigning it to a variable), it's a &lt;strong&gt;runtime operation&lt;/strong&gt;, not something handled during the compilation phase. This means that the variable associated with a function expression is indeed hoisted, but the assignment, which gives it a function value, is not hoisted. Therefore, attempting to call a function expression before it is assigned will result in a &lt;code&gt;TypeError&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="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Throws a TypeError&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sayHello&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="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason for this &lt;code&gt;TypeError&lt;/code&gt; is straightforward: &lt;strong&gt;you're trying to execute a variable that, at that point in the code, holds the value &lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt;. In contrast, with function declarations, the engine has a plan to initialize them to &lt;code&gt;undefined&lt;/code&gt; during compilation, which provides a safety net for calling them before their actual declaration.&lt;/p&gt;

&lt;p&gt;In essence, hoisting in JavaScript, whether for function declarations or expressions, is about ensuring that functions are available throughout their scope. However, the way they are initialized and made accessible varies between declarations and expressions, leading to differences in behavior and potential errors like &lt;code&gt;TypeError&lt;/code&gt; when calling an &lt;code&gt;undefined&lt;/code&gt; function expression.&lt;/p&gt;

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

&lt;p&gt;Understanding hoisting in JavaScript is essential for writing reliable and predictable code. Hoisting, facilitated by the parsing and scope analysis process, ensures that variables and functions are accessible where they should be. It plays a crucial role in differentiating between variable and function declarations, the behavior of &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, and the handling of function expressions. This knowledge empowers developers to write clean, organized, and error-free JavaScript code, embracing the intricate, yet powerful, mechanism of hoisting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 3)? Function Scope, Block Scope</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Wed, 25 Oct 2023 15:44:25 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-3-function-scope-block-scope-185n</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-3-function-scope-block-scope-185n</guid>
      <description>&lt;p&gt;In the world of JavaScript and programming, understanding scopes is crucial. Scopes, organized by functions, are the building blocks of your code's structure. But what can we do with this knowledge of scopes? Why is it important, and how can it help us solve real-world coding challenges? Let's dive into these questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Naming Collision Problem&lt;/li&gt;
&lt;li&gt;The Principle of Least Exposure&lt;/li&gt;
&lt;li&gt;The Function Approach&lt;/li&gt;
&lt;li&gt;IIFE Pattern&lt;/li&gt;
&lt;li&gt;A Better Solution: Block Scoping&lt;/li&gt;
&lt;li&gt;Understanding Block Scoping&lt;/li&gt;
&lt;li&gt;Benefits of Block Scoping&lt;/li&gt;
&lt;li&gt;Function Declaration vs Function Expression&lt;/li&gt;
&lt;li&gt;Named Function Expression vs Anonymous Function Expression&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Sources&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Naming Collision Problem
&lt;/h2&gt;

&lt;p&gt;One common challenge in programming is dealing with naming collisions. This occurs when two or more identifiers share the same name within the same scope, potentially leading to ambiguity and conflicts. To mitigate this issue, we can leverage our understanding of scopes.&lt;/p&gt;

&lt;p&gt;To illustrate the significance of scopes, consider a simple scenario. You have a variable, and later in your code, you use that variable, expecting it to still hold the same value. Everything seems fine until another developer, well-intentioned but unaware of the variable's usage, inserts some code in between. The result? A naming collision.&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;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ... (some code)&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this case, a variable named &lt;code&gt;teacher&lt;/code&gt; was unintentionally redeclared in the same scope, causing unexpected behaviour. The problem is not that the variable could be reassigned; it's the naming collision that's the root issue.&lt;/p&gt;

&lt;p&gt;The problem with using &lt;code&gt;const&lt;/code&gt; in this specific context is that &lt;code&gt;const&lt;/code&gt; prevents you from reassigning the variable, but it does not prevent you from declaring a new variable with the same name in the same scope. In other words, &lt;code&gt;const&lt;/code&gt; enforces that the variable assigned is not reassigned within its scope, but it doesn't guard against naming collisions within the same scope.Consider the following 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ... (some code)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Syntax error due to redeclaration&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This code will throw a &lt;code&gt;syntax error&lt;/code&gt; because you're attempting to redeclare a variable that has been declared with &lt;code&gt;const&lt;/code&gt;. In this context, the use of &lt;code&gt;const&lt;/code&gt; is causing a problem because you're not allowed to redeclare &lt;code&gt;teacher&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Principle of Least Exposure
&lt;/h2&gt;

&lt;p&gt;To solve such problems, we turn to the "Principle of Least Exposure" or "Principle of Least Privilege." This principle suggests defaulting to keeping everything private and only exposing what's absolutely necessary. Why is this principle so essential in software engineering?&lt;/p&gt;

&lt;h3&gt;
  
  
  Preventing Naming Collisions
&lt;/h3&gt;

&lt;p&gt;By reducing the surface area for name collisions, we avoid conflicts that arise when two different entities within the same scope try to use the same semantic name for different purposes. Hiding variables within a scope minimizes the chances of naming collisions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Preventing Misuse
&lt;/h3&gt;

&lt;p&gt;When something is hidden within a scope or kept private, it prevents accidental or intentional misuse. Exposing variables publicly often leads to unintended use by other developers, even if they're advised not to use them. Hiding variables restricts their access.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enabling Future Refactoring
&lt;/h3&gt;

&lt;p&gt;Perhaps the most crucial benefit of the principle is its ability to protect against future refactoring issues. When something is exposed publicly, it's almost guaranteed that someone will use it. Consequently, when you want to refactor or change the implementation, you risk breaking the existing code that depends on it. Hiding variables provides the freedom to refactor without worrying about breaking dependent code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Function Approach
&lt;/h2&gt;

&lt;p&gt;One way to apply the principle of least exposure is by creating functions to encapsulate variables and prevent naming collisions. For instance:&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="nx"&gt;anotherTeacherScope&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;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach introduces a new scope, reducing the likelihood of naming collisions. However, it comes with a trade-off. While it resolves the collision problem, it introduces a function with a name in that scope, potentially shifting the naming collision issue rather than solving it.&lt;/p&gt;

&lt;h2&gt;
  
  
  IIFE Pattern
&lt;/h2&gt;

&lt;p&gt;To address this problem more effectively, we need a way to create a scope without introducing a new variable name. This approach should allow us to prevent naming collisions without shifting the problem.&lt;/p&gt;

&lt;p&gt;One potential solution involves understanding how the code execution process works. By breaking down code execution into two steps - retrieving a variable's value and executing it - we can create a scope that doesn't pollute the enclosing scope with additional variable names. Consider the following 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="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="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&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 this code, we've wrapped our code in a function expression and immediately invoked it. While it may look unconventional, it achieves the desired result. It creates a new scope, isolates the variable, and prevents naming collisions without introducing a new variable name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Immediately Invoked Function Expressions (IIFE)
&lt;/h2&gt;

&lt;p&gt;Immediately Invoked Function Expressions (IIFE). An IIFE is an anonymous function expression that is defined and executed immediately. It serves as a temporary scope, providing structure and isolation for your code. IIFEs can be named or anonymous.&lt;/p&gt;

&lt;p&gt;The key advantage of IIFEs is that they allow you to create a scope without polluting the surrounding scope with names. They are often used to handle naming collision issues and provide clarity to your code.&lt;/p&gt;

&lt;p&gt;One common use case is to encapsulate code that should only run once, such as setting up configurations. IIFEs create a temporary context for this code to execute and then disappear.&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="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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Your code here&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;
&lt;strong&gt;Passing Values to IIFEs:&lt;/strong&gt; IIFEs are regular functions, which means you can pass arguments to them. This feature adds flexibility, enabling you to pass values and parameters to your IIFE's scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Avoid Anonymous IIFEs:&lt;/strong&gt; While you can use anonymous IIFEs, it's advisable to give them a name that reflects their purpose. Using meaningful names for your IIFEs improves code readability and debugging. Avoid anonymous function expressions, including IIFEs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  A Better Solution: Block Scoping
&lt;/h2&gt;

&lt;p&gt;When it comes to managing variable scope and preventing naming collisions, JavaScript provides a more robust solution known as block scoping. Unlike traditional variable declarations with &lt;code&gt;var&lt;/code&gt;, block scoping using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; allows developers to create isolated scopes within their code. This approach brings a higher level of precision to the control of variables and is particularly useful in scenarios where you want to confine the scope of variables to specific blocks of code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Block Scoping
&lt;/h2&gt;

&lt;p&gt;Block scoping leverages curly braces &lt;code&gt;{}&lt;/code&gt; to create discrete, limited scopes for your variables. With &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, you can declare variables within a block, ensuring they are only accessible and relevant within that specific block.&lt;/p&gt;

&lt;p&gt;Consider the following 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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&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;let&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Bob&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Alice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, two separate &lt;code&gt;teacher&lt;/code&gt; variables exist—one within the block and one outside of it. The inner &lt;code&gt;teacher&lt;/code&gt; variable, declared with &lt;code&gt;let&lt;/code&gt;, is confined to the block, ensuring it doesn't interfere with the outer variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Block Scoping
&lt;/h2&gt;

&lt;p&gt;Block scoping provides a range of benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Granular Control:&lt;/strong&gt; With block scoping, you can finely control where your variables are accessible. This is invaluable in scenarios where you want to avoid accidental variable interference.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Preventing Naming Collisions:&lt;/strong&gt; Block-scoped variables eliminate the risk of naming collisions within a function or code section. Each block maintains its own scope, ensuring variables don't clash.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improved Code Clarity:&lt;/strong&gt; By using block scoping, you explicitly define where a variable is meant to be used, enhancing the readability and maintainability of your code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced Bugs:&lt;/strong&gt; Naming conflicts and unintentional variable reassignments can lead to bugs and unexpected behavior. Block scoping helps you catch such issues at the development stage.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Function Declaration vs Function Expression
&lt;/h2&gt;

&lt;p&gt;JavaScript provides two primary mechanisms for creating functions: &lt;strong&gt;function declarations&lt;/strong&gt; and &lt;strong&gt;function expressions&lt;/strong&gt;. The choice between these two approaches significantly impacts your code's organization and visibility.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Function Declaration:&lt;/strong&gt;&lt;br&gt;
A function declaration is defined with the &lt;code&gt;function&lt;/code&gt; keyword as the first thing in a statement. It adds the function's &lt;code&gt;name&lt;/code&gt; to the enclosing scope, making it accessible from anywhere within that scope.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Function Expression:&lt;/strong&gt;&lt;br&gt;
A function expression is a more versatile construct. It arises when the &lt;code&gt;function&lt;/code&gt; keyword is not the first thing in a statement. Instead, it's often associated with variables, operators, or parentheses. If it lacks a name, it becomes an &lt;strong&gt;anonymous function expression&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The distinction between these two approaches is pivotal, influencing the scope and accessibility of your functions, ultimately affecting your project's organization and functionality.&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="nx"&gt;teacher&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;myTeacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;anotherTeacher&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;anotherTeacher&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// function teacher() {...}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myTeacher&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// function anotherTeacher() {...}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;anotherTeacher&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// ReferenceError: anotherTeacher is not defined.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;function expressions&lt;/strong&gt; are &lt;code&gt;read-only&lt;/code&gt;: you cannot assign it to other value.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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;clickHnadler&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="p"&gt;{}&lt;/span&gt;
&lt;span class="c1"&gt;// Anonymous Function Expression&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;keyHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myKeyHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//…&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Named Function Expression&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Named Function Expression vs Anonymous Function Expression &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The decision to use a named or anonymous function expression depends on your project's requirements and coding style. Consider the benefits of each:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Named Function Expression&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reliable Self-Reference:&lt;/strong&gt;
Named function expressions provide a reliable self-reference, making them suitable for recursion or self-referential use within the function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event Handling:&lt;/strong&gt; They are useful for event handlers that may need to unbind themselves.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accessing Properties:&lt;/strong&gt; If you need to access properties on the function object, such as its name, named function expressions are more appropriate.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Anonymous Function Expression&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Conciseness:&lt;/strong&gt; Anonymous function expressions offer brevity, which can be beneficial for small, simple functions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readability:&lt;/strong&gt; However, they may sacrifice readability, as developers need to infer the function's purpose solely from its body and context.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To tie these concepts back to scopes, consider how named and anonymous function expressions affect scope management. By choosing one over the other, you can control the visibility and accessibility of your functions, thereby influencing your project's scope organization.&lt;/p&gt;

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

&lt;p&gt;In the world of JavaScript, mastering scopes is akin to wielding a powerful tool for organized and unambiguous coding. Function declarations, function expressions, and IIFEs provide us with the means to structure our code effectively. The Principle of Least Exposure further reinforces the importance of encapsulating variables and preventing naming collisions. Always opt for named function expressions over anonymous ones, and remember that code should be written not only for machines but also for human developers. While arrow functions offer brevity, they can sometimes sacrifice readability, making it vital to choose the right tool for the job. By applying these concepts, you'll write cleaner, more maintainable code, and make your programming journey smoother.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 2)? The lexical scope mechanism of JavaScript</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Wed, 25 Oct 2023 15:41:32 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-2-the-lexical-scope-mechanism-of-javascript-52jb</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-2-the-lexical-scope-mechanism-of-javascript-52jb</guid>
      <description>&lt;p&gt;Understanding &lt;strong&gt;lexical scope&lt;/strong&gt; is essential for mastering JavaScript, as it dictates how variables are accessed and resolved at compile time. This article explores the core principles of &lt;strong&gt;lexical scope&lt;/strong&gt;, delving into the definition, how it shapes variable behavior, and its role in the JavaScript two-pass system. We'll also examine the creation of scopes through functions and blocks and explore key concepts like &lt;strong&gt;shadowing&lt;/strong&gt;, &lt;strong&gt;global variables&lt;/strong&gt;, &lt;strong&gt;strict mode&lt;/strong&gt;, types of errors, &lt;strong&gt;undeclared&lt;/strong&gt; vs. &lt;strong&gt;undefined&lt;/strong&gt; variables, nested scopes, and the role of arguments and parameters in JavaScript functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Content
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Lexical Scope Definition&lt;/li&gt;
&lt;li&gt;JavaScript's Two-Pass System&lt;/li&gt;
&lt;li&gt;Organizing Scopes with Functions and Blocks&lt;/li&gt;
&lt;li&gt;How JavaScript Creates Scopes&lt;/li&gt;
&lt;li&gt;Shadowing&lt;/li&gt;
&lt;li&gt;Global Variables and Strict Mode&lt;/li&gt;
&lt;li&gt;Types of Errors&lt;/li&gt;
&lt;li&gt;Undeclared vs. Undefined&lt;/li&gt;
&lt;li&gt;Nested Scopes&lt;/li&gt;
&lt;li&gt;Arguments and Parameters in JavaScript&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Sources&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Lexical Scope Definition
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Lexical scope&lt;/strong&gt; consists of two key components: &lt;strong&gt;lexical&lt;/strong&gt; and &lt;strong&gt;scope&lt;/strong&gt;. Lexical refers to the &lt;strong&gt;lexing&lt;/strong&gt; phase of compilation, which we discussed in a &lt;a href="https://dev.to/samanabbasi/how-javascript-works-part-1-look-at-js-engine-1kkb"&gt;previous article&lt;/a&gt;. But what exactly is &lt;strong&gt;scope&lt;/strong&gt;?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Scope is the region where JavaScript searches for and accesses variables.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It answers two essential questions:&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Questions
&lt;/h3&gt;

&lt;p&gt;Lexical scope helps answer two key questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What are we looking for?&lt;/strong&gt; Lexical scope is primarily about finding identifiers, such as variables or functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Where are we looking for them?&lt;/strong&gt;&lt;br&gt;
Lexical scope defines the location where identifiers are accessible in your code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In essence, we're primarily searching for identifiers, such as variables and functions, within various scopes in your code. Variables play two roles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Receiving the assignment of a value&lt;/strong&gt; Variables in this role act as &lt;code&gt;targets&lt;/code&gt;, where values are assigned, like &lt;code&gt;x = 42&lt;/code&gt; or &lt;code&gt;myFunc = function func(arguments) {}&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Retrieving a value from the variable&lt;/strong&gt; Variables in this role serve as &lt;code&gt;sources&lt;/code&gt;, where values are retrieved, like &lt;code&gt;console.log(y)&lt;/code&gt; or function calls, such as &lt;code&gt;ask()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During the code's execution, JavaScript engine processes scopes, asking two fundamental questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What is the position of this variable within the scope?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;To which scope does it belong?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  JavaScript's Two-Pass System &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Understanding how JavaScript handles &lt;strong&gt;lexical scope&lt;/strong&gt; involves recognizing that JavaScript is a two-pass system:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;process the code first:&lt;/strong&gt; Javascript processing of &lt;strong&gt;lexical scopes&lt;/strong&gt; and putting all of these identifiers, into their correct scope in the parsing stage where it goes through all of our code, produces this abstract syntax tree (&lt;code&gt;AST&lt;/code&gt;) and produces a plan. This plan is executable code that is handed off to be executed by the other part of the JavaScript engine (Virtual Machine),&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;execute it:&lt;/strong&gt; JavaScript virtual machine, which is embedded inside of the same JavaScript engine. It interprets all that plan and use all that information to execute the code, and one of the things that it interprets is, whenever it enters a scope it creates all the identifiers according to what the plan told it to do and locates memory for them&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Organizing Scopes with Functions and Blocks
&lt;/h2&gt;

&lt;p&gt;In JavaScript, &lt;strong&gt;lexical scope&lt;/strong&gt;s are created when the code encounters &lt;code&gt;functions&lt;/code&gt; and &lt;code&gt;blocks&lt;/code&gt; (curly braces &lt;code&gt;{}&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Functions:&lt;/strong&gt;&lt;br&gt;
When you declare a &lt;code&gt;function&lt;/code&gt;, it creates a new scope. Functions can be nested within other functions, forming a hierarchy of lexical scopes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Blocks:&lt;/strong&gt;&lt;br&gt;
Blocks defined by curly braces &lt;code&gt;{}&lt;/code&gt; in constructs like &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, and &lt;code&gt;while&lt;/code&gt; statements also introduce new lexical scopes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How JavaScript Creates Scopes
&lt;/h2&gt;

&lt;p&gt;When JavaScript encounters variable or function declarations, it creates scopes and places those identifiers within the appropriate scope. Consider the following 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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;kyle&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="nx"&gt;otherClass&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;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;saman&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="nx"&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;Welcome!&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="nx"&gt;ask&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;question&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;why&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&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 this code, you can observe how JavaScript creates scopes and assigns identifiers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variable &lt;code&gt;teacher&lt;/code&gt; is assigned in the global scope.&lt;/li&gt;
&lt;li&gt;Function &lt;code&gt;otherClass&lt;/code&gt; creates a new scope, and a local variable &lt;code&gt;teacher&lt;/code&gt; is assigned in that scope.&lt;/li&gt;
&lt;li&gt;The function &lt;code&gt;ask&lt;/code&gt; also creates a scope, where the variable &lt;code&gt;question&lt;/code&gt; is defined.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In lexically scoped languages like Javascript, all the &lt;strong&gt;lexical scope&lt;/strong&gt;s and identifiers, are determined at &lt;code&gt;compile&lt;/code&gt; time not in run time. It is used at run time but determined at compile time, this behaviour allows the engine to be much more effective in optimising because everything is known and it is fixed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;After all scopes are determined during the parsing stage, JavaScript knows the scope of each identifier. This knowledge helps optimize execution since everything is pre-determined at compile time.The decision about scopes are author's time decisions&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Shadowing
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;When you have multiple variables with the same name at different scopes, it is called shadowing&lt;/strong&gt;. This means the innermost variable with the same name takes precedence when accessing that identifier. For example, if you have a local variable named &lt;code&gt;x&lt;/code&gt; inside a &lt;code&gt;function&lt;/code&gt;, it shadows an outer variable &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Global Variables and Strict Mode
&lt;/h2&gt;

&lt;p&gt;JavaScript provides a mechanism for &lt;strong&gt;dynamic global variables&lt;/strong&gt; (auto globals) that are automatically created if you try to assign to a variable that has never been formally declared. This behaviour occurs when the variable is not declared in any accessible scope. However, auto globals are discouraged in favour of explicitly declared variables as they can lead to performance issues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strict mode&lt;/strong&gt; in JavaScript enforces stricter error handling and helps catch potential issues early in development. It's not always enabled by default but is commonly used in modern development tools and transpilers. Inside &lt;code&gt;class&lt;/code&gt; and &lt;code&gt;ES6 modules&lt;/code&gt;, you don't need to explicitly declare &lt;code&gt;"use strict"&lt;/code&gt;; &lt;code&gt;strict mode&lt;/code&gt; is assumed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Errors
&lt;/h2&gt;

&lt;p&gt;JavaScript can throw various types of errors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TypeError:&lt;/strong&gt;
Occurs when you perform illegal operations with variables, such as attempting to execute &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;, access properties on &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;, or reassign a &lt;code&gt;const&lt;/code&gt; variable. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;TypeError&lt;/code&gt; happens at runtime.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SyntaxError:&lt;/strong&gt;
Happens when there are syntax errors in your code, such as extra parentheses or curly braces, improper dots, or illegal operations with &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt;, like redefining a variable multiple times. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;SyntaxError&lt;/code&gt; occurs during the compile time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ReferenceError:&lt;/strong&gt; This error arises when you try to access a variable that doesn't exist, and JavaScript can't find it. &lt;code&gt;ReferenceError&lt;/code&gt; includes situations like accessing undeclared variables or trying to access variables before they are declared. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;ReferenceError&lt;/code&gt; happens at runtime.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Undeclared vs. Undefined
&lt;/h2&gt;

&lt;p&gt;Understanding the difference between &lt;code&gt;undeclared&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; is crucial in JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Undefined:&lt;/strong&gt; It signifies that a variable exists but currently has no assigned value. It might have never had a value or previously had a value but doesn't at the moment. It represents an empty state, where the value is missing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Undeclared:&lt;/strong&gt; An undeclared variable doesn't exist in any accessible scope. It's essentially unknown in your code, and trying to access it results in a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Nested Scopes
&lt;/h2&gt;

&lt;p&gt;JavaScript's lexical scopes can be envisioned as nested layers, similar to a multi-story building. JavaScript conducts a sequential search for variables, beginning from the current scope (the first floor) and progressively moving to outer scopes. This process mimics a linear search, akin to using an elevator to traverse floors within a building.&lt;/p&gt;

&lt;p&gt;In this conceptual analogy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first floor represents the current scope, where a reference is made.&lt;/li&gt;
&lt;li&gt;The top floor corresponds to the global scope.
The search progresses one floor at a time, ensuring that variables are resolved in a structured and predictable manner. This methodology forms the foundation of JavaScript's lexical scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Arguments and Parameters in JavaScript
&lt;/h2&gt;

&lt;p&gt;In JavaScript, functions play a crucial role in processing data and performing tasks. To understand how functions work, it's essential to grasp the concept of arguments and parameters, which are integral to how functions receive and process data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parameters: Receiving Data
&lt;/h3&gt;

&lt;p&gt;Parameters are the placeholders or variables defined in a function's declaration. They act as targets for the data you pass to the function when you call it. Parameters specify what kind of data the function expects to work with and provide names for accessing that data within the function's scope.&lt;/p&gt;

&lt;p&gt;Here's an example of a function with parameters:&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="nx"&gt;greet&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &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="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function, name is a parameter. It serves as a target for the data you pass when calling the greet function. For instance, if you call greet("Alice"), the parameter name will take on the value "Alice," and the function will greet Alice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arguments: Passing Data
&lt;/h3&gt;

&lt;p&gt;Arguments are the actual data or values you provide when calling a function. These values are assigned to the function's parameters. Arguments are the source of data that the function works with.&lt;/p&gt;

&lt;p&gt;Here's how you pass arguments to the greet function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Bob" is the argument&lt;/span&gt;
&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Carol&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Carol" is the argument&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In these examples, "Bob" and "Carol" are the arguments that get assigned to the name parameter within the greet function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parameters Create Identifiers
&lt;/h3&gt;

&lt;p&gt;It's important to note that when you declare parameters in a function, they formally create identifiers within the function's scope. These identifiers allow the function to access and work with the data you pass as arguments.&lt;/p&gt;

&lt;p&gt;In the greet function 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greet&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &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="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The name parameter is a formal identifier within the function's scope, allowing you to use it to reference the argument you pass when calling the function.&lt;/p&gt;

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

&lt;p&gt;By delving into these fundamental aspects of lexical scope, you'll gain a deeper understanding of JavaScript and how it manages variables, errors, and scope. This knowledge is invaluable for writing efficient, optimized code and solving complex programming challenges.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🚀How JavaScript Works (Part 1)? Look At JS Engine</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Mon, 16 Oct 2023 08:52:39 +0000</pubDate>
      <link>https://forem.com/samabaasi/how-javascript-works-part-1-look-at-js-engine-1kkb</link>
      <guid>https://forem.com/samabaasi/how-javascript-works-part-1-look-at-js-engine-1kkb</guid>
      <description>&lt;ul&gt;
&lt;li&gt;Two Pivotal Phases of a JavaScript Program: Compilation and Execution&lt;/li&gt;
&lt;li&gt;From Code to Bytecode (Executable Code)&lt;/li&gt;
&lt;li&gt;The Role of Compilers in JavaScript: Translating Human Logic into Machine Instructions&lt;/li&gt;
&lt;li&gt;Understanding Compilers&lt;/li&gt;
&lt;li&gt;Lexical Analysis (Lexing or Tokenization)&lt;/li&gt;
&lt;li&gt;Syntax Parsing (Abstract Syntax Tree - AST)&lt;/li&gt;
&lt;li&gt;Bytecode Generation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Have you ever wondered about the magic that transforms your code into the interactive web pages users adore? What truly unfolds behind the scenes when you engage with a web page? Join me on a journey as we unravel JavaScript's secrets, focusing on its powerhouse, the JavaScript engine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Demystifying the JavaScript Engine for Developers:
&lt;/h3&gt;

&lt;p&gt;Every web interaction owes its magic to the JavaScript engine—a mysterious wizard that interprets and executes code to create dynamic web experiences. In this article, we'll embark on a journey to demystify JavaScript, showing how it transforms your code into captivating web features. As developers, understanding this is key to mastering web development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the JavaScript Engine:
&lt;/h3&gt;

&lt;p&gt;Before we venture into the intricacies of JavaScript, let's familiarize ourselves with the star of the show – the JavaScript engine. A JavaScript engine is a formidable piece of software that takes on the formidable task of interpreting and executing JavaScript code. While different web browsers may employ distinct JavaScript engines, they all adhere to common principles and share the same fundamental goals.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two Pivotal Phases of a JavaScript Program: &lt;strong&gt;&lt;code&gt;Compilation&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;Execution&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As we delve deeper into the inner workings of JavaScript, we encounter two pivotal phases that define the lifecycle of a JavaScript program: compilation and execution. These phases dance in perfect harmony, ensuring that your code runs not only efficiently but also reliably.&lt;/p&gt;

&lt;h3&gt;
  
  
  From Code to Bytecode (Executable Code):
&lt;/h3&gt;

&lt;p&gt;Our journey into the heart of JavaScript commences with developers crafting lines of code, each intended to perform specific tasks. Yet, as brilliant as human-readable code may be, browsers are mere machines incapable of deciphering the intricacies of our linguistic marvel. Thus, the transformation of JavaScript code into a format that machines can comprehend is essential. This intermediary format is known as bytecode, a compact, lower-level representation of our high-level code.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flumzrk8lbe25knk1xewk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flumzrk8lbe25knk1xewk.gif" alt="Code to Bytecode"&gt;&lt;/a&gt;&lt;/p&gt;
From Code to Bytecode (Executable Code)


  

&lt;h3&gt;
  
  
  The Role of Compilers in JavaScript: Translating Human Logic into Machine Instructions
&lt;/h3&gt;

&lt;p&gt;To understand the compilation phase of JavaScript comprehensively, it's crucial to grasp the pivotal role played by compilers. A compiler is a fundamental component of JavaScript engines, serving as the bridge that transforms your human-readable code into a form that machines can interpret and execute efficiently.&lt;/p&gt;
&lt;h3&gt;
  
  
  Understanding Compilers:
&lt;/h3&gt;

&lt;p&gt;At its core, a compiler is a type of software that translates source code written in a high-level programming language (in our case, JavaScript) into a lower-level representation known as bytecode or machine code. This transformation involves several essential steps, collectively known as the compilation process.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;1. Lexical Analysis (Lexing or Tokenization):&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The compilation process begins with lexical analysis, also referred to as lexing or tokenization. During this phase, the compiler breaks down your JavaScript code into individual tokens. Tokens are the fundamental building blocks of your code and encompass keywords, variables, operators, and other essential elements. This initial step simplifies the code and makes it more manageable for further analysis.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
Consider the following JavaScript code snippet:&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;helloWorld&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lexical analysis breaks it into the following tokens:&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="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;keyword&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;identifier&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;helloWorld&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;punctuation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;(&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;punctuation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;)&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;punctuation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;identifier&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;console&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;punctuation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;identifier&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;log&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;punctuation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;(&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;punctuation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;)&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;punctuation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Lexical Analysis (Lexing or Tokenization)&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;2.Syntax Parsing (Abstract Syntax Tree - AST):&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Following lexing, the compiler proceeds to syntax parsing. During this phase, it constructs an Abstract Syntax Tree (AST) based on your code's grammatical structure. The AST serves as a structured representation of your code, capturing the hierarchical relationship between different elements.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
For the code snippet&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;helloWorld&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the resulting AST might appear as follows:&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;FunctionDeclaration&lt;/span&gt;
&lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Identifier &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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;helloWorld&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BlockStatement&lt;/span&gt;
    &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
        &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;ExpressionStatement&lt;/span&gt;
            &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;expression&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CallExpression&lt;/span&gt;
                &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="nx"&gt;callee&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MemberExpression&lt;/span&gt;
                &lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Identifier &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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;console&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Identifier &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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;log&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
                &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
                    &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nc"&gt;Literal &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;&lt;code&gt;FunctionDeclaration&lt;/code&gt;&lt;/strong&gt; node signifies the declaration of a function.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;id&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;Identifier&lt;/code&gt;&lt;/strong&gt; (name: "helloWorld") denotes the function name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;params&lt;/code&gt;&lt;/strong&gt;: An empty array &lt;strong&gt;&lt;code&gt;[]&lt;/code&gt;&lt;/strong&gt; signifies that there are no parameters for this function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;body&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;BlockStatement&lt;/code&gt;&lt;/strong&gt; represents the function's body.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;body&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;BlockStatement&lt;/code&gt;&lt;/strong&gt; represents the block of code inside the function.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;type&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;ExpressionStatement&lt;/code&gt;&lt;/strong&gt; signifies an expression statement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;expression&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;CallExpression&lt;/code&gt;&lt;/strong&gt; represents a function call.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;callee&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;MemberExpression&lt;/code&gt;&lt;/strong&gt; represents the member access of the console.log function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;object&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;Identifier&lt;/code&gt;&lt;/strong&gt; (name: "console") is the object (console) on which the function is called.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;property&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;Identifier&lt;/code&gt;&lt;/strong&gt; (name: "log") is the function being called.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;computed&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;false&lt;/code&gt;&lt;/strong&gt; indicates that the property is not computed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;arguments&lt;/code&gt;&lt;/strong&gt;: An array containing a single element:&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Literal&lt;/code&gt;&lt;/strong&gt; (value: "Hello World!") is the argument passed to the console.log function, a string literal with the value "Hello World!".&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

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

&lt;p&gt;Syntax Parsing (Abstract Syntax Tree - AST)&lt;/p&gt;

&lt;h4&gt;
  
  
  Errors Occur During the Parsing Phase:
&lt;/h4&gt;

&lt;p&gt;The parsing phase serves as the stage where errors come into the spotlight. These errors can be categorized into several types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Syntax Error:&lt;/strong&gt; The JS engine first parsing the entire program before any of it is executed.
Consider this example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;//SyntaxError: unexpected token '.'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This program produce no output, instead throws a &lt;code&gt;SyntaxError&lt;/code&gt;, since &lt;code&gt;SyntaxError&lt;/code&gt; happens after well-formed &lt;code&gt;console.log(..)&lt;/code&gt; statement, if JS was executing top-down line by line, one would expect the &lt;code&gt;"Hello"&lt;/code&gt; message being printed before the &lt;code&gt;SyntaxError&lt;/code&gt; being thrown.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Early Errors:&lt;/strong&gt;Consider this code:
&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Howdy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;saySomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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="s2"&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;// Uncaught SyntaxError: Duplicate parameter name&lt;/span&gt;
&lt;span class="c1"&gt;// not allowed in this context&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;saySomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&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="nx"&gt;greeting&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;The &lt;code&gt;SyntaxError&lt;/code&gt; here is thrown before the program is executed because &lt;code&gt;saySomething(..)&lt;/code&gt; function have duplicate parameter names and &lt;code&gt;strict-mode&lt;/code&gt; opted in it.Have duplicate parameter always been allowed in &lt;code&gt;non strict-mode&lt;/code&gt;.&lt;br&gt;
Although the error thrown is not a syntax error in the sense of being malformed strings of tokens (like &lt;code&gt;."Hi"&lt;/code&gt; in previous example), but in &lt;code&gt;strict-mode&lt;/code&gt; is nonetheless required by the specification to be thrown as an "early error" before any execution begins.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hoisting Error:&lt;/strong&gt;Consider this example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;saySomething&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;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Howdy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&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="nx"&gt;greeting&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="nf"&gt;saySomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// ReferenceError: cannot access 'greeting' before&lt;/span&gt;
&lt;span class="c1"&gt;// initialization&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;ReferenceError&lt;/code&gt; occurs from the line with the statement &lt;code&gt;greeting = "Howdy"&lt;/code&gt;, whats happening is that the &lt;code&gt;greeting&lt;/code&gt; variable for that statement belongs to the declaration on the next line &lt;code&gt;let greeting = "Hi"&lt;/code&gt;, rather than to the previous &lt;code&gt;var greeting = "Hello"&lt;/code&gt; statement.&lt;br&gt;
Because JS engine processed this code in an earlier pass and set up all the scopes and their variable associations, know at which line the error is thrown.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Bytecode Generation:
&lt;/h3&gt;

&lt;p&gt;Finally, after the transformation and optimization processes, the compiler generates bytecode. Bytecode is a compact, lower-level representation of your code's logic. It's an intermediary form that strikes a balance between human readability and machine executability. Bytecode is crucial for efficient execution by the JavaScript engine's virtual machine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Just-In-Time (JIT) Compilation: A Performance Boost
&lt;/h3&gt;

&lt;p&gt;In the world of JavaScript execution, JIT (Just-In-Time) Compilation is a silent hero. It dynamically optimizes your code just before execution, resulting in remarkable performance improvements. While we won't dive deep into JIT compilation in this article, it's worth noting that modern JavaScript engines employ this technique to make your web applications more responsive and efficient.&lt;/p&gt;

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

&lt;p&gt;From Code to Bytecode with JIT&lt;/p&gt;

&lt;h4&gt;
  
  
  Transitioning to the Next Article: Understanding JavaScript's Scope
&lt;/h4&gt;

&lt;p&gt;With a solid grasp of how compilers translate JavaScript code into bytecode, we're now well-prepared to delve into the exciting world of execution. This is where your code comes to life, interacts with the web page, and responds to user actions. But our journey doesn't end here. In the upcoming articles, we'll continue to explore the fascinating landscape of JavaScript.&lt;/p&gt;

&lt;p&gt;In the next article, we'll venture into the realm of "Scope." Scope is a fundamental concept in JavaScript that defines where variables and functions are accessible within your code. Understanding scope is like unlocking a superpower that allows you to control how data is shared and manipulated, leading to more efficient and maintainable code.&lt;/p&gt;

&lt;p&gt;Stay tuned for our exploration of JavaScript's scope, where we'll unravel the mysteries of global scope, function scope, block scope, and lexical scope. It's a journey that will empower you to write code that's not only functional but also elegant and organized. Join us on this captivating journey as we dive deeper into the heart of JavaScript's inner workings.&lt;/p&gt;

&lt;p&gt;This transition sets the stage for the next article's topic, "Scope." If you have any further adjustments or specific points you'd like to include, please let me know!&lt;/p&gt;

&lt;p&gt;If you have any questions or specific points to discuss, please feel free to reach out!&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kyle Simpson's "You Don't Know JS: Scope &amp;amp; Closures"&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;MDN&lt;/strong&gt; Web Docs - The Mozilla Developer Network (MDN)&lt;br&gt;
&lt;strong&gt;Official Documentation of JavaScript Engines&lt;/strong&gt; - V8 , SpiderMonkey, ChakraCore and others.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>visualied</category>
    </item>
    <item>
      <title>The Evolution of React Design Patterns: From HOCs to Hooks and Custom Hooks</title>
      <dc:creator>Sam Abaasi</dc:creator>
      <pubDate>Fri, 25 Aug 2023 14:34:41 +0000</pubDate>
      <link>https://forem.com/samabaasi/the-evolution-of-react-design-patterns-from-hocs-to-hooks-and-custom-hooks-44a</link>
      <guid>https://forem.com/samabaasi/the-evolution-of-react-design-patterns-from-hocs-to-hooks-and-custom-hooks-44a</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"To evolve is to find better ways of doing things." - Dan Abramov&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the early days of React development, developers faced a myriad of challenges. React applications were becoming increasingly complex, and issues related to component reuse, state management, and code maintainability emerged as common adversaries. React's core team, which boasts notable members like Dan Abramov, Sebastian Markbåge, Sophie Alpert, and Andrew Clark, recognized these challenges and set out on a quest to unearth better design patterns. This article takes you on a journey through the evolution of React design patterns, from the birth of High-Order Components (HOCs) to the groundbreaking era of React Hooks and the rise of Custom Hooks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 1: High-Order Component (HOC) Pattern
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Problems That Necessitated HOCs
&lt;/h3&gt;

&lt;p&gt;In the early React days, component reuse was a formidable hurdle. Managing shared state was a labyrinthine task, and codebases were evolving into tangled webs of complexity. The React team, including the influential Dan Abramov, acknowledged these problems and embarked on a quest to find solutions.&lt;/p&gt;

&lt;p&gt;Dan Abramov emphasized the issues with mixing data-fetching and rendering logic within components in his seminal article: "&lt;strong&gt;Extracting your components’ data dependencies lets you reuse presentational components with different data sources.&lt;/strong&gt;" This was a pivotal moment in the evolution of React patterns.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enter High-Order Components (HOCs)
&lt;/h3&gt;

&lt;p&gt;To address the issues at hand, the concept of High-Order Components (HOCs) was born. HOCs allowed developers to wrap components, thereby enhancing their functionality and reusability. With HOCs, shared logic and behavior could be abstracted and applied across the application.&lt;/p&gt;

&lt;p&gt;Here's a real-world example of a simple HOC that adds authentication logic to a component:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withAuthentication&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&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="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;WithAuthentication&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;isAuthenticated&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="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Check authentication logic here&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isAuthenticated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;// Your authentication check logic&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;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;isAuthenticated&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;render&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="o"&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isAuthenticated&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Authentication&lt;/span&gt; &lt;span class="nx"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Please&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&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;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&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;return&lt;/span&gt; &lt;span class="nx"&gt;WithAuthentication&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;withAuthentication&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pros of HOCs
&lt;/h4&gt;

&lt;p&gt;HOCs bestowed React development with several advantages. They facilitated code reuse by enabling the wrapping of components with specific behaviors. For example, a &lt;strong&gt;&lt;code&gt;withAuthentication&lt;/code&gt;&lt;/strong&gt; HOC could be used to protect routes requiring authentication. Developers reveled in the flexibility and composability that HOCs offered.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dan Abramov: "Higher-Order Components (HOCs) allow you to reuse component logic. This is achieved by wrapping a component in a function that returns a new component with the desired behavior."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Cons and Weaknesses of HOCs
&lt;/h4&gt;

&lt;p&gt;However, HOCs were not without their set of challenges. Wrapping components with HOCs led to an abundance of wrapper components, resulting in a complex hierarchy. Debugging and comprehending component structures became increasingly daunting, and the term "wrapper hell" found its place in React discourse.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dan Abramov: "HOCs don’t solve every problem. They can make your code more complex by introducing unnecessary layers of abstraction, and they can be a little harder to understand."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Chapter 2: Presentation and Container Pattern
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Transition to Presentation and Container
&lt;/h3&gt;

&lt;p&gt;As the React community grappled with the limitations of HOCs, a new pattern emerged: the Presentation and Container pattern. This pattern sought to address the issues brought about by HOCs while preserving their strengths. It delineated components into two distinct categories: presentation components responsible for rendering, and container components responsible for data logic.&lt;/p&gt;

&lt;p&gt;Here's an example of a presentation component:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PresentationComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Data&lt;/span&gt; &lt;span class="nx"&gt;Presentation&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&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;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;PresentationComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;And a container component that manages the state and logic:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ContainerComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&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="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Fetch data and update state here&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;// Your data fetching logic&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;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;render&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PresentationComponent&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ContainerComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pros of Presentation and Container
&lt;/h4&gt;

&lt;p&gt;The Presentation and Container pattern introduced clarity into React codebases. Presentation components evolved into pure, stateless components dedicated solely to rendering. Container components assumed the role of managing data logic and maintaining component state. This segregation simplified testing and bolstered code maintainability.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Sebastian Markbåge: "The goal of the presentation and container pattern is to improve reusability, testability, and the overall organization of your components."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Cons and Weaknesses of Presentation and Container
&lt;/h4&gt;

&lt;p&gt;Nonetheless, this pattern was not a panacea. Developers frequently found themselves creating multiple container components for a single presentation component, resulting in an abundance of files and an escalation of complexity. The pattern, while an improvement, had its complexities and challenges to contend with.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Sophie Alpert: "Sometimes you may need to write a container component even when you don't feel like it's reusing any logic, and that's okay. It's not about how much logic you reuse, but how much you can understand at a glance."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Chapter 3: The Emergence of React Hooks
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Introduction to React Hooks
&lt;/h3&gt;

&lt;p&gt;The React landscape experienced a monumental shift with the advent of React Hooks. Hooks were envisioned as a remedy for the challenges posed by both HOCs and the Presentation and Container pattern. They empowered functional components to manage state and side effects without the need for class components.&lt;/p&gt;

&lt;p&gt;Here's an example of a functional component using the &lt;strong&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;useEffect&lt;/code&gt;&lt;/strong&gt; hooks:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HooksComponent&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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="nx"&gt;useEffect&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Count: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Increment&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;HooksComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pros of React Hooks
&lt;/h4&gt;

&lt;p&gt;React Hooks ushered in a new era of simplicity and reusability. They enabled functional components to manage local state, side effects, and context with remarkable ease. The concise syntax and elimination of class components resonated with developers, resulting in cleaner and more readable code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dan Abramov: "React Hooks aim to solve the problems of render props and higher-order components in a way that feels more natural and comes with fewer trade-offs."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Cons and Weaknesses of React Hooks
&lt;/h4&gt;

&lt;p&gt;Despite their widespread acclaim, React Hooks were not entirely devoid of challenges. Adapting existing class components to functional components with hooks required effort and consideration. Additionally, certain lifecycle methods, such as &lt;strong&gt;&lt;code&gt;getSnapshotBeforeUpdate&lt;/code&gt;&lt;/strong&gt;, remained exclusive to class components.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Sophie Alpert: "Hooks don't cover all use cases yet, and they might never cover all use cases. That's why we're not deprecating classes. The goal is to provide alternatives, not to force people to change their habits."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Chapter 4: The Ascendance of Custom Hooks
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Custom Hooks as a Pattern
&lt;/h3&gt;

&lt;p&gt;The React community's insatiable appetite for innovation paved the way for the Custom Hooks pattern. Custom Hooks represented a paradigm shift in React design patterns, enabling developers to encapsulate and share logic across components with unparalleled simplicity.&lt;/p&gt;

&lt;p&gt;Here's an example of a custom hook that manages a toggle state:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useToggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialValue&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="o"&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;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;value&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;useToggle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pros of Custom Hooks
&lt;/h4&gt;

&lt;p&gt;Custom Hooks provided React developers with the ultimate tool for code organization and reuse. They unlocked the potential for logic abstraction and distribution, transforming complex components into concise compositions of custom hooks. The newfound modularity elevated React applications to new heights of maintainability.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Andrew Clark: "Custom Hooks let you extract component logic into reusable functions. This is especially useful for sharing behavior across components, such as form handling or animations."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Cons and Weaknesses of Custom Hooks
&lt;/h4&gt;

&lt;p&gt;While Custom Hooks significantly enriched the React ecosystem, they were not a silver bullet. The proliferation of custom hooks across codebases warranted thoughtful naming and documentation. Developers needed to ensure that the purpose and usage of custom hooks were well-understood to prevent confusion.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Andrew Clark: "Custom Hooks can be a double-edged sword. When used appropriately, they can make your code more organized and reusable. However, misuse or excessive use can lead to confusion and unnecessary complexity."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Chapter 5: The Ongoing Evolution
&lt;/h2&gt;

&lt;p&gt;As the React ecosystem matures, the landscape of design patterns and best practices continues to evolve. Let's take a closer look at how these patterns have adapted and explore real-world examples that demonstrate their ongoing evolution.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. High-Order Component (HOC) Pattern
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Evolution: Render Props
&lt;/h4&gt;

&lt;p&gt;The HOC pattern, once a cornerstone of React development, has seen a shift toward more flexible patterns like Render Props. While HOCs remain relevant, Render Props offer a more intuitive way to share code between components.&lt;/p&gt;

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

&lt;p&gt;Consider a common scenario where you need to fetch and display data. Instead of wrapping components with an HOC, you can use a Render Props component:&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;class&lt;/span&gt; &lt;span class="nx"&gt;DataFetcher&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;data&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="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Fetch data and update the state&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;render&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;DataFetcher&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;DisplayData&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;} /&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Render Props provide greater component composability and reduce the nesting of higher-order components.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Presentation and Container Pattern
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Evolution: Component Composition
&lt;/h4&gt;

&lt;p&gt;While the Presentation and Container pattern emphasized separation of concerns, modern React development promotes component composition. Composing smaller, reusable components has become a prevailing practice.&lt;/p&gt;

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

&lt;p&gt;Instead of creating a separate Container component for data fetching, you can compose your UI from smaller components:&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="nx"&gt;WeatherDisplay&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useWeatherData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&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="nx"&gt;loading&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;LoadingSpinner&lt;/span&gt; &lt;span class="o"&gt;/&amp;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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ErrorMessage&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;WeatherInfo&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This approach enhances reusability and simplifies component hierarchies.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. React Hooks
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Evolution: Concurrent Mode
&lt;/h4&gt;

&lt;p&gt;React Hooks represent a pivotal advancement, but the evolution doesn't stop there. Concurrent Mode, an experimental React feature, is set to further improve the user experience by allowing React to work on multiple tasks simultaneously.&lt;/p&gt;

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

&lt;p&gt;Suppose you're building a resource-intensive application. Concurrent Mode can help prioritize rendering updates for critical components, ensuring a smoother user experience.&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="nx"&gt;ResourceIntensiveComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Resource-intensive rendering logic&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ResourceIntensiveComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nextProps&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="c1"&gt;// Implement custom comparison logic&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;nextProps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;By implementing custom comparison logic, Concurrent Mode can focus resources where they're needed most.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Custom Hooks
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Evolution: External Libraries and the Community
&lt;/h4&gt;

&lt;p&gt;Custom Hooks have empowered developers to create their own libraries and share them with the community. As React's ecosystem expands, external libraries offer even more powerful and specialized solutions.&lt;/p&gt;

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

&lt;p&gt;Imagine you're working on a project that requires complex state management. Instead of reinventing the wheel, you can leverage external libraries like Redux or Mobx, which provide robust state management solutions.&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useDispatch&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-redux&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="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;state&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useDispatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;INCREMENT&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Increment&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DECREMENT&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Decrement&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;By embracing external libraries and community-driven solutions, you can harness the collective expertise of the React community.&lt;/p&gt;

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

&lt;p&gt;The journey through React design patterns serves as a testament to the dynamic nature of web development. From the early struggles of component reuse to the dawn of Hooks, React's evolution has been nothing short of extraordinary. As you navigate your React projects, remember the rich history of these patterns, and wield them to craft maintainable, efficient, and scalable applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Diverse Insights from React Team
&lt;/h3&gt;

&lt;p&gt;For deeper insights into the React team's perspectives, delve into their articles and opinions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0"&gt;Dan Abramov on Presentational and Container Components&lt;br&gt;
&lt;/a&gt;&lt;a href="https://twitter.com/sebmarkbage"&gt;Sebastian Markbåge on React Component Patterns&lt;/a&gt;&lt;br&gt;
&lt;a href="https://twitter.com/sophiebits"&gt;Sophie Alpert on Component Patterns and Composition&lt;br&gt;
&lt;/a&gt;&lt;a href="https://www.youtube.com/watch?v=ByBPyMBTzM0&amp;amp;ab_channel=ReactConf"&gt;Andrew Clark on the Adoption and Impact of Hooks&lt;/a&gt;&lt;br&gt;
These resources offer invaluable insights into the motivations and philosophies that shaped React's design patterns.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>hooks</category>
      <category>designpatterns</category>
    </item>
  </channel>
</rss>
