JavaScript Interview Question #117

What does it mean that functions are first-class citizens in JavaScript?

Closures, Prototypes, Classes & Advanced Functions Mid-Level Intermediate

Quick Interview Answer

Functions are first-class values, meaning they can be stored in variables and object properties, passed as arguments, returned from other functions, and placed in data structures.

Detailed Explanation

Functions are first-class values, meaning they can be stored in variables and object properties, passed as arguments, returned from other functions, and placed in data structures.

This capability enables callbacks, higher-order functions, closures, event-driven programming, and many functional programming patterns.

Code Example

const operations = {
    add: (a, b) => a + b,
    subtract: (a, b) => a - b
};

function execute(operation, a, b) {
    return operation(a, b);
}

console.log(execute(operations.add, 5, 3)); // 8