Middle
What is the difference between function declaration and function expression?
sobes.tech AI
Answer from AI
Function Declaration — is a function statement with a name that is hoisted into the scope. Such functions can be called before their definition in the code.
function greet() {
console.log('Hello');
}
greet(); // works
Function Expression — is an assignment of a function (anonymous or named) to a variable. These functions are not hoisted, and can only be called after their definition.
const greet = function() {
console.log('Hello');
};
greet(); // works
The difference is that function declaration creates a function at compile time, while function expression does so at runtime, affecting the order of calls and scope.