CoffeeScript Undefined

Jaider picture Jaider · Mar 29, 2012 · Viewed 61.6k times · Source

In javascript to check if a variable was never created, we just do

if (typeof MyVariable !== "undefined"){ ... }

I was wonder how I do that in coffeescript?... I try something like

if (MyVariable?false){ ... }

but this check if MyVariable is a function if so that will call MyVariable(false) if not that will call void(0) or something like that.

Answer

Jaider picture Jaider · Apr 20, 2012

Finally I found this easy way to do it:

if (MyVariable?){ ... }

that will generate:

if (typeof MyVariable !== "undefined" && MyVariable !== null){ ... }

UPDATE 04/07/2014 Demo Link

enter image description here