CoffeeSctipt : Existential operator & Multiline comment

25 Feb 2014

Eventhough I am using CoffeeScript for a couple of months now, many of the feature were still unknown to me. Two days before I got hands into two such features which I wish I have known from the beginning.

Existential Operator

Existential operator will help you to check whether the variable exists or not. It is similar to ruby’s .nil? method. consider we have a variable named open. we can check whether when open exists using open? which returns true if the variable is already defined.

if open?
  console.log "Hello"

This wil be compiled to

if (typeof open !== "undefined" && open !== null) {

Try it yourself.

This can be used to set the default values/ conditional assinments like

data = {} ?= true

It says, if is null then set it to true. This will be compiled to

if ( == null) { = true;

Try it yourself.

You can read more about exisitential operator below the operators section.

Multiline comments

Couple of days before only I came to know about the multiline comment with ### in CoffeeScript. Till that I were using #(single line comment).

   console.log "Hello"
If you find my work useful, I appreciate donations via PayPal.
comments powered by Disqus