Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Functional programming

Functional programming builds computations from small functions while limiting mutation and side effects. JavaScript supports this style through functions passed as values and methods such as filter, map, and reduce, but does not enforce function purity.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const transform = value => expression;
const result = values.filter(predicate).map(transform).reduce(reducer, initialValue);

Functional programming is a style of building computations by composing functions rather than managing shared mutable state. A pure function returns the same result for the same inputs and causes no observable side effects, such as changing an argument or writing to the console.

In JavaScript, functions are values: you can assign them to variables, pass them as arguments, and return them from other functions. A higher-order function accepts a function or returns one. Function composition means feeding one function's result into another; it does not require a special operator.

filter selects elements using a condition, map transforms them, and reduce combines them into one result. The example produces [2, 4], then [4, 8], and finally 12. The initial accumulator 0 also makes reduction of an empty array return 0.

This style is a convention, not a language guarantee. const does not freeze objects. filter and map create new arrays but do not deep-copy objects, and their callbacks can mutate data. Separate pure computation from input and output: the example's console.log calls are deliberate side effects outside the computation. The code uses ES2015 syntax and ES5 array methods; it requires no Web APIs.

Examples

const numbers = [1, 2, 3, 4];
const isEven = n => n % 2 === 0;
const double = n => n * 2;
const add = (total, n) => total + n;

const total = numbers
  .filter(isEven)
  .map(double)
  .reduce(add, 0);

console.log(total);
console.log(JSON.stringify(numbers));

Result

12
[1,2,3,4]

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources