SkillByAIOpen interactive version →

Lesson 10 / 40

Declarations vs Expressions

Define functions as declarations or expressions and understand how hoisting affects each.

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.