Show that a let variable inside braces does not escape them.
Acceptance criteria
- 1.#out should show undefined.
JavaScript Tutorial · JavaScript Scope & Hoisting
These 10 tasks go with the JavaScript Block Scope 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 block scope means let and const variables only exist inside the braces where they were declared.” .
1.Declare inside a blockexercise · easy · 10 min
Show that a let variable inside braces does not escape them.
Passes when: #out should show undefined.
2.A loop variable disappearsexercise · easy · 10 min · practice plan
Show that a let loop counter does not exist after the loop.
Passes when: #out should show undefined.
3.var escapes the blockexercise · easy · 10 min · practice plan
Show that a var declared in a loop is still readable afterwards inside the function.
Passes when: #out should show 3.
4.Each turn gets its own letexercise · easy · 10 min · practice plan
Collect three functions from a let loop and show the values they return.
Passes when: #out should show 1,2,3.
5.const still allows property changesexercise · medium · 10 min · practice plan
Change a property on a const object and show the new value.
Passes when: #out should show light.
6.Fix the leaked variablebug fix · easy · 8 min
Declared outside the block, the variable is not private at all.
Passes when: #out should show undefined.
7.Fix the var loop bugbug fix · easy · 8 min · practice plan
All three functions share one var, so they all report the same number.
Passes when: #out should show 1,2,3.
8.Fix the counter read too latebug fix · easy · 8 min · practice plan
A let counter does not exist after its loop.
Passes when: #out should show 3.
9.Fix the branch variable read outsidebug fix · easy · 8 min · practice plan
The variable is declared inside the if block.
Passes when: #out should show Pass.
10.Fix the const reassignmentbug fix · easy · 8 min · practice plan
A const cannot be pointed at a new object, only changed inside.
Passes when: #out should show light.
Between them these tasks cover let and const, var vs let and Loop bindings.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.