This was last updated for CoffeeScript 1.7.1.
Imagine that this sentence is a beautifully-crafted, flowing intro paragraph. "Wow, what a great introduction to this CoffeeScript guide," you whisper. Now, let's quickly explore all of CoffeeScript's ways to set default values.
As a function parameter
To quote the CoffeeScript documentation, functions can have "default values for arguments, which will be used if the incoming argument is missing (
undefined)." Here's how you might use it:
shootLaserBeam = (color = "red") -> # ...
Now, if the first parameter is
null, it'll be defined as the string "red". If it's anything else (even falsy values like 0 and the empty string), it won't be set to the default value.
Using the existential operator
CoffeeScript's existential operator can be used to set default values.
sendThreateningMessageFromUFO = (options) -> options.message ?= "I will hold your pathetic planet hostage" # ...
You can also set another variable using a sexier version of a ternary operator.
# this... message = evilMessage ? "Exterminate!" # ...is equivalent to this: if evilMessage? message = evilMessage else message = "Exterminate!" # ...or this: message = if evilMessage? then evilMessage else "Exterminate!"
The ternary operator makes sure that the variable isn't
undefined and isn't
You'll get a compile error for if you try to define not-yet-defined variables. For example, this gives a compiler error:
someUndefinedVariable ?= "I will destroy your worlds"
This makes sense, right? Why set a default value for something that's totally undefined? If you, for some reason, need to do this, you can use the existential operator on the variable:
someUndefinedVariable = "Flee, puny humans" unless someUndefinedVariable?
Discarding falsy values, too
If you want to set defaults for any falsy value (not just
null), you can use
angryAliens = false angryAliens or= true # angryAliens is now true # ||= is totally equivalent to or= ufoCount = 0 ufoCount ||= 50 # ufoCount is now 50 # compare it to the existential operator: numberOfAliens = 0 numberOfAliens ?= 1000 # numberOfAliens is still zero
The important takeaway here:
?= only discards
or= filters any falsy value.
For undefined only
Most of CoffeeScript either tests against falsy values or "is it
undefined". You might want to set things only if it's
undefined and nothing else, not even
null. Here's how you might do that:
cropCircleCount = 69 if cropCircleCount is undefined
null or anything falsy other than
undefined, it'll stay that way.
var isUndefined = window.myFunVariable === undefined; // This fails on older browsers because "undefined" is, well, undefined.
CoffeeScript makes sure that this works in older browsers (by compiling
void 0 to reliably produce an undefined value).
And now, a summary.
- Use default parameters if it's a function parameter. Only sets to default if the value is
?=to set a value to a default if the value is
||=) to reject other falsy values like 0 and the empty string.
Go forth and set your CoffeeScript defaults, friend.