2013-08-01 04:13:11 +04:00
---
language: coffeescript
contributors:
- ["Tenor Biel", "http://github.com/L8D"]
2014-03-14 22:18:23 +04:00
- ["Xavier Yao", "http://github.com/xavieryao"]
2013-08-01 04:13:11 +04:00
filename: coffeescript.coffee
---
2015-10-08 06:11:24 +03:00
CoffeeScript is a little language that compiles one-to-one into the equivalent JavaScript, and there is no interpretation at runtime.
2015-10-09 17:36:51 +03:00
As one of the successors to JavaScript, CoffeeScript tries its best to output readable, pretty-printed and smooth-running JavaScript code, which works well in every JavaScript runtime.
2014-02-05 17:37:17 +04:00
2014-01-30 06:34:56 +04:00
See also [the CoffeeScript website ](http://coffeescript.org/ ), which has a complete tutorial on CoffeeScript.
2014-09-08 15:08:28 +04:00
```coffeescript
2013-08-01 04:13:11 +04:00
# CoffeeScript is a hipster language.
# It goes with the trends of many modern languages.
2014-04-14 22:04:44 +04:00
# So comments are like Ruby and Python, they use number symbols.
2013-08-01 04:13:11 +04:00
###
Block comments are like these, and they translate directly to '/ *'s and '* /'s
for the resulting JavaScript code.
2014-09-14 23:18:43 +04:00
You should understand most of JavaScript semantics
2013-08-01 04:13:11 +04:00
before continuing.
###
# Assignment:
number = 42 #=> var number = 42;
opposite = true #=> var opposite = true;
# Conditions:
number = -42 if opposite #=> if(opposite) { number = -42; }
# Functions:
square = (x) -> x * x #=> var square = function(x) { return x * x; }
2014-02-05 17:37:17 +04:00
fill = (container, liquid = "coffee") ->
"Filling the #{container} with #{liquid}..."
#=>var fill;
#
#fill = function(container, liquid) {
# if (liquid == null) {
# liquid = "coffee";
# }
# return "Filling the " + container + " with " + liquid + "...";
#};
2013-08-01 04:13:11 +04:00
# Ranges:
list = [1..5] #=> var list = [1, 2, 3, 4, 5];
# Objects:
math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
#=> var math = {
2015-10-07 04:31:07 +03:00
# "root": Math.sqrt,
# "square": square,
# "cube": function(x) { return x * square(x); }
# };
2013-08-01 04:13:11 +04:00
# Splats:
race = (winner, runners...) ->
print winner, runners
2014-02-05 17:37:17 +04:00
#=>race = function() {
2015-10-07 04:31:07 +03:00
# var runners, winner;
# winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
# return print(winner, runners);
# };
2013-08-01 04:13:11 +04:00
# Existence:
alert "I knew it!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }
# Array comprehensions:
2015-10-08 06:11:24 +03:00
cubes = (math.cube num for num in list)
2014-02-05 17:37:17 +04:00
#=>cubes = (function() {
2015-10-07 04:31:07 +03:00
# var _i, _len, _results;
# _results = [];
2014-02-05 17:37:17 +04:00
# for (_i = 0, _len = list.length; _i < _len; _i++) {
2015-10-07 04:31:07 +03:00
# num = list[_i];
# _results.push(math.cube(num));
# }
# return _results;
# })();
2014-02-05 17:37:17 +04:00
foods = ['broccoli', 'spinach', 'chocolate']
eat food for food in foods when food isnt 'chocolate'
#=>foods = ['broccoli', 'spinach', 'chocolate'];
#
#for (_k = 0, _len2 = foods.length; _k < _len2; _k++) {
# food = foods[_k];
# if (food !== 'chocolate') {
# eat(food);
# }
#}
2013-08-01 04:13:11 +04:00
```
2014-05-12 23:47:35 +04:00
## Additional resources
- [Smooth CoffeeScript ](http://autotelicum.github.io/Smooth-CoffeeScript/ )
2014-05-13 01:12:39 +04:00
- [CoffeeScript Ristretto ](https://leanpub.com/coffeescript-ristretto/read )