0

what is the difference between void 0 and undefined in the following contexts?

function Test(value) {
    if(value === void 0) {
        value = 123;
    }
    this.value = value;
}


function Test(value) {
    if(value === undefined) {
        value = 123;
    }
    this.value = value;
}


function Test(value) {
    this.value = value || 123;
}

AFAIK, all three produce the same result.

undefined === void 0
// true

Thus, what is the difference between undefined and void 0, and what benefit does one have over the other?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • There are multiple duplicates... http://stackoverflow.com/questions/19369023/should-i-be-using-void-0-or-undefined-in-javascript Older versions used void 0 – zgr024 Nov 26 '14 at 16:40
  • The last version produces different results. Try with `Test(0)`. Either way, `void` is an operator that returns `undefined`. – Felix Kling Nov 26 '14 at 16:43
  • 1
    Please remember that `undefined` is now immutable in the global scope, other closures may shadow this. – Paul S. Nov 26 '14 at 16:44

0 Answers0