By Hemanta Sundaray on 2021-07-21
In declarative programming, you describe the desired result you want without explicitly listing all the steps that must be performed.
In imperative programming, you describe each of the steps that must be performed to get the desired result.
The concept of declarative and imperative programming is easy to understand with an analogy from real life.
Let’s say we meet on the street and you ask me the address of the nearest Domino's Pizza.
I have two options:
Above, option a would be the declarative approach, and option b would be the imperative approach.
Let’s consider another example:
Let’s write two programs that identify elements that are greater than 10 from an array and then return an array with those elements.
const nums = [5, 25, 100, 300]
const greaterThanTen = () => {
const array = []
for (const number of nums) {
if (number > 10) {
array.push(number)
}
}
return array
}
console.log(greaterThanTen(nums))
// [25, 100, 300]
const nums = [5, 25, 100, 300]
const filteredArray = nums.filter(number => number > 10)
console.log(filteredArray)
// [25, 100, 300]
As you can see, in program 1, we have explicitly given a set of instructions (a set of of control flow statements) to get the desired result. This is an example of imperative programming where we instruct our program how to do something.
On the contrary, in program-2, we achieved the desired result in just one line of code using the built-in Array.filter() method. This is an example of declarative programming, where we instruct our program what to do - we don’t bother ourselves with step-by-step instructions. The steps are abstracted away from us.