← Back to JavaScript, page 2

javascript-0028 · Easy fresher

What is the difference between a function declaration and a function expression? What does it mean that functions are first-class?

A declaration (function name() {}) is hoisted with its body, so it can be called before it appears. A function expression creates a function as a value, is not available before its line, and can be anonymous, named, or immediately invoked. Functions are first-class values: they can be stored in variables, passed as arguments, and returned.

Full answer

A function declaration starts a statement with the function keyword and a name. Because it is hoisted with its body, a call placed above it in the file works.

A function expression is a function used where a value is expected, for example on the right side of const add = function (a, b) { ... }, as an argument, or after return. The variable that holds it follows normal rules for let, const, or var, so calling it early fails. A named function expression, such as const fact = function inner(n) {...}, makes the name inner visible only inside the function, which helps recursion and stack traces. Arrow functions are always expressions.

An immediately invoked function expression (IIFE), (function () { ... })(), runs once and keeps its variables private. Before modules and let, this was the main way to avoid globals.

Functions are first-class: they behave like any other value. They can be put in arrays and objects, passed to other functions (callbacks), and returned from functions (closures and factories). Functions that take or return functions are called higher-order functions, and map, filter, and addEventListener are examples.

Function declarations inside blocks have confusing legacy rules in sloppy mode, so most guides advise using expressions or arrow functions for functions in blocks.

javascript
console.log(declared(2, 3));
function declared(a, b) {
  return a + b;
}

try {
  console.log(expressed(2, 3));
} catch (error) {
  console.log('expression called early:', error.name);
}
const expressed = function (a, b) {
  return a * b;
};
console.log(expressed(2, 3));

const factorial = function inner(n) {
  return n <= 1 ? 1 : n * inner(n - 1);
};
console.log(factorial(5), typeof inner);

const result = (function () {
  const secret = 'private';
  return `IIFE keeps ${secret}`;
})();
console.log(result);

function applyTwice(fn, value) {
  return fn(fn(value));
}
const addTax = (price) => price * 1.1;
console.log(applyTwice(addTax, 100).toFixed(2));

function makeMultiplier(factor) {
  return (n) => n * factor;
}
const double = makeMultiplier(2);
console.log(double(21), [1, 2, 3].map(double));

Run with Node.js v24.22.1 as js-28.js; two runs gave identical output. Output: 5 expression called early: ReferenceError 6 120 undefined IIFE keeps private 121.00 42 [ 2, 4, 6 ]

Key points

  • Declarations are hoisted with their body; expressions are not.
  • A function expression is a value and can be anonymous, named, or immediately invoked.
  • A name in a named function expression is visible only inside the function.
  • Functions are first-class: store, pass, and return them.
  • Higher-order functions accept or return functions.

Common mistakes

  • Calling a function expression before its definition.
  • Forgetting the parentheses around an IIFE, which makes the engine treat it as a declaration and fail.
  • Saying first-class functions are only about callbacks. Returning functions matters equally.
  • Declaring functions inside if blocks in sloppy mode and relying on inconsistent hoisting.

Follow-ups

  • Why does typeof inner print undefined outside the named function expression?
  • What is a higher-order function? Give two built-in examples.
  • Are modules and let/const a replacement for the IIFE pattern?

Tips

  • Start with hoisting; it is the difference interviewers listen for.
  • Explain first-class with three verbs: store, pass, return.
  • Give the IIFE as a historical pattern and say what replaced it.