0 of 8 problems solved • 0%
Use in a course

Scope and Variable Lifetime

8 exercisesIntermediate

Scope determines where a variable can be read. Variables declared inside a function stay local to that call, while let and const inside a block are unavailable once the block ends.

const unit = "points"; function formatScore(score) { const label = `${score} ${unit}`; return label; } console.log(formatScore(9)); // 9 points // console.log(label); // ReferenceError

Small tracing exercises reveal function and block scope, accidental shadowing, and out-of-scope access. Keeping values in the smallest useful scope reduces surprising interactions elsewhere in a program.