Possible Duplicate:
is there any Advantage of redeclaring javascript variables?
Why does the following code display 1 rather than undefined:
a = 1;
var a;
alert(a);
Possible Duplicate:
is there any Advantage of redeclaring javascript variables?
Why does the following code display 1 rather than undefined:
a = 1;
var a;
alert(a);
1) var
does not redeclare or delete a variable
2) even if it did, your code gets rewritten* using javascript hoisting rules (any variable or function declaration gets moved to the top of the nearest enclosing function) as follows:
var a;
a = 1;
alert(a);
(*effectively rewritten; see RobG's comments and links on entering of execution contexts for clarification)