DEV Community

Cover image for Breakdown of Declarative vs. Imperative programming
Kristina Hodges (Voroteliak)
Kristina Hodges (Voroteliak)

Posted on

Breakdown of Declarative vs. Imperative programming

JavaScript is an outstanding programming language that allows you to write code in different paradigms or combine them into a 'multi-paradigmatic' approach.

But what does that even mean?

Image description

A programming paradigm is an approach or a style of writing code. Most of the modern programming languages fall into two general paradigms: imperative and declarative.

So what is the difference?

Declarative programming is when you say what you want, and imperative language is when you say how to get what you want.

I like to think about it as the process of ordering a meal.

Image description

Imperative goes to a Subway restaurant and wants to order a sandwich. For that he chooses: size, type of bread, meat, veggies, cheese, extra ingredients, and a sauce, he also tells to heat up his bread. The waiter delivers exactly what he asked for.

Image description

On the other hand, Declarative goes to McDonald's and tells that he wants a double cheeseburger. Grabs the burger.

What is imperative programming?

Imperative programming is the oldest and most basic programming approach. Within the imperative paradigm, code describes a step-by-step process for a program’s execution.

Examples of imperative programming languages include:

  • Java
  • C
  • Pascal
  • Python
  • Ruby
  • Fortran
  • PHP

What is declarative programming?
In contrast with imperative programming, declarative programming describes what you want the program to achieve rather than how it should run.

In other words, within the declarative paradigm, you define the results you want a program to accomplish without describing its control flow.

Examples of declarative programming languages include:

  • SQL
  • Miranda
  • Prolog
  • Lisp
  • Many markup languages (e.g., HTML)

Let's look at some code:

Image description

Declarative programming describes what you’re trying to accomplish without defining how to do it.

Image description

For the first example which is an Imperative programming approach, it usually has more lines code, that’s the very obvious difference.
Another is, it explicitly follows a step-by-step instruction to get the even numbers from the array.
While in the declarative programming approach, it leverages an existing function, which is filter.


The differences are subtle, but hopefully you have a better background to understand them from now.

With experience, you are able to learn which situations call for which programming paradigms!

Top comments (0)