<?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: Naim Latifi</title>
    <description>The latest articles on DEV Community by Naim Latifi (@naimlatifi5).</description>
    <link>https://dev.to/naimlatifi5</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%2F136575%2Fcfd552d6-ee16-4294-a464-a84e2fafd134.jpg</url>
      <title>DEV Community: Naim Latifi</title>
      <link>https://dev.to/naimlatifi5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/naimlatifi5"/>
    <language>en</language>
    <item>
      <title>The basic React hooks - useState and useEffect</title>
      <dc:creator>Naim Latifi</dc:creator>
      <pubDate>Thu, 17 Mar 2022 21:33:12 +0000</pubDate>
      <link>https://dev.to/naimlatifi5/the-basic-react-hooks-usestate-and-useeffect-103</link>
      <guid>https://dev.to/naimlatifi5/the-basic-react-hooks-usestate-and-useeffect-103</guid>
      <description>&lt;p&gt;With the new version of React 16.8.0 were introduced React hooks which are functions that allow us to access state and other lifecycle features without a need to use classes. In this article, I am going to show you some of the basic hooks &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  useState
&lt;/h2&gt;

&lt;p&gt;The React &lt;code&gt;useState&lt;/code&gt; allow us to track state inside function components. The &lt;code&gt;useState&lt;/code&gt; hook that we import from React library accepts an initial value that is assigned only on initial render and returns an array. Let's see this by an example&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdgo7trcso%2Fimage%2Fupload%2Fv1646861846%2FWebitech%2FuseState.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdgo7trcso%2Fimage%2Fupload%2Fv1646861846%2FWebitech%2FuseState.png" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the example above we have initialized the state with value zero of type &lt;code&gt;integer&lt;/code&gt; but that value can be of any other types like &lt;code&gt;arrays, objects, null, string&lt;/code&gt; etc... The first value &lt;code&gt;count&lt;/code&gt; that is destructed is a variable that holds the current state and the second element &lt;code&gt;setCount&lt;/code&gt; is our function that takes a new value to update the state variable. So whenever we click the button the &lt;code&gt;setCount&lt;/code&gt; function will be called and update the state &lt;code&gt;count&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  useEffect
&lt;/h2&gt;

&lt;p&gt;It allows us to perform side effects inside functional components. But what do we mean with side effects? Well, these are some of the operations that we have already used with lifecycle methods in class-based components such as fetching data from the server, reading from a localStorage, updating the DOM, registering and deregistering event listeners, etc... It is important to state that the &lt;code&gt;useEffect&lt;/code&gt; differs completely from lifecycle methods in class-based components both in abstraction level and how we use it. The signature for the &lt;code&gt;useEffect&lt;/code&gt; accept a callback function and an optional dependency array looks as below:&lt;/p&gt;

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

&lt;p&gt;Now let's take an example and see how we can execute a side effects with &lt;code&gt;useEffect&lt;/code&gt;. For the demonstration purpose in the example below we will create two buttons where &lt;code&gt;Update name&lt;/code&gt; button will update name &lt;em&gt;Superman&lt;/em&gt; and button &lt;code&gt;Update versions&lt;/code&gt; will update &lt;em&gt;Superman's&lt;/em&gt; strongest versions.&lt;/p&gt;

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

&lt;p&gt;As we can see inside &lt;code&gt;useEffect&lt;/code&gt; we have omitted the dependancy array as a second argument thus a re-render is caused on initial load of component and whenever any of the buttons are clicked. As a result a console statements will get printed: &lt;code&gt;Name: Superman&lt;/code&gt; and &lt;code&gt;Strongest versions: Supermen prime&lt;/code&gt;. But what if we have a scenario so that we only want to load data when component is loaded (i.e. when fetching data from server). We can achieve this with the help of &lt;code&gt;useEffect&lt;/code&gt; dependency array as follow: &lt;/p&gt;

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

&lt;p&gt;Adding dependency array from the example above will output the console statement with a message on the initial component load. If any of the buttons are clicked it will not trigger a re-render of the component but will only update the state for the button clicked. &lt;br&gt;
Moreover, with the help of a dependency array we can also execute &lt;code&gt;useEffects&lt;/code&gt; that are dependent on a certain condition. From the example above if the state for &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;strongest versions&lt;/code&gt; has changed. &lt;/p&gt;

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

&lt;p&gt;From the example above a particular &lt;code&gt;useEffect&lt;/code&gt; is invoked when a particular button is clicked and a particular state has changed. Clicking on the button &lt;code&gt;Update name&lt;/code&gt; only one console statement is printed as &lt;code&gt;Name: Superman!&lt;/code&gt; and not both as in the earlier example where dependency array was omitted. &lt;/p&gt;

&lt;p&gt;Hope you enjoy it! &lt;/p&gt;

&lt;p&gt;Keep an eye  👁️  on my other series of articles where I am going to write for other React hooks &lt;code&gt;useContext&lt;/code&gt; &lt;code&gt;useRef&lt;/code&gt; &lt;code&gt;useReducer&lt;/code&gt; &lt;code&gt;useCallback&lt;/code&gt; &lt;code&gt;useMemo&lt;/code&gt; &lt;code&gt;useImperativeHandle&lt;/code&gt; &lt;code&gt;useLayoutEffect&lt;/code&gt; &lt;code&gt;useDebugValue&lt;/code&gt; 🍻 👨‍💻 👩‍💻&lt;/p&gt;

</description>
    </item>
    <item>
      <title>ECMAScript 2021 (ES12) new features</title>
      <dc:creator>Naim Latifi</dc:creator>
      <pubDate>Fri, 03 Sep 2021 10:08:37 +0000</pubDate>
      <link>https://dev.to/naimlatifi5/ecmascript-2021-es12-new-features-2l67</link>
      <guid>https://dev.to/naimlatifi5/ecmascript-2021-es12-new-features-2l67</guid>
      <description>&lt;p&gt;ECMAScript, the standardized version of JavaScript is increasing its popularity and is becoming powerful every day. Since the introduction of ECMAScript 2015 (ES6) which was an immense growth forward, new features are added every year around June.  Those features are mostly improvements to the JavaScript language by providing new functions and by expressing things in much simpler way. It also changes the way developers structure the program. &lt;/p&gt;

&lt;p&gt;On June 22 The ECMA International approved the latest version of the official specification ES12 aka ECMAScript 2021 by providing capabilities for strings, promises, classes and much more. The following list show those new features of ES12&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Numeric separators &lt;/li&gt;
&lt;li&gt;String replaceAll()&lt;/li&gt;
&lt;li&gt;Logical assignment operator&lt;/li&gt;
&lt;li&gt;Promise.any&lt;/li&gt;
&lt;li&gt;Private class methods&lt;/li&gt;
&lt;li&gt;Private getters and setters&lt;/li&gt;
&lt;li&gt;WeakRef&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Numeric separators
&lt;/h3&gt;

&lt;p&gt;Numeric separators are used for larger numbers that are hard to read by separating them with &lt;code&gt;(_)&lt;/code&gt; between a group of digits.  Let see this by an example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQHBMM2INKpsiA%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629918681182%3Fe%3D1635379200%26v%3Dbeta%26t%3D8AAiaiD69nteaZXj-vuY3r12buRJWTeoBEV4CgTJWWU" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQHBMM2INKpsiA%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629918681182%3Fe%3D1635379200%26v%3Dbeta%26t%3D8AAiaiD69nteaZXj-vuY3r12buRJWTeoBEV4CgTJWWU" alt="Night" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we see from the example above it is hard to read the value if it is a million, ten million, or a billion. With the help of the numeric separators &lt;code&gt;(_)&lt;/code&gt;, we can divide the same number in group of digits as in example below &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQGrFsjK0CXiDA%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629918634829%3Fe%3D1635379200%26v%3Dbeta%26t%3DI57fD-s2AEwoWyvfUw2Z64s7KBgborJR-htnWqNvtHs" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQGrFsjK0CXiDA%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629918634829%3Fe%3D1635379200%26v%3Dbeta%26t%3DI57fD-s2AEwoWyvfUw2Z64s7KBgborJR-htnWqNvtHs" alt="Night" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  String replaceAll()
&lt;/h3&gt;

