Basics

What is a pure function — and why React deliberately runs your code twice

Illustration: a press turns two identical blanks into two identical coins

You're building an app in React, you put a console.log inside a component — and the message shows up twice. One piece of code, seemingly one call. A bug?

No. In development mode React deliberately calls your component two times. That's StrictMode at work — it's on in the standard Vite and Next.js templates. React is checking whether your code is pure. If something drifts on the second run, the function is "impure", and one day that will bite you in production. Let's look at what purity is and why React watches it so closely.

What a pure function is, in plain words

A pure function follows two rules:

  1. Same input, same output. Give it the same parameters and you get the same result. Today, tomorrow, the hundredth time.
  2. No side effects. It doesn't change anything outside itself. It just computes and returns a result.

Picture a coin press. Put in the same blank, get the same coin. Every time. And the press doesn't touch anything around it: it doesn't move the table or rearrange other coins. That's a pure function.

function addTax(price) {
  return price * 1.12
}
addTax(100)   // 112. Always 112.

What makes a function impure

Anything that ties the function to the outside world. Here are the common cases.

The result depends on more than the input.

function greet(name) {
  const hour = new Date().getHours()
  return hour < 12 ? `Good morning, ${name}` : `Hi, ${name}`
}

The input is the same — "Anna". But the answer depends on the time of day. Same goes for Math.random() and reading outside variables that someone might change.

The function changes something outside. That's a side effect:

let total = 0
function addToCart(price) {
  total = total + price   // changes a variable outside
  return total
}
addToCart(100)   // 100
addToCart(100)   // 200 — same input, different answer

Side effects also include a server request, a database write, changing an array you were passed, printing to the console.

The function damages what it was given.

function sortPrices(prices) {
  return prices.sort()   // sorts the original array in place
}

You wanted a sorted copy, but you rearranged the original. Whoever passed the array has no idea. The pure version is prices.toSorted(): it returns a new array and leaves the old one alone.

Why you should care

Sounds like theory, but three very practical things depend on purity.

It's easy to test. A pure function can be tested in one line: "100 in — 112 out." No databases, servers or clocks. That's why, when you ask AI to write tests, they come out simple precisely for pure functions.

It's easy to fix. If the result is wrong, the culprit is the function and its input. No hunting for who changed a global variable three screens back.

React is built on it. A React component is a function that draws a piece of the screen from data. React counts on it drawing the same thing for the same data. It may call it an extra time, skip a call, restart it. If the component changes something outside while doing that, chaos follows: counters jump, requests go out twice. That's what the double run in development mode is for — it drags these bugs to the surface early.

But nothing works without side effects, right?

Right. An app that saves nothing and sends nothing is useless. The goal isn't to get rid of effects but to keep them separate.

A good setup: pure functions do the math, and effects live in a few clear places. In React those places are click handlers and useEffect. That's where you call the server and write to storage. Rendering itself stays pure.

You can ask AI for exactly this:

Weak promptMake a function for the cart
Strong promptWrite a calculateCartTotal function for a shopping cart. Requirements: 1) the function is pure: input is an array of items with price and qty fields plus a discount percent, output is the final total; 2) it does not modify the array it is given and does not read outside variables; 3) move saving to localStorage into a separate saveCart function; 4) add three example calls with the expected result.

Does console.log make a function impure?

Technically yes — printing to the console is an effect too. But for debugging it's harmless; nobody will scold you for a temporary console.log.

Is a pure function the same as a function with no parameters?

No. A pure function can have parameters, and usually does. What matters is that the result depends only on them and it changes nothing around it.

How do I tell whether a function is pure?

Ask two questions. Call it twice with the same input — does the answer match? After the call, did anything outside change? If it's "yes" and "no", the function is pure.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →