Show what a var holds before its assignment line runs.
Acceptance criteria
- 1.#out should show undefined.
JavaScript Tutorial · JavaScript Scope & Hoisting
These 10 tasks go with the JavaScript Hoisting chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JavaScript hoisting moves declarations to the top of their scope before the code runs.” .
1.A var read too earlyexercise · easy · 10 min
Show what a var holds before its assignment line runs.
Passes when: #out should show undefined.
2.Call a function before it appearsexercise · easy · 10 min · practice plan
Call a declared function on a line above it and show the answer for 6.
Passes when: #out should show 36.
3.Assignments are not hoistedexercise · easy · 10 min · practice plan
Show the value only after the assignment line has run.
Passes when: #out should show 5.
4.Hoisting is per functionexercise · easy · 10 min · practice plan
Show that a local var hides a global of the same name before it is assigned.
Passes when: #out should show undefined.
5.Declare before useexercise · medium · 10 min · practice plan
Add up a list with everything declared before it is used.
Passes when: #out should show 6.
6.Fix the expression called too earlybug fix · easy · 8 min
A function expression is not hoisted with its body.
Passes when: #out should show 36.
7.Fix the value read before assignmentbug fix · easy · 8 min · practice plan
Only the declaration is hoisted, never the value.
Passes when: #out should show 5.
8.Fix the shadowing surprisebug fix · easy · 8 min · practice plan
The local var is hoisted, so the global is never seen.
Passes when: #out should show global.
9.Fix the counter declared too latebug fix · easy · 8 min · practice plan
The total is used before it is set up.
Passes when: #out should show 6.
10.Fix the function used before assignmentbug fix · easy · 8 min · practice plan
Assigning a function to a variable late means it is not there early.
Passes when: #out should show Ready.
Between them these tasks cover var hoisting, Function hoisting and Declaration order.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.