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 =
function a(){}
is hoisted first and it behaves like var a = function () {};
, hence in local scope a
is created. a=10
, you are setting the local variable a
, not the global one.Hence, the value of global variable remain same and you get, alerted 1