Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mgol
Created April 10, 2013 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgol/5356300 to your computer and use it in GitHub Desktop.
Save mgol/5356300 to your computer and use it in GitHub Desktop.
Show's what the EcmaScript 5 undefined immutability really means.
(function ( undefined ) {
console.log( '0: ' + undefined );
})( 0 ); // => 0
(function ( undefined ) {
'use strict';
console.log( '1: ' + undefined );
})( 1 ); // => 1
(function () {
undefined = 2;
console.log( '2: ' + undefined );
})(); // => 2
(function () {
'use strict';
try {
undefined = 3;
console.log( '3: ' + undefined );
} catch (e) {
console.log( '3: ' + e );
}
})(); // => TypeError
(function () {
var undefined = 4;
console.log( '4: ' + undefined );
})(); // => 4
(function () {
'use strict';
var undefined = 5;
console.log( '5: ' + undefined );
})(); // => 5
var undefined = 6;
(function () {
console.log( '6: ' + undefined );
})(); // => undefined
var undefined = 7;
(function () {
'use strict';
console.log( '7: ' + undefined );
})(); // => undefined
@gsans
Copy link

gsans commented Nov 14, 2014

After checking the gist everything looks fine. #0-2 are not really uses of "undefined" but a parameter using a JavaScript built-in object name which is not recommended but possible. You could also use parameters names like window, document or location with similar results. #4-5 are local variables doing the same as said before. The rest look good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment