Track

Reference Semantics

Memory, Pointers & Allocation

WebContainer
0 XP
0d streak
Concept
Core Principles
Code Example
4Knowledge Check
5Hands-On Drill
60%
Lesson 1 · 6 min

Reference Semantics

Sharing mutable data accidentally is a common source of production bugs, especially once work becomes concurrent.

In this lesson

STEP 1

A shallow copy duplicates only the outer object; nested objects are still shared.

STEP 2

A deep copy duplicates the nested data you plan to mutate.

STEP 3

Choose copy versus share deliberately: copying protects isolation, while sharing can be useful when it is immutable.

Interactive Example

Nested objects need independent nested copies

Interactive Editor
typescript
Loading...
Output Console
Modify code and click "Run Code" (or ⌘+Enter) to evaluate output in real time.
What happens here?
  1. The outer object is copied.
  2. The nested settings object is also copied.
  3. Changing the copy cannot change the original.

Quick check

Why is `{ ...original }` not enough for nested data?

Next: Phase 2 Hands-On Drill

Ready to write code?

Solve the coding challenge in the in-browser IDE with live visible and hidden specification tests.