Name: CoffeeScript
Creator(s): Jeremy Ashkenas
Date: December 13, 2009
Website: coffeescript.org
CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.
To run a CoffeeScript file you must first compile it to JavaScript, to do that you can use the official CoffeeScript compiler. Once the compiler is installed use the command coffee -c file.coffee
which will create a corresponding file.js
. To run the code in your terminal use the command node file.js
.
The beauty of CoffeeScript is it's simplicity and readability! To declare and assign a new variable all you need is the variables name followed by an =
sign and then the value. No keyword var
, no need to declare a data type and no semicolon!
name = "Jacob"
If/Else statements in CoffeeScript are also cleaner than other languages. You don't need brackets around the condition and no curly braces around the body as shown below:
if true
console.log "True!"
else
console.log "False!"
Loops also emit the brackets and curly braces often seen in JavaScript. The syntax for CoffeeScript loops most closely resembles Python loops, see below:
letters = ["a", "b", "c", "d", "e", "f", "g"]
for letter in letters
console.log letter
Functions do require brackets around the arguments list but no curly braces around the body, see below:
greet = (message) ->
console.log message
greet("Hello World!")
CoffeeScript classes also use the class
keyword but omit curly braces and use an @
symbol rather than the keyword this
to denote properties and methods. A new instance of a class is initialized with the keyword new
.
class Student
constructor: (@name) ->
student = new Student("Mohammed")
console.log student.name
- Awesome CoffeeScript ~ A curated list of awesome CoffeeScript frameworks, libraries and software.