JavaScript Interview Question #18
What is a function expression in JavaScript?
JavaScript Fundamentals, Types, Scope & Functions Junior Beginner
Quick Interview Answer
A function expression creates a function as part of an expression, commonly by assigning it to a variable. The function may be anonymous or named.
Detailed Explanation
A function expression creates a function as part of an expression, commonly by assigning it to a variable. The function may be anonymous or named.
Unlike a function declaration, calling a const or let variable containing a function expression before the variable is initialized causes a ReferenceError because the variable is in the temporal dead zone. Function expressions are useful when functions are passed around as values or created conditionally.
Code Example
const multiply = function (a, b) {
return a * b;
};
console.log(multiply(4, 5)); // 20