CoffeeScript language Cheat Sheet

A Programming › CoffeeScript cheat sheet in English by apk

CoffeeScript language syntax

19,963 views 5,751 downloads 5 Aug 2013 History
4.9 avg · 3 ratings

Instal­lation

Install NodeJs (htttp­://­nod­ejs.org/)
Install npm (nodejs included)
npm install coffee­-script -g

Usage

~$ coffee src/fi­le.c­offee
Run Coffee­Script file
~$ coffee -c -o output/ src/
Compile files from src to output
~$ coffee -e "­con­sol­e.log '­Hello World'"
Evaluate Coffee­Script code
$ coffee --join output­/all.js --compile src/*.c­offee
Concat­enate the source Coffee­Script before compiling
~$ coffee
Start Read-e­val­-print loop
~$ coffee --output output/ -cw src/
Watch directory src for changes

Variables

language = '­Cof­fee­Scr­ipt­'

? - Existe­ntial Operator

coffee = '­esp­res­so' if morning?
cups = 0; cups ?= 1
sugar = count ? 1
coffee = one?.c­off­ee?­().p­le­ase­?.n­o?.s­ugar?

Functions

hello = -> consol­e.log "­Hel­lo"
define function
hello()
call function
sum = (a, b) -> a + b
define function with args
sum 1, 2
call function with args
sum_mu­ltiply = (a, b) -> [ a + b, a * b]
define function
[ sum, multiply ] = sum_mu­ltiply 1, 2
call function
myFunc = (param = '­def­aul­t') -> consol­e.log param
define function with default param
otherFunc = (first, other...) -> conole.log other
define function
otherFunc myArray...
call function with array as arguments

Scope

Notice how all of the variable declar­ations have been pushed up to the top of the closest scope, the first time they appear. outer is not redeclared within the inner function, because it's already in scope; inner within the function, on the other hand, should not be able to change the value of the external variable of the same name, and therefore has a declar­ation of its own.

Arrays

array = [1, 2, 3, 4]
Array with numbers from 1 to 4
array = [1...4]
Array with numbers from 1 to 3
array = [1..3]
Array with numbers from 1 to 3
array1 = array[­1..3]
Create new array from three items from array
array[­1..3] = [3, 2, 1]
Change three items of array
ground­_co­ffe­e.r­eve­rse()
Reverse elements in array
Math.max [12, 32, 11, 67, 1, 3]...
Max element of array
[1 ,2, 3].concat [ 4, 5, 6]
concat­enating arrays

Operators and aliases

Coffee­Script
JavaScript
this, @
this
is
===
isnt
!==
not
!
and
&&
or
||
true, yes, on
true
false, no, off
false
of
in
in
find element in array (No JS equiva­lent)

Badges

Useful Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 2

Comments (2)

apk 09:24 21 Nov 2015

updated

Reply
RSrini 11:11 15 Jul 2015

array = [1....3] Array with numbers from 1 to 4 There is no found dots. only 2 dots and 3 dots available. When i try this the generated array become empty. Please update.

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.