var name = ' Frog 'function hello () { // undefined var name = ' BBC ';}
In JavaScript, functions can form an independent scope, the search for variables, first of all, the nearest principle, first look at their own have no, they will automatically run to the outer layer to find, this and other languages may not be the same, it will automatically run outside to find.
In the entire hello scope, as long as the name of the variable is defined, it will not go to the window to find, but, in hello own scope, there is a rule, stating that the previous call, are undefined, declared and assigned after the call will have a value.
Alert (name) occurs before the declaration, so it pops up undefined that's the truth.
for (var i=0;i<10;i++) { //... }alert (i)//10
This block, refers to the area between the two curly braces, in JavaScript, originally did not exist in this problem, the question arises from those who have been engaged in C language and other people to engage in JavaScript brought. Their knowledge of the past
, after the loop, I was automatically destroyed, but not in JavaScript. As long as you remember JavaScript, the function is scoped.
JS with notes