Hint
Declarations are hoisted fully; expressions are not — and expression form gives more control
Both create functions but behave differently with hoisting and syntax.
// Function Declaration — hoisted completely (name + body)
greet(); // ✅ works BEFORE the declaration
function greet() { return 'hello'; }
// Function Expression — NOT hoisted as a function
sayHi(); // ❌ TypeError: sayHi is not a function (var hoisted as undefined)
var sayHi = function() { return 'hi'; };
// Arrow function expression
const add = (a, b) => a + b;
// Named function expression (NFE) — name only visible inside
const fact = function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1); // factorial = self
};
console.log(typeof factorial); // undefined — not accessible outside
When to prefer each: