I know I should not submit a question with this title, but I am really confused by the following code. anyone can explain why the last line print 1? 

enviroment: chrome 80.0.3987.163

```js
console.log(foo) // undefined
if (true) {
  foo = 1
  console.log(foo) // 1
  function foo() {
    console.log("a")
  }
  console.log(foo) // 1, function foo is hoist
  foo = 2
  console.log(foo) // 2
} else {
  function foo() {
    console.log("b")
  }
}

console.log(foo) // 1, why?
```

if remove the function declaration, it's OK.

```js
console.log(foo) // undefined
if (true) {
  foo = 1
  console.log(foo) // 1
  foo = 2
  console.log(foo) // 2
} else {
  function foo() {
    console.log("b")
  }
}

console.log(foo) // 2
```