Sub topic 1
Thinking Functionally: Declarative vs. Imperative Code
Functional programming (FP) is a way of building programs from small functions that take input and return output, without changing anything outside themselves. JavaScript is not a purely functional language, but it supports this style well. This chapter explains the mindset behind it before later chapters cover the individual tools.
Prerequisites
This chapter assumes you are comfortable with:
- Core JavaScript syntax: variables, functions, objects, and arrays
- Arrow functions and spread/rest syntax
- How scope works in JavaScript
- Running code in Node.js or a browser console
Imperative vs. Declarative
Imperative code tells the computer how to do something, step by step. You manage loop counters, temporary variables, and the order in which state changes.
Declarative code describes what result you want. The step-by-step mechanics are hidden behind well-named functions.
Here is the same task written both ways. The task is to get the uppercase names of users who are 18 or older.
const users = [
{ name: 'ada', age: 36 },
{ name: 'tim', age: 12 },
{ name: 'grace', age: 45 }
];
// Imperative: manage the loop and the result array yourself
const adultsImperative = [];
for (let i = 0; i < users.length; i++) {
if (users[i].age >= 18) {
adultsImperative.push(users[i].name.toUpperCase());
}
}
// Declarative: describe the result
const adultsDeclarative = users
.filter((u) => u.age >= 18)
.map((u) => u.name.toUpperCase());
console.log(adultsImperative); // [ 'ADA', 'GRACE' ]
console.log(adultsDeclarative); // [ 'ADA', 'GRACE' ]Both versions produce the same output. The declarative version reads like a sentence: keep the adults, then take their names in uppercase. It has no index variable to get wrong and no array being changed partway through.
The Core Ideas
Functional style in JavaScript rests on a few ideas, and each one gets its own chapter later:
| Idea | What it means |
|---|---|
| Functions as values | Functions can be stored, passed, and returned like any other value |
| Pure functions | Output depends only on input, with no hidden effects |
| Immutability | Create new data instead of changing existing data |
| Composition | Build larger behavior by chaining small functions |
Common Misconceptions
- "Functional means no loops." Loops are fine inside a function. The goal is to keep their state local and give the result a clear name.
- "Declarative is always faster." It often runs slightly slower, because each
filterormapcall creates a new array. You usually gain clarity, not speed. - "It's all or nothing." Most real JavaScript mixes styles. You can apply functional ideas wherever they make the code easier to follow.
Exercises
Exercise
Rewrite this loop declaratively: const out = []; for (const n of [1, 2, 3, 4]) { if (n % 2 === 0) out.push(n * 10); }
Show answer
const out = [1, 2, 3, 4].filter((n) => n % 2 === 0).map((n) => n * 10); The result is [20, 40].
Exercise
Name two things the imperative version of the users example manages explicitly that the declarative version does not.
Show answer
The loop index variable i and the mutable result array that gets push calls. The declarative version hides iteration and returns a new array.