JavaScript Hoisting — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Hoisting lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
A var read too early
easy10 minShow what a var holds before its assignment line runs.
- 2
Call a function before it appears
easy10 minCall a declared function on a line above it and show the answer for 6.
- 3
Assignments are not hoisted
easy10 minShow the value only after the assignment line has run.
- 4
Hoisting is per function
easy10 minShow that a local var hides a global of the same name before it is assigned.
- 5
Declare before use
medium10 minAdd up a list with everything declared before it is used.
Find the bug(5)
- 1
Fix the expression called too early
easy8 minA function expression is not hoisted with its body.
- 2
Fix the value read before assignment
easy8 minOnly the declaration is hoisted, never the value.
- 3
Fix the shadowing surprise
easy8 minThe local var is hoisted, so the global is never seen.
- 4
Fix the counter declared too late
easy8 minThe total is used before it is set up.
- 5
Fix the function used before assignment
easy8 minAssigning a function to a variable late means it is not there early.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace