CoffeeScript is JavaScript with different syntax. All types, methods, etc. are the same as JavaScript. We will only cover the differences between CoffeeScript and JavaScript.
- Function Declaration
challenge = (x, y, z) ->
x + y + z
- Logging
challenge = (x, y, z) ->
console.log x*y, y*z
x + y + z
- No curly braces - use indentation instead
- Implicit return - no need to write return at the end of a function
- Parentheses required around function arguments for only one argument
# CoffeeScript
f = (n) -> # `f = n ->` is not allowed
x = n + 1
x / 2 # return value of `f`
- function calls need no parentheses - can also chain without parentheses if using tagged template literals
(str.repeat 4 ) == (str.repeat(4))
(str.split"x".join"y" ) == (str.split("x").join("y"))
(str.split "x".join"y") == (str.split("x".join("y")))
- No ternary - analogue is
x = if p then a else b
but that is long - List comprehension
- String interpolation is
"a#{myVar}b"
until
loop - meanswhile !condition
- No
var
- no global variables