Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

New questions #1

Merged
merged 19 commits into from
May 4, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add question hoisting-example
  • Loading branch information
fejes713 committed May 4, 2018
commit 017493340812fc596e0dc17addb6465aa2204168
25 changes: 25 additions & 0 deletions questions/hoisting-example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
### What will be the output of this code?

```js
var foo = 1;
var foobar = function () {
console.log(foo);
var foo = 2;
};
foobar();
```

#### Answer

Firstly `foobar` won't look for the outer scope as `var foo = 2;`'s declaration is hoisted. On the other hand, JavaScript doesn't hoist initialization, so the output of this code is going to be `undefined`.

#### Additional links

* [MDN docs for hoisting](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting)

#### Good to hear

* Hoisting is Javascript’s default behavior of moving declarations to the top
* Mention of `strict` mode

<!-- tags: (javascript) -->