Sikau

JavaScript · Day 25 · about 20 min

Scope

Which boxes a function can see.

What's in the room stays in the room

In a house, things in the shared living room are available to everyone, but what's in your bedroom is yours. Variables work the same way. Where a variable is created decides who can see it — that's its scope.

Inside and outside

const shop = "Sikau Café";

function makeBill() {
  const total = 450;
  console.log(shop + ": Rs " + total);
}

makeBill();
console.log(shop);
// console.log(total); ← would be an error

One-way glass: A function can look out at variables around it, but code outside can't look in at the function's own variables.

The rest of this lesson

  • Blocks have scope tooexplain
  • Why scope protects youexplain
  • Predict the outputpredict
  • Inside or outside?predict
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript