Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations15 minLESSON 03 / 34

Block, function & global scope

01

Understand the idea

A value is printed inside an if , but the next line says it is not defined. The value exists; that next line is outside its .

Nested rooms for names

Without boundaries around names, every part of an app could accidentally change another part's work. Scope lets a small piece of code own a local value. You can read that piece without first memorizing every name in the application.

Blocks and functions

A block is usually a pair of braces used for statements, such as an if body. let and const stay in that block. A creates a scope for its and local ; var stays within that function even when declared in a nested block.

A parameter belongs to the function that receives it. A variable declared with let inside an if belongs to that block. Braces around an literal describe data; they do not, by themselves, create this kind of block.

Four boundaries for names
BoundaryNames kept inside itUseful for
Blocklet and const declarations within bracesTemporary values for one branch or loop
FunctionParameters, local var, and its other local declarationsPrivate work for one call
ModuleTop-level declarations not explicitly exportedKeeping different files independent
GlobalNames available across the global environmentA small amount of genuinely shared infrastructure

Why not make everything global?

Global scope is shared more widely, which makes accidental name collisions easier. A has its own top-level scope, so a top-level module variable is not automatically a on window. Keep values as local as the work allows and pass necessary inputs into functions.

When two names are the same

An inner scope can declare the same name as an outer scope. This is shadowing: while you are in the inner scope, the nearer binding is the one you read. The outer value has not been renamed or changed. Shadowing is legal, but it can hide the value you meant to update.

Getting a value back out

If work needs a function's local result, return it. If work needs a block's value afterward, declare an appropriate binding in the enclosing scope and assign it inside. Do not make everything global just to silence a ReferenceError; that spreads ownership across unrelated code.

If you cannot read a local value from outside, that is often the boundary doing its job. Decide what information the caller actually needs and return that value. It is a smaller, clearer than exposing the whole function's temporary work.

02

Mental model

FOLLOW THE VALUE

outer room
label
Inbox

The outer binding remains available outside showLabel.

01 / 03Trace the transformation

03

Live example

Both are named label. Calling showLabel reads the inner one; the final log reads the outer one.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Keep the local label local

Restore a local declaration inside showLabel so the outer Inbox label remains unchanged.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

Archive
Inbox

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which label does showLabel read?
    02How should a caller receive a function-local calculation?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson