Skip to main content

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. 1

    Declare inside a block

    easy10 min

    Show that a let variable inside braces does not escape them.

  2. 2

    A loop variable disappears

    easy10 min

    Show that a let loop counter does not exist after the loop.

  3. 3

    var escapes the block

    easy10 min

    Show that a var declared in a loop is still readable afterwards inside the function.

  4. 4

    Each turn gets its own let

    easy10 min

    Collect three functions from a let loop and show the values they return.

  5. 5

    const still allows property changes

    medium10 min

    Change a property on a const object and show the new value.

Find the bug(5)

  1. 1

    Fix the leaked variable

    easy8 min

    Declared outside the block, the variable is not private at all.

  2. 2

    Fix the var loop bug

    easy8 min

    All three functions share one var, so they all report the same number.

  3. 3

    Fix the counter read too late

    easy8 min

    A let counter does not exist after its loop.

  4. 4

    Fix the branch variable read outside

    easy8 min

    The variable is declared inside the if block.

  5. 5

    Fix the const reassignment

    easy8 min

    A 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