Skip to main content

JavaScript instanceof — exercises and bug fixes

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

    Check an instance

    easy10 min

    Report whether the object was built from the class.

  2. 2

    Follow inheritance

    easy10 min

    Report whether a dog is also an animal.

  3. 3

    Show it is not symmetric

    easy10 min

    Report whether an animal is a dog.

  4. 4

    Read the exact class

    easy10 min

    Report the exact class name of the instance.

  5. 5

    Check behaviour instead

    medium10 min

    Call the method only if the object actually has one.

Find the bug(5)

  1. 1

    Fix typeof used to identify a class

    easy8 min

    typeof only ever says object for an instance.

  2. 2

    Fix the operands the wrong way round

    easy8 min

    The object goes on the left and the class on the right.

  3. 3

    Fix the parent checked as a child

    easy8 min

    A parent instance is not an instance of the child class.

  4. 4

    Fix instanceof used for the exact class

    easy8 min

    instanceof is true for parents too, so it cannot tell the exact class.

  5. 5

    Fix the class compared as a string

    easy8 min

    constructor.name is a string, but the class itself is not.

Ready to write the code?

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

Open the workspace