Skip to content
Objects
step 1/7

Reading — step 1 of 7

Learn

~3 min readCollections

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

js

Keys are strings (or Symbols). Values can be ANY type — primitives, arrays, other objects, even functions.

Accessing properties

Two syntaxes:

js

Use dot when you know the key at write-time. Use brackets when the key is in a variable.

Adding, updating, removing

js

Objects are mutable even when declared with constconst locks the binding, not the contents.

Property shorthand

When variable name matches the key, you can omit the value:

js

Destructuring — pull out properties

js

Spread — copy and merge

js

The spread is the modern way to make shallow copies. Mutation-free updates: instead of obj.x = 5, write { ...obj, x: 5 }.

Iteration

js

Membership

js

Methods — functions stored as properties

js

Method shorthand { name() {} } is preferred over { name: function() {} }.

Common mistakes

  • Using === to compare objects — checks reference, not contents. {a:1} === {a:1} is false.
  • Forgetting that obj is passed by reference — modifying inside a function affects the caller.
  • Using for...in and forgetting it includes inherited properties — use Object.keys() for own properties.
  • Spreading deep objects expecting deep copy... is shallow. Use structuredClone(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…