if/else
statements can be written without the use of parentheses and curly brackets. As with functions and other block expressions, multi-line
conditionals are delimited by indentation. There’s also a handy postfix form, with the if
or unless
at the end.
CoffeeScript can compile if
statements into JavaScript expressions, using the ternary operator when possible, and closure
wrapping otherwise. There is no explicit ternary statement in CoffeeScript — you simply use a regular if
statement on a single line.
mood = greatlyImproved if singing
​
if happy and knowsIt
clapsHands()
chaChaCha()
else
showIt()
​
date = if friday then sue else jill
var date, mood;
​
if (singing) {
mood = greatlyImproved;
}
​
if (happy && knowsIt) {
clapsHands();
chaChaCha();
} else {
showIt();
}
​
date = friday ? sue : jill;