| Method | Does | Example |
|---|---|---|
Object.keys(obj) | Array of an object's own enumerable keys | Object.keys({a:1,b:2}) → ['a','b'] |
Object.values(obj) | Array of its values | Object.values({a:1,b:2}) → [1,2] |
Object.entries(obj) | Array of [key, value] pairs — pairs well with for...of and .map() | Object.entries({a:1}) → [['a',1]] |
Object.fromEntries(entries) | The inverse of .entries() — builds an object from [key, value] pairs | Object.fromEntries([['a',1],['b',2]]) → {a:1,b:2} |
Object.assign(target, ...sources) | Shallow-merges sources into target, mutating target | Object.assign({}, {a:1}, {b:2}) → {a:1,b:2} |
Object.freeze(obj) | Prevents adding/removing/reassigning top-level properties (shallow only) | Object.freeze({a:1}).a = 2 — silently fails (throws in strict mode) |
Object.isFrozen(obj) | Checks whether Object.freeze() was applied | Object.isFrozen(Object.freeze({})) → true |
Object.fromEntries and Object.entries are a common pairing for transforming every value in an object without a manual loop:
const prices = { apple: 1, banana: 2, cherry: 3 };
const doubled = Object.fromEntries(
Object.entries(prices).map(([key, value]) => [key, value * 2])
);
// { apple: 2, banana: 4, cherry: 6 }
Object.freeze() is shallow — freezing an object doesn't freeze objects nested inside it:
const obj = Object.freeze({ nested: { count: 1 } });
obj.nested.count = 99; // succeeds! freeze() only protects the top-level object, not `nested`