Global scope means a place where variables can be seen and used anywhere in your whole program. It helps share information across different parts of your code.
When you want to keep a value that many parts of your program need to use.
When you want to store settings or options that affect the whole program.
When you want to share data between different functions or files.
When you want to keep track of something that lasts while the program runs.
When you want to create simple examples or quick tests without complex structure.
Syntax
Javascript
let myGlobal = 10;
function show() {
console.log(myGlobal);
}
show();
Variables declared outside any function or block are in the global scope.
Global variables can be accessed from anywhere in the code.
Examples
This variable is global because it is outside any function.
Javascript
let greeting = 'Hello';
console.log(greeting);
Variable count is global and used inside the function.
Javascript
var count = 5;
function printCount() {
console.log(count);
}
printCount();
Constant PI is global and used in the function.
Javascript
const PI = 3.14;
function area(r) {
return PI * r * r;
}
console.log(area(2));
Sample Program
This program shows a global variable message used inside a function and outside it.
Javascript
let message = 'Good morning!';
function greet() {
console.log(message);
}
greet();
console.log(message);
OutputSuccess
Important Notes
Be careful with global variables because too many can make your program confusing.
Use global scope only when you really need to share data everywhere.
Modern JavaScript modules help avoid unwanted global variables.
Summary
Global scope means variables are available everywhere in your code.
Declare variables outside functions to make them global.
Use global variables carefully to keep your code clear and easy to understand.
Practice
(1/5)
1. What does it mean when a variable is in the global scope in JavaScript?
easy
A. The variable can be accessed anywhere in the code.
B. The variable can only be used inside a function.
C. The variable is hidden from all parts of the code.
D. The variable is deleted after a function finishes.
Solution
Step 1: Understand global scope meaning
Global scope means a variable is declared outside any function and is accessible everywhere in the code.
Step 2: Compare options with definition
Only The variable can be accessed anywhere in the code. correctly states that the variable can be accessed anywhere.
Final Answer:
The variable can be accessed anywhere in the code. -> Option A
Quick Check:
Global scope = accessible everywhere [OK]
Hint: Global variables are outside functions, accessible everywhere [OK]
Common Mistakes:
Thinking global variables are only inside functions
Confusing global with local scope
Believing global variables disappear after function ends
2. Which of the following is the correct way to declare a global variable in JavaScript?
easy
A. Inside a function using let
B. Outside any function using var
C. Inside a block using const
D. Inside a function using const
Solution
Step 1: Identify where to declare global variables
Global variables must be declared outside any function or block.
Step 2: Check declaration keywords
Using var outside functions creates a global variable. Options A, B, and D declare inside functions or blocks, so they are not global.
Final Answer:
Outside any function using var -> Option B
Quick Check:
Global variable = declared outside functions [OK]
Hint: Declare global variables outside functions with var [OK]
Common Mistakes:
Declaring variables inside functions and expecting global scope
Using let or const inside blocks thinking they are global
Confusing block scope with global scope
3. What will be the output of this code?
var x = 10;
function show() {
console.log(x);
}
show();
medium
A. 10
B. undefined
C. ReferenceError
D. null
Solution
Step 1: Identify variable scope
Variable x is declared globally with var and has value 10.
Step 2: Check function access
The function show logs x. Since x is global, it is accessible inside the function.
Final Answer:
10 -> Option A
Quick Check:
Global variable accessed inside function = 10 [OK]
Hint: Global variables are accessible inside functions [OK]
Common Mistakes:
Expecting undefined because variable is outside function
Thinking console.log will cause ReferenceError
Confusing local and global scope
4. Find the error in this code related to global scope:
function test() {
var a = 5;
}
console.log(a);
medium
A. No error, prints 5
B. TypeError: a is undefined
C. ReferenceError: a is not defined
D. SyntaxError: var inside function
Solution
Step 1: Check variable scope
Variable a is declared inside function test with var, so it is local to that function.
Step 2: Access variable outside function
Trying to log a outside the function causes a ReferenceError because a is not in global scope.
Final Answer:
ReferenceError: a is not defined -> Option C
Quick Check:
Local variable accessed globally = ReferenceError [OK]
Hint: Variables inside functions are not global [OK]
Common Mistakes:
Assuming var inside function is global
Expecting output 5 instead of error
Confusing ReferenceError with SyntaxError
5. You want to create a global counter variable that increases every time a function runs. Which code correctly uses global scope to do this?
hard
A.
let counter = 0;
function count() {
let counter = 0;
counter++;
console.log(counter);
}
B.
function count() {
var counter = 0;
counter++;
console.log(counter);
}
C.
function count() {
let counter = 0;
counter++;
console.log(counter);
}
D.
var counter = 0;
function count() {
counter++;
console.log(counter);
}
Solution
Step 1: Identify global variable declaration
var counter = 0;
function count() {
counter++;
console.log(counter);
}
declares counter outside the function, making it global and accessible inside the function.
Step 2: Check counter increment behavior
Inside count, counter++ increases the global variable, so the count persists across calls.
Final Answer:
var counter = 0; function count() { counter++; console.log(counter); } -> Option D
Quick Check:
Global counter increments inside function =
var counter = 0;
function count() {
counter++;
console.log(counter);
}
[OK]
Hint: Declare counter outside function to keep it global [OK]
Common Mistakes:
Declaring counter inside function resets it each call
Using let inside function hides global variable
Expecting local counter to remember previous value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.