Skip to main content

JavaScript Variables — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Variables 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 a variable with let

    easy10 min

    Declare a variable named score, set it to 42, and show it.

    • Declare score with let.
    • #out should show 42.
  2. 2

    Use const for a fixed value

    easy10 min

    Store the site name in a constant and display it.

  3. 3

    Reassign a let variable

    easy10 min

    Start count at 5, reassign it to 9, then show the final value.

  4. 4

    Combine two variables

    easy10 min

    Join a first and last name into one full name.

  5. 5

    Swap two variables

    medium10 min

    Given a = 1 and b = 2, swap them and show them as "2,1".

Find the bug(5)

  1. 1

    Fix the misspelled variable

    easy8 min

    The value never appears because the variable name is wrong.

  2. 2

    Fix the reassigned constant

    easy8 min

    This code throws because a const is being reassigned.

  3. 3

    Fix the variable used too early

    easy8 min

    The variable is read before it is declared.

  4. 4

    Fix the missing quotes

    easy8 min

    A text value was written without quotes, so it is treated as a variable.

  5. 5

    Fix the wrong concatenation

    easy8 min

    The full name is missing the space between the two parts.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace