Reading — step 1 of 7
Learn
If arrays are ordered lists, objects are key→value maps: you look values up by name, not by position. They're how you group related data into a single value. In modern JS, objects are everywhere: configuration, JSON-decoded data, return values, instance state.
Creating objects
Keys are strings (or Symbols). Values can be ANY type — primitives, arrays, other objects, even functions.
Accessing properties
Two syntaxes:
Use dot when you know the key at write-time. Use brackets when the key is in a variable.
Adding, updating, removing
Objects are mutable even when declared with const — const locks the binding, not the contents.
Property shorthand
When variable name matches the key, you can omit the value:
Destructuring — pull out properties
Spread — copy and merge
The spread is the modern way to make shallow copies. Mutation-free updates: instead of obj.x = 5, write { ...obj, x: 5 }.
Iteration
Membership
Methods — functions stored as properties
Method shorthand { name() {} } is preferred over { name: function() {} }.
Common mistakes
- Using
===to compare objects — checks reference, not contents.{a:1} === {a:1}isfalse. - Forgetting that
objis passed by reference — modifying inside a function affects the caller. - Using
for...inand forgetting it includes inherited properties — useObject.keys()for own properties. - Spreading deep objects expecting deep copy —
...is shallow. UsestructuredClone(obj)for deep copies.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…