Skip to the content

Scope

JavaScript · Unit 4: Functions

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. 1. Find the bug

    This program should print 100 minus a discount of 20. Which line has the error?

  2. 2. Find the bug

    Orders over 50 pay no shipping, so this program should print 100. Which line has the error?

  3. 3. Predict the output

    What does this code print?

  4. 4. Predict the output

    What does this code print?

  5. 5. Complete the code

    Fill in the blank so passed uses the average of the two scores.

  6. 6. Predict the output

    What does this code print?

  7. 7. Multiple choice

    Where can the variable total be used?

  8. 8. Put the lines in order

    Put in order the program that prints 70 plus 5 for shipping.

  9. 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. 10. 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 →