Declarative programming describes how a result should be, rather than prescribing every step to produce it. In React, this style lets you describe a view while React updates it automatically. The aim is code that is easier to read and understand. section 1.1.1
Compare two ways to count passwords that are at least nine characters long. The imperative version creates an array, loops through passwords, skips short ones, and adds the rest before counting them. section 1.1.1
function countGoodPasswords(passwords) {
const goodPasswords = []; #1
for (let i = 0; i < passwords.length; i++) {
const password = passwords[i]; #1
if (password.length < 9) { #2
continue; #2
}
goodPasswords.push(password); #1#3
}
return goodPasswords.length;
}
The step-by-step version makes each change to the working array explicit. section 1.1.1
function countGoodPasswords(passwords) {
return passwords.filter(p => p.length >= 9).length;
}
The declarative version expresses the condition with `filter` and counts the resulting array. section 1.1.1
Declarative is not a guarantee of simplicity: taken to an extreme, it can become difficult to read. Prefer the form that makes the intended result clear. section 1.1.1













