01 / 02

Explain the concept of 'immutability' in JavaScript

Immutability refers to the property of a data value that prevents it from being modified after its creation. In JavaScript, primitive data types (e.g., strings, numbers) are immutable, while objects and arrays are mutable. Immutable data structures are often used to prevent unintended side effects in functional programming.

In JavaScript, all primitive types (string, number, boolean, null, undefined, symbol) are inherently immutable. You can reassign a variable to a new value, but you can never change the value itself.

javascript

Objects and arrays in JavaScript are mutable. Even if you declare them with const, you can still change their internal properties or elements. const only prevents you from reassigning the variable itself.

javascript
Difficulty: 4/10
Topics: object freezing, pure functions, state management

Scenario Questions

0-2 years experience
  1. 1

    If you need to add an item to an array without mutating the original array, how would you do it in JavaScript?

  2. 2

    What will happen if you try to reassign a property of an object declared with const? Explain why.

  3. 3

    Given the code let obj = {a:1}; obj = {b:2}; does this violate immutability? Why or why not?

2-5 years experience
  1. 1

    You notice a bug where a Redux reducer is accidentally mutating the state object, causing UI glitches. How would you identify and fix the mutation?

  2. 2

    When using Object.freeze on a nested object, some inner properties still change. Why does this happen and how can you enforce deep immutability?

  3. 3

    A teammate suggests using Array.prototype.push inside a map function for performance. Explain the impact on immutability and what you would recommend.

5-8 years experience
  1. 1

    Our front‑end team shares a large immutable data store across multiple micro‑frontends. What trade‑offs do you consider when choosing a library (e.g., Immer, Immutable.js) versus plain JavaScript for deep immutability at scale?

  2. 2

    During a performance audit you see that copying large objects for every state update is causing GC pressure. How would you redesign the data handling to keep immutability while reducing overhead?

  3. 3

    Explain how you would enforce immutability contracts across a TypeScript codebase without runtime overhead.

8+ years experience
  1. 1

    We are migrating a legacy monolith that heavily mutates shared objects to a new architecture that relies on immutable data streams. What strategy would you propose for incremental migration while keeping the system stable?

  2. 2

    Across several teams, you need to define a company‑wide policy for immutable state. How would you balance developer ergonomics, runtime performance, and long‑term maintainability?

  3. 3

    If you had to design a cross‑service event bus that guarantees immutability of payloads across language boundaries, what patterns and tooling would you choose and why?

Follow-up Questions

  • Can you walk me through a quick code example of your solution?
  • What edge cases would you watch out for in that approach?
  • How would you test that immutability is actually being preserved?