From imperative to declarative JavaScript

From imperative to declarative JavaScript

In this post, I will explain why declarative code is better than imperative code. Then I will list some techniques to convert imperative JavaScript to a declarative one in common situations.

Introduction

In this post, I will explain why declarative code is better than imperative code.

Then I will list some techniques to convert imperative JavaScript to a declarative one in common situations, defining key terms along the way.

Why declarative ?

First, let’s define what declarative and imperative mean.

Declarative code is one that highlights the intent of what it’s doing.

It favors the “what” over the “how”.

In other words, the exact implementations actually doing the work (aka the “how”) are hidden in order to convey what that work actually is (aka the “what”).

At the opposite, imperative code is one that favors the “how” over the “what”.

Let’s see an example:

The snippet below perform two things: it computes the square of x, then check if the result is even or not.

// imperative way

    const x = 5;

    const xSquared = x * x;

    let isEven;

    if (xSquared % 2 === 0) {
      isEven = true;
    } else {
      isEven = false;
    }
view raw
block1.js hosted with ❤ by GitHub

Here, we can see that we finally get isEven after several steps that we must follow in order.

These steps describe “how” we arrive to know if the square of x is even, but that's not obvious.

If you take a non-programmer and show him this, he might have a hard time deciphering it.

Now let’s see another snippet where I introduce a magic isSquareEven function that performs the two same things than the previous one.

functional-programming javascript javascript-tips programming declarative-programming function

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Function Expression vs Function Declaration in JavaScript

In this article, we will acquaint ourselves with three simple terms: function declaration, function expression, and hoisting.

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

Functional Programming

Functional Programming: Functional Programming is a Declarative style of Programming Paradigm for writing computer programs.

The real reason why JavaScript has arrow functions

The real reason why JavaScript has arrow functions Nowadays, all my code is based on the use of arrow functions. If you are still not using them yourself, then don’t be ashamed of who you are. That’s your parent’s job. Instead, find about all the benefits that you can get by using arrow functions like the cool kids. You may notice that the code is shorter and that there is an arrow. Everything before the arrow is arguments of the function and everything after the arrow is always returned as the result of the function.

Functional Programming in JS

Functional programming(FP) is a programming paradigm where we think of code as a function. How fun is that everything is a function. Functional programming is not a new concept but it is gaining its popularity, Even Object-oriented programming languages are trying to have functional concepts. For eg: Lambda functions in Java.