3

While doing some typescript I came over this thing I havent seen before in javascript.

constructor(public x: number = 0, public y: string = "none"){
        this.color = "red";
    }

that part is compiling into:

    if (x === void 0) { x = 0; }
    if (y === void 0) { y = "none"; }

But shouldn't it be typeof x === 'undefined'? if not, which one is better and why?

thanks

m93a
  • 8,866
  • 9
  • 40
  • 58
Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
  • 4
    `void 0` gives the value `undefined` but is less typing. (Except of course in your case you didn't manually type it.) So `x === void 0` is like saying `x === undefined` but slightly shorter. `typeof x === 'undefined'` is a long way of testing the same thing, and necessary only if you're not sure if the `x` variable has even been declared - but in this case you know it definitely *is* declared since it's a function argument. – nnnnnn Apr 05 '15 at 01:21
  • 1
    @nnnnnn Seems like that might be worthy of promoting to an answer if you add a little more detail / a reference to explain to the OP why `void 0 === undefined` – Basic Apr 05 '15 at 01:24
  • In a modern JavaScript interpreter there's no meaningful difference. – Pointy Apr 05 '15 at 01:26
  • @Pointy yes, I know that (assuming no stupid redefiniton) but clearly the OP doesn't yet... I was too lazy to dig up a link and besides, nnnnnn bear me to it. Suppose I should dig one out now: http://stackoverflow.com/a/4806358/156755 – Basic Apr 05 '15 at 01:32
  • @nnnnnn I did saw the link now it does make more sense, but I was just confuse about it, since in my local compiler typescript goes with typeof `x === 'undefined'` but in the live playground goes `(x === void 0)` which made curiouse to see if there was a really and meaningful difference – Jorge Y. C. Rodriguez Apr 05 '15 at 01:37
  • @Basic I didnt see that link before, well this does prove why they decide it change the output of the code, since `void` perform faster with less code, cheers – Jorge Y. C. Rodriguez Apr 05 '15 at 01:40
  • For the curious. One big motiviation for this change is `void 0` takes lesser characters. – basarat Apr 05 '15 at 02:25

1 Answers1

3

There are differences.

If you're checking for a global variable x, then typeof x === 'undefined' will return true and x === void 0 throw a ReferenceError.
You would need to use window.x === void 0 to get true. However in this case it knows that x will at least be set to undefined because it is a function parameter, so that error will never be an issue.

I think for readability sake, I would prefer to use typeof x === 'undefined'.

Wio
  • 1,197
  • 10
  • 9
  • You think `typeof x === 'undefined'` is *more* readable? For sound programming practices sake, I'd never do that. Getting a `ReferenceError` is valuable information. –  Apr 05 '15 at 01:48
  • @Wio Now I do believe the MS took the right approach when they changed to `void` to test for variables... look here http://typeofnan.blogspot.com/2011/01/typeof-is-fast.html looks like this way is a lot faster, so I guess this way is better, dont you think? – Jorge Y. C. Rodriguez Apr 05 '15 at 01:51
  • When I change `undef` to `void 0`, I'm seeing `typeof` win. I said `typeof x === 'undefined'` is more readable, because more are familiar with `undefined` than they are with `void`. – Wio Apr 05 '15 at 02:07