Object Static Methods
EasyThe Object constructor provides a rich set of static methods for working with objects. From inspecting properties (keys, values, entries) to controlling mutability (freeze, seal) to creating objects with specific prototypes (Object.create).
Interactive Visualization
Object Patterns
Old Way
const user = { first: "Alice", last: "Smith" };
const full = user.first + " " + user.last;
// Must compute manually every timeModern Way
Click transform to see
Key Points
- Object.keys/values/entries return arrays of own enumerable string-keyed properties
- Object.assign performs shallow copy — nested objects are still shared by reference
- Object.freeze makes an object immutable (shallow) — nested objects are NOT frozen
- Object.seal prevents adding/deleting properties but allows modifying existing values
- Object.create sets the prototype directly — the basis of prototypal inheritance
- Object.hasOwn (ES2022) is the modern replacement for obj.hasOwnProperty(key)
Code Examples
Object.keys, values, entries
const user = { name: 'Alice', age: 30, role: 'admin' } console.log(Object.keys(user)) // ['name', 'age', 'role'] console.log(Object.values(user)) // ['Alice', 30, 'admin'] console.log(Object.entries(user)) // [['name','Alice'], ['age',30], ['role','admin']] // Transform object values const doubled = Object.fromEntries( Object.entries({ a: 1, b: 2 }).map(([k, v]) => [k, v * 2]) ) console.log(doubled) // { a: 2, b: 4 }
entries/fromEntries let you transform objects through the array pipeline pattern.
Object.assign and Shallow Copy
const defaults = { theme: 'light', lang: 'en' } const userPrefs = { theme: 'dark', fontSize: 14 } const config = Object.assign({}, defaults, userPrefs) console.log(config) // { theme: 'dark', lang: 'en', fontSize: 14 } // WARNING: shallow copy const original = { name: 'Alice', address: { city: 'NYC' } } const copy = Object.assign({}, original) copy.address.city = 'LA' console.log(original.address.city) // 'LA' — shared!
Object.assign copies own enumerable properties. It is shallow: nested objects are copied by reference.
Object.freeze and Object.seal
const frozen = Object.freeze({ x: 1, nested: { z: 3 } }) frozen.x = 99 // silently fails frozen.nested.z = 999 // works — freeze is shallow! const sealed = Object.seal({ a: 1, b: 2 }) sealed.a = 100 // ✅ allowed sealed.c = 3 // ❌ silently fails
freeze = no writes, no add, no delete. seal = writes OK, no add, no delete. Both are shallow.
Object.create and Object.hasOwn
const animal = { speak() { return `${this.name} makes a sound` } } const dog = Object.create(animal) dog.name = 'Rex' console.log(dog.speak()) // 'Rex makes a sound' // Object with NO prototype const dict = Object.create(null) dict.key = 'value' console.log(dict.toString) // undefined // Object.hasOwn (ES2022) console.log(Object.hasOwn(dog, 'name')) // true console.log(Object.hasOwn(dog, 'speak')) // false (inherited) console.log(Object.hasOwn(dict, 'key')) // true (works on null prototype!)
Object.create establishes the prototype chain. Object.hasOwn is the safe, modern own-property check.
Common Mistakes
- Assuming Object.freeze is deep — nested objects remain mutable
- Using Object.assign for deep cloning — use structuredClone()
- Calling obj.hasOwnProperty on Object.create(null) objects — use Object.hasOwn
- Object.keys only returns own enumerable string keys — inherited and symbol keys excluded
Interview Tips
- freeze vs seal: freeze = fully immutable, seal = can modify values only
- Object.create(null) for safe dictionaries without prototype pollution
- entries → map → fromEntries pipeline for object transformation
- Object.is differs from === for NaN (true) and -0 vs +0 (false)