liveBook · Manning

Side effect

As explained in Functional Programming in JavaScript

An externally observable change caused by executing code, such as I/O, mutation, or interaction with shared state.

A side effect is an externally observable change caused by executing code. Reading or modifying shared state, writing to the DOM, logging, and database access can make a function’s result depend on what happens outside its arguments. section 1.2

An unpredictable counter

The increment function changes the external variable counter, so another operation can alter its next result. Passing the counter in instead avoids that dependency: var increment = counter => counter + 1; section 1.2

Common side effects in the examples include:

  • Updating an HTML element with innerHTML. section 1.2
  • Reading a student from a database and throwing an error when it is missing. section 1.2
  • Mutating an array during an imperative loop instead of producing a mapped result. section 1.2

How Functional Programming in JavaScript covers it

How deep it goes
Explains side effects through DOM, database, array, and counter examples, stopping short of formal effect systems or advanced theory.
Examples
Uses counter mutation, DOM rendering, database lookup, error throwing, and imperative-versus-map array transformations.
What it assumes
Assumes basic JavaScript functions, variables, arrays, and browser output while introducing side effects through familiar examples.
What you can do after
Evaluate whether JavaScript functions depend on external state and refactor simple computations to accept inputs explicitly.
How it connects
Connects side effects with functional programming, mutation, referential transparency, pure functions, and higher-order function composition.

Also explained in

Compare how 24 books explain side effect →

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.