Imperative vs Declarative Languages

Imperative means giving an order or command. In imperative languages, we give orders to the computer and it performs the orders for us. Imperative programming focuses on describing how a program operates step by step.

Procedural programming is a type of imperative programming in which the program is built from one or more procedures (subroutines of functions). Object-oriented programming is another type of imperative programming but with a better structure to improve the maintainability of the programs.

In declarative programming, we define what the program must accomplish rather than tell it how to accomplish it as a sequence of commands. Two common declarative languages are database query languages like SQL and regular expressions. In a SQL command, we tell the program what we want and the query interpreter's role is to design and perform a process to produce the result. Another example is HTML markup language in which we define the content we want to see on the browser and it's up to the browser how to render them.

Functional programming is a type of declarative programming.

Most languages are not functional, object-oriented or procedural by default. The way we use them defines how we want them to act for us. However, the language features will encourage us to use that specific language more in an object-oriented or functional manner. JavaScript has many characteristics of a functional language out of the box.

  • Immutability: using const for defining variables.
  • Higher-order Functions: passing functions as a parameter to another function and returning a function as a result of a function.
  • Referential Transparency: each function only relies on its parameters for performing its job. These kinds of functions are predictable and they always return the same value for the same input no matter what the state of the system is.
  • Stateless: unlike the object-oriented paradigm which heavily relies on the state of the objects, in JavaScript by default we don't define classes hence no state.