CoffeeScript: Difference between revisions

Content deleted Content added
make JS comparison more fair
Correct JS syntax to use variable declaration keywords, and copyedit
Line 36:
 
<syntaxhighlight lang="javascript">
let mass = 72;
let height = 1.78;
let BMI = mass / height**2;
if (18.5 <= BMI && BMI < 25) alert('You are healthy!');
</syntaxhighlight>
 
Line 53:
To compute the [[greatest common divisor]] of two integers with the [[Euclidean algorithm]], in JavaScript one usually needs a ''while'' loop:
 
<syntaxhighlight lang="javascript">let gcd = (x, y) => {
gcd = (x, y) => {
do {
[x, y] = [y, x%y];
} while (y !== 0)
return x;
}</syntaxhighlight>
}
</syntaxhighlight>
 
Whereas in CoffeeScript one can use <code>until</code><ref>CoffeeScript calls this "[[pattern matching]]", which is a non-standard use of that term.</ref> instead:
Line 81 ⟶ 79:
This would alert "No person" if the variable is <code>null</code> or <code>undefined</code> and "Have person" if there is something there.
 
A common pre-es6[[ES6]] JavaScript snippet using the [[jQuery]] library is:
 
<syntaxhighlight lang="javascript">
Line 142 ⟶ 140:
The <code>for ... in</code> syntax allows looping over arrays while the <code>for ... of</code> syntax allows looping over objects.
 
CoffeeScript has been criticized for its unusual [[Scope (computer science)|scoping]] rules.<ref>{{cite web
rules.<ref>{{cite web
|url=http://lucumr.pocoo.org/2011/12/22/implicit-scoping-in-coffeescript/
|title=The Problem with Implicit Scoping in CoffeeScript
Line 152 ⟶ 149:
|date=25 July 2013
|access-date=2018-10-13
}}</ref> In particular, it completely disallows [[variable shadowing]] which makes reasoning about code more difficult and error-prone in some basic programming patterns established
by and taken for granted since [[procedural programming]] principles were defined.
error-prone in some basic programming patterns established
by and taken for granted since [[procedural programming]]
principles were defined.
 
For example, with the following code snippet in JavaScript
Line 172 ⟶ 167:
</syntaxhighlight>
 
In CoffeeScript there is no way to tell if the scope of a variable is limited to a block or not without looking outside the block.
is limited to a block or not without looking outside the block.
 
== Development and distribution ==