Download Prospectus

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.

, Understanding imperative and declarative programming

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.

, Understanding imperative and declarative programming

Let’s check out an example of declarative and imperative statements in JavaScript:


const expression = input => input.toLowerCase();

const expression2 = function(input)


    return input.toLowerCase();



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 Computer Science

Diploma in Artificial Intelligence

Alternatively, you can view all our online engineering courses here.

Recent Posts

What is the magnitude of deflection in beams and how do we determine it?

What is the magnitude of deflection in beams and how do we determine it? When a beam is subjected to a load, it deflects from its original position.  In other words, it bends to a certain extent.  The amount of deflection depends on the beam’s cross-section and its strength.    There are two criteria for […]

Cost management used in construction projects.

How cost management is used in construction projects. You may have read some previous articles about construction projects, and noticed cost management mentioned as being the responsibility of a few different project roles.  Cost management is an important part of a construction project to ensure a budget is in place.  It also ensures that the […]

What is important in flooring construction?

What is important in flooring construction? In our previous articles we discussed the uses of materials such as timber and masonry in construction.  Now we’re going to talk about floors, and what is important in constructing them, including materials. What is a floor? Let’s confirm what a floor is so everyone knows what we’re dealing […]