Skip to main content

JavaScript Primitives and References — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Primitives and References 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

    Copy a primitive

    easy10 min

    Copy a number, change the copy, and report the original.

  2. 2

    Share an object

    easy10 min

    Assign an object to a second name, change it, and report the first.

  3. 3

    Copy an object properly

    easy10 min

    Spread the object so the original is left alone.

  4. 4

    Copy an array

    easy10 min

    Spread an array, push to the copy, and report both lengths.

  5. 5

    Change an object in a function

    medium10 min

    Show that a property changed inside a function is visible outside.

Find the bug(5)

  1. 1

    Fix the object shared instead of copied

    easy8 min

    A plain assignment shares the object.

  2. 2

    Fix the array shared

    easy8 min

    Pushing to the second name changes the first array too.

  3. 3

    Fix the objects compared by contents

    easy8 min

    Two separate objects are never equal, however alike.

  4. 4

    Fix the reassignment inside a function

    easy8 min

    Replacing the whole object inside a function is not visible outside.

  5. 5

    Fix the nested object still shared

    easy8 min

    Spreading copies only the top level, so the inner object is shared.

Ready to write the code?

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

Open the workspace