惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
J
Java Code Geeks
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
腾讯CDC
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
V
V2EX
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
Jina AI
Jina AI
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
B
Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Mutating state | Immutating state
khushal jangid · 2026-06-23 · via DEV Community

khushal jangid

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.”