What is function composition in JavaScript?
Answer
Function composition is the technique of combining multiple functions where the output of one function becomes the input of the next. It creates a pipeline of transformations. Manual composition: const result = format(capitalize(trim(userInput))) — executes right to left. Compose helper: const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x). const process = compose(format, capitalize, trim). Pipe is the same but left-to-right (usually more readable): const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x). Example: const getActiveUserEmails = pipe(filterActive, mapToEmail, sortAlphabetically). Composition works best with: pure functions (no side effects), curried functions (single input, single output), and data transformation pipelines. Libraries: Lodash/fp _.compose(), Ramda R.pipe(). Point-free style: define functions in terms of other functions without mentioning data arguments.
Previous
What is the difference between synchronous and asynchronous error handling?
Next
What is an IIFE (Immediately Invoked Function Expression)?