Read more

Operators "in" and "of" are very inconsistent between CoffeeScript and JavaScript

Henning Koch
May 02, 2018Software engineer at makandra GmbH

CoffeeScript and JavaScript (ECMAScript) both have operators in and of. Each language use them for more than one purpose. There is not a single case where the same operator can be used for the same purpose in both languages.

Check if an object (or its prototype) has a property

CoffeeScript

var hasFoo = 'foo' of object

JavaScript

var hasFoo = 'foo' in object;

Iterate through all properties of an object

CoffeeScript

object = { ... }

for key, value of object
  console.log("key %o has value %o", key, value);

JavaScript

var object = { ... };

for (var key in object) {
  var value = x[key];
  console.log("key %o has value %o", key, value); 
}

Iterate through all values of an array

CoffeeScript

array = [...]

for entry in array
  console.log("value is %o", entry.value)

JavaScript

var array = [...]

for (var entry of array) {
   console.log("value is %o", value); 
})

Iterate through all values of an iterator

CoffeeScript

iterator = ...

while entry = iterator.next() && !entry.done
  console.log("value is %o", entry.value)

JavaScript

var iterator = ...;

for (var value of iterator) {
   console.log("value is %o", value); 
}
Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot
Henning Koch
May 02, 2018Software engineer at makandra GmbH
Posted by Henning Koch to makandra dev (2018-05-02 10:38)