if (true) {
function foo(){ return 1; }
}
else {
function foo(){ return 2; }
}
foo();
The above code is an example of function expression and returns 1 in Firefox 28 whereas 2 in Chrome ( expected result). Why is firefox giving wrong result ?
if (true) {
function foo(){ return 1; }
}
else {
function foo(){ return 2; }
}
foo();
The above code is an example of function expression and returns 1 in Firefox 28 whereas 2 in Chrome ( expected result). Why is firefox giving wrong result ?
It is a case of function hoisting. Any function declared with an identical function name, the last function of that name will gain precedence and be used even if it is null. In most cases function declaration is processed before the script executes, however, with Firefox this isn't the case, it takes it as it comes within an if block.