liveBook · Manning

Referential transparency

As explained in The Joy of JavaScript

A property of an expression whose value can be replaced by that value without changing program behavior, assuming no observable side effects.

Referential transparency means an expression can be replaced by its value without changing program behavior. The guarantee depends on predictable inputs, immutable state, and the absence of observable side effects such as I/O, mutation, filesystem access, or randomness. section 4.1.2

This property makes functional code composable: independently defined expressions can be swapped like Lego bricks because equivalent expressions produce the same outcome and do not depend on global or shared state. section 4.3.1 section 4.1.2

Composing a word-counting pipeline

A word-counting operation can be assembled from read, decode, split, and count functions: compose(count, split, decode, read). The composition clarifies the transformation pipeline, although reading the file remains an effectful boundary rather than a referentially transparent operation. section 4.3.1

Listing 4.6 countWordsInFile implemented with compose
const countWordsInFile = compose(
   count,
   split,
   decode,
   read
);

countWordsInFile('sample.txt'); // 9

Inspect the composed JavaScript implementation and its sample result. section 4.3.1

How The Joy of JavaScript covers it

How deep it goes
Defines substitution behavior, links it to purity and side effects, and demonstrates composition without formal semantic treatment.
Examples
Uses a file word-counting pipeline built from read, decode, split, count, compose2, compose, and reduce.
What it assumes
Assumes JavaScript functions and parameters, while introducing purity, immutable state, closures, side effects, and composition together.
What you can do after
Apply it to replace pure expressions, isolate effects, and compose small JavaScript functions into predictable pipelines.
How it connects
Connects referential transparency to pure functions, immutability, side effects, closures, and function composition.

Also explained in

Compare how 9 books explain referential transparency →

Related concepts

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