JavaScript Block Scope — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Block Scope 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
Declare inside a block
easy10 minShow that a let variable inside braces does not escape them.
- 2
A loop variable disappears
easy10 minShow that a let loop counter does not exist after the loop.
- 3
var escapes the block
easy10 minShow that a var declared in a loop is still readable afterwards inside the function.
- 4
Each turn gets its own let
easy10 minCollect three functions from a let loop and show the values they return.
- 5
const still allows property changes
medium10 minChange a property on a const object and show the new value.
Find the bug(5)
- 1
Fix the leaked variable
easy8 minDeclared outside the block, the variable is not private at all.
- 2
Fix the var loop bug
easy8 minAll three functions share one var, so they all report the same number.
- 3
Fix the counter read too late
easy8 minA let counter does not exist after its loop.
- 4
Fix the branch variable read outside
easy8 minThe variable is declared inside the if block.
- 5
Fix the const reassignment
easy8 minA const cannot be pointed at a new object, only changed inside.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace