<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Parth Sethi</title>
    <description>The latest articles on Forem by Parth Sethi (@victorakaps).</description>
    <link>https://forem.com/victorakaps</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%2F369143%2F4cb016fd-9faa-43da-b769-ea0e6ea40bb7.jpg</url>
      <title>Forem: Parth Sethi</title>
      <link>https://forem.com/victorakaps</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/victorakaps"/>
    <language>en</language>
    <item>
      <title>Arrow Functions</title>
      <dc:creator>Parth Sethi</dc:creator>
      <pubDate>Mon, 05 Oct 2020 11:34:59 +0000</pubDate>
      <link>https://forem.com/dsc_ciet/arrow-functions-7f2</link>
      <guid>https://forem.com/dsc_ciet/arrow-functions-7f2</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;WHAT WILL YOU LEARN IN THIS QUICK READ ABOUT JS FUNCTIONS&lt;/strong&gt;?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Regular and Arrow javascript functions&lt;/li&gt;
&lt;li&gt;How to convert a regular function into an arrow one&lt;/li&gt;
&lt;li&gt;An Introduction to &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword&lt;/li&gt;
&lt;li&gt;When and when Not to use ES6’s Arrow function(s)
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2D1mJ7_Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f26f3cpr1lhx45zvgrev.png" alt="Alt Text"&gt;
The image above depicts how a regular function in javascript looks like, you might know them as a collection of statements.
In javascript, a function can itself be a &lt;strong&gt;value&lt;/strong&gt; or an expression, which can be assigned to a variable.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Let’s assign the previous example function to a variable&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J7G3R1Xl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yfbf0e3yqu6b9jx5g8v8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J7G3R1Xl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yfbf0e3yqu6b9jx5g8v8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
The function name can be omitted in the syntax and the variable &lt;strong&gt;&lt;em&gt;doSomething&lt;/em&gt;&lt;/strong&gt; now hold’s this function as it’s &lt;strong&gt;value&lt;/strong&gt;. Const is usually preferred for assigning functions. You can use let too.&lt;br&gt;
Now moving towards arrow functions which were introduced in ES6. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How can we convert regular function into an arrow one?&lt;/strong&gt; Well its fairly simple
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Omit the function keyword&lt;/li&gt;
&lt;li&gt;Add the arrow notation (i.e. =&amp;gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s it :) &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WuvZ9akP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ckymcglxmj9ejxrwf6nc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WuvZ9akP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ckymcglxmj9ejxrwf6nc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
We can simplify it further , if the function body contains a single statement, and if that single statement is a return statement we can completely omit the { curly braces } and return keyword.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KeavECnC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ln5o0un4f02cgvs3j1d2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KeavECnC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ln5o0un4f02cgvs3j1d2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AN EXAMPLE&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jLYt4nda--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6r27tqlsm1pne2elk0fu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jLYt4nda--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6r27tqlsm1pne2elk0fu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;When and When not to use arrow functions&lt;/strong&gt;?
&lt;/h3&gt;

&lt;p&gt;Arrow functions do not have it’s own &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; context, this will cause some confusions when arrow fucntions are used inside of methods, and when you try to use &lt;em&gt;this&lt;/em&gt; to access the parent object. &lt;br&gt;
Well if you are wondering what’s &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword, well don’t worry we got you covered,&lt;br&gt;
&lt;strong&gt;&lt;em&gt;This&lt;/em&gt;&lt;/strong&gt; keyword refers to the object it belongs to, depending where it is used.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's grasp this with an example for both cases
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Here the function will log the global window object when it’s run&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZL_hnJIj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eprag00rny4ycy6cj56s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZL_hnJIj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eprag00rny4ycy6cj56s.jpg" width="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because in a standalone function this refers to the global object. Which in case of a browser, will be the window object.&lt;br&gt;
&lt;strong&gt;OUTPUT:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--myRavM37--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b79ltfusqymnvgqjpqer.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--myRavM37--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b79ltfusqymnvgqjpqer.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However in this case function will log the object which contains the method&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aiw5-Gh2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/llzcjvw7vgt66kraun1b.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aiw5-Gh2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/llzcjvw7vgt66kraun1b.jpg" width="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because here, the parent object of that method is “obj”,&lt;br&gt;
Using this keyword anywhere inside a method will be pointing out to the current object itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9kcZdPma--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iczzmimq98di7fsjshqv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9kcZdPma--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iczzmimq98di7fsjshqv.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Now back to the arrow functions and why using this keyword in them is tricky:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EAsXliT_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7lcrqcruoi3j074ch2a9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EAsXliT_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7lcrqcruoi3j074ch2a9.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What is happening here?&lt;/em&gt;&lt;/strong&gt; In the regular &lt;strong&gt;funGreet()&lt;/strong&gt;method, the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword is refering to the parent object, which is the &lt;strong&gt;greet object&lt;/strong&gt; in this case&lt;br&gt;
However In the &lt;strong&gt;arrow&lt;/strong&gt; function &lt;strong&gt;funArrowGreet()&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; does not point to the parent object, but it is pointing to the global object which is &lt;strong&gt;window&lt;/strong&gt; for browsers. That's why we are getting &lt;strong&gt;&lt;em&gt;undefined&lt;/em&gt;&lt;/strong&gt; as the output.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;So Now question arises where and when to use these tricky arrow functions?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;They are best used as &lt;strong&gt;callback functions&lt;/strong&gt;, and can be used as callbacks for &lt;em&gt;Array map&lt;/em&gt;, &lt;em&gt;reduce&lt;/em&gt;, &lt;em&gt;filter&lt;/em&gt;, &lt;em&gt;forEach&lt;/em&gt; etc. Basically anywhere where this isn’t an issue. They come handy when you just have one statement function as they return that single line expression, however when your function has a block body it has to be accompanied by an return statement. In my opinion Arrow functions looks &lt;strong&gt;neater&lt;/strong&gt; and  you have to code less, so that’s fancy :) &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>jstricks</category>
      <category>firstblog</category>
    </item>
  </channel>
</rss>
