SlideShare a Scribd company logo
S

CoffeeScript

No really, it’s just Javascript
Brian Mann
@be_mann
Atlanta, GA
What is CoffeeScript?

• Exposes the good parts
• Transpiles directly to JS
• Removes / Adds new syntax
• Enhances readability, and productivity
• Clearly expresses intents
Agenda

• An Emotional Assessment
• Dive Into Features
• Real World Experiences
• Challenges / Critiques
I am Biased.
CoffeeScript
is one of the most
BEAUTIFUL

/

UGLY

LOVED

/

HATED

EMBRACED

/

FEARED

LANGUAGES.

/

TOOLS.

/

SYNTAX.
Javascript
Landscape.
Golden Age
S

Golden Age

CoffeeScript
Why the hate?

• CoffeeScript looks different
• CoffeeScript acts different
Coffeescript: No really, it's just Javascript
FUD.
Fear. Uncertainty. Doubt.
Crutch.
Absolutely

NOT.
S
Nothing To Fear.
Its just Javascript.
Okay thx.
Code plz.
Before we begin

•
• No more commas
• Optional parenthesis
• Optional curly braces
• White space sensitive

No more semi-colons ;
,
()
{}
Vars.
var is, no, more;
Vars are long gone
var count, increment;

JAVASCRIPT

count = 0;
increment = function() {
var total;
count += 1;
return total = "The total is: " + count;
};

count = 0

COFFEESCRIPT

increment = ->
count += 1
total = "The total is: " + count
Rule:

/
100

99
Object Literals.
Objects Simplified
JAVASCRIPT

var person = {
name: "Toby Ho",
language: "javascript",
likes: ["node", "testem", "jazz"]
};

COFFEESCRIPT

person =
name: "Toby Ho"
language: "javascript"
likes: ["node", "testem", "jazz"]
Functions.
Functions in JS
function eat(){
console.log("eating");
};

var eat = function(){
console.log("eating");
};

DECLARATION

EXPRESSION
Functions in CS
COFFEESCRIPT
eat = ->
console.log "eating"

JAVASCRIPT
var eat;
eat = function() {
return console.log("eating");
};
Function Arguments
COFFEESCRIPT
eat = (options = {}) ->

JAVASCRIPT
var eat;
eat = function(options) {
if (options == null) {
options = {};
}
};
Function Context
JAVASCRIPT

COFFEESCRIPT

var person;
person = {
name: "Toby Ho",
energy: 0,
eat: function() {
return this.energy += 10;
}
};

> person.eat();
# 10

person =
name: "Toby Ho"
energy: 0
eat: ->
@energy += 10
Function Binding
JAVASCRIPT
var person;
person = {
name: "Toby Ho",
energy: 0,
work: function() {
var _this = this;
$("#stop").click(function() {
return _this.energy -= 20;
});
}
};

COFFEESCRIPT
person =
name: "Toby Ho"
energy: 0
work: ->
$("#stop").click =>
@energy -= 20
Function Binding
JAVASCRIPT
var person;

COFFEESCRIPT

person =
name: "Toby Ho"
person = {
energy: 0
name: "Toby Ho",
person = {
work: ->
energy: 0,
name: "Toby Ho",
$("#stop").click =>
energy: 0, work: function() {
@energy -= 20
return $("#stop").click((function(_this) {
work: function() {
return function() {
var _this = this;
return _this.energy -= 20;
$("#stop").click(function() {
};
return _this.energy -= 20;
})(this));
});
}
}
};
};
var person;
Function Splats
JAVASCRIPT
var person;

COFFEESCRIPT
person =
name: "Toby Ho"
dislikes: []
addDislikes: (args...) ->
@dislikes.push args...

person = {
name: "Toby Ho",
dislikes: [],
addDislikes: function() {
var args = [].slice.call(arguments);
return [].push.apply(this.dislikes, args);
}
};

> person.addDislikes(“frameworks”, “long plane rides”);
Ranges / Slicing
Array + String Slicing
COFFEESCRIPT

JAVASCRIPT

nums = [1..10]

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

nums[0...5]

nums.slice(0, 5);

[1,2,3,4,5]

nums[2..3] = [-3, -4]

[].splice.apply(nums,
[2,2].concat([-3, -4])
);

[1,2,-3,-4,5,
6,7,8,9,10]

"Toby Ho"[0..3]

"Toby Ho".slice(0, 4);

Toby
If, Else, Unless.
Flow Control
person =
name: "Toby Ho"
tired: true
work: ->
return if @tired
@program "javascript"
relax: ->

var person;

person = {
name: "Toby Ho",
tired: true,
work: function() {
if (this.tired) {
if moment.hour() > 10 then @read() else @tv()
return;
sleep: ->
}
@relax() unless @tired
return this.program("javascript");
},
relax: function() {
moment.hour() > 10 ? this.read() : this.tv();
},
sleep: function() {
if (!this.tired) {
return this.relax();
}
}
};

