The Tech Demystify: Imperative Methods Versus Declarative Methods in JavaScript

Ismaeel Shuaib
Facebook Developer Circle: Accra
3 min readAug 13, 2020

This weekend, we have Oliver Mensah(a Software Developer & Developer Community Lead @ DevCAccra) taking us through the Building Blocks of Declarative Methods in JavaScript.

When you’re writing a program, often you need a container to keep your data, easily access and manipulate the data. Collections are best suitable for such work. Oliver described Collection as

basically a container that holds a lot of data

How you implement your collections makes a huge difference in enhancing the performance of your program. Using the Classic Imperative approach against the Declarative approach.

Simply, an imperative approach is where you explicitly write out each step of how you want your code to run. The declarative approach is where you merely say what it is that you want the code to do.

Consider the following array;

An array of 2 students (names & emails)

Imperative Approach

With the Imperative approach, you need to access each data. To get the email of each student, you need to have some sort of a loop to go through the data and access it for you and put the data you access in some sort of storage.

Declarative Approach

Map

The map() the method creates a new array populated with the results of calling a provided function on every element.

Filter

The filter() the method creates a new array with all elements that pass the test implemented by the provided function.

Reduce

The reduce() the method executes a reducer function (that you provide) on each element of the array, resulting in single output value.

The declarative approach of coding is much shorter and readable.

Here’s Oliver’s video for a detailed explanation of the declarative approach of coding.

And here’s a GitHub repo on the last challenge.

If you enjoy this post so far, give us a clap and like our Facebook page.

#TheTechDemystify is in every Sunday at 7 pm on Facebook Group, make sure to tune in and enjoy every reverting conversation as well as tutorial sessions to help you grow in tech.

--

--