The Syntax of Arrow Functions in JavaScript

in #technology7 years ago (edited)

The general form of arrow functions in JavaScript consists of the parameter list, the fat arrow, and the body block. Here is an example:

    let f = (a, b) => {
        let c = 4;
        return a + b - c;
    };

However, this could be simplified in some cases. First, if the parameter list contains only one parameter, the parentheses can be removed. For example,

    let f = x => {
        let y = 4;
        return x + y;
    };

Of course, if there are no parameters, the parentheses are required:

    let f = () => {
        let y = 4;
        return 2 + y;
    };

Second, if the body block consists only of a return statement, the curly braces and the return keyword can be eliminated. This is by far the simplest version:

    let f = x => x * x;

In this case, if the return value is an object literal, we need to put it in parentheses to avoid confusion with a block:

    let f = aname => ({
        name: aname
    });

Without parentheses, this could lead to confusing cases, because there are now many shortcuts for object literals. For example, the above function could also be written as the following:

    let f = name => ({
        name
    });

Without parentheses, this would be ambiguous.

Sort:  

Dear sir
You always post knowledgeable blog.
Thanks for that.

Thank you. Glad you liked it.

It's my pleasure that you recognized me.

this post i like also...

great value post.....Loves to see my followed person's post getting more Upvote

Such an informative and intresting post @ghassemkiani !

Calling @originalworks :)
img credz: pixabay.com
Nice, you got a 5.0% @minnowbooster upgoat, thanks to @ghasemkiani
Want a boost? Minnowbooster's got your back!

The @OriginalWorks bot has determined this post by @ghasemkiani to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!
For more information, Click Here!
Special thanks to @reggaemuffin for being a supporter! Vote him as a witness to help make Steemit a better place!

Right theory

thank you very much for this post...
steemlogo.png

thank you very much for this post...
steemlogo.png

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63101.67
ETH 2588.03
USDT 1.00
SBD 2.74