==
operator frequently causes undesirable coercion, is intransitive, and has a different meaning than in other languages, CoffeeScript compiles ==
into ===
, and !=
into !==
. In addition, is
compiles into ===
, and isnt
into !==
.not
as an alias for !
.and
compiles to &&
, and or into ||
.while
, if/else
, and switch/when
statements.on
and yes
are the same as boolean true
, while off
and no
are boolean false
.unless
can be used as the inverse of if
.in
to test for array presence, and of
to test for JavaScript object-key presence.for
loop, from
compiles to the ES2015 of
. (Yes, it’s unfortunate; the CoffeeScript of
predates the ES2015 of
.)**
can be used for exponentiation and //
performs floor division. %
works just like in JavaScript, while %%
provides “dividend dependent modulo”:-7 % 5 == -2 # The remainder of 7 / 5
-7 %% 5 == 3 # n %% 5 is always between 0 and 4
​
tabs.selectTabAtIndex((tabs.currentIndex - count) %% tabs.length)
var modulo = function(a, b) { return (+a % (b = +b) + b) % b; };
​
-7 % 5 === -2; // The remainder of 7 / 5
​
modulo(-7, 5) === 3; // n %% 5 is always between 0 and 4
​
tabs.selectTabAtIndex(modulo(tabs.currentIndex - count, tabs.length));​
All together now :
CoffeeScript | JavaScript |
---|---|
is |
=== |
isnt |
!== |
not |
! |
and |
&& |
or |
|| |
true , yes , on |
true |
false , no , off |
false |
@ , this |
this |
a in b |
[].indexOf.call(b, a) >= 0 |
a of b |
a in b |
for a from b |
for (a of b) |
a ** b |
a ** b |
a // b |
Math.floor(a / b) |
a %% b |
(a % b + b) % b |