What is the difference between function declaration and function expression?
Answer
A function declaration defines a named function using the function keyword as a statement: function greet(name) { return "Hello, " + name; }. Function declarations are fully hoisted — they can be called before they appear in the code. A function expression assigns a function (named or anonymous) to a variable: const greet = function(name) { return "Hello, " + name; };. Function expressions are NOT hoisted (only the variable declaration is) — calling them before the assignment throws a TypeError. Function expressions can be anonymous (no function name) or named (the name is only accessible inside the function). Arrow functions are always function expressions. Named function expressions are useful for recursion and better stack traces. Prefer function declarations for main named functions and function expressions/arrows for callbacks.
Previous
What are truthy and falsy values in JavaScript?
Next
What is an arrow function in JavaScript?