JavaScript Function Declaration vs Function Expression

Written: August 15, 2026

A function declaration is hoisted as a full function, so you can call it before its line appears. A function expression assigns a function to a variable and follows that variable’s temporal rules.

Use declarations for named top-level helpers. Use expressions (including arrow functions) when passing callbacks or assigning conditionally.

Examples

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

// Expression — not usable before initialization
const mul = function (a, b) {
  return a  b;
};
console.log(mul(2, 3)); // 6

// Arrow expression
const dbl = (n) => n  2;

What to remember

Calling a const/let function expression before its line throws a ReferenceError (TDZ).

Declarations are clearer for recursive named functions; arrows shine for short callbacks.

Keep learning

If this walkthrough on JavaScript Function Declaration vs Function Expression helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.

Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.

Advertisement

Previous Article

C Program to Write a String into a File

Next Article

C Program to Print Number Patterns Using Nested Loops

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