The first example is interpreted (JavaScript) as:
var a = 1; function b() { var a = function () {}; a = 10; return; }
In JavaScript, all local variables (in this case, the local variable a, which will hold the function) are declared at the top of the function.
The local variable a gets the value 10, not the global one. Then it ceases to exist after returning.
source share