&lt;p&gt;I find this method as my favorit one to use further in my codebase when working with strings and substrings replacements as there is no need on using regular expressions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQEMm3sNWJ_jEg%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629919244553%3Fe%3D1635379200%26v%3Dbeta%26t%3DfjRR9GB4hnvIYI0Uh3PH_53ZeUL26ba_loVkn-Sq9ic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQEMm3sNWJ_jEg%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629919244553%3Fe%3D1635379200%26v%3Dbeta%26t%3DfjRR9GB4hnvIYI0Uh3PH_53ZeUL26ba_loVkn-Sq9ic" alt="Night" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the example above for the string &lt;code&gt;This is interesting book that contains interesting examples&lt;/code&gt; we want to replace the word &lt;code&gt;interesting&lt;/code&gt; with &lt;code&gt;JavaScript&lt;/code&gt; in all substrings. The first method that we usually think of when replacing strings is the &lt;code&gt;replace()&lt;/code&gt; method. However, as seen from the example above this method doesn't return the desired result since this method replace only the first occurrence on the substring &lt;code&gt;This is JavaScript book that contains interesting examples&lt;/code&gt; but what we want is to have the string as &lt;code&gt;This is JavaScript book that contains JavaScript examples&lt;/code&gt;. To achieve the desired result we usually use regular expressions for which they are unsafe to use as they contain escape characters. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQE-0vaRDdTssw%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629919429040%3Fe%3D1635379200%26v%3Dbeta%26t%3DpApSisMbVwe1Ub8JoCmOkOwnay6M2EewhFv7XhhZuPc" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQE-0vaRDdTssw%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629919429040%3Fe%3D1635379200%26v%3Dbeta%26t%3DpApSisMbVwe1Ub8JoCmOkOwnay6M2EewhFv7XhhZuPc" alt="Night" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without using regular expression and with the help of ES12 feature &lt;code&gt;replaceAll()&lt;/code&gt; we can achieve the same result as below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQGPyPvFObLw-w%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629919608177%3Fe%3D1635379200%26v%3Dbeta%26t%3DRqqPVbzbW_VGCjtGpSOO5JiusMMOn1aPADfZfhyNhGQ" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQGPyPvFObLw-w%2Farticle-inline_image-shrink_1000_1488%2F0%2F1629919608177%3Fe%3D1635379200%26v%3Dbeta%26t%3DRqqPVbzbW_VGCjtGpSOO5JiusMMOn1aPADfZfhyNhGQ" alt="Night" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Logical assignment operator
&lt;/h3&gt;

