OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Destructuring, spread and defaults

Read named fields concisely while recognising a shallow copy and a missing value.

Lesson 13 of 25 · About 20 minutes · Beginner · English

By the end of this lesson

Unpack a record and preserve meaningful zero values.

Before you start

  • Node.js 24 with a terminal and plain-text editor; the browser-only lesson is labelled separately.
  • Complete lesson 12 or be comfortable with its examples.

Three words to know

Destructuring
Binding names from selected array positions or object fields.
Spread
Expanding elements or properties into a new container.
Nullish fallback
A default used only when a value is null or undefined.

1. Extract the fields you need

Destructuring makes field names visible. A default in a destructuring pattern is used for undefined, not for null. It does not establish that an external record is valid; validation belongs before use.

Read a small recordJAVASCRIPT · Node.js · save as lesson.mjs
const record = { title: "Read", status: "open" };
const { title, status } = record;
console.log(`${title}: ${status}`);
What you should see · example output
Read: open

2. Spread copies the outer structure

A spread object is a new outer object. Its nested objects are still shared. Here changing copy.options also changes original.options. Deep-copying is a separate design decision, and copying does not sanitise untrusted data.

A shallow copyJAVASCRIPT · Node.js · save as lesson.mjs
const original = { options: { enabled: true } };
const copy = { ...original };
copy.options.enabled = false;
console.log(original.options.enabled);
What you should see · example output
false

3. Preserve valid zeroes

|| treats 0 and an empty string as reasons to use the fallback. ?? uses the fallback only for null or undefined. Choose the operator that matches the data contract instead of using one as a universal default.

Compare two defaultsJAVASCRIPT · Node.js · save as lesson.mjs
const count = 0;
console.log(count || 10);
console.log(count ?? 10);
What you should see · example output
10
0

YOUR TURN

A small check before you continue

For {name: null}, will const {name = "learner"} use the default? What will name ?? "learner" produce?

Show a suggested answer

The destructuring default does not replace null, so name remains null. The nullish expression produces learner.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.