Functions are defined by an optional list of parameters in parentheses, an arrow, and the function body. The empty function looks like this: ->
square = (x) -> x * x
cube = (x) -> square(x) * x
var cube, square;
​
square = function(x) {
return x * x;
};
​
cube = function(x) {
return square(x) * x;
};​
​
Functions may also have default values for arguments, which will be used if the incoming argument is missing (undefined
).
fill = (container, liquid = "coffee") ->
"Filling the #{container} with #{liquid}..."
var fill;
​
fill = function(container, liquid = "coffee") {
return `Filling the ${container} with ${liquid}...`;
};​
​