Skip to solution
mediumFrontend

What is a pure function?

131 views
01

Understand the problem

Question presented to candidate: "What makes a function pure, and why does that property matter in practice?"

What a strong answer should cover:

  • A pure function must satisfy two conditions: the same input always produces the same output (determinism), and it causes no observable side effects (no mutating arguments, no mutating outer/global state, no I/O, no relying on non-deterministic input like Math.random or the current time).
  • Purity is a property of the function's OWN logic, not its inputs -- a function that mutates an object it was passed is impure even if the caller never notices, because the mutation is an observable side effect.
  • Pure functions are trivially testable (no setup/teardown, no mocking), safely memoizable (same input always yields the cached output), and safe to run in parallel or reorder, since they cannot interfere with anything outside themselves.
  • Most real programs cannot be 100% pure everywhere -- I/O, rendering, and state updates are inherently impure -- so the practical goal is usually to push impurity to the edges and keep as much core logic pure as possible.
  • A function returning a NEW object/array instead of mutating the one it received is the most common way to keep transformation logic pure in JavaScript.

Code / implementation expected: Yes -- a runnable snippet contrasting a pure and an impure version of the same operation, with real observed output showing the impure version produce different results for identical input.

pure-functions
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interview questions. Difficulty: Easy-Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the printed outpu

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSPure vs impure: determinism (run directly)
JSPure vs impure: mutation of an argument (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 115 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track