Try   HackMD

Declarative vs Imperative Programming

Imperative programming is a way of describing how things work. It explicitly specifies each statement, mutating a program’s state.

Declarative programming expresses the logic without explicitly specifying its control flow. It is a way of describing what you want to do, without listing all the steps to make it work.

  • simple, easier to read, elegant code
  • requires less effort to be understood
  • no need to use variables
  • avoids creating and mutating the state

Let's check if a number's a palindrome the imperative way:

function isPalindrome(word) { const len = word.length; for (let i = 0; i < len / 2; i++) { if (word[i] !== word[len - 1 - i]) { return false; } } return true; }

A declarative solution would be:

function isPalindrome(word) { return word == word.split('').reverse().join('') }