Objects in depth
Shorthand, computed keys, methods, Object.keys/entries, copying, freezing and structuredClone.
You met objects in Arrays & objects. Objects are central to JavaScript — arrays, functions, dates and DOM elements are all objects — so it pays to know them well. This lesson covers modern object syntax, the Object helper functions, copying, comparing and freezing.
Modern object literal syntax#
Property shorthand
Method shorthand
Computed property names
Put an expression in [ ] to compute a key:
This is common when updating form state: { ...values, [event.target.name]: event.target.value }.
Getters and setters#
A getter looks like a property but runs a function; a setter runs when you assign:
Checking and listing properties#
Object.hasOwn(obj, key) is the modern replacement for obj.hasOwnProperty(key).
Transforming objects with entries
Object.entries turns an object into an array (so you can use array methods), and Object.fromEntries turns it back:
Property order#
Object keys keep insertion order, with one exception: integer-like keys come first, in ascending order.
All keys are strings (or symbols) — obj[1] and obj["1"] are the same property. Need non-string keys or guaranteed order? Use a Map.
Copying objects#
Shallow copies
A shallow copy duplicates only the top level. Nested objects are still shared references.
Deep copies with structuredClone
structuredClone (built into browsers and Node 17+) handles nested objects, arrays, Dates, Maps and Sets. It can't clone functions or DOM nodes. The old trick JSON.parse(JSON.stringify(obj)) loses Dates, undefined and more — avoid it.
Updating immutably
Rather than mutating, create an updated copy — a pattern you'll use constantly in React:
Comparing objects#
=== compares identity (is it the same object?), not contents:
For deep comparison in tests, use your test library's toEqual; in Node there's util.isDeepStrictEqual.
Freezing and sealing#
Object.seal is a weaker version: existing properties can change, but none can be added or removed. Both are shallow.
Optional chaining and nullish values#
Real data often has missing pieces. ?. stops and returns undefined instead of throwing:
More in Modern JavaScript features.
Objects as dictionaries#
Objects are often used as lookup tables:
Beware: keys like "constructor" or "__proto__" can clash with built-in properties. For user-controlled keys, use Object.create(null) or a Map.
JSON: objects as text#
JSON is the text format for sending objects over the network or saving them:
JSON keys must be double-quoted strings; functions and undefined values are dropped. You'll use JSON heavily in fetch, APIs & JSON.
Common mistakes#
- Assuming
{ ...obj }is a deep copy. - Comparing objects with
===and expecting a content check. - Using
obj.hasOwnPropertyon objects created withObject.create(null)— useObject.hasOwn. - Thinking
Object.freezeis recursive.
What's next#
Next: destructuring and spread — concise syntax for unpacking, copying and merging arrays and objects.
Check your understanding
Quick quiz
1.Given
const name = "Ada"; const user = { name };, what isuser?2.After
const b = { ...a };, which statement is true?3.What does
Object.freeze(settings)do?
Finished reading?
Mark this lesson complete to track your progress.