Similar to this topic: CoffeeScript Existential Operator and this
The coffeescript, I'm having a problem using the elvis operator:
foo = ->
y = foo()
console.log 'y is null' unless y?
console.log 'x is null' unless x?
Compiles to:
var foo, y;
foo = function() {};
y = foo();
if (y == null) {
console.log('y is null');
}
if (typeof x === "undefined" || x === null) {
console.log('x is null');
}
Output:
y is null
x is null
So the problem is that since y is assigned earlier that coffee takes the shortcut and assumes that y cannot be undefined. However, it is valid to return undefined from a function.
Is there a "safer" way to check that y is also not undefined?
UPDATED Clarified Examples and Explanation:
From the comments, in the first if statement (y == null) is using double equal instead of ( x === null) triple equal, as in the second if statement. Clever.