Search code examples
javascriptiife

Can anyone explain why this IIFE passed in argument mutates in-scope variable the way it does to undefined?


Any other cases not involving undefined that would cause this mutation? I'm having a hard time wrapping my head around this example:

(function(undefined){
var str = 'foo';
console.log(str === undefined); // true
})('foo');


Solution

  • undefined is a variable that you can reassign outside of strict mode, although strict mode aparently doesn't protect you from naming arguments that way.

    console.log(typeof undefined);
    (function(undefined){
    var str = 'foo';
    console.log(str === undefined); // true
    console.log(typeof undefined);
    })('foo');