๐ Scenario: Imagine you are working on a small JavaScript program that uses variables and functions. You want to understand how JavaScript moves variable and function declarations to the top before running the code. This is called hoisting. Sometimes, hoisting can cause unexpected results if you don't know how it works.
๐ฏ Goal: You will create a simple JavaScript program that shows how hoisting works with var, let, and function declarations. You will see common mistakes and how to avoid them.
๐ What You'll Learn
Create a variable using var and assign a value after using it
Create a variable using let and try to use it before declaration
Create a function declaration and call it before its definition
Print the results to observe hoisting behavior
๐ก Why This Matters
๐ Real World
Hoisting is a key concept in JavaScript that affects how variables and functions behave when the code runs. Knowing it helps you debug errors and write better code.
๐ผ Career
Many JavaScript jobs require understanding hoisting to maintain legacy code and avoid common pitfalls in web development.
Progress0 / 4 steps
1
Create a var variable and use it before assignment
Create a variable called message using var. Then write a console.log(message) line before assigning message = 'Hello from var'.
Javascript
Hint
Variables declared with var are hoisted but their value is undefined until assigned.
2
Create a let variable and try to use it before declaration
Add a console.log(count) line before declaring a variable called count using let and assigning it the value 10.
Javascript
Hint
Variables declared with let are not hoisted like var. Using them before declaration causes an error.
3
Create a function declaration and call it before definition
Write a call to a function named greet before its declaration. Then declare the function greet that prints 'Hello from function' using console.log.
Javascript
Hint
Function declarations are hoisted completely, so you can call them before they appear in the code.
4
Print all outputs to observe hoisting behavior
Run the program and observe the output printed by the console.log statements. The output should show undefined for the var variable before assignment, an error for the let variable, and the greeting message from the function.
Javascript
Hint
Look carefully at the console output to understand how hoisting affects each case.
Practice
(1/5)
1. What happens when you access a var variable before its declaration in JavaScript?
easy
A. It returns undefined because var is hoisted and initialized with undefined.
B. It throws a ReferenceError because the variable is not defined yet.
C. It returns the value assigned later in the code.
D. It causes a SyntaxError.
Solution
Step 1: Understand var hoisting behavior
Variables declared with var are hoisted to the top and initialized with undefined.
Step 2: Access before declaration
Accessing the variable before its declaration returns undefined instead of an error.
Final Answer:
It returns undefined because var is hoisted and initialized with undefined. -> Option A
Quick Check:
var hoisting = undefined [OK]
Hint: var is hoisted and set to undefined before code runs [OK]
Common Mistakes:
Thinking accessing var before declaration throws error
Confusing var with let or const hoisting
Assuming var is not hoisted at all
2. Which of the following is the correct way to declare a variable that will cause a ReferenceError if accessed before declaration?
easy
A. x = 10;
B. var x = 10;
C. function x() {}
D. let x = 10;
Solution
Step 1: Recall let hoisting
let is hoisted but not initialized, causing a temporal dead zone.
Step 2: Identify which causes ReferenceError before declaration
Accessing let before declaration throws ReferenceError. Here, let x = 10; uses let.
Final Answer:
let x = 10; -> Option D
Quick Check:
let before declaration = ReferenceError [OK]
Hint: let causes ReferenceError if accessed early [OK]
Common Mistakes:
Thinking var causes ReferenceError before declaration
Confusing function declarations with variables
Assuming const behaves like var
3. What is the output of the following code?
console.log(foo());
function foo() {
return 'Hello';
}
var foo = function() {
return 'World';
};
medium
A. 'Hello'
B. 'World'
C. TypeError
D. ReferenceError
Solution
Step 1: Understand function declaration hoisting
Function declarations are fully hoisted, so foo is initially the function returning 'Hello'.
Step 2: Analyze var function expression assignment
The var foo declaration is hoisted but the assignment to function expression happens after the call, so it does not overwrite the function declaration at the time of call.
Final Answer:
'Hello' -> Option A
Quick Check:
Function declarations hoisted fully = 'Hello' [OK]
Hint: Function declarations hoisted fully, var function expressions not [OK]
Common Mistakes:
Assuming var function expression overwrites before call
Confusing function declaration and expression hoisting
Expecting TypeError or ReferenceError
4. Identify the error in the following code and fix it:
console.log(a);
let a = 5;
medium
A. No error, outputs 5
B. SyntaxError due to missing semicolon
C. ReferenceError because let is accessed before declaration
D. TypeError because a is not a function
Solution
Step 1: Recognize temporal dead zone with let
Variables declared with let are hoisted but not initialized, causing ReferenceError if accessed before declaration.
Step 2: Fix by moving declaration before usage
Move let a = 5; before console.log(a); to avoid ReferenceError.
Final Answer:
ReferenceError because let is accessed before declaration -> Option C
Quick Check:
let before declaration = ReferenceError [OK]
Hint: Declare let variables before use to avoid ReferenceError [OK]
Common Mistakes:
Thinking let behaves like var
Ignoring temporal dead zone
Assuming missing semicolon causes error
5. Consider this code snippet:
console.log(typeof bar);
var bar = 'test';
console.log(typeof baz);
let baz = 'hello';
What will be the output and why?
hard
A. 'string' followed by 'string'
B. 'undefined' followed by ReferenceError
C. ReferenceError followed by 'undefined'
D. 'undefined' followed by 'undefined'
Solution
Step 1: Analyze typeof bar with var
var bar is hoisted and initialized with undefined, so typeof bar returns 'undefined'.
Step 2: Analyze typeof baz with let
let baz is hoisted but not initialized, accessing it before declaration causes ReferenceError, even with typeof.
Final Answer:
'undefined' followed by ReferenceError -> Option B
Quick Check:
var hoisted initialized, let TDZ causes ReferenceError [OK]
Hint: typeof var before declaration = 'undefined', let causes ReferenceError [OK]
Common Mistakes:
Assuming typeof let before declaration returns 'undefined'
Confusing var and let hoisting behavior
Expecting no error from typeof on let
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.