COFFEESCRIPT

JAVASCRIPT
Operators.
Operator Comparison
COFFEESCRIPT

JAVASCRIPT

is

===

true is true

isnt

!==

@name isnt "randall"

not

!

and

&&

or

||

true yes

on

true

false no

off

false

relax() if not working()
exercise() unless @tired and @sleepy

happy() if @happy or @energy

> 50
Existential
Operator.
Do You Really Exist?
person =
name: "Toby Ho"
energy: 0
address:
city: "Alpharetta"
state: "GA"
zip: 30307
phone: null
mail: -> "mailing..."

var _ref;
if ((_ref = person.address) != null) {
_ref.city;
}
if (typeof person.call === "function") {
person.call();
}

> person.address?.city

=>

Alpharetta

> person.phone?.cell

=>

undefined

> person.call?( )

=>

undefined

> person.call?( ) ? person.mail( )

=>

mailing...
String
Interpolation.
No more awkward ‘+’
JAVASCRIPT
"Hey, " + person.name + " is " + person.age + " years young.";

COFFEESCRIPT
"Hey, #{person.name} is #{person.age} years young."
Destructuring
Assignment.
Easy var extraction
person =
name: "Toby Ho"
energy: 0
address:
city: "Alpharetta"
state: "GA"
zip: 30307

COFFEESCRIPT
{name, energy, address} = person

JAVASCRIPT
var address, energy, name;
name
= person.name;
energy = person.energy;
address = person.address;
IIFE’s.
Module Pattern
JAVASCRIPT
App = (function() {
var privateVar = "can't get to me!";
var privateFn = function() {
return "can't invoke me either";
};
var obj = {
data: [],
props: {},
publicFn: function() {}
};
return obj;
})();

COFFEESCRIPT
App = do ->
privateVar = "can't get to me!"
privateFn = -> "can't invoke me either"
obj =
data: []
props: {}
publicFn: ->
return obj
Local Dependencies
JAVASCRIPT
(function($, BB) {
//reference jQuery as $
//reference Backbone as BB
})(jQuery, Backbone);

COFFEESCRIPT
do ($ = jQuery, BB = Backbone) ->
#reference jQuery as $
#reference Backbone as BB
Switch / Case.
...minus the case
COFFEESCRIPT
switch
when
when
when
else

state
"GA"
then "humid"
"WA"
then "rainy"
"AZ", "UT", "NV" then "dry"
"moderate"

JAVASCRIPT
switch (state) {
case "GA":
"humid";
break;
case "WA":
"rainy";
break;
case "AZ":
case "UT":
case "NV":
"dry";
break;
default:
"moderate";
}
No Control Expression
COFFEESCRIPT
energyLevel = switch
when @energy < 10 then "exhausted"
when @energy < 20 then "okay"
when @energy < 40 then "caffeinated"
else "bouncing off walls"

JAVASCRIPT
var energyLevel;
energyLevel = (function() {
switch (false) {
case !(this.energy < 10):
return "exhausted";
case !(this.energy < 20):
return "okay";
case !(this.energy < 40):
return "caffeinated";
default:
return "bouncing off walls";
}
}).call(this);
Loops /
Comprehensions.
For Loops
person =
name: "Toby Ho"
language: "javascript"
likes: ["node", "testem", "jazz"]

COFFEESCRIPT
for like, num in person.likes
console.log "#{num + 1}. #{like}"
var like, num, _i, _len, _ref;

=>

1. node
2. testem
3. jazz

JAVASCRIPT

_ref = person.likes;
for (num = _i = 0, _len = _ref.length; _i < _len; num = ++_i) {
like = _ref[num];
console.log("" + (num + 1) + ". " + like);
}
Property Iteration
heros = { john: "resig", ryan: "dahl" };

COFFEESCRIPT
for first, last of heros
console.log first, last

var first, last;

JAVASCRIPT

for (first in heros) {
last = heros[first];
console.log(first, last);
}

=>

john resig
ryan dahl
Array ‘in’ checking
person =
name: "Toby Ho"
dislikes: ["frameworks", "long plane rides"]

COFFEESCRIPT
if "coffeescript" in person.dislikes then "hater" else "friend :-)"

var __indexOf = [].indexOf || function(item) { for (var i = 0, l
= this.length; i < l; i++) { if (i in this && this[i] === item)
return i; } return -1; };
if (__indexOf.call(this.dislikes, "coffeescript") >= 0) {
"hater";
} else {
"friend :-)";
}

JAVASCRIPT
Comprehensions
{100 Objects}

COFFEESCRIPT
nums = (num: i for i in [1..100])

var i, nums;

=>

JAVASCRIPT

nums = (function() {
var _i, _results;
_results = [];
for (i = _i = 1; _i <= 100; i = ++_i) {
_results.push({
num: i
});
}
return _results;
})();

