Skip to content

Instantly share code, notes, and snippets.

@alexnault
Last active April 8, 2019 11:33
Show Gist options
  • Save alexnault/865013bfe06a8c24138093ac42a29e34 to your computer and use it in GitHub Desktop.
Save alexnault/865013bfe06a8c24138093ac42a29e34 to your computer and use it in GitHub Desktop.
Declarative pattern example in JavaScript
const names = ["Han", "Chewbacca", "Luke", "Leia"];
// imperative
const shortNames = [];
for (let i = 0; i < names.length; i++) {
if (names[i].length < 5) {
shortNames.push(names[i]);
}
}
// declarative
const shortNames = names.filter(name => name.length < 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment