user3089840 user3089840 - 19 days ago 6
CoffeeScript Question

Why would you need a "undefined" at the end of every block in coffeescript?

I'm working on a very large coffeescript file another team originally developed. For some reason, there is an "undefined" at the end of pretty much every single block of code in the file. For instance:

someFunction = () ->
ajaxReq(
blah blah
undefined
)
undefined

$("#some_id").click((e)->
e.preventDefault()
blah blah
undefined
)

$(".some_class").change(->
blah blah
undefined
)


Could someone please help me understand why they did this, and if it is necessary? Thank you.

Answer

Because CoffeeScript returns the last expression from the function. In JavaScript, all functions return undefined by default. You don't have to write undefined in these cases since you aren't using the return value but if you were and wanted it to be consistent, you would have to return undefined.

Comments