Scope
JavaScript · Unit 4: Functions
- Full plan
- JavaScript
- 10 exercises
The variables you create inside a function are local: they are born when you call it and disappear when it ends.
Outside they don't exist. What does come out is the value of the return.
function calculate() {
const total = 5 * 3;
return total;
}
const result = calculate();
console.log(result);Prints
15
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. Find the bug
This program should print 100 minus a discount of 20. Which line has the error?
2. Find the bug
Orders over 50 pay no shipping, so this program should print 100. Which line has the error?
3. Predict the output
What does this code print?
4. Predict the output
What does this code print?
5. Complete the code
Fill in the blank so
passeduses the average of the two scores.6. Predict the output
What does this code print?
7. Multiple choice
Where can the variable
totalbe used?8. Put the lines in order
Put in order the program that prints 70 plus 5 for shipping.
9. Find the case that fails
pay(total, age)should charge 50 less to anyone who is 65 or older. Which call makes it fail?10. Predict the output
What does this code print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.