&lt;p&gt;As we already may know JavaScript currently support arithmetic i.e a +=b (equiv. to a = a + b) and bitwise i.e a &amp;amp;=b (equiv. a = a &amp;amp; b) assignment operators but what was missing is the ability to combine logical operator (&amp;amp;&amp;amp; || and ??) with assignment. With ES12 feature there are three such kind of logical assignment operator: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Logical nullish assignment &lt;code&gt;(??=) -&amp;gt; (x ??= y)&lt;/code&gt;  (only assigns if x is null or undefined)&lt;/li&gt;
&lt;li&gt;Logical AND assignment &lt;code&gt;(&amp;amp;&amp;amp;=) -&amp;gt; (x &amp;amp;&amp;amp;=y)&lt;/code&gt; (only assigns if x is truthy)&lt;/li&gt;
&lt;li&gt;Logical OR assignment &lt;code&gt;(||=) -&amp;gt; (x ||=y )&lt;/code&gt; (only assigns if x is falsy)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The logical nullish assignment operator performs the assignment only if the left-hand operand is nullish (&lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;). From the example below the string &lt;code&gt;Superman&lt;/code&gt; is assigned to variable user only if the user is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; and not otherwise. Nullish assignment operator is much simple as we just need to write one line of code compared to the old way as we need to have an if statement and check if the variable user is &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt; then assign the value to the user. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQFqqxK6jwjAkg%2Farticle-inline_image-shrink_1000_1488%2F0%2F1630003600703%3Fe%3D1635379200%26v%3Dbeta%26t%3D-kuyjl4CCpByfTEwPn_QFS00dhyRrONJ5NADDdU8Skc" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQFqqxK6jwjAkg%2Farticle-inline_image-shrink_1000_1488%2F0%2F1630003600703%3Fe%3D1635379200%26v%3Dbeta%26t%3D-kuyjl4CCpByfTEwPn_QFS00dhyRrONJ5NADDdU8Skc" alt="Night" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Logical AND assignment operator&lt;/em&gt; performs assignment only if the left-hand operand is truthy. From the example below &lt;code&gt;num2&lt;/code&gt; is assigned to &lt;code&gt;num1&lt;/code&gt; if &lt;code&gt;num1&lt;/code&gt; is truthy and not  &lt;code&gt;null,undefined, false, 0, or NaN&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdgo7trcso%2Fimage%2Fupload%2Fv1630700886%2FGreetings%2Fcarbon_19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdgo7trcso%2Fimage%2Fupload%2Fv1630700886%2FGreetings%2Fcarbon_19.png" alt="Night" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Logical OR assignment operator&lt;/em&gt; is opposite of logical AND assignment operator and performs assignment if the left-hand operand is falsy. From the example below &lt;code&gt;num2&lt;/code&gt; is assigned to &lt;code&gt;num1&lt;/code&gt; if &lt;code&gt;num1&lt;/code&gt; is &lt;code&gt;null, undefined, false, or 0&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC5612AQHy6x5FRKV8ng%2Farticle-inline_image-shrink_1000_1488%2F0%2F1630008877936%3Fe%3D1635379200%26v%3Dbeta%26t%3DD4rnwLpXONeP0b00hMPbeEfwzrGb_JXTNg0I96nX6SI" alt="Night" width="800" height="400"&gt; 
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Promise.any
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Promise.any&lt;/code&gt; accepts an iterable of promises and resolves as soon as one of the promise is resolved. If none of the promises aren't resolved then an &lt;code&gt;AggregateError&lt;/code&gt; error is returned as an array with individual errors for each promise. &lt;code&gt;Promise.any&lt;/code&gt; works in the opposite way of &lt;code&gt;Promise.all()&lt;/code&gt; where all iterable promises needs to be resolved for a single promise to resolve.&lt;/p&gt;

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

&lt;p&gt;From the example above we have set &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt; promises where A and B resolve and C rejects and as result &lt;code&gt;A&lt;/code&gt; is returned since it is resolved faster than &lt;code&gt;B&lt;/code&gt; and C that is rejected whereas in case  &lt;code&gt;D&lt;/code&gt; and &lt;code&gt;E&lt;/code&gt; we set all promises to be rejected and as a result an &lt;code&gt;AggregateError&lt;/code&gt; is printed. &lt;/p&gt;




&lt;h3&gt;
  
  
  Private class methods
&lt;/h3&gt;

&lt;p&gt;Since the introduction of ES6 developers can create classes in JavaScript that changed the way when thinking Object-oriented models. These classes by default have public properties and methods that are accessible from outside of the class. To overcome this issue there was a need for a naming convention with an underscore &lt;code&gt;(_)&lt;/code&gt; for those properties and methods to be private even though this was just a hint and nothing preventing them from accessing outside of the class. Now with the new class features of ES12 we can define properties or methods as private by setting a &lt;code&gt;#&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQFfBvhC18T1DA%2Farticle-inline_image-shrink_1000_1488%2F0%2F1630181575350%3Fe%3D1635379200%26v%3Dbeta%26t%3DKOosiBkqSEubhEcbC3c_Y7ZIuZ6cVVPRymc7r88HZ4g" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC4E12AQFfBvhC18T1DA%2Farticle-inline_image-shrink_1000_1488%2F0%2F1630181575350%3Fe%3D1635379200%26v%3Dbeta%26t%3DKOosiBkqSEubhEcbC3c_Y7ZIuZ6cVVPRymc7r88HZ4g" alt="Night" width="800" height="400"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;From the example above when we try to access the method &lt;code&gt;#private()&lt;/code&gt; we get an error because this method is private in the class. If we want to access this method outside of the class then we need to create a public method inside a class and call the &lt;code&gt;#private()&lt;/code&gt; as seen in method &lt;code&gt;showAll()&lt;/code&gt;. &lt;/p&gt;




