ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Introduction to Computer Programming• 6 min read

Scope, Variable Lifetime & Closures

Understand block scope, lexical scope, variable lifetime in memory, and how closures encapsulate state.

Global Scope vs. Local Block Scope

Scope defines the visibility and lifetime of a variable:

Global Scope: Declared outside all functions; accessible from anywhere, but dangerous due to accidental overwrites.
Local / Block Scope: Declared inside a function or {} block; only visible inside that block and destroyed when the block finishes execution.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Can code outside of a function access variables declared locally inside that function?