DEV Community

khushal jangid
khushal jangid

Posted on

Mutating state | Immutating state

Your confusion is coming from mixing JavaScript objects + mutation + React-style immutability concepts. Let’s clear it step by step.

🧠 1. First fix your code (syntax)

You wrote:

let state ={
count:0;
}

Correct:

let state = {
count: 0
};
πŸ”₯ 2. What is happening in your code (MUTATION)
Code:
let state = {
count: 0
};

state.count = 1;

console.log(state);
πŸ‘‰ Meaning:

You directly changed the object.

This is called mutation.

Memory idea:

state β†’ { count: 0 }

After mutation:

state β†’ { count: 1 }

So object is SAME, only value inside changed.

🧠 3. Your function (mutation again)
function increment() {
state.count = state.count + 1;
}
What happens:
Before:
state = { count: 1 }
After calling increment():
state = { count: 2 }

Again:
πŸ‘‰ SAME object, just modified inside

This is called:

πŸ”΄ Mutation (changing original state)

❓ Why console shows undefined sometimes?

If you do:

console.log(increment());

Function returns nothing β†’ so output is:

undefined

Because you did not return anything.

πŸš€ 4. IMMUTABLE version (VERY IMPORTANT CONCEPT)

You wrote:

{ count: state.count + 1 }

This is NOT mutation.

This creates a NEW object.

🧠 Mutation vs Immutability
πŸ”΄ Mutation (old object modified)
state.count = state.count + 1;
same object
memory same
risky in React/Redux
🟒 Immutability (new object created)
state = { count: state.count + 1 };

Now:

Before:

state β†’ { count: 1 }

After:

state β†’ { count: 2 } (NEW object)

πŸ‘‰ Old object is NOT changed

🎯 5. Why immutability is important (React mindset)

React depends on:

β€œDid object reference change?”

So:

Mutation ❌
state.count++;

React may NOT detect change properly

Immutability βœ…
setState({ count: state.count + 1 });

React sees:

new object
re-render happens
🧠 Simple analogy
Mutation:

You repaint same house 🏠

Immutability:

You build new house 🏑

πŸ”₯ 6. Final clean understanding
Concept Meaning
Mutation change same object
Immutability create new object
state.count++ mutation
{count: state.count+1} new object
⚑ One-line memory trick

Mutation = β€œsame object, change inside”
Immutability = β€œnew object, old unchanged”

If you want, I can explain this with
React useState example + interview trick + real DOM re-render story (very important for interviews).

explain this for writing blog

Here’s a clean blog-style explanation you can directly use or adapt.

🧠 Mutation vs Immutability in JavaScript (Simple Explanation)

When working with JavaScript objects, especially in frameworks like React, you will often hear two important terms:

Mutation and Immutability

These concepts decide how data changes in your application and how efficiently your UI updates.

Let’s understand them in a very simple way.

πŸ“¦ 1. What is an Object in JavaScript?

In JavaScript, objects are stored in memory like this:

let state = {
count: 0
};

Here, state is not the actual data. It is a reference pointing to the object in memory.

πŸ”΄ 2. Mutation (Changing the Same Object)

Mutation means modifying the original object directly.

Example:
let state = {
count: 0
};

state.count = 1;

console.log(state);
What is happening?
We are changing the value inside the same object
No new object is created
Memory reference stays the same
Visualization:

Before:

state β†’ { count: 0 }

After mutation:

state β†’ { count: 1 }

πŸ‘‰ Same object, just updated inside

⚠️ Problem with mutation

Mutation can create issues in frameworks like React because:

The reference does NOT change
React may not detect updates properly
UI updates can become unpredictable
🟒 3. Immutability (Creating a New Object)

Immutability means not changing the original object, but creating a new one with updated values.

Example:
let state = {
count: 0
};

state = {
count: state.count + 1
};

console.log(state);
What is happening?
A new object is created
Old object remains unchanged
Reference changes
Visualization:

Before:

state β†’ { count: 0 }

After immutability:

old state β†’ { count: 0 }
new state β†’ { count: 1 }

πŸ‘‰ Completely new object is created

βš–οΈ 4. Mutation vs Immutability (Key Difference)
Feature Mutation Immutability
Object change Same object New object
Memory Same reference New reference
Safety Risky in large apps Safe and predictable
React behavior May not re-render Always triggers update
πŸš€ 5. Why React Prefers Immutability

React depends on detecting changes using reference comparison:

If object reference changes β†’ React re-renders UI

So this works well:

setState({ count: state.count + 1 });

But this can fail:

state.count++;

Because React may not detect the change properly.

🧠 6. Simple Real-Life Analogy
Mutation:

You edit the same notebook page ✏️
Everything changes in place.

Immutability:

You write a new page πŸ“„
Old page stays unchanged.

🎯 7. Final Summary
Mutation = change the same object
Immutability = create a new object
React prefers immutability for predictable updates
Good developers avoid mutation in state handling
πŸ”₯ One-line takeaway

β€œMutation changes data in place, immutability creates a new version of data without touching the original.”

Top comments (0)