typestar

Objects in JavaScript

Spreading, inspecting, and safely reading objects.

const defaults = { theme: "dark", sound: true };
const overrides = { sound: false };
const settings = { ...defaults, ...overrides };

const keys = Object.keys(settings);
const values = Object.values(settings);
const pairs = Object.entries(settings);

const nested = { user: { profile: null } };
const city = nested.user?.profile?.city ?? "unknown";
const has = "theme" in settings;

How it works

  1. Spread merges objects; later keys win.
  2. Object.keys/values/entries iterate them.
  3. ?. and ?? guard against null and undefined.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
381
Tokens
98
Three-star pace
95 tpm

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

Type this snippet

Step 1 of 8 in Objects & collections, step 27 of 43 in Language basics.

← Previous Next →