&lt;h3&gt;
  
  
  Private getters and setters
&lt;/h3&gt;

&lt;p&gt;In a similar way as private class method works also private getters and setters in the class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdgo7trcso%2Fimage%2Fupload%2Fv1630607799%2FGreetings%2FScreenshot_2021-09-02_at_20.33.20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdgo7trcso%2Fimage%2Fupload%2Fv1630607799%2FGreetings%2FScreenshot_2021-09-02_at_20.33.20.png" alt="Night" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  WeakRef
&lt;/h3&gt;

&lt;p&gt;WeakRef that stands for Weak references are primarily used to implement caches or mappings for holding large objects. JavaScript has a garbage collector mechanism that collects and automatically removes variables that are not reachable but this mechanism differs with reference objects which keeps the object in memory and leaves you with less memory. Thus, with use of WeakRef large objects are not kept alive because they appears in a cache or mapping. However you should be careful when using them and possible avoid them as advised from TC39 even though they can be useful in some cases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdgo7trcso%2Fimage%2Fupload%2Fv1630608108%2FGreetings%2FScreenshot_2021-09-02_at_20.41.19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdgo7trcso%2Fimage%2Fupload%2Fv1630608108%2FGreetings%2FScreenshot_2021-09-02_at_20.41.19.png" alt="Night" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the example above we created an imaginary large object and then created Weak reference by using new instance with new WeakRef. We can access the reference by calling the method &lt;code&gt;deref()&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Final thoughts
&lt;/h3&gt;

&lt;p&gt;Whenever I learn and try something new I am always passionate to share my knowledge by helping other people on reaching their goals.I really hope you find this article interesting and helpful on learning ES12 features from it.&lt;/p&gt;

&lt;p&gt;Any feedback, suggestions or recommendations are appreciated.  This will really help and motivate me for further post sharing&lt;/p&gt;

&lt;h4&gt;
  
  
  Happy coding!
&lt;/h4&gt;

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

</description>
      <category>es12</category>
      <category>ecmascript2021</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Infinite Scroll with React.js</title>
      <dc:creator>Naim Latifi</dc:creator>
      <pubDate>Mon, 11 May 2020 11:32:28 +0000</pubDate>
      <link>https://dev.to/naimlatifi5/infinite-scroll-with-react-js-4ei1</link>
      <guid>https://dev.to/naimlatifi5/infinite-scroll-with-react-js-4ei1</guid>
      <description>&lt;p&gt;As part of my react app series I just finished "Infinite scrolling with React.js" app. Check it out &lt;a href="https://heuristic-tesla-2575dc.netlify.app/"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>infinitescroll</category>
      <category>api</category>
    </item>
    <item>
      <title>Quote CRUD App build with React.js</title>
      <dc:creator>Naim Latifi</dc:creator>
      <pubDate>Wed, 15 Apr 2020 14:40:07 +0000</pubDate>
      <link>https://dev.to/naimlatifi5/quote-crud-app-build-with-react-js-51k6</link>
      <guid>https://dev.to/naimlatifi5/quote-crud-app-build-with-react-js-51k6</guid>
      <description>&lt;p&gt;We frontend developers are not limited to only one framework for building Single Page Applications (SPA). Recently I have been working mostly with Vue.js framework for building those applications but I wanted to extend my knowledge further and learn how React.js works under the hood. So, I created this simple Quote CRUD app with the purpose of applying React.js concepts and shared it with you. 😊&lt;/p&gt;

