Functions as values
JavaScript · Unit 10: More on functions
- Full plan
- JavaScript
- 10 exercises
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. Predict the output
What does this code print?
2. Multiple choice
After the last line, what does
fhold?3. Complete the code
Fill in the blank so
actionis the functionhalfand it prints 5.4. Predict the output
What does this code print?
5. Find the bug
This program should print only the positive numbers,
[ 4, 7 ]. Which line has the error?6. Predict the output
What does this code print?
7. Complete the code
Fill in the blank so it prints "Buy bread" and then "Buy salt".
8. Multiple choice
What does the function
countdo?9. Put the lines in order
Put the lines in order so
doTimescalls the action with 1, 2 and 3, and it prints 1, 2 and 3.10. Predict the output
Functions can also live in an object. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.