You might want to use the first example when you don't want to permanently change the value of global_variable
, for some reason. E.g. after executing this code the local copy will be changed but the global variable will be unchanged.
global_variable=true; (function(i){ i=false; return i; }(global_variable));
This code however, obviously alters global_variable
:
global_variable=true; (function(){ global_variable=false; }());
Edit: somewhat tangentially, this variation looks like it alters the global variable, but it doesn't because calling the function creates a shadow copy of the global variable. You should probably avoid this pattern since it's likely to create confusion:
g=true; (function(g){ g=false; return g; }(g));