Skip to the workspace
Pebellslearn by building
All about Variables  ›  Lesson

Variable Scope: Global, Function and Block

Variable Scopes 12 min

javascript / Variable Scope: Global, Function and Block

Explore each scene at your own pace.

01 · Variable Scope: Global, Function and Block

Variable Scope: Global, Function and Block

Variable Scope: Global, Function and Block

Scope determines where an identifier can be accessed. let and const are block-scoped; var is scoped to its surrounding function or script/module environment, not an ordinary block.

Braces can create a boundary for a name without creating a copy of every outside value.

Keep two reading sessions independent. Experiment with an inner declaration and an outer assignment, then use the panel to see which value actually changed.

Opening your JavaScript project…

Reference and extra examples

Open this whenever you want a fuller explanation. Your project above is the main activity.

Reference model

These authored snapshots explain the example below; they are not a trace of your edited project.

Walk through the modelVariable Scope: Global, Function and BlockAuthored snapshots of the example; use Run below to test the code.
title"Outside"
The outer binding exists before the block begins.
1 of 3

Follow one example

javascript

The inner declaration shadows the outer title only inside its block. Leaving the block reveals the original outside binding, which was never reassigned. Functions also introduce scope. A top-level declaration’s relationship to globalThis depends on scripts versus modules and the declaration form; a top-level name is not automatically a window property. The added function returns 1 because var is not restricted to the if block. Replacing var with let makes the later read inaccessible.

Predict, change and check

Predict: If the inner const declaration is removed and the outer title is let, what would title = "Inside" change?

Test your explanation: Try that replacement and compare the outside output. Distinguish creating an inner binding from assigning to an existing outer one.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Global is a scope, not always a window property

A block is commonly the region between braces used by a statement, but an object literal is not a new variable scope. let and const follow lexical block scope; var follows function scope. A function can read an outer binding but code outside its scope cannot directly read its locals.

In a browser classic script, top-level var and function declarations can create global-object properties, while top-level let and const create global lexical bindings. ESM has module scope, and Node CommonJS has its own wrapper scope. globalThis gives access to the host's global object; it does not make all top-level bindings properties of that object. Inspect the actual host and script type before drawing conclusions.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Inspect the execution context

This example uses a real classic script and a real module in the preview frame. The console editor wraps its code inside a function, so it is not a test of page-level global declarations. Predict which declarations become global-object properties before running.

html
Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.