Skip to content

Instantly share code, notes, and snippets.

@flaki flaki/undefined.js
Created Apr 5, 2017

What would you like to do?
Shadowing undefined
(function() {
// Won't work, 'undefined' is a property on the global object, but it's unconfigurable (not modifiable)
try {
Object.defineProperty(window, 'undefined', { value: 42 });
} catch (e) { console.log(e.toString()) }
// But 'undefined' could be still shadowed, because it's not a keyword - its name is usable as an identifier
var undefined = 42;
// foo is a non-existent property on the window object
console.log(`typeof => '${typeof}' `);
// This won't work, foo is not '42', returns false
console.log(` === undefined => ${ === undefined} `);
// It is possible to get an untainted 'undefined' value by using void, which is an operator
// void always returns undefined, regardless of its operand
console.log(` === void 0 => ${ === void 0} `);
// For the object property case, one could also use the 'in' operator for this test
console.log(`'foo' in window => ${'foo' in window} `);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.