Pure Functions and Unchanged Inputs

Keep the original report available for comparison

A teacher corrects Maya’s mark from 35 to 37. If every screen shares one record and it is changed in place, the earlier report can silently change too.

Immutability means a value is not changed after creation. A pure function returns a result determined by its inputs and causes no externally visible side effects. Creating an updated value can make changes easier to compare and test.

  1. Preserve an input

    Return a new student record for a correction.

  2. Identify side effects

    Distinguish returning data from changing shared state.

  3. Recognize copy limits

    Explain const and nested object sharing.

Create a corrected record beside the original

  1. Original input

    Keep the existing report value.

    Maya: 35
  2. Return a new value

    Copy fields and replace the mark.

    correction: 37
  3. Compare both records

    Old and new versions coexist.

    35 → 37
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

Separate a calculation from an action

A pure correction function takes the record and new mark explicitly, then returns a new record. It does not change the input, print a message, write a file or depend on the current time.

Real applications still need actions such as saving a report. Keeping calculations separate from those effects makes it easier to test the rule before performing the action. Purity is a property of behavior, not a JavaScript keyword.

Before starting, review objects and shared references. These examples use JavaScript; other languages can use different syntax and rules.

Return a corrected student record

The object spread copies the top-level fields into a new object. The later mark field replaces the copied mark in that new object only.

The outputs show the original mark, corrected mark and a false identity comparison. These are different records with related contents.

JavaScript · local teaching example

function correctMark(student, mark) {
  return { ...student, mark };
}
const original = { name: "Maya", mark: 35 };
const corrected = correctMark(original, 37);
console.log(original.mark);
console.log(corrected.mark);
console.log(original === corrected);

Expected output

35
37
false

A top-level copy still shares nested records

The two outer objects are distinct, but address still points to the same nested object. Changing the copied address changes the original student’s address as well.

To preserve this nested value, copy the address at the level being changed. A shallow spread is not a deep-copy guarantee.

JavaScript · local teaching example

const original = { name: "Maya", address: { city: "Pune" } };
const copied = { ...original };
copied.address.city = "Delhi";
console.log(original.address.city);

Expected output

Delhi

Apply the rule to a small change

Add Ravi to a names list while keeping the earlier list unchanged.

Worked answer

Array spread creates a new list with the original entries, followed by Ravi. The old list still contains only Maya. The strings themselves do not need nested copying.

JavaScript · local teaching example

function addStudent(names, name) {
  return [...names, name];
}
const original = ["Maya"];
const updated = addStudent(original, "Ravi");
console.log(original.join(","));
console.log(updated.join(","));

Expected output

Maya
Maya,Ravi

Recognize mistakes and limits

const prevents reassigning the variable binding; it does not freeze the object or array it refers to. original.mark = 37 can still mutate a const-bound record. Object.freeze is also shallow unless nested values are handled.

A function that reads a changing global bonus or the current time is not determined by its explicit inputs alone. Pass the needed value in if a repeatable calculation is the goal.

Copying large structures costs time and memory. Choose a deliberate state strategy rather than copying everything blindly; small immutable updates can be useful without making every operation pure.

For the language or tool rules used here, see JavaScript shallow-copy behavior.

Check the core idea

What does const guarantee for a student object?
Choose one answer. You can try again.

Answer and explanation

The binding cannot be reassigned to a different value.

Correct. The referenced object may still be mutable.

Apply the idea to a new situation

JavaScript · code for this question

const original = { name: "Maya", address: { city: "Pune" } };
const copied = { ...original };
copied.address.city = "Delhi";
console.log(original.address.city);
Why did the shallow-copy example change the original city?
Choose one answer. You can try again.

Answer and explanation

Both outer objects refer to the same nested address.

Correct. The top-level copy did not copy that nested object.

Make changes explicit and keep calculation inputs clear

A pure function computes from its inputs without observable side effects. An immutable update returns a replacement value, but copying and const have limits you must understand.

Connect this idea with repeatable calculations in tests and controlling changes behind an interface.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Closures and Higher-Order Functions →