liveBook · Manning

Pure function

As explained in Acing the Frontend Interview

A pure function is a function that returns the same output for the same input without producing side effects or depending on external mutable state.

A pure function always returns the same output for the same input, avoids side effects, and neither modifies nor depends on external state. This determinism makes it predictable, testable, and easier to debug. section 3.2

Impure versus pure JavaScript

An impure function changes external state: `let counter = 0; function increment() { counter++; return counter; }`. By contrast, `function add(x, y) { return x + y; }` depends only on its arguments. Array `map` applies the same idea: `const doubled = [1, 2, 3].map(n => n * 2);` produces `[2, 4, 6]` without modifying the source array.

Use these points to explain the concept in an interview:

  • Identify whether a function depends only on its inputs or reads and changes outside state. section 3.2
  • Connect immutability and side-effect-free transformations with maintainability, predictable behavior, testing, and debugging. section 3.2
  • Explain that functional programming also includes map, filter, reduce, higher-order functions, currying, and partial application. section 3.2
Table 3.2 Core concepts of functional paradigms
Topic
Fundamentals
Applied skills
Advanced
Functional programming
Identify pure functions; use map, filter, reduce; explain immutability.
Refactor code for immutability; apply currying and partial application; use higher-order functions (HOF).
Architect functional modules; optimize performance in functional pipelines; blend FP with OOP patterns.
Iterators & generators
Use for...of; explain iterators and iterable protocols.
Create custom iterators; use yield to build generator functions.
Build asynchronous flows with generators; integrate iterables in advanced design patterns.
Symbols & meta-programming
Explain Symbol basics and well-known symbols ( Symbol.iterator, etc.).
Use Proxy for validation, intercept API calls; utilize Reflect API for meta-operations.
Architect meta-programming utilities for libraries/frameworks; design extensible APIs with Symbol and meta-programming.

Use the functional-paradigms table to compare pure functions with immutability, higher-order functions, and progressively more advanced functional techniques. section 3.2

How Acing the Frontend Interview covers it

How deep it goes
Defines purity, contrasts code examples, explains practical benefits, and connects it to map without exploring runtime internals.
Examples
Uses counter mutation, addition, and array doubling with map to distinguish impure functions from pure transformations.
What you can do after
Apply it via JavaScript functions and map transformations, then explain their effects on predictability, testing, and maintenance.
How it connects
Connects pure functions with functional programming, immutability, higher-order functions, map, filter, reduce, testing, and debugging.

Also explained in

Compare how 23 books explain pure function →

Related concepts

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