CoffeeScript: Difference between revisions

Content deleted Content added
I moved some stuff down bc I was going to add a paragraph on how js has incorporated many features in coffeescript, but that paragraph should probably go under "Adoption"...
Line 50:
</syntaxhighlight>
 
To compute the [[greatest common divisor]] of two integers with the [[euclidean algorithm]], in JavaScript one usually needs a ''while'' loop:{{Update inline|date=May 2021|reason=JavaScript also supports destructuring assignment}}
 
<syntaxhighlight lang="javascript">
Line 61:
</syntaxhighlight>
 
Whereas in CoffeeScript one can use <code>until</code> and destructuring assignment<ref>CoffeeScript calls this "[[pattern matching]]", which is a non-standard use of that term.</ref> instead:
<syntaxhighlight lang="coffeescript">
gcd = (x, y) ->
Line 67:
x
</syntaxhighlight>
 
Any ''for'' loop can be replaced by a [[list comprehension]]; so that to compute the squares of the positive odd numbers smaller than ten (i.e. numbers whose remainder modulo 2 is 1), one can do:
 
<syntaxhighlight lang="coffeescript">
alert n*n for n in [1..10] when n%2 is 1
</syntaxhighlight>
 
Alternatively, there is:
 
<syntaxhighlight lang="coffeescript">
alert n*n for n in [1..10] by 2
</syntaxhighlight>
 
A [[linear search]] can be implemented with a one-liner using the when keyword:
 
<syntaxhighlight lang="coffeescript">
names = ["Ivan", "Joanna", "Nikolay", "Mihaela"]
linearSearch = (searchName) -> alert(name) for name in names when name is searchName
</syntaxhighlight>
 
The <code>for ... in</code> syntax allows looping over arrays while the <code>for ... of</code> syntax allows looping over objects.
 
The <code>?</code> keyword quickly checks if a variable is <code>null</code> or <code>undefined</code> :
Line 101 ⟶ 80:
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 JavaScript snippet using the [[jQuery]] library is:
 
<syntaxhighlight lang="javascript">
Line 140 ⟶ 119:
sentence = "#{ 22 / 7 } is a decent approximation of π"
</syntaxhighlight>
 
Any ''for'' loop can be replaced by a [[list comprehension]]; so that to compute the squares of the positive odd numbers smaller than ten (i.e. numbers whose remainder modulo 2 is 1), one can do:
 
<syntaxhighlight lang="coffeescript">
alert n*n for n in [1..10] when n%2 is 1
</syntaxhighlight>
 
Alternatively, there is:
 
<syntaxhighlight lang="coffeescript">
alert n*n for n in [1..10] by 2
</syntaxhighlight>
 
A [[linear search]] can be implemented with a one-liner using the when keyword:
 
<syntaxhighlight lang="coffeescript">
names = ["Ivan", "Joanna", "Nikolay", "Mihaela"]
linearSearch = (searchName) -> alert(name) for name in names when name is searchName
</syntaxhighlight>
 
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 scoping