Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does CoffeeScript allow JavaScript-style == equality semantics?

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.

like image 616
Justin Morgan Avatar asked Aug 11 '11 20:08

Justin Morgan


People also ask

Is CoffeeScript slower than JavaScript?

CoffeeScript tends to run as fast, or faster than hand-written JavaScript.

What is CoffeeScript used for?

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.

What are the benefits of CoffeeScript over JavaScript?

Advantages of CoffeeScript Using CoffeeScript, we can write clean, clear, and easily understandable codes. Write less do more − For a huge code in JavaScript, we need comparatively very less number of lines of CoffeeScript. Reliable − CoffeeScript is a safe and reliable programming language to write dynamic programs.


1 Answers

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); 
like image 156
Jeremy Avatar answered Oct 02 '22 12:10

Jeremy