3

The following coffeescript code

if  a isnt undefined
  b = 1

if a?
  b = 1

compiles to the following javascript

var b;

if (a !== void 0) {
  b = 1;
}

if (typeof a !== "undefined" && a !== null) {
  b = 1;
}

Are the two checks equivalent? Under what conditions do they differ?

wmil
  • 3,179
  • 2
  • 21
  • 24
  • possible duplicate of [How does CoffeeScript's existential operator work?](http://stackoverflow.com/questions/17253772/how-does-coffeescripts-existential-operator-work) – mu is too short Oct 30 '13 at 17:28
  • There's also some finer points to the behaviour of the `typeof` operator. For example, compare the compiled output of `a?` and `a=a;a?`. http://es5.github.io/#x11.4.3 – forivall Oct 30 '13 at 17:30
  • Did you look up `void` to see what it does? You literally have the answer in front of you. – Evan Davis Oct 30 '13 at 17:36
  • It's not just javascript, coffeescript has a special case for `undefined?` so I wanted to make sure there wasn't anything I was missing. – wmil Oct 30 '13 at 22:07

4 Answers4

1

TLDR: In general, when checking for presence of a variable, foo? will be the safest.

foo? checks that foo is neither null nor undefined. It will not throw an error if the foo has not been declared:

typeof foo !== "undefined" && foo !== null;
> false

var foo = null;
typeof foo !== "undefined" && foo !== null;
> false

var foo = 123;
typeof foo !== "undefined" && foo !== null;
> true

foo isnt undefined checks that foo is not equal to undefined. If foo has not been declared it will throw an error. If foo is null it will return true:

foo !== void 0;
> ReferenceError // OH NO!

var foo;
foo !== void 0;
> false

var foo = 123;
foo !== void 0;
> true

var foo = null;
foo !== void 0;
> true // OH NO!

NOTE: void 0 is equivalent to undefined

tybro0103
  • 48,327
  • 33
  • 144
  • 170
0

In JavaScript, the void operator evaluates the expression and then returns undefined. So void 0 returns undefined. Therefore isnt undefined strictly tests against undefined whereas ? checks against undefined and null.

The two would differ in any case that the value being checked was null rather than undefined as the type values are distinctly different.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

From the Coffeescript website:

CoffeeScript's existential operator ? returns true unless a variable is null or undefined.

isnt, on the other hand, just translates to !==, which means it is an unequal comparison without type conversion.

So, taken together, the first check in your example would return true if and only if a is not undefined. The second one would return true if a is not undefined and not null.

Hannes Obweger
  • 258
  • 1
  • 4
0
a isnt undefined

evaluates to false if a is undefined, true otherwise

a?

evaluates to false if a is undefined or null, true otherwise

So the only functional difference is whether null is true or false.

In terms of readability the first one will be clear to pretty much anyone, the second is shorter and is clear to anyone who has a basic knowledge of coffeescript syntax.

Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
  • Also important: `foo !== undefined` raises an error when foo has not been declared. – tybro0103 Oct 30 '13 at 17:42
  • @tybro0103 yes, but thats not one of the options in this case. – Ben McCormick Oct 30 '13 at 17:46
  • 1
    @tybro0103 first of all this is coffeescript, which handles that for you. Regardless, he also doesn't say foo !== undefined anywhere. – Ben McCormick Oct 30 '13 at 18:29
  • `alert a isnt undefined` evaluates to `alert(a !== void 0)`. It *will* throw an error unless `a` has been declared. Sure, you don't explicitly declare variables in CS, but if you haven't first done something to cause that declaration in CS you're going to get the error. – tybro0103 Oct 30 '13 at 20:11
  • Also, when the question is about checking for the presence of a variable, the variable being undeclared is a very real possibility. Maybe you're checking for the presence of the console (doesn't exist in old IEs). `console isnt undefined` will throw an error; `console?` will not. The OP didn't say whether or not he had declared `a` elsewhere, but his code definitely did not. His code as-is will throw an error. – tybro0103 Oct 30 '13 at 20:15
  • oh, and yes he does do `foo !== undefined`... that's what coffeescript evaluates `foo isnt undefined` to. OK, technically it evaluates to `foo !== void 0`... same result – tybro0103 Oct 30 '13 at 20:28
  • @tybro ok that makes more sense. Thats true that it doesn't define it if you don't define it anywhere. I hadn't thought that case through. As to the rest of it, its good to be specific in your language :) – Ben McCormick Oct 31 '13 at 01:22