Understanding imperative and declarative programming
While we discussed programming paradigms in a previous article, the most common explanation of the difference between imperative and declarative programming is that imperative tells the computer how to do things, whereas declarative focuses on what we want to get from the computer.

Statements vs expressions
Imperative code uses statements and declarative code uses expressions. Expressions evaluate to a value, while statements tell the computer to do something.
In imperative programming, your code is based on statements that change the program state by telling the computer how to do things. In other words, your code is based on defining variables and changing the values of those variables.
In declarative programming, your code is based on expressions that evaluate their result based on the computer what you want.

Let’s check out an example of declarative and imperative statements in JavaScript:
Declarative:
const expression = input => input.toLowerCase();
const expression2 = function(input)
{
return input.toLowerCase();
};
Imperative:
const statement = () => console.log(“hello world”);
const statement2 = function()
{
console.log(“hello world”);
};
Expressions focus on taking input and providing output while relying only on the input itself. This is called a pure function, as we saw above.
Statements don’t necessarily need any input or output; they can just call other functions or change some value somewhere outside of their internal state. When we talk about imperative code telling the computer how to do things, we mean that it focuses on creating statements that tell the computer how to do its thing. When we talk about declarative code focusing on telling a computer what to do, we mean working with expressions that map inputs into outputs.
Programming paradigms and programming languages
If you read our previous article on programming paradigms you read that some programming languages are suited to one type of paradigm, where others can be suited to more than one. Take JavaScript, the language from our example above. It is an amazing programming language that allows us to write code in different paradigms or combine them into a multiparadigmatic approach.
However, if we look at HTML, it’s a declarative language, and all of the instructions you provide when you use it follow that paradigm. You are telling the computer what you want to see, but you leave it to the deployment package to determine how it processes that. This makes HTML ideal for automation.
State Management
To fully understand the difference between declarative and imperative languages, we need to understand state management. The state is anything in your code that holds the current values of your system, for example:
let state = {
foreground: ‘#999999’,
background: ‘#FFFFFF’
};
const imperativeMakeBackgroundBlack = () => {
state.background = ‘#000000’;
};
//directly changes the state object outside its internal scope.
const declarativeMakeBackgroundBlack = state => ({…state, background: ‘#000000’});
//takes current state as its input and returns new state with changed value
//without changing the original state
Imperative code directly accesses the state and changes it, whereas declarative expressions never change the external state.
The challenge of state management in imperative code is that, with growing complexity, you may have many parts of the code touching the same state, and when your system starts having trouble it may be quite difficult to debug.
In declarative code, you focus on building a function composition that takes several small functions and strings them together so one function sequentially passes its output as an input to the next function in line.
Imperative programming is how all programming started and goes all the way back to Assembly in 1949 which built code based on changing the registry values that hold the current state of the application.
You can read more about imperative versus declarative from this article.
Interested in our courses?
Interested in computer engineering? Find out more about all the computer engineering courses we have available by clicking here.
Diploma in Computer Engineering
Diploma in Artificial Intelligence
Alternatively, you can view all our online engineering courses here.
Recent Posts
Starting Your Engineering Journey: The Higher International Certificate (Level 4)
Starting Your Engineering Journey: The Higher International Certificate (Level 4) Embarking on a career in engineering is a significant decision, and choosing the right starting point can make all the difference. The Higher International Certificate (HIC) from iLearn Engineering® offers a flexible, accessible, and globally recognised route into the profession. Whether you are beginning your […]
Understanding Qualification Levels and Credits: a guide for engineering students
Understanding Qualification Levels and Credits: a guide for engineering students 1. Introduction to Qualification Levels 2. What Are Credits and Why Do They Matter? While 120 credits is broadly equivalent to one academic year, in traditional university settings this would usually be delivered across approximately 39 weeks with fixed timetables. In contrast, asynchronous learning models—such […]
What is Inertia and how to Calculate it ?
What is Inertia and how to Calculate it ? Inertia is the property of matter that causes it to resist changes in its velocity. This includes changes to the object’s speed or direction of motion. Inertia is directly related to mass, the greater the mass, the greater the inertia. In simple terms: “An object in […]