JavaScript — Prerequisites for Functional Programming: Declarative Code

Keerthana
2 min readApr 15, 2022

An article for Mid level JavaScript Developers.
This is part of a series “JavaScript — Prerequisites for Functional Programming”

Series “JavaScript — Prerequisites for Functional Programming”

Functional Programming is the process of building software by following a certain rules. One of which is discussed in this article.

Use Declarative (what) rather than imperative code (how)

Imperative Programming involves typing lines of code describing how a result is achieved.

A statement (Imperative code) is a piece of code which performs some action. Examples of commonly used statements include for, if, switch, throw, etc…

const doubleMap = numbers => {
const doubled = [];
for (let i = 0; i < numbers.length; i++) {
doubled.push(numbers[i] * 2);
}
return doubled;
};
console.log(doubleMap([2, 3, 4])); // [4, 6, 8]

Declarative Programmings involves describing desired results without explicitly listing steps that must be performed.

An expression (Declarative code) is a piece of code which evaluates to some value. Expressions are usually some combination of function calls, values, and operators which are evaluated to produce the resulting value.

const doubleMap = numbers => numbers.map(n => n * 2);
console.log(doubleMap([2, 3, 4])); // [4, 6, 8]

Other Topics in this Series

JavaScript - Prerequisites for Functional Programming

5 stories

Pure functions
Function Composition
Higher-Order Functions
Currying
Declarative code
Data Immutability

--

--