1

If we declare a variable without an initial value, the value will be undefined:

var foo;
foo;
// → undefined

But, is there a case (e.g. some old browser, or maybe a special environment) where when declaring a variable, this variable could have a value different than undefined?

I am just thinking why in some cases we use void 0 instead of undefined to check against undefined?

// Why this...
if (foo === void 0) {
  // ...
}

// and not this?
var U;
if (foo === U) {
  ...
}
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • There is basically undefined and not-defined – Thanveer Shah Jan 11 '19 at 11:49
  • 1
    `(() => { const undefined = 'baz'; console.log('baz' === undefined) })()` – CertainPerformance Jan 11 '19 at 11:50
  • 1
    @ThanveerShah While that is a difference to keep in mind (one will cause a `ReferenceError`, the other won't), the question is asking about something else – CertainPerformance Jan 11 '19 at 11:52
  • @CertainPerformance One of the answers in the question you linked mine as duplicate says _`window.undefined` is writable whereas void operator will always return undefined_ — I'm sorry, but I can't understand how `window.undefined` can be overridden... – Ionică Bizău Jan 21 '19 at 15:59
  • 2
    It's not possible in modern browsers, thankfully, but it *used* to be. But defining a variable named `undefined` is still a possibility. – CertainPerformance Jan 21 '19 at 20:05

1 Answers1

3

You cannot override window.undefined.

This used to be possible in the past, in some browsers, but not it's not possible anymore.

Like mentioned in the comments you can create an undefined variable like this:

(() => { 
    const undefined = 'baz';
    console.log('baz' === undefined) 
})()
GhitaB
  • 3,275
  • 3
  • 33
  • 62