Introductions
Scope determines where a variable can be accessed in JavaScript. Hoisting explains how JavaScript handles variable and function declarations before the code actually runs. These concepts can seem confusing at first, but simple examples make them much easier to understand. In this chapter, you will practice global scope, function scope, block scope, let, const, var, and hoisting with beginner-friendly examples. JavaScript Scope and Hoisting practice questions with solutions help to understand the concepts.
Question 1: Understand Global Scope
Problem
Create a variable outside a function and access it from inside the function.
Solution
let message = "Hello JavaScript";
function showMessage() {
console.log(message);
}
showMessage();
Output
Hello JavaScript
Step-by-step Explanation
messageis created outside the function.- Therefore, it has global scope in this example.
- The
showMessage()function can accessmessage. - Calling
showMessage()displays the value. - Variables in a wider scope can generally be accessed from an inner scope.
Question 2: Understand Local Function Scope
Problem
Create a variable inside a function and use it inside the same function.
Solution
function showName() {
let name = "Rahul";
console.log(name);
}
showName();
Output
Rahul
Step-by-step Explanation
nameis declared insideshowName().- Therefore, it belongs to the function’s local scope.
- The variable can be accessed inside that function.
- Calling the function displays
"Rahul".
A variable declared inside a function is not normally available outside that function.
Question 3: Understand Block Scope with let
Problem
Create a variable using let inside an if block. Try to understand where it can be accessed.
Solution
if (true) {
let message = "Inside the block";
console.log(message);
}
Output
Inside the block
Step-by-step Explanation
- The
ifstatement creates a block using{}. messageis declared usingletinside that block.- It can be accessed inside the block.
- It is not available outside the block.
For example, this would cause an error:
if (true) {
let message = "Hello";
}
console.log(message);
The variable message exists only inside the if block.
Question 4: Understand Block Scope with const
Problem
Create a constant inside a block and access it inside the same block.
Solution
{
const age = 18;
console.log(age);
}
Output
18
Step-by-step Explanation
- A block is created using
{}. ageis declared usingconst.constvariables are block-scoped.- The variable can be accessed inside the block.
- It cannot be accessed outside the block.
Question 5: Understand var Function Scope
Problem
Create a variable using var inside an if block and observe whether it can be accessed outside the block.
Solution
if (true) {
var message = "Hello";
}
console.log(message);
Output
Hello
Step-by-step Explanation
messageis declared usingvar.- Unlike
letandconst,varis not block-scoped. - The
ifblock does not create a separate scope forvar. - Therefore,
messagecan be accessed after the block. - This behavior is one reason modern JavaScript code generally prefers
letandconst.
Question 6: Understand Hoisting with var
Problem
Try to access a variable before its var declaration and observe the result.
Solution
console.log(age);
var age = 18;
Output
undefined
Step-by-step Explanation
JavaScript processes the var declaration before executing the code.
Conceptually, the code behaves somewhat like:
var age;
console.log(age);
age = 18;
- The declaration of
ageis hoisted. - Its value is initially
undefined. console.log(age)therefore displaysundefined.- After that,
agereceives the value18.
Important: The assignment age = 18 is not hoisted in the same way as the declaration.
Question 7: Understand Hoisting with let
Problem
Try to access a let variable before its declaration.
Solution
console.log(age);
let age = 18;
Output
ReferenceError
Step-by-step Explanation
ageis declared usinglet.- The declaration is processed before execution, but the variable cannot be accessed before its declaration is reached.
- This period is known as the Temporal Dead Zone (TDZ).
- Accessing
agebefore the declaration causes aReferenceError. - Therefore, unlike
var, you cannot safely access aletvariable before its declaration.
Question 8: Understand Hoisting with const
Problem
Try to access a const variable before its declaration.
Solution
console.log(name);
const name = "Aman";
Output
ReferenceError
Step-by-step Explanation
nameis declared usingconst.constvariables cannot be accessed before their declaration.- The variable is in the Temporal Dead Zone before the declaration is executed.
- Accessing it too early causes a
ReferenceError. - Therefore, declare the variable before using it.
Correct approach:
const name = "Aman";
console.log(name);
Question 9: Understand Function Declaration Hoisting
Problem
Call a function before its declaration and observe what happens.
Solution
greet();
function greet() {
console.log("Hello JavaScript");
}
Output
Hello JavaScript
Step-by-step Explanation
greet()appears before the function declaration.- JavaScript hoists function declarations.
- Therefore, the function can be called before its declaration in this example.
- The function executes normally.
"Hello JavaScript"is displayed.
This is different from variables declared with let and const.
Question 10: Understand Scope with Nested Functions
Problem
Create an outer function containing a variable and an inner function. Access the outer variable from the inner function.
Solution
function outer() {
let message = "Hello from outer function";
function inner() {
console.log(message);
}
inner();
}
outer();
Output
Hello from outer function
Step-by-step Explanation
outer()is the outer function.messageis created insideouter().inner()is created insideouter().inner()can accessmessage.- This works because the inner function can access variables from its surrounding scope.
inner()is called from insideouter().- The message is displayed.
This idea becomes very important later when learning closures.
Key Takeaways
- Scope determines where a variable can be accessed.
- Global variables can be accessed from wider parts of a program.
- Variables declared inside functions have local function scope.
letandconstare block-scoped.varis function-scoped rather than block-scoped.- Hoisting affects how declarations are handled before code execution.
varvariables can produceundefinedwhen accessed before their assignment.letandconstcannot be accessed before their declaration.- The period before a
letorconstdeclaration is called the Temporal Dead Zone. - Function declarations are hoisted.
- Inner functions can access variables from their outer scopes.
- Understanding scope and hoisting helps prevent unexpected JavaScript errors.
FAQs
1. What is scope in JavaScript?
Scope defines where a variable can be accessed in a JavaScript program.
For example:
function test() {
let message = "Hello";
console.log(message);
}
Here, message is available inside the function.
2. What is global scope?
A variable declared outside functions or blocks can have global scope.
let name = "Rahul";
function showName() {
console.log(name);
}
The function can access the outer name variable.
3. What is function scope?
Function scope means a variable is available within the function where it was declared.
function test() {
let age = 18;
console.log(age);
}
The age variable belongs to the function’s local scope.
4. What is block scope?
Block scope limits a variable to a block created by {}.
let and const are block-scoped.
if (true) {
let age = 18;
}
age cannot be accessed outside this block.
5. What is hoisting?
Hoisting is JavaScript’s behavior around processing declarations before executing code. Function declarations can be called before their declaration, while var, let, and const behave differently.
6. What is the difference between var, let, and const?
The main difference includes their scope and behavior before declaration.
| Keyword | Scope | Access before declaration |
|---|---|---|
var | Function scope | undefined in typical cases |
let | Block scope | ReferenceError |
const | Block scope | ReferenceError |
7. What is the Temporal Dead Zone?
The Temporal Dead Zone, or TDZ, is the period between entering a scope and reaching the declaration of a let or const variable. Accessing the variable during this period causes a ReferenceError.
Example:
console.log(age);
let age = 18;
This produces a ReferenceError.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
