1

Is there a way to force Coffeescript to always convert

if x?
  console.log "hello"

to

 if (typeof x !== "undefined" && x !== null) {
  console.log("hello");
}

The reason I'm asking is because if we have the following:

x = "hello"

if x?
  console.log "hello"

It gets converted to:

var x;

x = "hello";

if (x != null) {
  console.log("hello");
}

While this is not a problem in the code above, it is problematic in functions to which an undefined variable is passed.

Abdo
  • 12,299
  • 7
  • 72
  • 94

1 Answers1

0

x != null also covers x != undefined so the converted code should not be entering the if statement if x is undefined.

See more in this answer

Community
  • 1
  • 1
NullPointer
  • 452
  • 3
  • 7