JavaScriptObject Methods

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.

JS
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:

JS
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.

JS
// 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.

JS
// 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 99
Shallow only
`Object.assign` and the `{ ...obj }` spread both copy one level deep. Nested objects are shared by reference — mutating them through the copy mutates the original.

In 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.

JS
"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);               // 3000
Freeze is shallow
Nested objects are *not* frozen. `Object.freeze({ a: { b: 1 } }).a.b = 9` succeeds. For deep immutability, freeze recursively or reach for a library.
Object.seal — softer lockdown

Object.seal prevents adding or removing properties, but existing ones can still be reassigned. It is rarer in practice than freeze.

JS
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.

JS
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 into target (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 for hasOwnProperty.call.

Pick the right tool
If you find yourself doing manual loops over keys and values, `Object.entries` + `map`/`filter` + `Object.fromEntries` almost always reads better. Save `Object.assign` for cases where you must mutate an existing target.
One thing to remember
`keys/values/entries` give you an array; `fromEntries` gives you back an object. With those four, plain objects become as easy to manipulate as arrays.