Global Scope
What you'll learn: How variables declared outside functions become accessible throughout your entire JavaScript program.
The Global Playing Field
When you declare a variable outside of any function, block, or module, it lives in the global scope. Think of global scope as the outermost container—the "main stage" where your JavaScript program begins. Any variable declared here is visible and accessible from anywhere in your code.
How It Works
Variables declared in global scope exist for the entire lifetime of your program. They're not hidden inside any particular function or code block—they're shared everywhere:
let userName = "Alice";
const MAX_SCORE = 100;
// These variables are globally accessible
// You can use them anywhere in your program
If you later define a function (a concept coming soon), that function can "see" and use userName and MAX_SCORE because they're global.
Why Global Scope Matters
Global scope is where your program starts executing. It's the default context before you create any smaller, enclosed spaces. Every variable you've declared so far in simple scripts has been in global scope—that's why they've worked everywhere.
However, be aware: because global variables are accessible everywhere, changing them anywhere affects your entire program. This power requires care as your programs grow larger.
Key Takeaway: Variables declared outside all functions and blocks live in the global scope and are accessible throughout your entire program—they're shared by all parts of your code.