Check if a variable is defined in CoffeeScript
In JavaScript it’s pretty common to check if a variable has been created. You would usually perform the check with the following code:
if (type someVariable !== "undefined") {
// code goes here
}
This is the equivalent syntax in CoffeeScript.
if someVariable?
# code goes here
This will generate the following JavaScript:
if (typeof someVariable !== "undefined" && someVariable !== null) {
// code goes here
}
Much neater. Happy coding.