ES6 is a short form of 6th Edition of the ECMAScript standard.
We can mostly use a function anywhere for example in jQuery,
$(“#confirm-btn”).click(function (event) {
SendtheDetails();
RecordIt();
});
In earlier time JavaScript added lambda function feature before Java, python, C++ and C#.
So JavaScript lambda function little bit longer than others.
//simple function in six languages.
function (a) { return a > 0; } // JS
[](int a) { return a > 0; } // C++
(lambda (a) (> a 0)) ;; Lisp
lambda a: a > 0 # Python
a => a > 0 // C#
a -> a > 0 // Java
That’s why ES6 introduces a new syntax for writing functions.
// ES5
var selected = allJobs.filter(function (job) {
return job.isSelected();
});// ES6
var selected = allJobs.filter(job => job.isSelected())
When you just need a simple function with one argument, the new arrow function syntax is simply Identifier => Expression. You get to skip typing function and return, as well as some parentheses, braces, and a semicolon
Here we should consider some constraints in this feature let’s see.
var selected = numbers.map(x => x*x);
here I used only one identifier so it’s not necessary to put parenthesis but if,
var selected = numbers.map((x,y) => x+y);
we used more than one identifier we should put it inside the parenthesis.
$(“#confetti-btn”).click(event => {
playTrumpet();
fireConfettiCannon();
});
In this case it contains more than one expression so we put inside braces as well.
Note that an arrow function with a block body does not automatically return a value. Use a return statement for that.
That’s it. Here I tried to give a brief knowledge about one of the special feature in ES6 is arrow function.
I strongly recommended to learn this to work efficiently because it reduce lines of codes and helps to avoid mistakes as well.
In the ReactJs this Arrow function plays key role not only React and others also.
In my point of view if you are willing to become a Smartest Developer with less number of code then learn about these type of technologies.
THANK YOU.
reference: Mozilla hacks Docs & https://www.appbrewery.co/.