Functional JavaScript
Last updated
Was this helpful?
Last updated
Was this helpful?
Functional programming approaches computer programs as consisting of data transformations, much like functions in math. Your program is a collection of functions. Each function takes some inputs and produces some outputs. Simple functions are used as building blocks for more complex ones. This is one of the earliest approaches to programming, dating back to the 1960s and based on math developed in the 1930s.
JavaScript allows for different styles of programming: object oriented, imperative, and functional.
React brings these functional programming concepts to the UI. It provides abstractions such as components which are (mostly) pure functions. Given some state (as props) they return some DOM. This allows us to get away from imperatively touching the DOM.
The functional style is more flexible, and makes testing and debugging easier. Two core concepts of functional programming are immutability and stateless, both of which are covered in later sections.
A pure function is a function where the return value is only determined by its input values, without observable side effects.
For example, slice
and splice
complete the same functionality, however, splice
has the undesired impact of mutating the origin input.
Array helper methods map
, filter
, and reduce
are examples of functional programming, which take in functions and do not mutate the original array.
Currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument.
This section covers the basics, check out this gitbook for an in-depth on Functional Programming