JavaScript Hoisting var, let, const is an important JavaScript topic because it appears in real projects, debugging sessions, and interviews. Learn the meaning first, then connect it to a small working example so the rule does not stay abstract.
For this page, focus on what problem JavaScript Hoisting var, let, const solves, where developers usually make mistakes, and how to verify the result. The audit note for this lesson was: under 650 content words; limited checklist/practice/mistake/FAQ notes .
A strong understanding of JavaScript Hoisting var, let, const should include syntax, behavior, one realistic use case, one failure case, and one quick way to check your work with tools or output.
JavaScript Hoisting var let const should be studied as a practical JavaScript lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
In the javascript > hoisting page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.
Hoisting is JavaScript's default behavior of moving function and variable declarations to the top of their scope before code execution. It means, no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.
Hoisting is not possible with variable initializations, only declarations are hoisted.
Like variable hoisting, JavaScript compiler moves the function definition at the top.
JavaScript compiler does not move function expression at the top. So, function hoisting in JavaScript is only possible with definition.
x = 10;
y = 20;
console.log(x+y); // 30
var x;
var y;
console.log(x+y); // undefined
var x = 10;
var y = 20;
console.log(Sum(10, 20)); // 30
var Total = function Sum(x, y) {
return x + y;
}
console.log(Sum(10, 20)); // 30
function Sum(x, y) {
return x + y;
}
Variables declared with let and const are hoisted to the top of their block but are NOT initialized. Accessing them before declaration causes a ReferenceError - this is called the Temporal Dead Zone (TDZ).
// var - hoisted and initialized as undefined
console.log(a); // undefined (no error)
var a = 5;
// let - hoisted but NOT initialized (Temporal Dead Zone)
// console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 10;
// const - same as let, must be initialized at declaration
// console.log(c); // ReferenceError
const c = 15;
// Practical example of TDZ
function checkTDZ() {
// TDZ starts here for 'x'
console.log(typeof x); // ReferenceError in strict mode
let x = 'hello'; // TDZ ends here
}
Like let and const, class declarations are hoisted but not initialized. You cannot use a class before it is declared.
// This will throw ReferenceError
// const obj = new Animal(); // ReferenceError
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a sound.`;
}
}
// This works fine - class is declared before use
const dog = new Animal('Dog');
console.log(dog.speak()); // Dog makes a sound.
| Declaration | Hoisted? | Initialized? | Accessible Before Declaration? |
|---|---|---|---|
| var | Yes | Yes (undefined) | Yes (returns undefined) |
| let | Yes | No (TDZ) | No (ReferenceError) |
| const | Yes | No (TDZ) | No (ReferenceError) |
| function declaration | Yes | Yes (full body) | Yes |
| function expression | Partial (var only) | No | No (TypeError) |
| class | Yes | No (TDZ) | No (ReferenceError) |
When studying JavaScript Hoisting var, let, const, separate three things: the concept, the syntax, and the situation where it is useful. This prevents the lesson from becoming a list of commands with no practical meaning.
In JavaScript, JavaScript Hoisting var, let, const becomes easier when you build a tiny example first, then increase complexity. Add one realistic input, one invalid or boundary input, and one explanation of why the result changes.
class JavaScriptHoistingvarletconstReview {
public static void main(String[] args) {
String state = "ready";
System.out.println("JavaScript Hoisting var let const: " + state);
}
}
String value = null;
if (value == null) {
System.out.println("JavaScript Hoisting var let const: handle the missing value before continuing");
}
Calling a value before checking whether it actually holds a function reference.
Trace the variable assignment, the property lookup, and the actual call expression.
Memorizing JavaScript Hoisting var let const without the situation where it is useful.
Connect JavaScript Hoisting var let const to a concrete JavaScript task.
Testing JavaScript Hoisting var let const only with the perfect input.
Include empty, missing, duplicate, incompatible, or failed cases when relevant.
Memorizing JavaScript Hoisting var let const without the situation where it is useful.
Connect JavaScript Hoisting var let const to a concrete JavaScript task.
The common mistake is memorizing syntax without understanding when the behavior changes or fails.
Remember the problem it solves in JavaScript, then attach the syntax or steps to that problem.
You can predict the result of a small example, explain a failure case, and choose it over a nearby alternative for a clear reason.
They often copy the syntax but skip the state, input, dependency, selector, route, type, or configuration that controls the behavior.
Explore 500+ free tutorials across 20+ languages and frameworks.