Object Methods
The static Object methods — keys , values , entries , assign , and freeze — let you inspect, transform, merge, and protect plain objects without manual loops.
Learn Object Methods in our free JavaScript course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
They turn objects into something you can iterate and reshape with the same array tools you already know.
📚 Prerequisites: You should be comfortable with objects and array methods like map and reduce . Many object tasks become array tasks once you call Object.entries .
🗝️ Real-World Analogy: Think of an object as a labeled filing cabinet :
Plain objects aren't directly iterable, but these three convert them into arrays you can loop and transform. Object.entries pairs with destructuring in a for...of loop, and Object.fromEntries does the reverse — turning [key, value] pairs back into an object. Together they let you map / filter an object as if it were an array.
Transform pattern: entries → map → fromEntries is the standard way to build a new object from an old one — like doubling every value or renaming keys.
Object.assign(target, ...sources) copies properties into a target (and returns it). The spread form {' '} does the same but always creates a fresh object — usually the safer choice. Later sources win on key conflicts. Object.freeze makes an object read-only; Object.isFrozen reports whether it's locked.
freeze is shallow: a nested object inside a frozen object can still be changed. Freeze each level if you need deep immutability.
Spread and assign only copy one level deep — nested objects stay shared. When you truly need an independent copy, structuredClone recursively clones the whole structure. Object.groupBy (ES2024) buckets an array into an object keyed by whatever your callback returns, which is the cleanest way to categorize data.
structuredClone limits: it can't clone functions, DOM nodes, or class instances with methods — it's for plain data (objects, arrays, dates, maps, sets).
Use the right method to get an array of all the values in the object.
Predict the output before revealing the answer.
{' '} — the later spread wins on a key conflict.
1 — the object is frozen, so the assignment is ignored.
Swap keys and values of a small dictionary using entries, map, and fromEntries.
Up next: Symbols & Well-Known Symbols — truly unique keys and hidden behaviors. 🔣
Practice quiz
What does Object.keys({ a: 1, b: 2 }) return?
Object.keys returns an array of the property names: ['a', 'b'].
What does Object.values({ a: 1, b: 2 }) return?
Object.values returns an array of the values: [1, 2].
What does Object.entries({ a: 1, b: 2 }) return?
- a
- b
Object.entries returns an array of [key, value] pairs: [['a', 1], ['b', 2]].
Which method turns [[key, value]] pairs back into an object?
- Object.keys
- Object.fromEntries
- Object.assign
- Object.values
Answer: Object.fromEntries. Object.fromEntries is the reverse of entries, building an object from [key, value] pairs.
What is the standard pattern to build a new object from an old one?
- entries → map → fromEntries
- keys → filter → join
- values → reduce
- freeze → clone
Answer: entries → map → fromEntries. entries → map → fromEntries is the standard way to transform an object's keys or values.
In { ...{ a: 1 }, ...{ a: 2 } }, what is the value of a?
- 1
On a key conflict, the later spread wins, so a is 2.
Is Object.freeze shallow or deep?
- Deep — it freezes nested objects too
- Shallow — nested objects can still be changed
- It only freezes arrays
- It throws on nested objects
Answer: Shallow — nested objects can still be changed. freeze is shallow; nested objects inside a frozen object remain mutable.
After const f = Object.freeze({ n: 1 }); f.n = 5; what is f.n?
- 5
- undefined
- It throws
- 1
Answer: 1. The object is frozen, so the assignment is ignored (silently here) and f.n stays 1.
Why use structuredClone instead of spread for a nested object?
- It is shorter
- spread is a shallow copy that shares nested objects; structuredClone copies deeply
- structuredClone is faster for everything
- spread can't copy arrays
Answer: spread is a shallow copy that shares nested objects; structuredClone copies deeply. Spread shares nested references; structuredClone makes a true deep, independent copy.
What does Object.groupBy([1, 2, 3, 4, 5], n => n % 2 ? 'odd' : 'even') return for the 'odd' key?
Object.groupBy buckets items by the callback's key, so 'odd' holds [1, 3, 5].