Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Pure Functions

Pure Functions

In this lesson — part of Functional Foundations — you'll learn pure functions in TypeScript and why it matters in real work.

Why it matters

Functional style — pure functions and immutability — makes code easier to test and reason about.

Key ideas

  • Pure functions
  • Immutability
  • No shared mutable state
  • Predictable outputs

In practice

Here's how it looks in idiomatic TypeScript:

// Pure: output depends only on inputs, no side effects
function add(a: number, b: number): number {
  return a + b;
}
console.log(add(2, 3)); // always 5

TypeScript note: Mark inputs readonly (e.g. readonly number[]) to let the compiler enforce that a pure function never mutates its arguments.

Try it yourself

Exercise: In TypeScript, rewrite a function that mutates a global so it's pure.

Recap

You now understand pure functions and can apply it in TypeScript. Mark this lesson complete and continue to the next one.