liveBook · Manning

Either

As explained in Functional Programming in JavaScript

A monadic type that represents a value that can be one of two possibilities, commonly used to encode success or error outcomes.

Either represents a value that can be one of two cases, making success and failure travel together as one result. It is used to reify errors, avoid exception throwing, and keep function chains composable. section 5.3

The treatment contrasts it with Maybe: Maybe handles missing values, while Either carries an actual error object or message when a lookup or decode fails. section 5.3

Lookup and decode examples

A database search returns Either.of(obj) on success and a left value with an error message on failure. A URL decoder wraps decodeURIComponent in try/catch, then maps only if decoding succeeds.

This approach keeps downstream code clean: later steps can map over a successful value, and failures remain available for logging or fallback handling.

How Functional Programming in JavaScript covers it

How deep it goes
Defines Left and Right behavior, then shows wrapping, mapping, fallback, and exception capture.
Examples
Uses database lookup, student retrieval, URL decoding, and error logging examples.
What it assumes
Assumes basic function composition and nullable checks, then introduces monadic error handling from scratch.
What you can do after
Apply Either to return one value for success or failure, then recover, log, or keep composing safely.
How it connects
Connects to Maybe, monads, map, chain, and IO.

Also explained in

Compare how 4 books explain Either →

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.