What is the concept of immutability in JavaScript?

Answer

Immutability means data cannot be changed after creation — instead of modifying existing data, you create new data with the desired changes. JavaScript primitives are inherently immutable (strings, numbers). Objects and arrays are mutable by default but can be treated immutably. Benefits: predictable state management (no side effects), pure functions (same input → same output), enables efficient change detection (shallow equality check is sufficient if data is immutable — used by React, Redux), easier debugging (no hidden mutations), and safe sharing between components. Techniques: Object.freeze(obj) — shallow freeze (prevents property modification; nested objects still mutable). Spread/destructuring for updates: const newState = {...state, count: state.count + 1}. Immer.js — write mutating code inside a produce() callback and get an immutable result automatically (uses Proxy). Immutable.js — provides persistent immutable data structures with structural sharing (efficient memory). React's useState and Redux rely heavily on immutability for change detection.