I tried the following in JavaScript (Firefox 3.5, Windows XP):
(function(){
window.foobar = 'Welcome!';
})();
var foobar = 'PWN3D!';
alert(foobar);
The output was ‘PWN3D!’. Why did my code PWN me? I thought var name = value; executed first.
From the specification (page 87):
So the
varcauses the variable to be created first, but the value ('PWN3D!') is assigned to it in the normal execution order.