&lt;p&gt;The purpose of this app is as a user to be able to create, read, edit and delete a quote. Technologies and React concepts I applied in this app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quickly get started with create-react-app CLI ✅&lt;/li&gt;
&lt;li&gt;Created class components for showing and creating quotes. ✅&lt;/li&gt;
&lt;li&gt;Instead of writing pure vanilla JS for creating HTML elements and appending to DOM I learned to use JSX and understood that everything in React is JavaScript. ✅&lt;/li&gt;
&lt;li&gt;In order to navigate between pages, I installed a react-router. ✅&lt;/li&gt;
&lt;li&gt;Local state in React class components and setState method for mutating the state values. ✅&lt;/li&gt;
&lt;li&gt;As my app began to grow I noticed that it could be better to separate it in multiple function components and reuse those components. ✅&lt;/li&gt;
&lt;li&gt;Controlled components for handling user form inputs and events. ✅&lt;/li&gt;
&lt;li&gt;Props for passing data between components and conditional rendering to render part of the code/component. ✅&lt;/li&gt;
&lt;li&gt;React-bootstrap is used for UI components. ✅&lt;/li&gt;
&lt;li&gt;Sass as CSS pre-processor for component styling. ✅&lt;/li&gt;
&lt;li&gt;React masonry CSS for masonry layout. ✅&lt;/li&gt;
&lt;li&gt;Firebase real database for saving, fetching and updating the data. ✅&lt;/li&gt;
&lt;li&gt;For this tiny app I would probably not need an extra library for managing the state of this application however I wanted to integrate Redux and hold the application state into a store, using reducers to specify how the application's state changes, using actions to send data from my application to the store by dispatching, etc. ✅&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://quotes-app-react.firebaseapp.com/" rel="noopener noreferrer"&gt;Link: Quote app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you find it interesting!&lt;/p&gt;

&lt;p&gt;Cheers 🍻&lt;br&gt;
Stay healthy!&lt;/p&gt;

</description>
      <category>react</category>
      <category>sass</category>
      <category>firebase</category>
      <category>redux</category>
    </item>
    <item>
      <title>Question: What is your favorite JavaScript framework?</title>
      <dc:creator>Naim Latifi</dc:creator>
      <pubDate>Fri, 13 Mar 2020 10:56:56 +0000</pubDate>
      <link>https://dev.to/naimlatifi5/question-what-is-your-favorite-javascript-framework-3352</link>
      <guid>https://dev.to/naimlatifi5/question-what-is-your-favorite-javascript-framework-3352</guid>
      <description>&lt;p&gt;Please comment below why you would prefer React, Vue or Angular. Pros and cons of each :) &lt;/p&gt;

</description>
      <category>react</category>
      <category>vue</category>
      <category>angular</category>
    </item>
    <item>
      <title>Understanding "this" in JavaScript</title>
      <dc:creator>Naim Latifi</dc:creator>
      <pubDate>Thu, 08 Aug 2019 21:31:23 +0000</pubDate>
      <link>https://dev.to/naimlatifi5/understanding-this-in-javascript-cm2</link>
      <guid>https://dev.to/naimlatifi5/understanding-this-in-javascript-cm2</guid>
      <description>&lt;p&gt;Many times &lt;code&gt;this&lt;/code&gt; keyword in JavaScript can be confused if it is not understood correctly. In this article, I will show you how &lt;code&gt;this&lt;/code&gt; works with some code examples and how its value is assigned in a different context in JavaScript. There are different scenarios where &lt;code&gt;this&lt;/code&gt; can change its value: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      - `this` in a global context, function invocation

      - `this` in an object constructor

      - `this` in an object method.

      - `this` with arrow function (=&amp;gt;) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But wait before jumping to the examples we just mentioned &lt;strong&gt;context&lt;/strong&gt; so what is it in JavaScript? The &lt;strong&gt;context&lt;/strong&gt; in JavaScript is always the value of &lt;code&gt;this&lt;/code&gt; that is determined during the execution code. To clearly understand let's go now with code examples and see how it works on each scenario.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What is 'this' value?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Window&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;simpleFunction&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What is the value of this inside function?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//  Window&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;simpleFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the code example 1 &lt;code&gt;this&lt;/code&gt; refers to the global object which is determined from the execution context and wherein the browser is the global object (&lt;em&gt;window&lt;/em&gt;). Similarly, when invoking the function &lt;code&gt;this&lt;/code&gt; inside that function refers to the global object. But what &lt;code&gt;this&lt;/code&gt; refers inside constructor function? We see this in the code example 2 as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lastName&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;displayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Your name &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and lastName 
     &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;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;George&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Superman&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// George Superman&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we see from the code above we create a construction function &lt;em&gt;Person&lt;/em&gt; and then we invoke a new instance object &lt;em&gt;george&lt;/em&gt;. &lt;code&gt;this&lt;/code&gt; value, in this case, refers to the newly created object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;greetings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="na"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="na"&gt;printGreeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What 'this' refers to inside object method? &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "this" in object refers to the object itself- greetings&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;printGreeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From code example 3 above we created an object which has a property and method &lt;em&gt;printGreeting&lt;/em&gt;. We are calling the property name inside the method with &lt;code&gt;this&lt;/code&gt; which its value refers to the object itself. &lt;/p&gt;

