Pure Functions and Immutability
Write predictable functions without side effects.
Pure Functions and Immutability is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Pure Function?
A pure function returns the same output for the same input and has no side effects. It does not read or write external state, mutate its arguments, or perform I/O.
A Pure Example
This function depends only on its arguments and produces only a return value. Calling it never changes anything outside.
function add(a: number, b: number): number {
return a + b;
}
console.log(add(2, 3)); // 5, alwaysAn Impure Example
This one reads and mutates a variable outside its scope, so its result depends on hidden state and it leaves a trace behind. That makes it impure.
let counter = 0;
function next(): number {
counter += 1; // side effect
return counter;
}
console.log(next(), next()); // 1 2, depends on historyReferential Transparency
A pure call is referentially transparent: you can replace the call with its result without changing the program. add(2, 3) can be swapped for 5 anywhere.
const x = add(2, 3) * 2;
const y = 5 * 2; // identical meaning
console.log(x === y); // trueWhy Purity Helps
Pure functions are easy to test (no setup), easy to cache, safe to run in parallel, and simple to reason about because they have no hidden dependencies.
Avoid Mutating Arguments
Mutating an input array or object is a side effect the caller may not expect. Instead, build and return a new value.
// Impure: mutates input
function pushImpure(xs: number[], v: number) { xs.push(v); return xs; }
// Pure: returns a new array
function pushPure(xs: number[], v: number): number[] { return [...xs, v]; }Immutable Updates with Spread
The spread operator copies an object or array, letting you override fields without touching the original. This is the workhorse of immutable updates in TS.
interface User { name: string; age: number }
function birthday(u: User): User {
return { ...u, age: u.age + 1 };
}
const before = { name: "Ada", age: 30 };
const after = birthday(before);
console.log(before.age, after.age); // 30 31Updating Arrays Immutably
Use map, filter, and spread to derive new arrays instead of mutating. The original array is left intact for other code to rely on.
const nums = [1, 2, 3];
const doubled = nums.map(n => n * 2);
console.log(nums, doubled); // [1,2,3] [2,4,6]readonly for Safety
Marking parameters readonly tells the compiler (and readers) that the function will not mutate them, reinforcing purity at the type level.
function sum(xs: readonly number[]): number {
return xs.reduce((a, b) => a + b, 0);
}
console.log(sum([1, 2, 3])); // 6Pushing Effects to the Edges
Real programs need effects (logging, network). The functional style keeps the core pure and isolates effects at the boundaries, so most code stays predictable.
A Mental Checklist
For each function ask: does it depend only on its inputs? Does it return only a value? Does it leave inputs and the outside world unchanged? Three yeses means it is pure.
Quick Check
Quick check on this lesson.
Recap
Pure functions depend only on inputs and produce no side effects, giving referential transparency. Use spread, map/filter, and readonly for immutable updates, and push real-world effects to the program edges.
Frequently asked questions
Is the “Pure Functions and Immutability” lesson free?
Yes — the full text of “Pure Functions and Immutability” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Pure Functions and Immutability”?
Write predictable functions without side effects. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Pure Functions and Immutability” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Pure Functions and Immutability
- Currying and Partial Application
- Function Composition with Types
- Typed pipe and flow Utilities