Skip to main content

JavaScript call, apply and bind — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript call, apply and bind 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

    Use call

    easy10 min

    Use call to run a greeting with the person object as this.

  2. 2

    Pass arguments with call

    easy10 min

    Use call to pass a city as well as setting this.

  3. 3

    Use apply

    easy10 min

    Do the same job with apply, passing the arguments as an array.

  4. 4

    Use bind

    easy10 min

    Use bind to make a greeting function that always uses the person object.

  5. 5

    Borrow a method

    medium10 min

    Use call so the same function reports two different names, joined by a comma.

Find the bug(5)

  1. 1

    Fix the missing this value

    easy8 min

    Calling the function directly loses the object.

  2. 2

    Fix apply given loose arguments

    easy8 min

    apply needs its arguments inside an array.

  3. 3

    Fix bind used like call

    easy8 min

    bind returns a function; it does not run it.

  4. 4

    Fix the argument in the wrong place

    easy8 min

    With call, this comes first and the arguments follow.

  5. 5

    Fix the same object used twice

    easy8 min

    Both calls borrow the method with the same object.

Ready to write the code?

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

Open the workspace