Problem Statement
How does function hoisting work? Can you call a function before defining it?
Explanation
Function declarations are hoisted to the top of their scope during compilation.
That means you can call a declared function before it appears in your code.
However, function expressions and arrow functions are not hoisted. They act like normal variables and exist only after assignment.
Calling them early causes a reference error.
Understanding hoisting helps you organize code properly and avoid unpredictable results.
Code Solution
SolutionRead Only
sayHi(); // Works
function sayHi() {
console.log('Hello');
}
// Not hoisted
// greet(); // Error
const greet = function() { console.log('Hi'); };