liveBook · Manning

Monad

As explained in Functional Programming in JavaScript

An abstraction that represents computations with context and provides a way to sequence them while controlling how values and effects propagate.

A monad is a container that carries a value together with rules for what happens next. It makes function pipelines safer: instead of scattering null checks, exceptions, and effectful operations through every caller, each step passes along either a usable value or a controlled outcome. The goal is practical fault-tolerant composition, not category theory. section 5.0 section 5.5

Functors can map a function over a wrapped value, but composing functions that each return wrappers can produce nested containers. A monad adds operations such as join or chain to flatten that structure and continue with the contained value. chain is map followed by flattening: use it when the next function already returns a monad. section 5.3 section 5.4

Common monadic containers represent different kinds of context:

  • Maybe represents a value that may be absent. Just holds a value; Nothing propagates absence, so later map calls are skipped. getOrElse supplies a fallback at the boundary. section 5.3
  • Either represents success or failure in one return type. Right carries a usable result, while Left carries an error or failure value and bypasses ordinary transformations. section 5.3
  • IO represents a delayed effect. Mapping builds a description of reading or writing, and run performs the stored effect explicitly. section 5.3

Safe student lookup pipeline

A student-display pipeline can start by wrapping an SSN, clean the input, validate its length, find the student, select fields, convert them to CSV, and append the result to the page. Use chain for validation and lookup because they return monads; use map for ordinary transformations. If validation or lookup fails, dependent steps are skipped, and an error handler or fallback can handle the outcome. section 5.4 section 5.4

Figure 5.11. Same two functions as in figure 5.10; but this time the null value travels in a monad (Either or Maybe), which causes the rest of the functions in the pipeline to gracefully fail.

Compare an unsafe composition with one where a missing value travels inside Maybe or Either, allowing the remaining pipeline to fail gracefully. section 5.4

Use monads when values can be missing, operations can fail, or effects need an explicit boundary. By keeping those conditions in containers with a shared interface, pipelines stay fluent while error-handling behavior remains local to the container type. section 5.4 section 5.5

How Functional Programming in JavaScript covers it

How deep it goes
Explains Wrapper, Maybe, Either, and IO implementations, then demonstrates map, join, chain, composition, and execution without category theory.
Examples
Uses student lookup and address access, even-number halving, URI decoding, DOM updates, and a student-information display pipeline.
What it assumes
Builds from functors and wrapped values, deliberately avoiding category theory while using JavaScript functional composition.
What you can do after
Apply it via JavaScript container types to compose validation, lookup, error handling, and DOM effects while safely propagating failure.
How it connects
Connects functors, map, join, chain, function composition, currying, Maybe, Either, IO, null handling, and side effects.

Also explained in

Compare how 6 books explain monad →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.