-------------------------[{
num: 1
},{
num: 2
}]
What was missed?

•
• Block Regular Expressions
Some Operators
•
• Chained Comparisons
Trailing Commas
•
• Reserved Words
Literate Coffeescript
•
• Classes, Inheritance, & Super
Block Strings
Debugging.
Syntax Errors
@PE.module "HeaderApp", +>

SUBLIME TEXT
Source Maps
CoffeeConsole
Literal CoffeeScript
Criticisms.
Dispelling the Myths

• Compiler Errors (WAT?)
• Performance
• Restrictions*
• Harder to Debug
• Less Readability
• You can skip learning JS
Lies,
Damned Lies,
& Statistics.
Productivity Gains
LINES OF
CODE

34,014

84,681

2.49X

CHARS

1,198,518

2,704,135

2.25X
S

CoffeeScript
The End
Brian Mann
@be_mann

More Related Content

PDF
CoffeeScript
PDF
Why (I think) CoffeeScript Is Awesome
PDF
Introduction to CoffeeScript
PPTX
Less ismorewithcoffeescript webdirectionsfeb2012
PDF
CoffeeScript
PDF
Coffee Script
PDF
Ruby and Rails by example
PDF
Ruby on Rails Presentation
CoffeeScript
Why (I think) CoffeeScript Is Awesome
Introduction to CoffeeScript
Less ismorewithcoffeescript webdirectionsfeb2012
CoffeeScript
Coffee Script
Ruby and Rails by example
Ruby on Rails Presentation

What's hot (20)

PPTX
5 Tips for Better JavaScript
PDF
Real life-coffeescript
KEY
Backbone js
KEY
Ruby/Rails
KEY
Why ruby
PDF
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
PPTX
Taming that client side mess with Backbone.js
KEY
PDF
JavaScript - From Birth To Closure
PDF
Testing javascriptwithjasmine sydjs
PDF
Rails on Oracle 2011
ODP
ES6 PPT FOR 2016
PDF
Proxies are Awesome!
PDF
RubyEnRails2007 - Dr Nic Williams - Keynote
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
KEY
JavaScript Neednt Hurt - JavaBin talk
PDF
Mulberry: A Mobile App Development Toolkit
KEY
(Parameterized) Roles
PDF
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
PDF
A New Baseline for Front-End Devs
5 Tips for Better JavaScript
Real life-coffeescript
Backbone js
Ruby/Rails
Why ruby
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Taming that client side mess with Backbone.js
JavaScript - From Birth To Closure
Testing javascriptwithjasmine sydjs
Rails on Oracle 2011
ES6 PPT FOR 2016
Proxies are Awesome!
RubyEnRails2007 - Dr Nic Williams - Keynote
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
JavaScript Neednt Hurt - JavaBin talk
Mulberry: A Mobile App Development Toolkit
(Parameterized) Roles
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
A New Baseline for Front-End Devs
Ad

Similar to Coffeescript: No really, it's just Javascript (20)

PDF
CoffeeScript, An Introduction for Nodejs developers
PDF
A piece of sugar in your client-side development
PDF
Damn Fine CoffeeScript
KEY
CoffeeScript - A Rubyist's Love Affair
PDF
CoffeeScript Design Patterns
PPTX
Coffee script
PDF
CoffeeScript
KEY
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
PPTX
CoffeeScript - An Introduction
KEY
Quick coffeescript
PDF
Webmontag Berlin "coffee script"
PDF
Seattle.rb 6.4
PPT
CoffeeScript: A beginner's presentation for beginners copy
KEY
Async. and Realtime Geo Applications with Node.js
PDF
CoffeeScript-Ruby-Tuesday
PPTX
Coffee script
PPTX
CoffeeScript
PDF
CoffeeScript
KEY
Exciting JavaScript - Part II
KEY
JavaScript Growing Up
CoffeeScript, An Introduction for Nodejs developers
A piece of sugar in your client-side development
Damn Fine CoffeeScript
CoffeeScript - A Rubyist's Love Affair
CoffeeScript Design Patterns
Coffee script
CoffeeScript
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
CoffeeScript - An Introduction
Quick coffeescript
Webmontag Berlin "coffee script"
Seattle.rb 6.4
CoffeeScript: A beginner's presentation for beginners copy
Async. and Realtime Geo Applications with Node.js
CoffeeScript-Ruby-Tuesday
Coffee script
CoffeeScript
CoffeeScript
Exciting JavaScript - Part II
JavaScript Growing Up
Ad

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Electronic commerce courselecture one. Pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
Teaching material agriculture food technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
The AUB Centre for AI in Media Proposal.docx
Electronic commerce courselecture one. Pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Review of recent advances in non-invasive hemoglobin estimation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Teaching material agriculture food technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Building Integrated photovoltaic BIPV_UPV.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx

Coffeescript: No really, it's just Javascript