Does CoffeeScript allow JavaScript-style == equality semantics?

As a possible extension to this, is there a way to inline blocks of regular JS into CoffeeScript code so that it isn’t compiled?

Yes, here’s the documentation. You need to wrap the JavaScript code in backticks (`). This is the only way for you to directly use JavaScript’s == in CoffeeScript. For example:

CoffeeScript Source [try it]

if `a == b`
  console.log "#{a} equals #{b}!"

Compiled JavaScript

if (a == b) {
  console.log("" + a + " equals " + b + "!");
}

The specific case of == null/undefined/void 0 is served by the postfix existential operator ?:

CoffeeScript Source [try it]

x = 10
console.log x?

Compiled JavaScript

var x;
x = 10;
console.log(x != null);

CoffeeScript Source [try it]

# `x` is not defined in this script but may have been defined elsewhere.
console.log x?

Compiled JavaScript

var x;
console.log(typeof x !== "undefined" && x !== null);

Leave a Comment