&lt;p&gt;We saw from the code examples above that &lt;code&gt;this&lt;/code&gt; value inside a function refers to the global object and inside of an object method refers to the object itself but what about if we want to invoke an object method inside another function and reference &lt;code&gt;this&lt;/code&gt;. What will be the value of &lt;code&gt;this&lt;/code&gt; in this context? Let's see with the code example 4 and where we will run it with &lt;strong&gt;strict mode&lt;/strong&gt; as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;George&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="nf"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Uncaught TypeError: Cannot read property 'firstName' of undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the code we see that we get an error because JavaScript interpret does not understand object property &lt;em&gt;this.firstName&lt;/em&gt; because &lt;code&gt;this&lt;/code&gt; value inside a function is set during the execution context. But, how to overcome this issue? JavaScript functions are a special type of objects which contains methods &lt;em&gt;call()&lt;/em&gt;, &lt;em&gt;apply()&lt;/em&gt; and &lt;em&gt;bind()&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  call(), apply() and bind()
&lt;/h4&gt;

&lt;p&gt;These methods are inherited by all functions in order to get the value of &lt;code&gt;this&lt;/code&gt; in any desired context. The differences between these function methods are that &lt;em&gt;call()&lt;/em&gt; method requires parameters as a comma-separated and apply() method in the form of the array list and are executed immediately whereas bind() method can be executed later in any desired context. The code example 5 below shows how we can use these methods and set &lt;code&gt;this&lt;/code&gt; in any desired context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;George&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="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="c1"&gt;// Hello George&lt;/span&gt;
  &lt;span class="nx"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello there&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// Hello there George&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;executeLater&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;executeLater&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// George&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; with arrow function =&amp;gt;
&lt;/h4&gt;

&lt;p&gt;With ECMAScript 2015 (ES6) arrow function was introduced as a new feature in the world of JavaScript. There are a lot of benefits on using arrow functions which brings shorter function syntax to no need on binding &lt;code&gt;this&lt;/code&gt;. With the use of arrow function &lt;code&gt;this&lt;/code&gt; is bounded lexically in the enclosing context. Let's see this in action with code example to make it clear.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;greetings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;printGreeting&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// Hello&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 we go back to the previous code example 2 we saw that &lt;code&gt;this&lt;/code&gt; inside object refers to the object itself but what if we are required that our greeting to be displayed after 1 second. Then to get the required result we need to modify the code as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;greetings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;printGreeting&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// we can set bind(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;greetings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;printGreeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see that after executing the method &lt;em&gt;printGreeting()&lt;/em&gt; we get undefined because the &lt;em&gt;setTimout()&lt;/em&gt; function invocation sets &lt;code&gt;this&lt;/code&gt; context to the global object. However, here to achieve the result we can use &lt;em&gt;bind()&lt;/em&gt; method as we saw previously or we can create a closure just before the &lt;em&gt;setTimeout()&lt;/em&gt; function with &lt;em&gt;self=this&lt;/em&gt; but we don't need to do this as we can achieve the desired result with the use of arrow function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;greetings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;printGreeting&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;printGreeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Hello&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now as the result on the console we will get greeting &lt;strong&gt;hello&lt;/strong&gt; because with use of arrow function "&lt;em&gt;this&lt;/em&gt;" is bound to the enclosing context and thus our "&lt;em&gt;greetings&lt;/em&gt;" object.&lt;/p&gt;

&lt;p&gt;That's it! :)&lt;/p&gt;

&lt;p&gt;Hopefully, you find interesting this article and will help you in understanding &lt;code&gt;this&lt;/code&gt; in JavaScript and its value in a different context. &lt;/p&gt;

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