JavaScript Interview Question #19

What is the difference between a function declaration and a function expression?

JavaScript Fundamentals, Types, Scope & Functions Junior Beginner

Quick Interview Answer

A function declaration is declared directly with the function keyword and a name, while a function expression creates a function as a value inside an expression, often assigned to a variable.

Detailed Explanation

A function declaration is declared directly with the function keyword and a name, while a function expression creates a function as a value inside an expression, often assigned to a variable.

The most common interview distinction is hoisting behavior. A function declaration is available throughout its scope after declaration instantiation, whereas a function expression stored in let or const cannot be used before that variable is initialized. Both are function objects and can be passed or returned as values.

Code Example

declared(); // Works

function declared() {
    console.log("Declaration");
}

// expressed(); // ReferenceError
const expressed = function () {
    console.log("Expression");
};