58

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.

Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
  • 8
    Why do you need `==` ? The accepted way to do this stuff is via explicit coercion. `a.toString() === b.toString()` or `parseInt(a, 10) === parseInt(b, 10)`. `==` is not to be trusted except for a very few specific cases that arguably should be handled for you by the coffee script compiler. – Alex Wayne Aug 11 '11 at 21:52
  • @Squeegy - Partly an academic question, actually, but I was mostly looking for a shorter form of `parseInt(a, 10) === parseInt(b, 10)`. – Justin Morgan - On strike Aug 11 '11 at 22:02
  • 2
    @Joseph - "Pored" is a word, and it doesn't mean the same as "poured". – Justin Morgan - On strike Aug 11 '11 at 22:11
  • 1
    My point is simply to say that most coffee scripters would insist that using backticks is "doing it wrong". But if you are cool with that, go crazy :) – Alex Wayne Aug 11 '11 at 22:17
  • 9
    @Justin `+a === +b` will do what you want in that example. Nice little trick to have up your sleeve. :) – Trevor Burnham Aug 12 '11 at 02:33
  • @TrevorBurnham this should be a standalone answer. It's excellent! – oma Jun 19 '12 at 10:11
  • the `CoffeeScript compiles == into the JavaScript === operator.` link is dead – Zoltán May 29 '15 at 21:33
  • @Zoltán: Fixed. Thanks. – Justin Morgan - On strike Jun 01 '15 at 14:20
  • 2
    @TrevorBurnham That's not really equivalent unless you know `a` and `b` contain integer values. For instance, `parseInt('2.3', 10) === parseInt('2.4', 10)` is true, but `+'2.3' === +'2.4'` is false. – Sean the Bean Jan 19 '16 at 16:34
  • @SeantheBean is exactly right, and I would add that there are still differences in the case of `parseFloat`. For example, `parseFloat("", 10)` evaluates to `NaN`, but `+""` evaluates to 0. Depending on the application, that could be a problem. – Justin Morgan - On strike Sep 19 '19 at 17:29

2 Answers2

83

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);
Jeremy
  • 1
  • 85
  • 340
  • 366
0

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 
Tim Scollick
  • 1,242
  • 1
  • 16
  • 17