<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bo Louie </title>
    <description>The latest articles on DEV Community by Bo Louie  (@bolouie).</description>
    <link>https://dev.to/bolouie</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%2F967026%2F8d504835-ed00-4dcc-b7e2-ca22f770622c.jpeg</url>
      <title>DEV Community: Bo Louie </title>
      <link>https://dev.to/bolouie</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bolouie"/>
    <language>en</language>
    <item>
      <title>JavaScript Fundamentals: Type Coercion and Strict Equality Explained</title>
      <dc:creator>Bo Louie </dc:creator>
      <pubDate>Wed, 17 May 2023 01:34:19 +0000</pubDate>
      <link>https://dev.to/bolouie/javascript-fundamentals-type-coercion-and-strict-equality-explained-2emh</link>
      <guid>https://dev.to/bolouie/javascript-fundamentals-type-coercion-and-strict-equality-explained-2emh</guid>
      <description>&lt;p&gt;JavaScript, a versatile and dynamic language, brings to the table a concept known as 'coercion'. This refers to JavaScript's ability to convert a value from one data type to another, similar to a bilingual person translating languages. Coercion in JavaScript can be either implicit or explicit, influenced by the context or deliberately induced using specific methods.&lt;/p&gt;

&lt;p&gt;Consider this example of implicit coercion when we mix a string with a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The magic of 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;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: "The magic of JavaScript: 10"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;y&lt;/code&gt; (a number) and &lt;code&gt;x&lt;/code&gt; (a string) are like oil and water. Yet, when we attempt to combine them with the + operator, JavaScript seamlessly converts &lt;code&gt;y&lt;/code&gt; into a string. The outcome? A new string, "The magic of JavaScript: 10".&lt;/p&gt;

&lt;p&gt;Explicit coercion, on the other hand, is like ordering your coffee 'just so'. We specify the conversion using functions such as &lt;code&gt;Number()&lt;/code&gt;, &lt;code&gt;String()&lt;/code&gt;, or &lt;code&gt;Boolean()&lt;/code&gt;. Here's how it works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Another JavaScript trick: &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;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: "Another JavaScript trick: 10"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've used the &lt;code&gt;String()&lt;/code&gt; function to explicitly convert &lt;code&gt;y&lt;/code&gt; into a string before blending it with &lt;code&gt;x&lt;/code&gt;. The result is identical to the previous example, but the process is more transparent.&lt;/p&gt;

&lt;p&gt;So, why does JavaScript offer this unique feature of coercion?&lt;/p&gt;

&lt;p&gt;First, it empowers developers to write versatile code that can accommodate a plethora of inputs, eliminating the need for exhaustive data type checking. This lends the code brevity and readability.&lt;/p&gt;

&lt;p&gt;Second, coercion simplifies operations across different data types. When you're concatenating strings and numbers, JavaScript automatically transmutes the number into a string, making the operation smooth.&lt;/p&gt;

&lt;p&gt;Lastly, coercion is instrumental in how JavaScript handles type comparison. It tries to convert values to a common type before comparison, allowing for meaningful comparison between different data types.&lt;/p&gt;

&lt;p&gt;Despite its advantages, the power of coercion can sometimes morph into a pitfall.&lt;/p&gt;

&lt;p&gt;If you're not aware of JavaScript's type conversion rules, coercion can lead to unexpected results. A common issue arises when using the equality operator (&lt;code&gt;==&lt;/code&gt;), which performs type coercion before comparison. This can yield surprising results if you're not aware of how the operator handles type coercion.&lt;/p&gt;

&lt;p&gt;Another potential source of confusion is when converting between numeric types. JavaScript boasts both integer and floating-point number types. Coercion might lead to precision loss when converting between these types, much like shrinking a high-resolution image.&lt;/p&gt;

&lt;p&gt;The antidote to these unexpected results is understanding how coercion works in JavaScript. Enter the triple equals operator (&lt;code&gt;===&lt;/code&gt;), JavaScript's version of a strict teacher. It allows no coercion and insists on comparing values of the same type.&lt;/p&gt;

&lt;p&gt;Take a look at this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10&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;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: true&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;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are like identical twins separated at birth – same value, different types. When we use the equality operator (&lt;code&gt;==&lt;/code&gt;), JavaScript converts the string "10" into the number 10 before comparison, hence returning &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, the strict comparison operator (&lt;code&gt;===&lt;/code&gt;) is less accommodating. It prohibits coercion, resulting in &lt;code&gt;false&lt;/code&gt; because &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are of different types.&lt;/p&gt;

&lt;p&gt;In essence, the triple equals operator (&lt;code&gt;===&lt;/code&gt;) is your go-to tool for strict comparisons, ensuring the values being compared share the same type and value, sans any type conversion. This makes your code more predictable and easier to comprehend, akin to reading a well-written novel.&lt;/p&gt;

&lt;p&gt;Understanding and effectively using coercion and strict equality in JavaScript is an art, one that comes with practice and experience. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Mastering JavaScript Variables: Hoisting, Temporal Dead Zone, and Top 10 Best Practices</title>
      <dc:creator>Bo Louie </dc:creator>
      <pubDate>Sat, 08 Apr 2023 21:38:14 +0000</pubDate>
      <link>https://dev.to/bolouie/mastering-javascript-variables-hoisting-temporal-dead-zone-and-top-10-best-practices-38bo</link>
      <guid>https://dev.to/bolouie/mastering-javascript-variables-hoisting-temporal-dead-zone-and-top-10-best-practices-38bo</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In JavaScript, hoisting and the temporal dead zone (TDZ) are essential concepts to understand, as they play a significant role in how the language treats variable and function declarations. This article aims to explain hoisting and the TDZ in a clear, concise manner to help beginner developers grasp these fundamental concepts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hoisting in JavaScript
&lt;/h2&gt;

&lt;p&gt;Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope (global or function scope for &lt;code&gt;var&lt;/code&gt;, block scope for &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;) during the compilation phase, before the code execution.&lt;/p&gt;

&lt;p&gt;There are three types of variable declarations in JavaScript:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;var&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;let&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;const&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The hoisting behavior differs for each type of declaration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables declared with &lt;code&gt;var&lt;/code&gt; are hoisted to the top of their containing function or global scope. Only their declarations are hoisted, not their initializations. The variable is initialized with undefined until the actual assignment in the code is executed.&lt;/li&gt;
&lt;li&gt;Variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are hoisted to the top of their containing block scope (the nearest enclosing {} block). Their initializations are not hoisted, which leads to the concept of the temporal dead zone.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Temporal Dead Zone (TDZ)
&lt;/h2&gt;

&lt;p&gt;The temporal dead zone is the period between the hoisting of a variable declared with &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; and its initialization. During this time, the variable is in the TDZ, and attempting to access the variable before its initialization will result in a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The TDZ exists to prevent variables from being accessed before they are properly initialized, ensuring that the code behaves as intended and helping to prevent bugs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are some examples to illustrate hoisting and the TDZ in JavaScript:&lt;/p&gt;

&lt;p&gt;i) &lt;code&gt;var&lt;/code&gt; hoisting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;myVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: undefined&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="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;myVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ii) &lt;code&gt;let&lt;/code&gt; hoisting and TDZ:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;myLet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: ReferenceError: myLet is not defined&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myLet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="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;myLet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 10 (if the first console.log didn't throw an error)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;iii) &lt;code&gt;const&lt;/code&gt; hoisting and TDZ:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;myConst&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: ReferenceError: myConst is not defined&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myConst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="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;myConst&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 10 (if the first console.log didn't throw an error)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Understanding hoisting and the temporal dead zone is crucial for writing clean, efficient, and bug-free JavaScript code. By grasping these fundamental concepts, beginner developers can avoid common pitfalls and better comprehend the inner workings of JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Top 10 Best Practices
&lt;/h2&gt;

&lt;p&gt;This list of best practices for dealing with variables in JavaScript provides practical tips for developers to follow and helps reinforce the concepts of hoisting and the temporal dead zone that are explained above.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declare variables at the top of their scope to avoid confusion and unintended behavior caused by hoisting.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; instead of &lt;code&gt;var&lt;/code&gt; whenever possible, as they provide better scoping and are not hoisted like &lt;code&gt;var&lt;/code&gt; declarations are.&lt;/li&gt;
&lt;li&gt;Prefer the use of &lt;code&gt;const&lt;/code&gt; over &lt;code&gt;let&lt;/code&gt; when declaring variables that won't be reassigned, as it communicates the intent that the variable should remain constant and helps prevent accidental reassignment.&lt;/li&gt;
&lt;li&gt;Use descriptive and meaningful variable names to make your code easier to read and understand.&lt;/li&gt;
&lt;li&gt;Initialize variables when they are declared to ensure that they have a defined value when they are first used.&lt;/li&gt;
&lt;li&gt;Avoid using global variables whenever possible, as they can be accessed and modified from anywhere in your code and can lead to unintended side effects.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;strict&lt;/code&gt; mode directive to help catch errors and enforce best practices in your code.&lt;/li&gt;
&lt;li&gt;Avoid reassigning values to variables that have been declared with &lt;code&gt;const&lt;/code&gt;, as this can lead to unexpected behavior.&lt;/li&gt;
&lt;li&gt;Be mindful of the temporal dead zone (TDZ) when working with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; declarations, as attempting to access or use a variable or function in the TDZ will result in a reference error.&lt;/li&gt;
&lt;li&gt;Always test your code thoroughly to ensure that it is working as intended, and debug any issues that you encounter.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Referenced JavaScript Courses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Anthony Alicea's JavaScript: Understanding the Weird Parts on Udemy&lt;/p&gt;

&lt;p&gt;Jonas Schmedtmann's The Complete JavaScript Course 2023: From Zero to Expert on Udemy&lt;/p&gt;

&lt;p&gt;Disclaimer: This post was produced with the help of artificial intelligence.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>ai</category>
    </item>
    <item>
      <title>A closure challenge</title>
      <dc:creator>Bo Louie </dc:creator>
      <pubDate>Tue, 31 Jan 2023 02:13:52 +0000</pubDate>
      <link>https://dev.to/bolouie/a-closure-challenge-dl8</link>
      <guid>https://dev.to/bolouie/a-closure-challenge-dl8</guid>
      <description>&lt;p&gt;This closure challenge was featured in The Complete JavaScript Course 2023 by Jonas Schmedtmann on Udemy. &lt;/p&gt;

&lt;h2&gt;
  
  
  Challenge
&lt;/h2&gt;

&lt;p&gt;Take the following IIFE provided and at the end of the function, attach an event listener that changes the color of the selected &lt;code&gt;h1&lt;/code&gt; element ('header') to blue, each time the &lt;code&gt;body&lt;/code&gt; element is clicked. Do not select the h1 element again! And then explain why this worked, as if to a 5 year old. Use analogies if possible. &lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&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;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&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="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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;p&gt;This code makes the header text turn red when the page loads, and blue every time you click on any part of the body. Give this a try at &lt;a href="https://codepen.io/bolouie/pen/dyjjKKV"&gt;codepen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Think of it like having a big red crayon and a blue crayon. When the page starts, you color the header with the red crayon. Then, you tell the computer that every time you click on the body, you want to color the header with the blue crayon. So every time you click, the header changes color from red to blue, just like using different crayons.&lt;/p&gt;

&lt;h2&gt;
  
  
  React
&lt;/h2&gt;

&lt;p&gt;Can you show an example of the same scenario using React?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="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;Header&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;headerColor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setHeaderColor&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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;handleBodyClick&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;setHeaderColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;headerColor&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Header&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleBodyClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&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;Header&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code uses the &lt;code&gt;useState&lt;/code&gt; hook from React to keep track of the color of the header text. Initially, the color is set to red. The body is given an &lt;code&gt;onClick&lt;/code&gt; handler, &lt;code&gt;handleBodyClick&lt;/code&gt;, which updates the color of the header text to blue every time it is clicked.&lt;/p&gt;

&lt;p&gt;In React, hooks are functions that let you "hook into" React state and lifecycle features from your functional components.&lt;/p&gt;

&lt;p&gt;Think of hooks as special tools that you can use in your components to add extra functionality and interact with React. They let you reuse stateful logic and change the behavior of your components without having to write a class component.&lt;/p&gt;

&lt;p&gt;In the example I provided, &lt;code&gt;useState&lt;/code&gt; is being used to manage the color of the header text. The &lt;code&gt;useState&lt;/code&gt; hook returns an array with two values: the current state and a function to update the state. The header color is stored in the state, and the function to update it is used in the &lt;code&gt;handleBodyClick&lt;/code&gt; handler to change the header color to blue every time the body is clicked.&lt;/p&gt;

&lt;p&gt;There are several built-in hooks in React, such as &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;, that you can use to add state management, handle side effects, and control how your component renders. You can also write your own custom hooks to reuse logic across your components.&lt;/p&gt;

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

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Clarity on closures in JavaScript</title>
      <dc:creator>Bo Louie </dc:creator>
      <pubDate>Sat, 28 Jan 2023 17:45:57 +0000</pubDate>
      <link>https://dev.to/bolouie/clarity-on-closures-in-javascript-4pmg</link>
      <guid>https://dev.to/bolouie/clarity-on-closures-in-javascript-4pmg</guid>
      <description>&lt;p&gt;In Section 10 of The Complete JavaScript Course 2023 (Udemy, Jonas Schmedtmann), there is a confusing concept about closures mentioned towards the end. In my last post on &lt;a href="https://dev.to/bolouie/data-encapsulation-in-javascript-1e9"&gt;Data encapsulation in JavaScript&lt;/a&gt;, I discussed closures.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I understand about closures
&lt;/h2&gt;

&lt;p&gt;Jonas stated: "A closure always makes sure that a function does not lose the connection to the variables that were present at its birthplace." &lt;/p&gt;

&lt;p&gt;Ok, that makes sense. A closure is a function that retains access to variables in its scope, even after the scope has closed.&lt;/p&gt;

&lt;p&gt;When an inner function is defined within an outer function, the inner function has access to the variables of the outer function, and any variables defined within the inner function are not accessible from the outside.&lt;/p&gt;

&lt;p&gt;This allows you to encapsulate data within the closure and make it accessible only through the returned function.&lt;/p&gt;

&lt;p&gt;This feature of closures is particularly useful when you want to create a function that needs to maintain some state, or when you want to pass data to a callback function without making it globally accessible.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I didn't understand about closures
&lt;/h2&gt;

&lt;p&gt;Jonas stated: "So whenever something like this happens, where you reassigned functions even without returning them, keep in mind that also this will create a closure."&lt;/p&gt;

&lt;p&gt;I was confused by this statement and the code reference didn't clarify it. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hRbJ8Sbp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sf3uwbkmwfo3mkbxac5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hRbJ8Sbp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sf3uwbkmwfo3mkbxac5p.png" alt="Screenshot of code editor used by Udemy course instructor for Section 10: A closer look at functions" width="880" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I asked ChatGPT to help me understand closures using Jonas' words.&lt;/p&gt;

&lt;p&gt;Me: Does this mean that whenever something like this happens, such as where you have reassigned functions even without returning them, that also this will create a closure?&lt;/p&gt;

&lt;p&gt;ChatGPT: Reassigning a function does not create a closure. A closure is created when an inner function is defined within an outer function and has access to the variables in the outer function's scope even after the outer function has finished executing.&lt;/p&gt;

&lt;p&gt;In this case, when you reassign a function, you are simply creating a new reference to the same function, but it does not create a closure. However, if you reassign a function and the new function references variables from the outer scope, it creates a closure. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;outerFunction&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;myVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm in 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;innerFunction&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;myVar&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;innerFunction&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;newFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;newFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// this will print "I'm in the outer function"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, when you reassign the &lt;code&gt;innerFunction&lt;/code&gt; to &lt;code&gt;newFunction&lt;/code&gt;, it creates a closure because &lt;code&gt;newFunction&lt;/code&gt; still has access to the variable &lt;code&gt;myVar&lt;/code&gt; from the &lt;code&gt;outerFunction&lt;/code&gt;. So, whenever &lt;code&gt;newFunction&lt;/code&gt; is invoked, it will be able to access the &lt;code&gt;myVar&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;It is important to note that simply reassigning a function does not create a closure, it's only when the reassigned function references variables from the outer scope that creates a closure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clarity on closures
&lt;/h2&gt;

&lt;p&gt;Reassigning a function without returning it does not create a closure.&lt;/p&gt;

&lt;p&gt;A closure is created when an inner function references variables from its outer scope after the outer function has finished executing.&lt;/p&gt;

&lt;p&gt;If you reassign a function without returning it, the inner function will no longer have access to the outer function's scope and it will not be a closure. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm a global variable&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;outerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;myVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm in 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="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;outerFunction&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;myVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// this will print "I'm in the outer function"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;outerFunction&lt;/code&gt; does not return an inner function that references to &lt;code&gt;myVar&lt;/code&gt;, so it is &lt;strong&gt;not a closure&lt;/strong&gt;. The reassignment of &lt;code&gt;myVar&lt;/code&gt; is performed in the outer scope and it becomes a global variable.&lt;/p&gt;

&lt;p&gt;It's important to note that, closures are created when an inner function references variables from its outer scope after the outer function has finished executing and those variables are not available in the global scope.&lt;/p&gt;

&lt;p&gt;In summary, closures are functions that refer to independent (free) variables. In other words, the function defined in the closure ‘remembers’ the environment in which it was created. Using closures in your code can help you keep track of and access variables in different scopes when needed. Closures can be used to create private variables and methods, and can provide better structure and organization to your code. Closures can also be used to create callback functions, which are passed as arguments to other functions and executed at a later time.&lt;/p&gt;

&lt;p&gt;Thanks for reading! Disclaimer: This was produced with the help of artificial intelligence. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>openai</category>
    </item>
    <item>
      <title>Data encapsulation in JavaScript</title>
      <dc:creator>Bo Louie </dc:creator>
      <pubDate>Sat, 28 Jan 2023 02:12:36 +0000</pubDate>
      <link>https://dev.to/bolouie/data-encapsulation-in-javascript-1e9</link>
      <guid>https://dev.to/bolouie/data-encapsulation-in-javascript-1e9</guid>
      <description>&lt;p&gt;Data encapsulation and data privacy are crucial for programming. In JavaScript, &lt;strong&gt;encapsulation&lt;/strong&gt; refers to the practice of hiding the internal state and implementation details of an object from the outside world, and providing a public interface for interacting with the object.&lt;/p&gt;

&lt;p&gt;We need to defend our variables from being unintentionally overwritten by any part of the program, including external scripts and libraries. To achieve this, we must remember to hide variables and use scopes. &lt;/p&gt;

&lt;p&gt;PART I&lt;br&gt;
Immediately Invoked Function Expressions (IIFEs), closures, and blocks are all related to data encapsulation in JavaScript. &lt;/p&gt;

&lt;p&gt;PART II&lt;br&gt;
React's state management promotes encapsulation, which is the idea of keeping data and functionality in one entity.&lt;/p&gt;
&lt;h2&gt;
  
  
  IIFEs
&lt;/h2&gt;

&lt;p&gt;IIFEs are functions defined and immediately executed, creating a private scope for variables inside the function, inaccessible from outside.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&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;myData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is some private data&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;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;myData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ReferenceError: myData is not defined&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Closures
&lt;/h2&gt;

&lt;p&gt;Closures are similar to IIFEs in that they also create a new scope for variables, but they do so by returning a function that retains access to the variables in its scope. &lt;/p&gt;

