I just read a great article about JavaScript Scoping and Hoisting by Ben Cherry in which he gives the following example:
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);
Using the code above, the browser will alert "1".
I'm still unsure why it returns "1". Some of the things he says come to mind like: All the function declarations are hoisted to the top. You can scope a variable using function. Still doesn't click for me.
Function hoisting means that functions are moved to the top of their scope. That is,
will be rewritten by the interpeter to this
Weird, eh?
Also, in this instance,
behaved the same as
So, in essence, this is what the code is doing:
Hoisting is behavioural concept of JavaScript. Hoisting (say moving) is concept that explains how and where variables should be declared.
In JavaScript, a variable can be declared after it has been used because Function declarations and variable declarations are always moved (“hoisted”) invisibly to the top of their containing scope by the JavaScript interpreter.
We encounter two types of hoisting in most cases.
1.Variable declaration hoisting
Lets understand this by this piece of code.
Here declaration of variable a will be hosted to top invisibly by the javascript interpreter at the time of compilation. So we were able to get value of a. But this approach of declaration of variables is not recommended as we should declare variables to top already like this.
consider another example.
is actually interpreted like this:
In this case x will be undefined
It does not matter if the code has executed which contains the declaration of variable. Consider this example.
This function turns out to be like this.
In variable declaration only variable definition hoists, not the assignment.
Unlike the variable hoisting the function body or assigned value will also be hoisted. Consider this code
Now as we understood both variable and function hoisting, let's understand this code now.
This code will turn out to be like this.
The function a() will have local scope inside b(). a() will be moved to top while interpreting the code with its definition (only in case of function hoisting) so a now will have local scope and therefore will not affect the global scope of a while having its own scope inside function b().
function a(){}
is hoisted first and it behaves likevar a = function () {};
, hence in local scopea
is created.a=10
, you are setting the local variablea
, not the global one.Hence, the value of global variable remain same and you get, alerted 1
scpope & closure & hoisting (var/function)
Its all depends on the scope of variable 'a'. Let me explain by creating scopes as images.
Here JavaScript will create 3 scopes.
i) Global scope. ii) Function b() scope. iii) Function a() scope.
Its clear when you call 'alert' method scope belongs to Global that time, so it will pick value of variable 'a' from Global scope only that is 1.
It is happening because of the Variable name is same as the function name means "a". Thus due to Javascript hoisting it try to solve the naming conflict and it will return a = 1.
I was also confused about this until i read this post on "JavaScript Hoisting" http://www.ufthelp.com/2014/11/JavaScript-Hoisting.html
Hope it helps.