typestar

Working with objects in JavaScript

Keys, values, entries, spread, and the two kinds of freeze.

const tour = { slug: "basics", steps: 30, sets: { count: 6 } };

console.log(Object.keys(tour), Object.values(tour).slice(0, 2));
console.log(Object.entries(tour).map(([k]) => k));

const renamed = Object.fromEntries(
  Object.entries(tour).map(([key, value]) => [key.toUpperCase(), value]),
);
console.log(Object.keys(renamed));

const copy = { ...tour, steps: 31 };
copy.sets.count = 99; // shared: spread is shallow
console.log(tour.sets.count, copy.steps);

const frozen = Object.freeze({ a: 1 });
console.log(Object.isFrozen(frozen), "b" in frozen, tour?.sets?.count);

How it works

  1. Object.entries pairs with fromEntries to transform a whole object.
  2. Spread copies one level: nested objects are still shared.
  3. Object.freeze is shallow, and silent unless in strict mode.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
571
Tokens
193
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 122 seconds.

Type this snippet

Step 2 of 8 in Objects & collections, step 28 of 43 in Language basics.

← Previous Next →