Arrow functions and return values

David Bruant bruant.d at gmail.com
Thu Nov 29 01:21:42 PST 2012


Le 29/11/2012 09:41, Jussi Kalliokoski a écrit :
> 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. To actually return b * c, you 
> should drop the semicolon:
>
> var a = (c) => {
>   var b = 2;
>   b * c
> }
>
> a(4);
I initially only read the 2 snippets and saw no difference. I realized 
the dropped semicolon only after reading the text in the middle.
Making a semantic difference out of a present or missing semicolon seems 
it would cause a lot of wasted time debugging a program. Especially in 
JavaScript where semicolons are optional. JS devs have their eyes 
trained to not worry about semicolons (which is probably why I didn't 
see a difference at first).

I would expect 8 to be returned in both cases, semicolon or not.

> This would be consistent with, for example, Rust and would help avoid 
> annoying accidental returns (see [1] for discussion about this wrt 
> CoffeeScript).
I wish JavaScript kept being consistent with JavaScript.

David


More information about the es-discuss mailing list