<?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: Reddy Divya</title>
    <description>The latest articles on DEV Community by Reddy Divya (@reddydivya).</description>
    <link>https://dev.to/reddydivya</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%2F668181%2Fb4e7672d-9862-4d8b-90c6-284405047112.jpeg</url>
      <title>DEV Community: Reddy Divya</title>
      <link>https://dev.to/reddydivya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/reddydivya"/>
    <language>en</language>
    <item>
      <title>Arrow Function</title>
      <dc:creator>Reddy Divya</dc:creator>
      <pubDate>Thu, 02 Jun 2022 12:01:33 +0000</pubDate>
      <link>https://dev.to/reddydivya/arrow-function-4eob</link>
      <guid>https://dev.to/reddydivya/arrow-function-4eob</guid>
      <description>&lt;p&gt;Hello Readers 👋&lt;/p&gt;

&lt;p&gt;In JavaScript, we often &lt;strong&gt;don't need to name our functions&lt;/strong&gt;, &lt;strong&gt;especially when passing a function as an argument to another function&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Instead, we can create &lt;strong&gt;inline functions&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;We don't need to name these functions because we don't reuse them anywhere else.&lt;/p&gt;

&lt;p&gt;Let's see the syntax 👇&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Syntax
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Arrowfun = function() {
  const myVar = "value";
  return myVar;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can &lt;strong&gt;omit&lt;/strong&gt; the &lt;strong&gt;function keyword&lt;/strong&gt;. See the below syntax👇&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(or)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Arrowfun = () =&amp;gt; {
  const myVar = "value";
  return myVar;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are different syntaxes to use an arrow function for different scenarios. Confused?&lt;br&gt;
Let me clarify. &lt;/p&gt;
&lt;h3&gt;
  
  
  Case 1: One parameter
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Parentheses are optional&lt;/strong&gt; for one parameter of an arrow function. &lt;/p&gt;

&lt;p&gt;See the example👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let cube = a =&amp;gt; {
    const sqr = a * a * a;
    return sqr;
} 

console.log(cube(3));   // 27
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Case 2:  No return statement
&lt;/h3&gt;

&lt;p&gt;When there's no return we can &lt;strong&gt;omit the body braces&lt;/strong&gt; of an arrow function.&lt;/p&gt;

&lt;p&gt;See the example👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let cube = a =&amp;gt; a * a * a;

console.log(cube(3));   // 27
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Return keyword is a must if we keep body braces. Otherwise, it gives &lt;strong&gt;undefined&lt;/strong&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Case 3:  Multiple parameters
&lt;/h3&gt;

&lt;p&gt;Multiple parameters &lt;strong&gt;need parentheses&lt;/strong&gt;. See the example👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let product = (a, b) =&amp;gt;  a * b;

console.log(product(12, 4));   // 48
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;  It throws an error when there are no parentheses for multiple parameters&lt;/p&gt;

&lt;p&gt;of an arrow function. See the image👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z6lB7Fm6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isc03bdq3tj49qqaxzlz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z6lB7Fm6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isc03bdq3tj49qqaxzlz.png" alt="Image description" width="578" height="45"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 4: Multiline statements
&lt;/h3&gt;

&lt;p&gt;Multiline statements &lt;strong&gt;require body braces and return statements&lt;/strong&gt; otherwise, it throws an error.&lt;/p&gt;

&lt;p&gt;See the example👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let square = a =&amp;gt; {
  let result = a * a;
  return result;
};

console.log( square(12) );      //144
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Case 5: Multiple params &amp;amp; Multiline statements
&lt;/h3&gt;

&lt;p&gt;Multiple params &lt;strong&gt;require parentheses&lt;/strong&gt;. Multiline statements &lt;strong&gt;require body braces and return statements&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;See the example👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let product = (a, b) =&amp;gt; {
  let result = a * b;
  return result;
};

console.log( product (11, 12) );     //132
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Well done readers&lt;/strong&gt;, We are done with the basic syntaxes of an arrow function. &lt;/p&gt;

&lt;p&gt;Now, Let's dive into the &lt;strong&gt;Advanced syntax&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced syntax
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Case 1: Object literal
&lt;/h3&gt;

&lt;p&gt;We &lt;strong&gt;require parentheses around the expression&lt;/strong&gt; to return an object's literal. See the example👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myFunc = params =&amp;gt; ({name: "divya"}) // returns the object {name: "divya"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code inside braces ({}) is parsed as a sequence of statements i.e. name is treated like a label, not a key in an object literal. &lt;/p&gt;

&lt;p&gt;Arrow function supports &lt;strong&gt;Rest parameters&lt;/strong&gt;, &lt;strong&gt;Default parameters&lt;/strong&gt;, &lt;strong&gt;Destructuring&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Let's look into the examples👇&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 2: Rest parameters
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var lang = (arg1, arg2, ...moreArgs) =&amp;gt; {

    console.log("arg1 : ", arg1);   //logs arg1
    console.log("arg2 : ", arg2);  //logs arg2
    console.log("moreArgs: ", moreArgs); //logs an array of any other arguments you pass in after arg2
};

console.log(lang('C', 'C++', 'Java', 'HTML', 'JS', 'CSS'));

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

&lt;/div&gt;



&lt;p&gt;Let's see the output👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MsqXTzkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux17hzlqchwvymzgv3u8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MsqXTzkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux17hzlqchwvymzgv3u8.png" alt="Image description" width="639" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 3: Default parameters
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var lang = (arg1, arg2 = "HTML") =&amp;gt; {

    console.log("arg1 : ", arg1);   //logs arg1
    console.log("arg2 : ", arg2);  //logs arg2
};

console.log(lang("JavaScript"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see the output👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bkUXJSJZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7wsrw7ca0tg7vvt92eu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bkUXJSJZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7wsrw7ca0tg7vvt92eu.png" alt="Image description" width="454" height="77"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 4: Destructuring within params
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;([a, b] = [10, 20]) =&amp;gt; a + b;  // result is 30
({ a, b } = { a: 10, b: 20 }) =&amp;gt; a + b; // result is 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Line breaks:
&lt;/h2&gt;

&lt;p&gt;An arrow function cannot contain a line break between its parameters and its arrow. See below code snippet👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var func = (a, b, c)
  =&amp;gt; 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This throws an error &lt;strong&gt;SyntaxError: Unexpected token '=&amp;gt;'&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;However, this can be amended(&lt;strong&gt;making minor changes&lt;/strong&gt;) by putting the line break after the arrow. You can also put line breaks between arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var func = (a, b, c) =&amp;gt;
  1;

var func = (a, b, c) =&amp;gt; {
  return 1
};

var func = (
  a,
  b,
  c
) =&amp;gt; 1;

// no SyntaxError thrown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see a few more examples👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myFunc = () =&amp;gt; "value";      //This code will still return the string value by default.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const magic  = () =&amp;gt;  new Date();       //returns a Date
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sum = a =&amp;gt; a + 100;

console.log(sum(12));   //logs 112
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hopefully, you got some clarity on &lt;strong&gt;Arrow Function&lt;/strong&gt; ✌.&lt;/p&gt;

&lt;p&gt;I value your time and I gave my best to write this article. Thanks for sparing your time in reading this article till the end.&lt;/p&gt;

&lt;p&gt;So what are you waiting for? Any further doubts? Drop it below 👇&lt;/p&gt;

&lt;p&gt;Please, feel free to share your feedback on this article.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