&lt;p&gt;Here is an example of data encapsulation in JavaScript using closures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createCounter&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;count&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;increment&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;count&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="na"&gt;decrement&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;count&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="na"&gt;getCount&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;count&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;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createCounter&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;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCount&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&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;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCount&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrement&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;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCount&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the &lt;code&gt;createCounter&lt;/code&gt; function returns an object with three methods: &lt;code&gt;increment&lt;/code&gt;, &lt;code&gt;decrement&lt;/code&gt;, and &lt;code&gt;getCount&lt;/code&gt;. The &lt;code&gt;count&lt;/code&gt; variable is declared inside the &lt;code&gt;createCounter&lt;/code&gt; function, so it is only accessible within the scope of that function. The object that is returned by the &lt;code&gt;createCounter&lt;/code&gt; function has access to the &lt;code&gt;count&lt;/code&gt; variable through closure, but it is not directly accessible from outside the function. This is an example of data encapsulation, as the internal state of the object (the &lt;code&gt;count&lt;/code&gt; variable) is hidden from the outside world, and can only be modified through the provided public methods (&lt;code&gt;increment&lt;/code&gt;, &lt;code&gt;decrement&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Closures are useful for creating functions that maintain state or pass data to a callback without making it globally accessible. If this is unclear, you may find my post &lt;a href="https://dev.to/bolouie/clarity-on-closures-in-javascript-4pmg"&gt;Clarity on closures in JavaScript&lt;/a&gt; helpful.&lt;/p&gt;

&lt;p&gt;Nowadays, JavaScript has made it so that IIFEs are not as necessary as they once were. If we need to create a new scope for data privacy, we can simply create a block instead - no need to create a function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Blocks
&lt;/h2&gt;

&lt;p&gt;JavaScript blocks are also used to encapsulate data, by creating a new scope for variables defined within the block. &lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords allow you to declare variables with block scope, rather than function scope. This means you can create a new scope within a block of code, such as a &lt;code&gt;for&lt;/code&gt; loop or an &lt;code&gt;if&lt;/code&gt; statement, rather than having to wrap the code in an IIFE.&lt;/p&gt;

&lt;p&gt;Here is an example of how you can use a block to achieve data encapsulation, instead of using an IIFE:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&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;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="nf"&gt;increment&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="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="nf"&gt;decrement&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="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="nf"&gt;getCount&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;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;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;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
    &lt;span class="nf"&gt;increment&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;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
    &lt;span class="nf"&gt;decrement&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;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 0&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;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: count is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the variable &lt;code&gt;count&lt;/code&gt; is declared within a block, and it's only accessible within that block, making it private. Once the block ends, the variable is not accessible anymore, so trying to access it outside the block will raise a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Though not essential as before, IIFEs may be useful in creating a new scope to prevent naming clashes or to maintain a certain state.&lt;/p&gt;

&lt;p&gt;Use closures to create private variables and methods and encapsulate data to promote modularity and maintainability while controlling access. &lt;/p&gt;

&lt;p&gt;Closures enable code to maintain state and context, by allowing inner functions to access variables from the outer function even after the outer function has finished running. This is useful for tasks such as event handling, async programming, and data encapsulation.&lt;/p&gt;

&lt;h2&gt;
  
  
  React
&lt;/h2&gt;

&lt;p&gt;State management is a key concept in React and other JavaScript libraries and frameworks for building user interfaces. In React, &lt;strong&gt;state&lt;/strong&gt; refers to the data or variables that determine a component's behavior and render information to the user.&lt;/p&gt;

&lt;p&gt;React provides a way to manage state and props (a component's input) through a component's &lt;code&gt;state&lt;/code&gt; object and the &lt;code&gt;setState&lt;/code&gt; method. The &lt;code&gt;state&lt;/code&gt; object contains the data that the component needs to render, and the &lt;code&gt;setState&lt;/code&gt; method is used to update the state and trigger a re-render of the component. This allows the component to respond to user interactions, network responses, or other events and update the user interface accordingly.&lt;/p&gt;

&lt;p&gt;React ensures components render consistently and re-render when state changes by storing it within the component and using &lt;code&gt;setState()&lt;/code&gt;. It also allows for state to be managed in a functional way with hooks, which is less verbose and easier to understand than class-based components.&lt;/p&gt;

&lt;p&gt;React promotes encapsulation with its state management, hiding internal state from outside and only allowing access through methods like &lt;code&gt;setState&lt;/code&gt;, which makes components more predictable and prevents side effects.&lt;/p&gt;

&lt;p&gt;Thanks for reading! This was produced with the help of artificial intelligence. &lt;/p&gt;

</description>
      <category>career</category>
      <category>productivity</category>
      <category>developers</category>
      <category>workplace</category>
    </item>
    <item>
      <title>.this keyword in JavaScript</title>
      <dc:creator>Bo Louie </dc:creator>
      <pubDate>Sat, 14 Jan 2023 17:21:16 +0000</pubDate>
      <link>https://dev.to/bolouie/about-this-keyword-in-javascript-4b2i</link>
      <guid>https://dev.to/bolouie/about-this-keyword-in-javascript-4b2i</guid>
      <description>&lt;p&gt;My goal in writing this post is to promote a better understanding of &lt;code&gt;this&lt;/code&gt; keyword by sharing aspects of &lt;code&gt;this&lt;/code&gt; that I discovered while learning JavaScript. Skip the specifics and jump to the Cliffs Notes if you'd prefer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Why this exists&lt;/li&gt;
&lt;li&gt;How this works&lt;/li&gt;
&lt;li&gt;What defines this&lt;/li&gt;
&lt;li&gt;Function Methods&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why &lt;em&gt;this&lt;/em&gt; exists
&lt;/h2&gt;

&lt;p&gt;In JavaScript, the &lt;code&gt;this&lt;/code&gt; keyword is a means to refer to the object or context that the code is currently executing within. In object-oriented programming, &lt;code&gt;this&lt;/code&gt; keyword makes it possible to reference the current object in callback functions and event handlers, allowing access to and manipulation of the current object's properties and methods. Additionally, &lt;code&gt;this&lt;/code&gt; enables programmers to build methods that preserve the context of the object they were called on while being reused at different instances.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword can also be used to develop closures, inner functions that have access to the variables and information in their containing functions. It serves as a method to refer to the object whose property the function is, or to the object which is carrying out the current function.&lt;/p&gt;

&lt;h2&gt;
  
  
  How &lt;em&gt;this&lt;/em&gt; works
&lt;/h2&gt;

&lt;p&gt;In JavaScript, the value of &lt;code&gt;this&lt;/code&gt; keyword within a function is typically determined by &lt;em&gt;&lt;strong&gt;how&lt;/strong&gt;&lt;/em&gt; the function is invoked, not by the location of where the function was declared. Except for arrow functions, which have a different syntax and behaviour than regular functions. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword can be passed to a function as an argument, or it can be implicitly set depending on the execution context.  &lt;/p&gt;

&lt;p&gt;The call(), apply(), and bind() methods can also be used to explicitly set the value of &lt;code&gt;this&lt;/code&gt; when a function is called. Each method can be used to call a function and modify &lt;em&gt;&lt;strong&gt;how&lt;/strong&gt;&lt;/em&gt; it is invoked, but they work in slightly different ways.&lt;/p&gt;

&lt;p&gt;A few of the common factors that dictate how the &lt;code&gt;this&lt;/code&gt; keyword is determined in different contexts will be discussed next. Then I will discuss the function methods in greater detail. &lt;/p&gt;

&lt;h2&gt;
  
  
  What defines &lt;em&gt;this&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;The context of the code that is presently running determines what the &lt;code&gt;this&lt;/code&gt; keyword represents. The call-site (the place in your code where a function is called) of the function is what determines the default behaviour of the &lt;code&gt;this&lt;/code&gt; keyword. &lt;/p&gt;

&lt;p&gt;The following are often the most important factors to consider when defining &lt;code&gt;this&lt;/code&gt; keyword as a JavaScript programmer:&lt;/p&gt;

&lt;h3&gt;
  
  
  1st Factor
&lt;/h3&gt;

&lt;p&gt;In the global scope, by default, &lt;code&gt;this&lt;/code&gt; refers to the global object, also known as the &lt;code&gt;window&lt;/code&gt; in the browser or &lt;code&gt;global&lt;/code&gt; in Node.js.&lt;/p&gt;

&lt;h3&gt;
  
  
  2nd Factor
&lt;/h3&gt;

&lt;p&gt;In a regular function call, &lt;code&gt;this&lt;/code&gt; refers to the global object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myFunc&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="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs the global object (window or global)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3rd Factor
&lt;/h3&gt;

&lt;p&gt;In a method call, &lt;code&gt;this&lt;/code&gt; refers to the object on which the method is defined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;myMethod&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myMethod&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4th Factor
&lt;/h3&gt;

&lt;p&gt;In a constructor function, &lt;code&gt;this&lt;/code&gt; refers to the new object being constructed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyConstructor&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;myProp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="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="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myObj&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;MyConstructor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs the new object, with a myProp property&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5th Factor
&lt;/h3&gt;

&lt;p&gt;When a function is called as a method of an object, &lt;code&gt;this&lt;/code&gt; refers to the object within the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// First Object&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;myProp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Netflix&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;getMethod&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myProp&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;// Second Object&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;otherObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;myProp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Disney&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getMethod&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs 'Netflix'&lt;/span&gt;
&lt;span class="nx"&gt;otherObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getMethod&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs 'Disney'&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;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getMethod&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// logs 'Netflix'&lt;/span&gt;
&lt;span class="nx"&gt;otherObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getMethod&lt;/span&gt; &lt;span class="c1"&gt;// logs f() { return this.myProp }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the first object is called &lt;code&gt;object&lt;/code&gt; that has a &lt;code&gt;myProp&lt;/code&gt; property with a value of &lt;code&gt;Netflix&lt;/code&gt;, and a &lt;code&gt;getMethod&lt;/code&gt; method that returns the value of the &lt;code&gt;myProp&lt;/code&gt; property using &lt;code&gt;this.myProp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the &lt;code&gt;object.getMethod()&lt;/code&gt; method is called, &lt;code&gt;this&lt;/code&gt; inside the method refers to the &lt;code&gt;object&lt;/code&gt; on which the method is defined, so &lt;code&gt;this.myProp&lt;/code&gt; evaluates to &lt;code&gt;object.myProp&lt;/code&gt; which is &lt;code&gt;Netflix&lt;/code&gt;. Alternatively, &lt;code&gt;getMethod()&lt;/code&gt; can be called on the second object called &lt;code&gt;otherObject&lt;/code&gt; to return it's &lt;code&gt;myProp&lt;/code&gt; property as &lt;code&gt;Disney&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Clear as mud, right!?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword is dynamic and its value is determined implicitly by the execution context of &lt;em&gt;&lt;strong&gt;how&lt;/strong&gt;&lt;/em&gt; the function is called. &lt;/p&gt;

&lt;p&gt;However, for arrow functions, the value of &lt;code&gt;this&lt;/code&gt; is not dynamic, it's determined by the scope where the arrow function was defined. Instead of having its own &lt;code&gt;this&lt;/code&gt; keyword, the arrow function must access &lt;code&gt;this&lt;/code&gt; from the parent scope. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I plan on posting about &lt;em&gt;arrow functions&lt;/em&gt; soon to better understand their atypical nature, so stay tuned!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Function methods make JavaScript more predictable by enabling the explicit definition of the value of the &lt;code&gt;this&lt;/code&gt; keyword. The &lt;code&gt;call()&lt;/code&gt;, &lt;code&gt;apply()&lt;/code&gt;, and &lt;code&gt;bind()&lt;/code&gt; methods allow you to explicitly specify the value of &lt;code&gt;this&lt;/code&gt; when a function is called. These methods modify &lt;strong&gt;&lt;em&gt;how&lt;/em&gt;&lt;/strong&gt; functions get invoked, but they work in slightly different ways.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;call()&lt;/code&gt; and &lt;code&gt;apply()&lt;/code&gt; methods invoke the function immediately, while the &lt;code&gt;bind()&lt;/code&gt; method returns a new function with &lt;code&gt;this&lt;/code&gt; bound to a specified value. This new function can be called later.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's take a closer look!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Function Methods
&lt;/h2&gt;

&lt;p&gt;The following Booking Flights code snippet is from Jonas Schmedtmann's The Complete JavaScript Course 2023, Section 10: A Closer Look at Functions, available on Udemy. It refers to booking flights with the second-largest European airline group and has one main object that I'll now discuss in greater detail on function methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Booking Flights&lt;/span&gt;
&lt;span class="c1"&gt;// Main Object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lufthansa&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;airline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Lufthansa&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;iataCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LH&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;bookings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
    &lt;span class="c1"&gt;// book: function() {}&lt;/span&gt;
    &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flightNum&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;`&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; booked a seat on &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;airline&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; flight &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;iataCode&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;flightNum&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;bookings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;flight&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;iataCode&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;flightNum&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;name&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;lufthansa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;239&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jonas Schmedtmann&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;lufthansa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;635&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Smith&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;eurowings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;airline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Eurowings&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;iataCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;EW&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;bookings&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;book&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lufthansa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;
&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sarah Williams&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ERROR&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;swiss&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;airlines&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Swiss Air Lines&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;iataCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Lx&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;bookings&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;If you run this code, you will get the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Jonas&lt;/span&gt; &lt;span class="nx"&gt;Schmedtmann&lt;/span&gt; &lt;span class="nx"&gt;booked&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;seat&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;Lufthansa&lt;/span&gt; &lt;span class="nx"&gt;flight&lt;/span&gt; &lt;span class="nx"&gt;LH239&lt;/span&gt;
&lt;span class="nx"&gt;John&lt;/span&gt; &lt;span class="nx"&gt;Smith&lt;/span&gt; &lt;span class="nx"&gt;booked&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;seat&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;Lufthansa&lt;/span&gt; &lt;span class="nx"&gt;flight&lt;/span&gt; &lt;span class="nx"&gt;LH635&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;book&lt;/code&gt; method of the &lt;code&gt;lufthansa&lt;/code&gt; object is called with the correct value for &lt;code&gt;this&lt;/code&gt; (i.e. &lt;code&gt;this&lt;/code&gt; refers to the &lt;code&gt;lufthansa&lt;/code&gt; object), so the bookings are added to the &lt;code&gt;lufthansa&lt;/code&gt; object's &lt;code&gt;bookings&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;However, when the &lt;code&gt;book&lt;/code&gt; function is assigned to the &lt;code&gt;book&lt;/code&gt; variable and then called with the &lt;code&gt;book(23, 'Sarah Williams')&lt;/code&gt;, the value of &lt;code&gt;this&lt;/code&gt; is determined by the way in which the function is called, and in this case it is called as a standalone function, not as a method of an object. &lt;/p&gt;

&lt;p&gt;As a result, the value of &lt;code&gt;this&lt;/code&gt; is the global object (in a web browser, this is the &lt;code&gt;window&lt;/code&gt; object). This means that the &lt;code&gt;airline&lt;/code&gt; and &lt;code&gt;iataCode&lt;/code&gt; properties of &lt;code&gt;this&lt;/code&gt; object will not be defined, and you will see an error message&lt;br&gt;
logged to the console similar to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Uncaught&lt;/span&gt; &lt;span class="nx"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cannot&lt;/span&gt; &lt;span class="nx"&gt;read&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;airline&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To fix this error, we need to use the &lt;code&gt;call()&lt;/code&gt; method on the &lt;code&gt;book&lt;/code&gt; function to properly set the value of &lt;code&gt;this&lt;/code&gt; keyword to the &lt;code&gt;eurowings&lt;/code&gt; object. As a result, we get the booking added to the &lt;code&gt;eurowings&lt;/code&gt; object's &lt;code&gt;bookings&lt;/code&gt; array. &lt;/p&gt;

&lt;h2&gt;
  
  
  Call Method
&lt;/h2&gt;

&lt;p&gt;Using the &lt;code&gt;call()&lt;/code&gt; method effectively specifies the value of &lt;code&gt;this&lt;/code&gt; when calling the &lt;code&gt;book&lt;/code&gt; function, like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Call Method&lt;/span&gt;
&lt;span class="nx"&gt;book&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;eurowings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sarah Williams&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;eurowings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs Sarah Williams booked a seat on Eurowings flight EW23&lt;/span&gt;

&lt;span class="nx"&gt;book&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;lufthansa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;239&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mary Cooper&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;lufthansa&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs Mary Cooper booked a seat on Lufthansa flight LH239 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the &lt;code&gt;book&lt;/code&gt; function is defined as a method of the &lt;code&gt;lufthansa&lt;/code&gt; object, but the &lt;code&gt;call()&lt;/code&gt; method on the &lt;code&gt;book&lt;/code&gt; function can set the value of &lt;code&gt;this&lt;/code&gt; to the &lt;code&gt;eurowings&lt;/code&gt; object as well.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;call()&lt;/code&gt; method allows you to invoke a function and specify the value of &lt;code&gt;this&lt;/code&gt; as well as any arguments that should be passed to the function when it is called, regardless of where the function is defined.&lt;/p&gt;

&lt;p&gt;The value of the &lt;code&gt;this&lt;/code&gt; keyword is determined dynamically based on &lt;strong&gt;how&lt;/strong&gt; a function is invoked. It is also possible to explicitly set the value of the &lt;code&gt;this&lt;/code&gt; keyword within a function by using the &lt;code&gt;call()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;call()&lt;/code&gt; method allows you to specify the value of &lt;code&gt;this&lt;/code&gt; explicitly, rather than relying on the default behavior of the &lt;code&gt;this&lt;/code&gt; keyword. This can be useful when you want to reuse a function that was defined in one context, but use it in a different context where the value of &lt;code&gt;this&lt;/code&gt; needs to be different.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply Method
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;apply()&lt;/code&gt; method allows you to call a function with a specified value for &lt;code&gt;this&lt;/code&gt;, and an array of arguments.  This can be useful when calling a function with a variable number of arguments.&lt;/p&gt;

&lt;p&gt;For example, where &lt;code&gt;flightData&lt;/code&gt; is set to an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Apply Method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;swiss&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;airline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Swiss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;iataCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LX&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;bookings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="na"&gt;book&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;flightNum&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;`&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; booked a seat on &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;airline&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; flight&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;iataCode&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;flightNum&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;bookings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;flight&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;iataCode&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;flightNum&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;name&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;// Array as an argument&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;flightData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;583&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Carlos Santana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;book&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;swiss&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;flightData&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;swiss&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following is logged to the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Carlos&lt;/span&gt; &lt;span class="nx"&gt;Santana&lt;/span&gt; &lt;span class="nx"&gt;booked&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;seat&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;Swiss&lt;/span&gt; &lt;span class="nx"&gt;flight&lt;/span&gt; &lt;span class="nx"&gt;LX583&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;airline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Swiss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iataCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LX&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bookings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;flight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LX583&lt;/span&gt;&lt;span class="dl"&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;Carlos Santana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}],&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the &lt;code&gt;apply()&lt;/code&gt; method is called on the &lt;code&gt;book&lt;/code&gt; function, and it is passed two arguments: the value of &lt;code&gt;this&lt;/code&gt; (the &lt;code&gt;swiss&lt;/code&gt; object) and an array of arguments (&lt;code&gt;flightData&lt;/code&gt;). The booking is then added to the &lt;code&gt;swiss&lt;/code&gt; objects &lt;code&gt;bookings&lt;/code&gt; array.  &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;apply()&lt;/code&gt; method is less commonly used in modern JavaScript because there is a better way to accomplish the same task. &lt;/p&gt;

&lt;p&gt;I'm referring to the spread operator (&lt;code&gt;...&lt;/code&gt;), which allows you to spread an array of values into individual arguments when calling a function. This can be a more concise and convenient way of calling a function with an array of arguments in comparison to the &lt;code&gt;apply()&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;For example, instead of using the &lt;code&gt;apply()&lt;/code&gt; method as shown in the previous example, you could use the spread operator like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Spread Operator&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;flightData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;583&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Carlos Santana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;book&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;swiss&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;flightData&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;swiss&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs Carlos Santana booked a seat on Swiss flight LX583&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Invoking the &lt;code&gt;book&lt;/code&gt; function, by spreading the &lt;code&gt;flightData&lt;/code&gt; into separate arguments with the spread operator effectively adds &lt;code&gt;Carlos Santana&lt;/code&gt; to the &lt;code&gt;swiss&lt;/code&gt; objects &lt;code&gt;bookings&lt;/code&gt; array!&lt;/p&gt;

&lt;p&gt;Because the spread operator is a relatively new addition to JavaScript, it may not be supported in all environments. It is, however, widely supported in modern browsers and JavaScript runtimes, making it a convenient and concise method of calling a function with an array of arguments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bind Method
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;bind()&lt;/code&gt; method creates a new function that, when called, has its &lt;code&gt;this&lt;/code&gt; keyword set to the provided value,  that you specify. This new function can be called later.&lt;/p&gt;

&lt;p&gt;By calling the &lt;code&gt;bind()&lt;/code&gt; method on the &lt;code&gt;book&lt;/code&gt; function and pass different values for &lt;code&gt;this&lt;/code&gt;, you can create new functions with fixed values for &lt;code&gt;this&lt;/code&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bind Method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bookEW&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;book&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;eurowings&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;bookLH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;book&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;lufthansa&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;bookLX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;book&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;swiss&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;bind()&lt;/code&gt; method returns a new function with the value of &lt;code&gt;this&lt;/code&gt; set to the specified value. &lt;/p&gt;

&lt;p&gt;In the above example, the &lt;code&gt;bind()&lt;/code&gt; method is called on the &lt;code&gt;book&lt;/code&gt; function three times, each time with a different value for &lt;code&gt;this&lt;/code&gt;, resulting in three new functions: &lt;/p&gt;

&lt;p&gt;-&lt;code&gt;bookEW&lt;/code&gt;, which has &lt;code&gt;this&lt;/code&gt; fixed to the &lt;code&gt;eurowings&lt;/code&gt; object&lt;br&gt;
-&lt;code&gt;bookLH&lt;/code&gt;, which has &lt;code&gt;this&lt;/code&gt; fixed to the &lt;code&gt;lufthansa&lt;/code&gt; object&lt;br&gt;
-&lt;code&gt;bookLX&lt;/code&gt;, which has &lt;code&gt;this&lt;/code&gt; fixed to the &lt;code&gt;swiss&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;These functions are identical to the &lt;code&gt;book&lt;/code&gt; function, except the value of &lt;code&gt;this&lt;/code&gt; is fixed to the value that was passed to &lt;code&gt;bind()&lt;/code&gt;. You can then call these functions at a later time, and the value of &lt;code&gt;this&lt;/code&gt; will be fixed to the value that you specified when you created the function with &lt;code&gt;bind()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The following gets logged to the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;bookEW&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Steven Williams&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Adds a booking to the eurowings object's bookings array&lt;/span&gt;
&lt;span class="nx"&gt;bookLH&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;654&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 Smith&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Adds a booking to the lufthansa object's booking array&lt;/span&gt;
&lt;span class="nx"&gt;bookLX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;583&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 Johnson&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Adds a booking to the swiss object's bookings array&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be useful when you want to reuse a function in different contexts, but you want the value of &lt;code&gt;this&lt;/code&gt; to be different in each context. The &lt;code&gt;bind&lt;/code&gt; method allows you to create new functions with fixed values for &lt;code&gt;this&lt;/code&gt;, which can be called at a later time. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Calling a single function for additional flight bookings&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bookEW23&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;book&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;eurowings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;bookEW23&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jonas Schmedtmann&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;bookEW23&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Martha Stewart&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;The &lt;code&gt;bind()&lt;/code&gt; method in this example, was used to create new functions with the &lt;code&gt;this&lt;/code&gt; value set to either &lt;code&gt;lufthansa&lt;/code&gt; or &lt;code&gt;eurowings&lt;/code&gt;, depending on which object is passed as an argument to &lt;code&gt;bind()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bookEW&lt;/code&gt; function is created with the &lt;code&gt;this&lt;/code&gt; value set to &lt;code&gt;eurowings&lt;/code&gt;, so when it is called with &lt;code&gt;(23, 'Steven Williams')&lt;/code&gt;, it logs the message: &lt;code&gt;"Steven Williams booked a seat on Eurowings flight EW23"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bookEW23&lt;/code&gt; function is also created with the &lt;code&gt;this&lt;/code&gt; value set to &lt;code&gt;eurowings&lt;/code&gt;, but it is already partially applied with the first argument set to &lt;code&gt;23&lt;/code&gt;. When it is called with a single argument &lt;code&gt;'Jonas Schmedtmann'&lt;/code&gt;, it logs the message: &lt;code&gt;"Jonas Schmedtmann booked a seat on Eurowings flight EW23"&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When you create a new function using the &lt;code&gt;bind()&lt;/code&gt; method and partially apply some of its arguments, the resulting function will only have placeholders for the remaining arguments. &lt;/p&gt;

&lt;p&gt;Partial application refers to the process of creating a new function by fixing some of the arguments of an existing function. The new function is then called with the remaining arguments.&lt;/p&gt;

&lt;p&gt;For example, in the case of the &lt;code&gt;bookEW23&lt;/code&gt; function, the &lt;code&gt;flightNum&lt;/code&gt; argument is already set to &lt;code&gt;23&lt;/code&gt;, so when you call &lt;code&gt;bookEW23&lt;/code&gt; function, you only need to provide the &lt;code&gt;name&lt;/code&gt; argument. The resulting function call will look like this: &lt;code&gt;bookEW23(name)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;code&gt;bind()&lt;/code&gt; method to set the value of multiple arguments at once. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bookEW23Martha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;book&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;eurowings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Martha&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;bookEW23Martha&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs Martha booked a seat on Eurowings flight EW23&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Functional programmers often use partial application to code reusable, configurable functions that are simple to adapt for particular use cases. The &lt;code&gt;bind()&lt;/code&gt; method is one way to implement partial application in JavaScript, but there are other ways as well, such as using higher-order functions or creating a function that returns a partially applied function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cliffs Notes
&lt;/h2&gt;

&lt;p&gt;Back to Contents&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;call()&lt;/code&gt;, &lt;code&gt;apply()&lt;/code&gt;, and &lt;code&gt;bind()&lt;/code&gt; methods are all ways to specify the value of &lt;code&gt;this&lt;/code&gt; keyword when calling a function.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;call()&lt;/code&gt; method allows you to call a function with a specified value for &lt;code&gt;this&lt;/code&gt;, and pass any number of arguments to the function as separate parameters.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;apply()&lt;/code&gt; method allows you to call a function with a specified value for &lt;code&gt;this&lt;/code&gt;, and pass any number of arguments to the function as an array.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bind()&lt;/code&gt; method allows you to create a new function with a specified value for &lt;code&gt;this&lt;/code&gt;, and optionally set some or all of the arguments to the new function. The new function can then be called at a later time, with the remaining arguments passed when the function is called.&lt;/p&gt;

&lt;p&gt;These methods can be useful when you want to reuse a function in different contexts, but you want the value of &lt;code&gt;this&lt;/code&gt; or the arguments to be different in each context. They allow you to specify the value of &lt;code&gt;this&lt;/code&gt; and the arguments explicitly, rather than relying on the default behavior of the &lt;code&gt;this&lt;/code&gt; keyword and the arguments.&lt;/p&gt;

&lt;p&gt;Thanks for reading and hopefully this was helpful! I welcome constructive feedback, and if you find an error, please kindly let me know. Disclaimer: This post was produced with the help of artificial intelligence. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>openai</category>
    </item>
    <item>
      <title>How do you check for valid email input?</title>
      <dc:creator>Bo Louie </dc:creator>
      <pubDate>Sat, 07 Jan 2023 21:20:37 +0000</pubDate>
      <link>https://dev.to/bolouie/how-do-you-check-for-valid-email-input-3b3j</link>
      <guid>https://dev.to/bolouie/how-do-you-check-for-valid-email-input-3b3j</guid>
      <description>&lt;p&gt;I'm working with strings in section 9 of The Complete JavaScript Course 2023: From Zero to Expert, which I purchased on Udemy. &lt;/p&gt;

&lt;p&gt;Jonas Schmedtmann, one of Udemy's Top Instructors, demonstrates how to check the validity of email input using various methods on strings (shown in CodeSnap below).&lt;/p&gt;

&lt;p&gt;![A CodeSnap of JavaScript string methods for converting to lowercase and getting rid of white spaces]](&lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mta0cxlfexunswr33de9.png" rel="noopener noreferrer"&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mta0cxlfexunswr33de9.png&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Jonas mentioned the challenge of creating a function with checking or validating behaviour, that could return a boolean. I took on the challenge and generated three functions by prompting ChatGPT, as outlined below.&lt;/p&gt;

&lt;h2&gt;
  
  
  1st Function
&lt;/h2&gt;

&lt;p&gt;Use a regular expression to check if the email is in the correct format&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isValidEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&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;re&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;(([^&lt;/span&gt;&lt;span class="sr"&gt;&amp;lt;&amp;gt;()&lt;/span&gt;&lt;span class="se"&gt;\[\]\\&lt;/span&gt;&lt;span class="sr"&gt;.,;:&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="sr"&gt;@"&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;(\.[^&lt;/span&gt;&lt;span class="sr"&gt;&amp;lt;&amp;gt;()&lt;/span&gt;&lt;span class="se"&gt;\[\]\\&lt;/span&gt;&lt;span class="sr"&gt;.,;:&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="sr"&gt;@"&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;*&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;|&lt;/span&gt;&lt;span class="se"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;".+"&lt;/span&gt;&lt;span class="se"&gt;))&lt;/span&gt;&lt;span class="sr"&gt;@&lt;/span&gt;&lt;span class="se"&gt;((\[[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]{1,3}\.[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]{1,3}\.[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]{1,3}\.[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]{1,3}\])&lt;/span&gt;&lt;span class="sr"&gt;|&lt;/span&gt;&lt;span class="se"&gt;(([&lt;/span&gt;&lt;span class="sr"&gt;a-zA-Z&lt;/span&gt;&lt;span class="se"&gt;\-&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;\.)&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-zA-Z&lt;/span&gt;&lt;span class="se"&gt;]{2,}))&lt;/span&gt;&lt;span class="sr"&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&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;This function takes an email as an argument and uses a regular expression to check if the email is in the correct format. The regular expression checks for the presence of an @ symbol and a ., and it also checks that the email does not contain any invalid characters.&lt;/p&gt;

&lt;h2&gt;
  
  
  2nd Function
&lt;/h2&gt;

&lt;p&gt;To use this function, you can pass the email that you received as an input to the function and check if it is valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello@Jonas.Io&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;isValidEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&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;Valid email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid email&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;This code will output Valid email, because the email is in the correct format.&lt;/p&gt;

&lt;h2&gt;
  
  
  3rd Function
&lt;/h2&gt;

&lt;p&gt;To fix the capitalization and remove the spaces before and after the email, you can use the trim and toLowerCase methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fixedEmail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&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="nf"&gt;isValidEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fixedEmail&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;Valid email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid email&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;This code will output Valid email, because the trim method removes the leading and trailing whitespace from the email and the toLowerCase method converts the email to lowercase.&lt;/p&gt;

&lt;p&gt;In ECMAScript 2019 (ES2019), two new string trimming methods were introduced: trimStart and trimEnd.&lt;/p&gt;

&lt;p&gt;The trimStart method removes leading whitespace from a string, while the trimEnd method removes trailing whitespace.&lt;/p&gt;

&lt;p&gt;Here is an example of how you can use these methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;  Hello   &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="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;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trimStart&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Hello   "&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;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trimEnd&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: "   Hello"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;You can use these methods instead of the trim method if you only want to remove leading or trailing whitespace from a string.&lt;/p&gt;

&lt;p&gt;Keep in mind that the trimStart and trimEnd methods are not supported in all browsers, so you may want to use a polyfill if you need to support older browsers.&lt;/p&gt;

&lt;p&gt;This article was produced with the help of &lt;a href="https://openai.com/blog/chatgpt/" rel="noopener noreferrer"&gt;ChatGPT&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>development</category>
      <category>productivity</category>
    </item>
    <item>
      <title>ChatGPT is helping me learn JavaScript</title>
      <dc:creator>Bo Louie </dc:creator>
      <pubDate>Sat, 24 Dec 2022 21:04:49 +0000</pubDate>
      <link>https://dev.to/bolouie/chatgpt-is-helping-me-learn-javascript-15n1</link>
      <guid>https://dev.to/bolouie/chatgpt-is-helping-me-learn-javascript-15n1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QKG8FN4Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4abgrxiwzzemr1fjrl1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QKG8FN4Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4abgrxiwzzemr1fjrl1.jpg" alt="Image of open note-book showing handwriting about JavaScript Operator concepts" width="880" height="660"&gt;&lt;/a&gt;I'm halfway through JavaScript: Understanding the Weird Parts instructed by Anthony Alicea on Udemy. I purchased this course way back in January of 2020 and completed roughly a quarter of the lessons. That's when I fell off track and left it at that, until recently.&lt;/p&gt;

&lt;p&gt;About six weeks ago, I re-started the course and got through the first quarter of the lessons for the second time. I've been dedicating at least four days a week (few hours per day) to taking notes, reading MDN web docs, but not much coding. &lt;/p&gt;

&lt;p&gt;I'm determined to fully grasp all of the weird parts and as many advanced topics of JavaScript. &lt;/p&gt;

&lt;p&gt;Tony makes JavaScript programming less intimidating. The 'big word alerts' or 'conceptual and frameworks asides' get presented in simplistic and straight-forward ways. The material nicely builds section-by-section and each 'weird part' gets thoroughly explained using minimalistic visuals. &lt;/p&gt;

&lt;p&gt;However, a major blocker to my JavaScript learning progress has been my method of capturing Tony's material. I've been note-taking, hand-writing snippets of code, even color-coding the syntax on paper! Gasp ...right!? &lt;/p&gt;

&lt;p&gt;Well, my method has changed since ChatGPT was released!&lt;/p&gt;

&lt;p&gt;With the help of &lt;a href="https://openai.com/blog/chatgpt/"&gt;&lt;strong&gt;ChatGPT&lt;/strong&gt;&lt;/a&gt;, my lessons with Tony on the weird parts of JavaScript feel way more motivating, flowing, and easier to review. My pace and depth of capturing the course material has drastically improved with ChatGPT. When I don't fully understand what Tony explains in the course, I turn to ChatGPT to gain insight, generate different code, or search for specific context. For instance, I was able to ask ChatGPT to explain how function factories and closures are important to front-end development. &lt;/p&gt;

&lt;p&gt;ChatGPT gave me clarity, instantly: &lt;em&gt;"As a front-end developer, you can use function factories and closures to create reusable and modular code for your web applications.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Lately, I've been trying to figure what platform is best for archiving the collections of my programming notes. I want to be able to revisit this material for future projects and interviews.  &lt;/p&gt;

&lt;p&gt;I recently leveraged ChatGPT to help me draw neat &lt;a href="https://bolouie.medium.com/a-comparison-of-this-in-javascript-and-that-self-in-python-ec19f58ca418"&gt;comparisons&lt;/a&gt; between JavaScript and Python––the programming language that I'm most comfortable coding at this time. I've been taking advantage of ChatGPT by drafting stories of my JavaScript lessons on my Medium account but I'm thinking of switching to Notion.&lt;/p&gt;

&lt;p&gt;What's your go-to method of managing your programming notes?&lt;/p&gt;

&lt;p&gt;I'm also now aware of the new &lt;a href="https://dev.to/devteam/guidelines-for-ai-assisted-articles-on-dev-17n6"&gt;Guidelines for AI-assisted articles on DEV&lt;/a&gt;, which makes me curious about how others might share their AI-assisted programming learning journey on here.  &lt;/p&gt;

</description>
      <category>chatgpt</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
