liveBook · Manning

Declarative programming

As explained in Functional Programming in JavaScript

A programming paradigm that describes what should be done by specifying desired results rather than the step-by-step control flow to achieve them.

Declarative programming describes the result or logic you want without spelling out every control-flow step or state change used to produce it. It contrasts with imperative programming, where a program is a top-to-bottom sequence of statements that changes system state. Functional programming uses this style to separate program description from evaluation, making the main logic easier to inspect and alter without rewriting its internal steps. section 1.2

Square an array by describing the transformation

An imperative solution loops through an array and overwrites each item with its square. A declarative rewrite uses map with a squaring function: the code states that each number should be transformed, while map handles the traversal. Both produce the same squared array, but the latter keeps the desired operation separate from the iteration mechanics. section 1.2

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(
      function(num) {
         return Math.pow(num, 2);                         #1
      }); 
//-> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

This map expression names the transformation to apply to every number instead of exposing a loop, index, and assignment. section 1.2

Function chains extend the same idea to data processing. Operations such as map, filter, and reduce can be linked so that each step describes a transformation, selection, or aggregation of data. This removes many manual loops and shifts attention toward the output and the structure of the data. section 3.3 section 3.0

Select and format people

A chain can filter people born in 1903, map them to full names, and join the names into one string. Rather than managing a result array and loop conditions directly, the chain states the successive data operations: select matching people, extract names, then combine them. section 3.3

Declarative style works especially well with pure functions and function composition. Small functions can serve as reusable building blocks, while avoiding unnecessary changes to global state improves readability, testability, modularity, and maintainability. section 1.2 section 1.4

How Functional Programming in JavaScript covers it

How deep it goes
Contrasts imperative loops with map and develops chained map, filter, and reduce operations, without detailing declarative implementation internals.
Examples
Uses DOM message rendering, array squaring, person-name processing, country statistics, and filtering people born in 1903.
What you can do after
Apply it via Lodash.js and JavaScript function chains to express collection transformations without manual loops.
How it connects
Connects functional programming with pure functions, referential transparency, immutability, function chaining, map, filter, reduce, and reactive programming.

Also explained in

Compare how 15 books explain declarative programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.