Skip to content

Declarative and imperative programming

 Published on 9th March 2023

You may have come across the terms declarative and imperative programming, but aren’t sure what they mean. If you’ve been writing a code for a reasonable amount of time, you most likely know these concepts, you just don’t know what they’re called.

Declarative programming

With declarative programming you are telling the program what you want to happen, but not how to make it happen. For example:

let res = data.filter((d) => d % 2 !== 0);

This code is saying "Give me everything where it is odd", rather than "Step through this collection. Check this item and if it is odd add it to the res array".

As a side note, functional programming is a declarative paradigm.

Imperative programming

With imperative programming you are telling the program how you want something to happen. For exampe:

let res = [];

data.forEach((d) => {
if (d % 2 !== 0) {

This code is saying:

  1. Create a results array
  2. Step through each item in the data array
  3. Check the number and if it is odd, add it to the res array

As a side note, imperative programming can also be known as procedual or object oriented programming.


Hopefully this helps to clear up any confusion you may have about the meaning of these terms.