Function expression on firefox - expected result

if (true) {
  function foo(){ return 1; }
}
else {
  function foo(){ return 2; }
}
foo();

      

The above code is an example of a function expression and returns 1 in Firefox 28 and 2 in Chrome (expected result). Why is firefox giving the wrong result?

+3


source to share


1 answer


This is a case of lifting a function. Any function declared with the same function name, the last function of that name will take precedence and will be used even if it is zero. In most cases, the function declaration is processed before the script is executed, however this is not the case with Firefox, it takes it when it enters the if block.



0


source







All Articles