Broad Network


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

This is part 2 of my series, Functions in ECMAScript. In this part of the series, I talk about arrow functions in ECMAScript. An arrow function is a function expression, which typically does not have a name or the reserved word, function. It can be assigned to a variable. Between the parameters group (list) and the function body, is the punctuator, => .

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);

If there is only one statement, then there is no need for the block (curly brackets). The result of the statement is returned. Try the following code:

        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 Basics
ECMAScript 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

Comments

Become the Writer's Follower
Send the Writer a Message