ES6 Arrow Functions
ES6 Arrow Functions
ES6HOME
Arrow function in es6 anonymous function. It does not have any name and does not start with function keyword.
Arrow function in ES6:
var arrowfun = () => {
console.log("Example Arrow Function");
};
Optional parentheses for the single parameter
var data = n => {
console.log(n);
}
data(10);
Optional braces for single statement/parameter and the empty braces if there is no parameter to be passed.
var display = () => console.log( "Hello World" );
display();
Arrow function with multiple parameters:
let sum = (a, b) => a + b;
document.write(sum(10, 20)); // Output 30
GO TOP
Arrow function with default parameters:
In ES6, parameters can initialize with some default values, if there is no value passed to it, or it is undefined. You can see the illustration for the same in the following code:
For example
var display = (a, b=200) => {
console.log(a + " " + b);
}
display(100);
o/p
100 200
display(100,300);
100 300 // default value overwritten
MOVETOP
Arrow function with Rest parameters:
Rest parameters allow us to pass any number of parameters of the same type.
Declaring the rest parameter:
the parameter prefixed with the spread operator with three dots(...)
example :
var display = (a, ...args) => {
console.log(a + " " + args);
}
display (10,20,30,40,50,60,70,80);
o/p
10 20 30 40 50 60 70 80
Comments
Post a Comment