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
- Spread merges objects; later keys win.
Object.keys/values/entriesiterate them.?.and??guard against null and undefined.
Keywords and builtins used here
Objectconst
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.
Step 1 of 8 in Objects & collections, step 27 of 43 in Language basics.