It seems like CoffeeScript automatically returns the last item in a scope. Can I avoid this functionality?
-
Why would you want to? Can't you just discard the returned value when you like? – Chris Sep 12 '11 at 17:22
-
11@Chris: When there's no meaningful return value, leaking whatever happens to be the result of the last statement isn't a good idea. – Sep 12 '11 at 17:24
-
11@Chris this can have performance implications with comprehensions / for / while loops too, as coffeescript may collect the result of each iteration in an array, if the return value of the loop is used (or if it's the last expression of a function) – Arnaud Le Blanc Sep 12 '11 at 17:34
-
1I just had this issue with a contructor. I defined a function in the last line of the constructor. So this was returned instead of my "Class-Function". I had to explicitly return this. Just saying, that this can be an issue. – Markus May 09 '14 at 14:56
5 Answers
You have to explicitly return nothing, or to leave an expression evaluating to undefined at the bottom of your function:
fun = ->
doSomething()
return
Or:
fun = ->
doSomething()
undefined
This is what the doc recommends, when using comprehensions:
Be careful that you're not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value — like true — or null, to the bottom of your function.
You could, however, write a wrapper like this:
voidFun = (fun) ->
->
fun(arguments...)
return
(Notice the splat operator here (...
))
And use it like this when defining functions:
fun = voidFun ->
doSomething()
doSomethingElse()
Or like this:
fun = voidFun(->
doSomething()
doSomethingElse()
)

- 98,321
- 23
- 206
- 194
-
Thank you for this informative answer. I too have been wondering about this as I'm presently learning coffeescript. I'm curious if you can shed some light on what the best choice, or what the worst choice would be when preventing the return of a value? Specifically, I have been told the options are to add either `return`, `undefined`, or `null` to the end of my function. However, adding `null` seems wrong to me for some reason. Am I right to assume it the worst choice out of the three? – stefmikhail Mar 29 '12 at 20:32
-
2In javascript a function without return statement (or an empty return statement) returns undefined, so the best option is to either add an empty return or leave an `undfined` at the end. Using an empty `return` statement seems to express *return nothing* better that leaving an `undefined` at the end of the function, so the empty `return` seems to be the better option. – Arnaud Le Blanc Dec 20 '12 at 13:56
-
2Instead of `fun(arguments...)`, it would be better to call `fun.apply(this, arguments)`. – Miguel Madero Aug 06 '13 at 23:32
-
4Actually, "return" and "undefined" generate different javascript. Using "Try Coffeescript" at http://coffeescript.org, one can see that explicitly including "return" in coffeescript removes the return from the javascript; whereas, with "undefined", the javascript function ends with "return void 0;" – Daniel Mar 28 '14 at 16:39
-
1I think you might need to be careful with voidFun because I suspect the inner function will still return whatever it wants which could possibly afaik cause the performance degradation. – AturSams Oct 27 '14 at 07:08
-
@Daniel it's good to point out the different javascript generated, but the code evaluates the same way: `undefined` is the return value. Personally I prefer `return` - the javascript is shorter (but it's a very commonly repeated phrase so gzip should make this negligible), and it seems more expressive to me. – jrz Feb 17 '15 at 17:57
Yes , with a return
as the last line of a function.
For example,
answer = () ->
42
extrovert = (question) ->
answer()
introvert = (question) ->
x = answer()
# contemplate about the answer x
return
If you'd like to see what js the coffee compiles to, look at this. (I've used coffeescript redux for my example)

- 4,555
- 31
- 31
- 45

- 15,589
- 9
- 43
- 57
-
1I think it was downvoted because this answer does not add any value apart from the one already there in the top-voted answer, which also happens to be given 2 years before this one! – kumarharsh Jan 20 '15 at 09:15
Just something fun(ctional)
suppressed = _.compose Function.prototype, -> 'do your stuff'
Function.prototype
itself is a function that always return nothing. You can use compose to pipe your return value into this blackhole and the composed function will never return anything.

- 2,870
- 25
- 24
It seems functions in CoffeeScript must always return something, even null
. In C, you have void
as a return type.
->
, the empty function, compiles to (function() {})
, so it's the only function that doesn't return anything.

- 1,521
- 10
- 27
-
That's not true. `return;` works in C, JavaScript and CoffeeScript to return a void value. – yyny Nov 28 '15 at 09:14