imperative-vs-declarative

Explain the differences between imperative and declarative programming.

Answer

These two types of programming can roughly be summarized as:

  • Imperative: how to achieve something

  • Declarative: what should be achieved

A common example of declarative programming is CSS. The developer specifies CSS properties that describe what something should look like rather than how to achieve it. The “how” is abstracted away by the browser.

On the other hand, imperative programming involves the steps required to achieve something. In JavaScript, the differences can be contrasted like so:

Imperative

const numbers = [1, 2, 3, 4, 5]
const numbersDoubled = []
for (let i = 0; i < numbers.length; i++) {
  numbersDoubled[i] = numbers[i] * 2
}

We manually loop over the numbers of the array and assign the new index as the number doubled.

Declarative

const numbers = [1, 2, 3, 4, 5]
const numbersDoubled = numbers.map(n => n * 2)

We declare that the new array is mapped to a new one where each value is doubled.

Good to hear

  • Declarative programming often works with functions and expressions. Imperative programming frequently uses statements and relies on low-level features that cause mutations, while declarative programming has a strong focus on abstraction and purity.

  • Declarative programming is more terse and easier to process at a glance.

Additional links

Last updated