Skip to main content

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

    A var read too early

    easy10 min

    Show what a var holds before its assignment line runs.

  2. 2

    Call a function before it appears

    easy10 min

    Call a declared function on a line above it and show the answer for 6.

  3. 3

    Assignments are not hoisted

    easy10 min

    Show the value only after the assignment line has run.

  4. 4

    Hoisting is per function

    easy10 min

    Show that a local var hides a global of the same name before it is assigned.

  5. 5

    Declare before use

    medium10 min

    Add up a list with everything declared before it is used.

Find the bug(5)

  1. 1

    Fix the expression called too early

    easy8 min

    A function expression is not hoisted with its body.

  2. 2

    Fix the value read before assignment

    easy8 min

    Only the declaration is hoisted, never the value.

  3. 3

    Fix the shadowing surprise

    easy8 min

    The local var is hoisted, so the global is never seen.

  4. 4

    Fix the counter declared too late

    easy8 min

    The total is used before it is set up.

  5. 5

    Fix the function used before assignment

    easy8 min

    Assigning 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