Skip to content

Functional Programming in JavaScript

Billy Bunn edited this page Mar 20, 2019 · 1 revision

Largely based on a reading of the article Functional Programming in JavaScript — With Practical Examples (Part 1) by Raja Rao DV

Summary of Functional Programming in JavaScript

Functional Programming (or "FP") refers to a methodology where you modularize your program as much as possible by making a series of single-purpose functions. The article referenced above puts it best:

Functional Programming is a style of writing programs by simply composing a set of functions.

Essentially, FP asks us to wrap virtually everything in functions, write lots of small reusable functions and simply call them one after the other to get the result like: (func1.func2.func3) or in a compose fashion, like: func1(func2(func3())).

FP also emphasizes not changing the values that you pass into functions. You'll see this said like "a function that takes in an array and reverses it without mutating the original input array (so we can reference it later)".

JavaScript is not a purely functional programming language (like Haskell), but JS has enough built-in to mimic the behavior of a purely FP language. If you were to build these FP behaviors in JavaScript, you could put them in a library that you can reference in your programs. (Hint, hint: there are some nifty JS libraries out there that do exactly this).

This article won't dive into the details, but here are a few of the big Functional Programming concepts/tools you can google for more info:

  • Currying
  • Pure Functions
  • “Fantasy-land” specs
  • “Functors”
  • “Monads”
  • “Maybe Monads”
  • “Either Monads”

Resources

Read

  • inheritance
  • ydkjs - objects
  • eloquent js - functional programming
  • functional programming in js - fcc

Skim

  • mdn new
  • mdn inheritance and prototype chain
  • mdn this

Watch

  • javascript context tutorial
  • Bookmark
  • node error docs
  • mdn object prototype
  • mdn class
Clone this wiki locally