Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Hoisting is JavaScript's weird behavior where declarations are "lifted" to the top of their scope. It's like JavaScript reads your code twice - first to find all declarations, then to run it!
console.log(x); // ??? var x = 5; console.log(x); // 5
var x; // Moved to top! console.log(x); // undefined x = 5; console.log(x); // 5
Declaration moves to top, but value assignment stays in place
console.log(age); // ❌ ERROR! let age = 25; console.log(city); // ❌ ERROR! const city = 'Boston'; // Error: Cannot access before initialization
You must declare let and const before using them
Function declarations are fully hoisted - you can call them anywhere!
// Call function BEFORE declaring it
greet(); // Works! Prints "Hello!"
// Function declaration
function greet() {
console.log('Hello!');
}
greet(); // Also worksFunction declarations can be called before they appear in code
See all hoisting behaviors in one example
Can use before declaring (becomes undefined)
Must declare before using (gives error)
Can call before declaring
Always declare at the top to avoid confusion