Arrow Functions in ECMAscript 2015
Functions in ECMAScript - Part 2
ECMAScript 6
Foreword: In this part of the series, I talk about arrow functions in ECMAScript.
By: Chrysanthus Date Published: 25 May 2016
Introduction
Illustration
The following has a function expression assigned to the variable, fn:
fn = (word1, word2) =>
{
str = word1 + " who laughs " + word2 + ", laughs best.";
return str;
}
stri = fn('He','last');
alert(stri);
If there is only one parameter, then parentheses for the parameter group is optional Try the following code:
<script type="text/ECMAScript">
fn = word1 =>
{
str = word1 + " who laughs last, laughs best.";
return str;
}
stri = fn('He','last');
alert(stri);
</script>
A function with no parameters still requires parentheses. Try the following code:
fn = () =>
{
str = "He who laughs last, laughs best.";
return str;
}
stri = fn('He','last');
alert(stri);
fn = () =>
"He who laughs last, laughs best.";
stri = fn('He');
alert(stri);
Default parameters are supported. Try the following code:
fn = (word1, word2="last") =>
{
str = word1 + " who laughs " + word2 + ", laughs best.";
return str;
}
stri = fn('He');
alert(stri);
Destructuring within the parameter list is also supported. In the following code, each parameter is an expression resulting in a value. The values passed to the function body are 1, 2 and 3, for a, b, and c respectively. The parameters are separated by commas.
fn = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;
ret = fn();
alert(ret);
The display (output) is 6.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
ECMAScript BasicsECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course
BACK NEXT