Functions, Scope & Object Model
Arrays, Collections & Immutability
Arrays are ordered indexed objects; Maps and Sets provide clearer collection semantics for key-value lookup and uniqueness. Immutable update patterns reduce accidental shared-state bugs.
- Arrays preserve order and support indexed access
- Objects are records, Maps are collections
- Sets model uniqueness
- Immutable updates preserve old references
- Shallow copies are shallow
| Need | Use | Reason |
|---|---|---|
| Ordered sequence | Array | Indexing and order |
| Known object shape | Object | Named properties |
| Dynamic key-value collection | Map | Arbitrary keys and clear size/iteration |
| Unique values | Set | Membership and dedupe |
| Object metadata without preventing GC | WeakMap | Weak object keys |
const nextUsers = users.map((user) =>
user.id === changed.id ? { ...user, ...changed } : user
)
const usersById = new Map(nextUsers.map((user) => [user.id, user]))