Beginner JavaScript Interview QuestionsBeginnerComparison
JavaScript · Question 14
What is the difference between a function declaration and a function expression?
Direct answer
A function declaration defines a named function as a declaration and is initialized before normal execution, while a function expression creates a function as part of an expression and follows the initialization rules of its containing variable.
A declaration looks like function greet() {}. An expression can look like const greet = function () {}; or const greet = () => {};.
- Function declarations can usually be called earlier in the same scope because the function binding is initialized during environment setup.
- A function expression assigned to let or const cannot be used before that variable is initialized.
- Function expressions are convenient when functions are passed around as values, assigned conditionally, or created inline.