This is because of JS hoisting. As mentioned here,
Order of precedence
It’s important to keep a few things in mind when declaring
JavaScript functions and variables.
- Variable assignment takes precedence over function declaration
- Function declarations take precedence over variable declarations
Function declarations are hoisted over variable declarations but
not over variable assignments.
Notice this part specially,
Function declarations are hoisted over variable declarations but not over variable assignments.
After hoisting your code will execute like:
foo = "ffsds";
function bar() {
function foo() { return "func"; };
return foo;
foo = 10;
foo = "5353";
}
alert(typeof bar());