Arrow functions and return values

Brendan Eich brendan at mozilla.com
Thu Nov 29 01:32:55 PST 2012


Jussi Kalliokoski wrote:
> It's a bit unclear to me how arrow functions react to semicolons, for 
> example:
>
> var a = (c) => {
>   var b = 2;
>   b * c;
> }
>
> a(4);
>
> To me, it seems like this should return undefined. After all, the last 
> statement in the function is empty.

Not by the grammar.

You would need a second ; after b * c; to spell the empty statement.

> To actually return b * c, you should drop the semicolon:
>
> var a = (c) => {
>   var b = 2;
>   b * c
> }
>
> a(4);
>
>
> This would be consistent with, for example, Rust and would help avoid 
> annoying accidental returns (see [1] for discussion about this wrt 
> CoffeeScript).

That's a fine thing in languages like Rust without ASI, which also have 
type-checking needs not in JS. JS has ASI and no type-checking, so no-go.

> Cheers,
> Jussi
>
> [1] https://github.com/jashkenas/coffee-script/issues/2477

CoffeeScript has no way to avoid implicit return, but JS has 'function' 
and always will.

/be


More information about the es-discuss mailing list