Lesson 10 / 38

Declarations vs Expressions

Two ways to define a function, and how hoisting differs.

Function declaration

Hoisted — callable even before its definition appears in the code.

function add(a, b) {
  return a + b;
}
console.log(add(2, 3));  // 5

Function expression

Assigned to a variable — not hoisted the same way; usable only after the assignment line runs.

const multiply = function (a, b) {
  return a * b;
};
console.log(multiply(2, 3));  // 6

Named vs anonymous

Give function expressions a name (const f = function greet() {}) — it shows up in stack traces and makes debugging easier.