Dan Rubio Dan Rubio - 1 year ago 73
CoffeeScript Question

Why do I get a syntax error for placing if/else in a coffeescript function

I am placing conditional logic in a coffeescript function and I am getting a on helpful compiling error for rails

can't parse 'compile ((execjs):17:19)'

Here is my simplified code:

defaultFilterByDate = () ->
if (true) {
alert 'hello'

When I reload the page. My rails environment does not like this. Why am I getting this error?

Answer Source

Because coffeescript is valid without {}, the correct code would be:

defaultFilterByDate = () ->
  if (true)
    alert 'hello'

Without () or even in 1 line is also okay:

defaultFilterByDate = () -> if true then alert 'hello'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download