The introduction

Object-oriented programming has long been the dominant paradigm in JavaScript. JavaScript is a multi-paradigm programming language. However, functional programming is becoming more and more popular among developers in recent years. Functional programming is an approach that emphasizes reducing changes to the external state of a program. Therefore, it encourages the use of immutable data structures and pure functional programming, and it encourages declarative programming rather than imperative programming, and it encourages simple and understandable naming of functions to describe the properties of the object and get the desired result rather than flow and instruction. Functional programming does not rely on or modify external state, making code easier to understand, maintain, test, and be error-resistant.

The five characteristics

  • No side effect
  • Immutable data
  • Pure functions
  • Function composition
  • The function is currified

No side effect

A function side effect is a change in the state outside the function when a function is called, in addition to returning the value of the function. Function side effects can cause unnecessary problems in the program design, introduce potential errors, and reduce the readability of the program.

An example of a side effect




An example with no side effects




Immutable data

Immutable data is a value that, once created, will never change. In JavaScript primitives, numbers, strings, and booleans are never changed, but the data structures of objects and arrays are mutable under certain operations.

A mutable example




An array variable





The object variable

An immutable example




Array immutable





Object immutable

Pure functions

A pure function is a function that does not change the external state of the program and does not cause data variability. The output of a pure function depends entirely on its input value, always returning the same output for the same input.

An example of a pure function




Function composition

Function combination refers to the nesting of multiple functions, the function as the input parameter, the function as the return value out, combined into a new function, forming a pipeline data flow.

An example of a combination of functions




The function is currified

Function currification refers to the conversion of a multi-parameter function that accepts multiple parameters into a single-parameter function that accepts only one parameter at a time, and the return of a new function that can accept the remaining parameters. The biggest feature is delayed execution, which increases the applicability of the function.

Multi-parameter function




One parameter function (Currization function)




Typical applications of functional programming

The design and usage of Redux is entirely based on the concept of functional programming. It is recommended to read the source code to further improve the understanding of functional programming