Object Methods
The Object global hosts a small set of static helpers that show up in almost every codebase: iterating keys and values, copying properties, freezing data, and converting back and forth from key-value lists. They are the everyday tools for working with plain objects.
Object.keys, Object.values, Object.entries
These three methods turn an object into an array. They only see own, enumerable, string-keyed properties — inherited ones and symbols are skipped.
const user = { name: "Ada", age: 36, email: "ada@example.com" };
console.log(Object.keys(user)); // [ 'name', 'age', 'email' ]
console.log(Object.values(user)); // [ 'Ada', 36, 'ada@example.com' ]
console.log(Object.entries(user)); // [ [ 'name', 'Ada' ], [ 'age', 36 ], ... ]entries is what makes plain objects iterable with for...of:
for (const [key, value] of Object.entries(user)) {
console.log(`${key} = ${value}`);
}name = Ada age = 36 email = ada@example.com
Object.fromEntries — the inverse
Given an array of [key, value] pairs (or any iterable that produces them), Object.fromEntries builds an object. It is the natural way to convert from a Map, URLSearchParams, or the result of entries().map(...) back to a plain object.
// Filter an object's properties
const user = { name: "Ada", age: 36, secret: "***" };
const safe = Object.fromEntries(
Object.entries(user).filter(([k]) => k !== "secret")
);
console.log(safe); // { name: 'Ada', age: 36 }
// Transform values
const prices = { apple: 1, banana: 2, cherry: 5 };
const doubled = Object.fromEntries(
Object.entries(prices).map(([k, v]) => [k, v * 2])
);
console.log(doubled); // { apple: 2, banana: 4, cherry: 10 }
// From URLSearchParams
const params = new URLSearchParams("a=1&b=2");
console.log(Object.fromEntries(params)); // { a: '1', b: '2' }Object.assign — copy properties
Object.assign(target, ...sources) copies own enumerable properties from each source into the target, left to right, and returns the target.
// Merge — later sources win
const defaults = { theme: "light", fontSize: 14 };
const user = { fontSize: 16 };
const settings = Object.assign({}, defaults, user);
console.log(settings); // { theme: 'light', fontSize: 16 }
// Clone (shallow)
const original = { a: 1, b: 2 };
const copy = Object.assign({}, original);
copy.a = 99;
console.log(original.a, copy.a); // 1 99In modern code, { ...defaults, ...user } is the idiomatic equivalent. Object.assign is still useful when the target itself must be mutated (e.g. assigning into this or an existing object).
Object.freeze and Object.isFrozen
Object.freeze makes an object immutable: you cannot add, remove, or change properties. Attempts silently fail in sloppy mode and throw in strict mode.
"use strict";
const config = Object.freeze({ port: 3000, host: "localhost" });
try {
config.port = 9999;
} catch (err) {
console.log("blocked:", err.message);
}
console.log(Object.isFrozen(config)); // true
console.log(config.port); // 3000Object.seal — softer lockdown
Object.seal prevents adding or removing properties, but existing ones can still be reassigned. It is rarer in practice than freeze.
const user = Object.seal({ name: "Ada", age: 36 });
user.age = 37; // ok — reassignment allowed
user.email = "a@b"; // silently ignored — cannot add
delete user.name; // silently ignored — cannot remove
console.log(user); // { name: 'Ada', age: 37 }Object.create — make with a prototype
Object.create(proto) builds a new object whose prototype is the one you pass. Object.create(null) gives you a dictionary object with no inherited methods at all — handy when keys may collide with names like toString.
const dict = Object.create(null);
dict.hello = 1;
console.log("toString" in dict); // false — no inherited stuff
const parent = { greet() { return "hi from parent"; } };
const child = Object.create(parent);
console.log(child.greet()); // 'hi from parent'A quick reference
Object.keys(o)/Object.values(o)/Object.entries(o)— arrays of own enumerable string-keyed properties.Object.fromEntries(pairs)— build an object from[key, value]pairs.Object.assign(target, ...sources)— copy own enumerables intotarget(shallow).Object.freeze(o)— make immutable;Object.isFrozen(o)to check.Object.seal(o)— prevent add/remove but allow reassignment.Object.create(proto)— make a new object with a chosen prototype.Object.hasOwn(o, key)— modern replacement forhasOwnProperty.call.