Does CoffeeScript allow JavaScript-style == equality semantics?

JavascriptCoffeescript

Javascript Problem Overview


I love that CoffeeScript compiles == into the JavaScript === operator. But what if you want the original JS == semantics? Are they available? I've pored over the documentation and can't find anything enabling this.

More generally, is there a way to inline plain JS into my CoffeeScript code so that the compiler doesn't touch it?

I'd prefer to avoid editing the compiled JavaScript output, since I'm using Chirpy to auto-generate it in Visual Studio.

Javascript Solutions


Solution 1 - Javascript

> 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);

Solution 2 - Javascript

This isn't exactly the answer but this problem came up for me because jQuery's .text() was including whitespace and 'is' was failing in Coffeescript. Get around it by using jQuery's trim function:

$.trim(htmlText) is theExpectedValue 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustin MorganView Question on Stackoverflow
Solution 1 - JavascriptJeremyView Answer on Stackoverflow
Solution 2 - JavascriptTim ScollickView Answer on Stackoverflow