CoffeeScript Undefined

CoffeescriptUndefined

Coffeescript Problem Overview


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.

Coffeescript Solutions


Solution 1 - Coffeescript

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

Solution 2 - Coffeescript

First, to answer your question:

if typeof myVariable isnt 'undefined' then # do stuff

Magrangs' solution will work in most cases, except when you need to distinguish between undefined and false (for example, if myVariable can be true, false or undefined).

And just to point out, you shouldn't be wrapping your conditions in parentheses, and you shouldn't be using curly braces.

The then keyword can be used if everything is on the same line, otherwise use indentation to indicate what code is inside of the condition.

if something  
    # this is inside the if-statement  
# this is back outside of the if-statement

Hope this helps!

Solution 3 - Coffeescript

This answer is for an older version of coffeescript. See Jaider's answer above if you want a more up to date answer (as of July 2014)

This coffeescript does what you want I think:

if not MyVariable?
  MyVariable = "assign a value"

Which produces:

if (!(typeof MyVariable !== "undefined" && MyVariable !== null)) {
  MyVariable = "assign a value";
}

N.b. if you make an assignment to MyVariable first, even if you set MyVariable to undefined as in this code, then this compiles to:

if (!(MyVariable != null)) {
  MyVariable = "assign a value";
}

I believe this works because the != used by CoffeeScripts Existential Operator (the question mark) coerces undefined to be equal to null.

p.s. Can you actually get if (MyVariable?false){ ... } to work? It doesn't compile for me unless there's a space between the existential operator and false i.e. MyVariable? false which then makes CoffeeScript check it like a function because of the false which it thinks is a parameter for your MyVariable, for example:

if MyVariable? false
  alert "Would have attempted to call MyVariable as a function"
else
  alert "but didn't call MyVariable as it wasn't a function"

Produces:

if (typeof MyVariable === "function" ? MyVariable(false) : void 0) {
  alert("Would have attempted to call MyVariable as a function");
} else {
  alert("but didn't call MyVariable as it wasn't a function");
}

Solution 4 - Coffeescript

typeof MyVariable isnt "undefined" 

from js2coffee

Solution 5 - Coffeescript

In addition to Jaider's answer above (I couldn't comment due to insufficient reputation), be careful that it's a different case if it's something inside an object/array:

someArray['key']?

will be converted to:

someArray['key'] != null

Screenshot from js2coffee.org:

js2coffee.org

Solution 6 - Coffeescript

I just use:

if (myVariable)
    //do stuff

As undefined is falsy it will only do stuff if myVariable is not undefined.

You just have to be aware that it will 'do stuff' for values that are 0, "" and null

Solution 7 - Coffeescript

The cleanest way I've found to assign a variable to an undefined and not-null variable is using unless:

unless ( myVar? )
    myVar = 'val'

Solution 8 - Coffeescript

Why not just use the OR idiom?

myVar or 'val'

So, the result will equal myVar, unless it is undefined in which case it will equal 'val'.

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
QuestionJaiderView Question on Stackoverflow
Solution 1 - CoffeescriptJaiderView Answer on Stackoverflow
Solution 2 - CoffeescriptsoundslikeneonView Answer on Stackoverflow
Solution 3 - CoffeescriptAJPView Answer on Stackoverflow
Solution 4 - CoffeescriptJonathanView Answer on Stackoverflow
Solution 5 - CoffeescriptLongYCView Answer on Stackoverflow
Solution 6 - Coffeescriptmbx-mbxView Answer on Stackoverflow
Solution 7 - CoffeescriptfoolingView Answer on Stackoverflow
Solution 8 - Coffeescriptl3xView Answer on Stackoverflow