JavaScriptHard
How does hoisting affect function declarations and expressions?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
Hoisting in JavaScript means that function declarations are moved to the top of their containing scope during the compile phase, making them available throughout the entire scope. This allows you to call a function before it is defined in the code. However, function expressions are not hoisted in the same way. If you try to call a function expression before it is defined, you will get an error because the variable holding the function is hoisted but not its assignment.
js
// Function declaration
console.log(foo()); // Works fine
function foo() {
return "Hello";
}
// Function expression
console.log(bar()); // Throws TypeError: bar is not a function
var bar = function () {
return "Hello";
};
