I am bit confuse to understand logic of this function
function foo(){
var bar = function() {
return 3;
};
function bar() {
return 8;
};
return bar();
}
alert(foo());
alert(foo()); print 3. But as per my logic
var bar = function() {
return 3;
};
is local function because of var keyword and
function bar() {
return 8;
};
is global function for function foo(). Also java script execute line by line so the function with same name which call last should print output. So as per my logic this function should print 8. But it is printing 3.
Also the another example
function foo(){
return bar();
var bar = function() {
return 3;
};
var bar = function() {
return 8;
};
}
alert(foo());
This function throw error Uncaught TypeError: bar is not a function
Why it is giving error. Can anyone please solve my confusion with both function?
Sorry if my logic goes wrong please correct me. Thanks in advance.