4 ms·
It's not just syntax, but some semantics too. Last expression in a function is returned. This includes assignment statements which are in fact expressions.
by hasenj 16y ago
It's not just syntax, but some semantics too.
Last expression in a function is returned. This includes assignment statements which are in fact expressions.
foo = () ->
a = bar()
z = () ->
if a
b = fu()
else
b = kar()
Here, foo returns the function z, and z returns either fu() or kar() while assigning the value to b
One could argue this is just syntax but I think this way beyond the kind of thing that comes to mind when somebody thinks of "syntactic sugar".
- Homunculiheaded 16y agoDespite all of the weirdness of JavaScript, the lack of implicit return is it's biggest design flaw imho. The functional/scheme-like virtues of JS are well touted, but this small missing feature means implementing them always requires the slight but constant overhead of ending every expression with 'return', which then means that programming in a way which should be natural to javascript, isn't.
- jashkenas 16y agoFYI, you don't have to include an empty parameter list for defining functions that don't take any arguments. Just the arrow will do. foo = -> a = bar() z = -> if a b = fu() else b = kar()