Warm-up1 / 2
Can you write this from memory?
Create an empty Map called `cache`.
JavaScript's spread makes a shallow copy, not a deep one. Object.keys returns strings, even for numeric keys. Map preserves insertion order, objects mostly do. Set uses strict equality.
Small details, real bugs. Practice here tests the collection APIs so you stop making assumptions about how they work.
Related JavaScript Topics
When to Use JavaScript Collections
- Use Map when keys are not strings or when order matters.
- Use Set for de-duplication and fast membership checks.
- Use object/array for simple structured data and JSON.
Check Your Understanding: JavaScript Collections
Prompt
Remove duplicates from an array of ids.
What a strong answer looks like
Use new Set(ids) and spread back into an array: [...new Set(ids)].
What You'll Practice: JavaScript Collections
Array spread and destructuringObject spread and destructuringMap and Set basicsArray methods (find, findIndex, includes)Object.keys/values/entriesOptional chaining (?.) and nullish coalescing (??)
Common JavaScript Collections Pitfalls
- Shallow vs deep copy with spread
- Object reference equality
- Array includes() uses strict equality
JavaScript Collections FAQ
Map vs object?
Map preserves insertion order and allows non-string keys; objects are simpler for JSON-like data.
Does spread make a deep copy?
No, spread only makes a shallow copy of the first level.
JavaScript Collections Syntax Quick Reference
Spread
const merged = { ...obj1, ...obj2 };Destructuring
const { name, age = 0 } = user;Optional chain
const city = user?.address?.city ?? "Unknown";JavaScript Collections Sample Exercises
Example 1Difficulty: 1/5
Create an empty object called `item`.
const item = {};Example 2Difficulty: 1/5
Create an object `item` with name "hello" and value 1.
const item = { name: "hello", value: 1 };Example 3Difficulty: 1/5
Access the `name` property of `item`.
item.name+ 56 more exercises
Further Reading
- GDScript Dictionary map() and map_in_place12 min read