Functional programming, like other programming paradigms, follows different principles or has special characteristics. These are essentially as follows.
Functions are first-class objects (also referred to as first-class citizens). Functions can be assigned to variables like other objects and primitive values, and they can be used as arguments of other functions or as their return value. In nonfunctional programming languages, however, functions are not represented as objects and consequently can’t be treated as such.
Data structures in functional programming are usually unchangeable or are not changed. Rather, operations performed on data structures create new data structures if necessary and return them as results. In purely functional programming languages, for example, once lists or other data structures have been created, they cannot be changed later (or only in a roundabout way).
Taking the previous point a bit further, there is also the fact that in functional programming the functions usually have no side effects at all and behave more like mathematical functions. This means that functions in functional programming always return the same result for the same input but do not trigger any side effects. In purely functional languages, side effects are already prevented by the language itself.
Imperative programming is a programming paradigm in which you give the computer very precise individual instructions on how to solve a problem. Imperative programs use explicit loop statements (while loops, for loops, etc.), conditional statements (if-else), and sequences of these.
Functional programs, on the other hand, are declarative, meaning that you, as the developer, formulate your program more to say what should be done. As a result, functional programs are usually more readable, more meaningful, and more compact than the equivalent imperative code.
The figure below shows how to classify the functional programming paradigm.
Unlike object-oriented programming, functional programming focuses on functions, not objects. For example, JavaScript combines the two programming paradigms. You can structure your program in an object-oriented manner—that is, work with objects—and then program functionally in turn within object methods.
Note: Functional programs are usually leaner than the equivalent variant in object-oriented or imperative programming.
Examples of (almost) purely functional programming languages include Haskell, Lisp, and Miranda. JavaScript is not a purely functional programming language. For example, data structures such as arrays can be modified even after they have been defined, whereas in Haskell, for example, this is not allowed.
Editor’s note: This post has been adapted from a section of the book JavaScript: The Comprehensive Guide by Philip Ackermann.