Skip to main content

JavaScript return Statement — exercises and bug fixes

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

    Return a value

    easy10 min

    Return the sum of 3 and 4 from a function.

  2. 2

    Return early

    easy10 min

    Return a warning when dividing by zero, otherwise divide. Show the zero case.

  3. 3

    No return means undefined

    easy10 min

    Show the word undefined for a function that returns nothing.

  4. 4

    Return an object property

    easy10 min

    Return an object from a function and show its name property.

  5. 5

    Return from inside a loop

    medium10 min

    Return the first even number in the list.

Find the bug(5)

  1. 1

    Fix the unreachable code

    easy8 min

    The real answer sits after a return, so it never runs.

  2. 2

    Fix the missing guard

    easy8 min

    Dividing by zero should be caught before the calculation.

  3. 3

    Fix the value on the next line

    easy8 min

    A line break after return makes the function give undefined.

  4. 4

    Fix the return that never happens

    easy8 min

    The loop finishes without returning the match.

  5. 5

    Fix the return outside the check

    easy8 min

    Returning on every turn means only the first item is ever seen.

Ready to write the code?

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

Open the workspace