Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
All about Variables  ›  Lesson

Variable Scope: Global, Function and Block

Variable Scopes 12 minA variable is visible only in its scope: let and const are limited to their block, var to its function, and global names are visible everywhere.

Variable Scope: Global, Function and Block

In short: every variable has a region of code where it exists, called its scope — outside that region, the name simply is not there. let and const live inside the braces { } where they were declared, whether that is an if, a loop, or a bare block. var ignores block braces entirely and belongs to the whole surrounding function (or the global scope, if it is not inside one). A name declared outside every function and block is global and visible everywhere. Looking a name up always walks outward, from where it is used to the braces around it.

Nested rooms: a global room holding const playlistName, containing a function room "function playNext() — function scope" where var fromVar lives anywhere, containing a smaller if-block room "block scope" holding let fromLet and var fromVar — with a note that inner code can see outward but outward code can't see in

Read a global name from inside a function

Code inside a function can see names declared outside it — lookup walks outward until it finds a match:

javascript

show declares no mood of its own, so the lookup steps outward one level and finds the global one, printing calm.

A block-scoped counter leaking out of an if

let and const treat every pair of braces as a wall. Declare one inside an if, then read it from outside, and there is nothing to find:

javascript

This throws ReferenceError: n is not defined — n's scope ended with the if block. Swap let for var and the story changes completely:

javascript

This prints 1. var ignores the if's braces and belongs to the surrounding scope, so the name is still there once the block ends.

A variable declared inside a function, read outside it

Unlike a bare block, a function's braces stop even var:

javascript

This throws ReferenceError: secret is not defined. var respects function boundaries; it only ignores the smaller block boundaries (if, for, a bare { }) that sit inside a function.

The trap: a loop counter that escapes

Because var ignores block braces, a for loop's counter written with var is still readable after the loop ends:

javascript

This prints 3 — the value that failed the loop's condition. With let, the same read would throw a ReferenceError, because let i belongs only to the loop's block. This is one of the clearest reasons modern code defaults to let: it keeps a loop's counter from leaking into code that should never see it.

Tracing a loop counterStep 1 of 4
  1. The code

    for (let i = 0; i < 3; i++) { } console.log(i);

Quick practice

quick drill

What does let mood = "calm"; function show() { console.log(mood); } show(); log?

quick drill

What does if (true) { let n = 1; } console.log(n); do?

quick drill

At the top level of a script, what does if (true) { var n = 1; } console.log(n); log?

quick drill

What does function load() { var secret = 42; } load(); console.log(secret); do?

quick drill

What does for (var i = 0; i < 3; i++) {} console.log(i); log?

🎯 Your turn

Explain what function total() { for (let i = 0; i < 2; i++) { var sum = i; } console.log(sum); console.log(i); } total(); does, line by line, in terms of scope.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhere does it live?

Explain what function total() { for (let i = 0; i < 2; i++) { var sum = i; } console.log(sum); console.log(i); } total(); does, line by line, in terms of scope.