liveBook · Manning

orElse

As explained in Functional Programming in JavaScript

An operation that provides an alternative computation or action when a value is absent or a failure case occurs.

orElse supplies a fallback action when a computation fails, letting the chain recover instead of stopping at the first error. In this treatment, it appears as the branch that logs a missing student after a lookup fails, complementing default-value handling with getOrElse. section 5.3

It fits the Either pattern, where successful values stay on the right and failures carry error information on the left. The fallback can be another function, so the failure path remains explicit and reusable.

Used here to handle failure cases by handoff, not by throwing away the error:

  • returns a default value with getOrElse when only a substitute result is needed section 5.3

Logging a failed student lookup

findStudent('444-44-4444').orElse(errorLogger) sends the failure branch to a logger instead of trying to extract a value immediately.

How Functional Programming in JavaScript covers it

How deep it goes
Explains the fallback role and shows one logging example, but does not define orElse's implementation details.
Examples
Uses a missing-student lookup and a logger output as the concrete fallback scenario.
What it assumes
Assumes familiarity with success-versus-failure containers and default-value handling.
What you can do after
Operate fallback handling for failed Either computations, including logging or other side effects.
How it connects
Connects to Either, getOrElse, Maybe, and logging through error handling and fallback control flow.

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.