Ever wondered why JavaScript lets you use variables before you declare them? The secret is hoisting!
Why What hoisting is in Javascript? - Purpose & Use Cases
Imagine you write a JavaScript program where you try to use a variable or function before you actually write it down in your code. You might expect the program to stop and tell you it doesn't know what you mean. But sometimes, it doesn't behave that way, which can be confusing.
Without understanding hoisting, you might spend a lot of time debugging why your code runs strangely or why variables seem to exist before you declare them. This can make your code unpredictable and hard to fix, especially when you expect things to happen in the order you wrote them.
Hoisting is JavaScript's way of moving variable and function declarations to the top of their scope before running the code. This means you can use functions and some variables before you write them down, making the code run without errors but sometimes causing confusion if you don't know about it.
console.log(x);
var x = 5;var x = 5;
console.log(x);Understanding hoisting helps you write clearer code and avoid unexpected bugs by knowing how JavaScript treats your variables and functions behind the scenes.
Think of hoisting like packing for a trip: you put all your essentials at the top of your suitcase so you can find them quickly, even if you packed them later. Similarly, JavaScript 'packs' declarations at the top so it can find them when running your code.
Hoisting moves declarations to the top before code runs.
It allows using functions and variables before they appear in code.
Knowing hoisting prevents confusing bugs and helps write better code.
Practice
Solution
Step 1: Understand hoisting concept
Hoisting means JavaScript moves declarations of variables and functions to the top of their scope before running the code.Step 2: Compare options
Only JavaScript moves variable and function declarations to the top before running code. correctly describes this behavior. Other options describe unrelated behaviors.Final Answer:
JavaScript moves variable and function declarations to the top before running code. -> Option BQuick Check:
Hoisting = Moving declarations up [OK]
- Thinking hoisting moves variable assignments, not just declarations
- Confusing hoisting with asynchronous code execution
- Believing hoisting applies to all variable types equally
Solution
Step 1: Identify hoisting behavior for variable types
Variables declared withvarare hoisted and initialized asundefined.letandconstare hoisted but not initialized, causing a temporal dead zone.Step 2: Match options to behavior
var z = 15; usesvar, so it is hoisted and initialized asundefined. Others do not behave this way.Final Answer:
var z = 15; -> Option DQuick Check:
var hoisted as undefined [OK]
- Assuming let and const behave like var with hoisting
- Confusing function declarations with variable declarations
- Thinking function expressions are hoisted like functions
console.log(myVar); var myVar = 10;
Solution
Step 1: Understand var hoisting with initialization
The declarationvar myVaris hoisted to the top, but the assignment= 10stays in place.Step 2: Evaluate console.log output
At the time ofconsole.log(myVar),myVarexists but isundefinedbecause assignment hasn't happened yet.Final Answer:
undefined -> Option AQuick Check:
var declaration hoisted, value undefined [OK]
- Expecting the value 10 to print immediately
- Confusing ReferenceError with undefined for var
- Thinking let or const behave the same way
console.log(foo());
var foo = function() { return 'Hello'; };Solution
Step 1: Understand hoisting of function expressions
Variablefoois hoisted asundefined, but the function assignment is not hoisted.Step 2: Analyze calling foo before assignment
Callingfoo()before assignment tries to callundefinedas a function, causing a TypeError.Final Answer:
TypeError: foo is not a function -> Option CQuick Check:
Function expressions not hoisted as functions [OK]
- Thinking function expressions are fully hoisted
- Expecting no error because of hoisting
- Confusing ReferenceError with TypeError
console.log(greet());
function greet() {
return 'Hi!';
}
var greet = function() {
return 'Hello!';
};
console.log(greet());What will be the output?
Solution
Step 1: Analyze hoisting of function declaration
The function declarationfunction greet()is fully hoisted, so the firstgreet()call returns 'Hi!'.Step 2: Analyze var assignment after first call
Thevar greetdeclaration is hoisted but initialized as undefined. The assignment to a function expression happens after the first call, so the secondgreet()call uses the new function returning 'Hello!'.Final Answer:
'Hi!' then ReferenceError -> Option AQuick Check:
Function declaration hoisted, var assignment overrides later [OK]
- Thinking var assignment overrides before first call
- Expecting ReferenceError due to var redeclaration
- Confusing function declaration and expression hoisting
Ready to prove it?
Pass the test and this pattern moves unknown
