Variable declarations (and function declarations) go up at the top of the area in which they appear. Assignments take place on site. The code is effectively interpreted as follows:
var a;
var a;
a = 'string1';
For example, consider what happens if you declare a variable inside the statement body if:
console.log(myVar);
if (something === somethingElse) {
var myVar = 10;
}
console.log(myVar);
JavaScript , . , , . :
var myVar;
console.log(myVar);
if (something === somethingElse) {
myVar = 10;
}
console.log(myVar);
, false, myVar . , JSLint , .
... ECMAScript 5 ( ):
VariableDeclaration VariableDeclarationNoIn d , do
- dn - d.
- varAlreadyDeclared - envs HasBinding , dn .
- varAlreadyDeclared
false,- envs CreateMutableBinding , dn configurableBindings .
- envs SetMutableBinding , dn,
undefined .
, , , .