Skip to the content

Functions as values

JavaScript · Unit 10: More on functions

A function is a value too, like a number or a string: you can store it in another variable.

Without parentheses, double is the function; with parentheses, double(4) is what it returns. If you print the function without calling it, the console just tells you it's a function.

function double(n) {
  return n * 2;
}
const other = double;
console.log(other(4));
console.log(double);

Prints

8
[Function: double]

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. Multiple choice

    After the last line, what does f hold?

  3. 3. Complete the code

    Fill in the blank so action is the function half and it prints 5.

  4. 4. Predict the output

    What does this code print?

  5. 5. Find the bug

    This program should print only the positive numbers, [ 4, 7 ]. Which line has the error?

  6. 6. Predict the output

    What does this code print?

  7. 7. Complete the code

    Fill in the blank so it prints "Buy bread" and then "Buy salt".

  8. 8. Multiple choice

    What does the function count do?

  9. 9. Put the lines in order

    Put the lines in order so doTimes calls the action with 1, 2 and 3, and it prints 1, 2 and 3.

  10. 10. Predict the output

    Functions can also live in an object. What does it 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 →