117

It seems like CoffeeScript automatically returns the last item in a scope. Can I avoid this functionality?

4
  • Why would you want to? Can't you just discard the returned value when you like?
    – Chris
    Sep 12, 2011 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.
    – user395760
    Sep 12, 2011 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) Sep 12, 2011 at 17:34
  • 1
    I 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 9, 2014 at 14:56

5 Answers 5

150

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()
)
6
  • 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? Mar 29, 2012 at 20:32
  • 2
    In 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. Dec 20, 2012 at 13:56
  • 2
    Instead of fun(arguments...), it would be better to call fun.apply(this, arguments). Aug 6, 2013 at 23:32
  • 4
    Actually, "return" and "undefined" generate different javascript. Using "Try Coffeescript" at 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, 2014 at 16:39
  • 1
    I 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, 2014 at 7:08
10

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)

1
  • 1
    I 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, 2015 at 9:15
5

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.

1
longRunningFunctionWithNullReturn = ->
  longRunningFunction()
  null
-1

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
  • That's not true. return; works in C, JavaScript and CoffeeScript to return a void value.
    – yyny
    Nov 28, 2015 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.