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
We manually loop over the numbers of the array and assign the new index as the number doubled.
Declarative
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