Skip to the content

function and return

JavaScript · Unit 4: Functions

A function is code with a name. function defines it, but doesn't run it:

it runs when you call it by its name and parentheses.

What it does goes between braces, and you can call it as many times as you want.

function greet() {
  console.log("Hello");
  console.log("How are you?");
}

greet();
greet();

Prints

Hello
How are you?
Hello
How are you?

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Put the lines in order

    Put the lines in order to define the function and then call it.

  3. 3. Predict the output

    What does this code print?

  4. 4. Complete the code

    Fill in the blanks so the function hands back the square and the program prints 26.

  5. 5. Multiple choice

    What does this function do?

  6. 6. Predict the output

    What does this code print?

  7. 7. Find the bug

    This program should print what two 50 tickets cost. Which line has the error?

  8. 8. Find the case that fails

    sign(n) should hand back "positive", "negative" or "zero". Which call makes it fail?

  9. 9. Predict the output

    What does this code print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →