๐ Scenario: Imagine you are learning how JavaScript handles variables before the code runs. This is called hoisting. It means some variables are moved to the top of their scope automatically.We will explore how var, let, and const behave differently when hoisted.
๐ฏ Goal: You will write small pieces of code to see how JavaScript treats variables declared with var, let, and const before they are assigned values.By the end, you will understand why some variables can be used before declaration and others cannot.
๐ What You'll Learn
Create variables using var, let, and const with specific names
Assign values after declaration
Try to access variables before declaration to see hoisting effects
Print outputs to observe results
๐ก Why This Matters
๐ Real World
Understanding variable hoisting helps avoid bugs when variables are used before they are declared in JavaScript code.
๐ผ Career
Many JavaScript jobs require knowledge of hoisting to write clean, bug-free code and debug issues effectively.
Progress0 / 4 steps
1
Create variables with var, let, and const
Create three variables: varVariable using var, letVariable using let, and constVariable using const. Assign the values "I am var", "I am let", and "I am const" respectively.
Javascript
Hint
Use var, let, and const keywords to declare variables with the exact names and values given.
2
Access variables before declaration
Write three console.log statements to print varVariable, letVariable, and constVariable before their declarations. Place these console.log lines above the variable declarations from Step 1.
Javascript
Hint
Place the console.log lines before the variable declarations to see what happens when you access variables before they are declared.
3
Explain hoisting behavior with comments
Add comments above each console.log line explaining what you expect to see when running the code for varVariable, letVariable, and constVariable. Use the exact comments: // undefined due to var hoisting, // ReferenceError due to temporal dead zone, and // ReferenceError due to temporal dead zone respectively.
Javascript
Hint
Write the exact comments above each console.log to describe the expected output for each variable.
4
Print variable values after declaration
Add three console.log statements after the variable declarations to print the values of varVariable, letVariable, and constVariable.
Javascript
Hint
Print the variables after their declarations to see their assigned values.
Practice
(1/5)
1. What happens to a var variable declaration in JavaScript before code execution?
easy
A. It is not moved and must be declared before use.
B. It causes a syntax error if used before declaration.
C. It is moved to the top and initialized as undefined.
D. It is moved to the top and initialized with its assigned value.
Solution
Step 1: Understand var hoisting behavior
JavaScript moves var declarations to the top of their scope and initializes them as undefined.
Step 2: Check initialization timing
The variable is not assigned its value until the code runs the assignment line, so before that it is undefined.
Final Answer:
It is moved to the top and initialized as undefined. -> Option C
Quick Check:
var hoisted = undefined [OK]
Hint: Remember: var is hoisted and set to undefined first [OK]
Common Mistakes:
Thinking var is not hoisted
Assuming var is initialized with assigned value immediately
Confusing var with let or const hoisting
2. Which of the following is the correct way to declare a variable that is hoisted but causes a ReferenceError if accessed before declaration?
easy
A. var x = 5;
B. let x = 5;
C. function x() {}
D. const x;
Solution
Step 1: Identify hoisting behavior of let
let declarations are hoisted but not initialized, so accessing them before declaration causes a ReferenceError.
Step 2: Check other options
var is hoisted and initialized as undefined, so no ReferenceError; const must be initialized at declaration, so const x; is a syntax error; function declarations are hoisted differently.
Final Answer:
let x = 5; -> Option B
Quick Check:
let hoisted but uninitialized causes ReferenceError [OK]
Hint: let and const cause errors if used before declaration [OK]
Common Mistakes:
Using const without initialization
Confusing var and let hoisting
Thinking function declarations behave like let
3. What is the output of the following code?
console.log(a);
var a = 10;
console.log(a);
medium
A. undefined 10
B. 10 10
C. ReferenceError undefined
D. undefined undefined
Solution
Step 1: Analyze var hoisting for variable a
The declaration var a is hoisted and initialized as undefined before any code runs.
Step 2: Trace console.log outputs
The first console.log(a) prints undefined because assignment hasn't happened yet. After a = 10, the second console.log(a) prints 10.
Final Answer:
undefined 10 -> Option A
Quick Check:
var hoisted as undefined, then assigned [OK]
Hint: var logs undefined before assignment, then assigned value [OK]
Common Mistakes:
Expecting ReferenceError on first log
Thinking var is assigned before declaration
Confusing let/const behavior with var
4. Find the error in the following code and choose the correct fix:
console.log(b);
let b = 20;
medium
A. Change let to var to avoid ReferenceError.
B. Wrap the code in a function to fix hoisting.
C. Initialize b with a value before logging.
D. Move console.log(b) after the declaration.
Solution
Step 1: Identify cause of ReferenceError
let variables are hoisted but not initialized, so accessing b before declaration causes ReferenceError.
Step 2: Fix by reordering code
Moving console.log(b) after let b = 20; ensures b is declared before use, avoiding the error.
Final Answer:
Move console.log(b) after the declaration. -> Option D
Quick Check:
Access let after declaration to avoid error [OK]
Hint: Use variables only after let/const declaration [OK]
Common Mistakes:
Changing let to var without understanding scope
Trying to initialize before declaration (syntax error)
Assuming wrapping in function fixes hoisting
5. Consider this code:
console.log(x);
if (true) {
var x = 5;
let y = 10;
console.log(y);
}
console.log(x);
console.log(y);
What will be the output and why?
hard
A. undefined 10 5 ReferenceError
B. ReferenceError 10 5 ReferenceError
C. undefined 10 5 10
D. undefined ReferenceError 5 ReferenceError
Solution
Step 1: Analyze var x hoisting and scope
var x is hoisted to the function or global scope and initialized as undefined. So, first console.log(x) prints undefined. After assignment inside the block, x becomes 5.
Step 2: Analyze let y block scope and access
let y = 10 is block scoped. Inside the block, console.log(y) prints 10. Outside the block, console.log(y) causes ReferenceError because y is not defined there.
Step 3: Trace remaining console.logs
After the block, console.log(x) prints 5. The last console.log(y) throws ReferenceError.
Final Answer:
undefined 10 5 ReferenceError -> Option A
Quick Check:
var hoisted globally, let block scoped [OK]
Hint: var is function/global scoped; let is block scoped [OK]
Common Mistakes:
Assuming let is accessible outside block
Expecting var to be block scoped
Confusing initialization timing of var and let
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.