<?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: Nay Lin Aung</title>
    <description>The latest articles on DEV Community by Nay Lin Aung (@nladev).</description>
    <link>https://dev.to/nladev</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%2F225678%2F19bc87d8-ebd7-4715-b6b8-0a96ae0a1797.png</url>
      <title>DEV Community: Nay Lin Aung</title>
      <link>https://dev.to/nladev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nladev"/>
    <language>en</language>
    <item>
      <title>Javascript Arrow Function</title>
      <dc:creator>Nay Lin Aung</dc:creator>
      <pubDate>Sat, 01 Aug 2020 15:46:15 +0000</pubDate>
      <link>https://dev.to/nladev/javascript-arrow-function-2jpa</link>
      <guid>https://dev.to/nladev/javascript-arrow-function-2jpa</guid>
      <description>&lt;p&gt;The Arrow Function Syntax&lt;br&gt;
From an unnamed function it is a small change to move to an arrow function. All we need to do is go from this format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ( ...args... ) { ...body... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;( ...args... ) =&amp;gt; { ...body... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example add() function from the previous section can be written as an arrow function as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = (a, b) =&amp;gt; {
    return a + b;
}

console.log('1 + 2 is ' + add(1, 2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the function body is a single return statement, then the syntax can be further simplified by removing the curly braces and the return keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = (a, b) =&amp;gt; a + b;
&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;console.log('1 + 2 is ' + add(1, 2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the function takes no arguments, then you must use an empty argument list, just like you would on a function defined in the old style. Below I have added a second function called func that takes no arguments and returns the string 'foo':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = (a, b) =&amp;gt; a + b;
const func = () =&amp;gt; 'foo';
&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;console.log('1 + 2 is ' + add(1, 2));
console.log(func());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's run this new version of func.js to confirm the new function works as expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ node func.js
1 + 2 is 3
foo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Arrow Function Support&lt;br&gt;
Arrow functions have very good support, which includes Node.js and all major web browsers with the only exception of Internet Explorer. See the compatibility matrix for specific details about browser versions.&lt;/p&gt;

&lt;p&gt;This means that in most cases you should be able to include arrow functions in your JavaScript code without worries.&lt;/p&gt;

&lt;p&gt;Transpiling Fancy JavaScript to Basic JavaScript&lt;br&gt;
If your application needs to work on Internet Explorer or any other non-mainstream legacy browser, you can use arrow functions in your codebase and then transpile the code to the older style using babel.&lt;/p&gt;

&lt;p&gt;To see how this would work, you can create a JavaScript package in the directory where you have the func.js file with this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will have to answer a few questions regarding the package, but for this test it is sufficient to press Enter on all of them to accept the defaults. Then install the three packages needed for babel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install --save-dev @babel/cli @babel/core @babel/preset-env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These three packages respectively implement the CLI, the core compiler/transpiler, and a convenient configuration preset that converts all the new JS constructs back into a safe version of JavaScript that works everywhere.&lt;/p&gt;

&lt;p&gt;To convert the func.js file and output the result to the terminal, execute the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./node_modules/.bin/babel --presets @babel/preset-env func.js
"use strict";

var add = function add(a, b) {
  return a + b;
};

var func = function func() {
  return "foo";
};

console.log('1 + 2 is ' + add(1, 2));
console.log(func());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need to support legacy JavaScript platforms, then incorporating babel into your project build should allow you to use not only arrow functions but also many other language features.&lt;/p&gt;

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