0

I have seen this question in on of the JS Quizes. Not able to understand how, How does these two different codes return the same result. I read about JS Hoisting but unable to understand this.

function bar(){
return foo;
function foo(){}
var foo='11'
}

bar()

function bar(){
return foo;
var foo='11'
function foo(){}
}
bar()
Ein2012
  • 1,103
  • 1
  • 13
  • 33
  • 1
    Just don't use an identifier twice ... :) – Jonas Wilms May 03 '19 at 17:53
  • When the body of a function is evaluated, all variable and function declarations are "collected". For every variable declaration, and entry is created in the new scope (and initialized to `undefined` in case of `var`). For every function declaration, the function is evaluated and set in the scope. Function declarations are set "last" so they overwrite any existing variable entry that might exist. – Felix Kling May 03 '19 at 17:56

0 Answers0