
JavaScript Hoisting Demystified
Key Conclusion: Only function declarations are fully hoisted and can be invoked before their position in the code. In contrast, function expressions and arrow functions (also called lambda functions) are not hoisted in the same way—they must be declared before they can be called.
What comes to mind when we hear "hoisting"? The term suggests lifting or elevating something to a higher position—which is exactly what JavaScript does with declarations. What is Hoisting? Hoisting is JavaScript's default behaviour of moving declarations to the top of their scope during the compilation phase, before code execution begins. Mental Model: JavaScript processes code in two phases: first, it reads and registers all declarations; then, it executes the code line by line.
doSomething();
function doSomething() {
console.log("declaration function was hoisted");
}
const doSomethingExpression = function () {
console.log("expression function not hoisted");
};
doSomethingExpression();
const lexicaldoSomething = () => {
console.log("lexical function not hoisted");
};
lexicaldoSomething();Comments
No comments yet — be the first to say something.