Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 15 / 31DATA AND OBJECTS

Taking data apart.

Destructuring names parts; spread gathers or copies parts.

01 / THE BASICS

Start here.

Destructuring is a compact way to assign names from an existing data shape. Given const article = { title: 'North', pages: 80 };, the line const { title } = article; reads article.title into a local binding named title. For an array, const [first, second] = items; takes elements by position. The braces or brackets on the left describe where the values should come from; they do not create another article or array.

You can provide a default in the pattern: const { title = 'Untitled' } = article. The fallback applies only when article.title is undefined, including when the property is missing. It does not replace null, false, or an empty string. The rest syntax ...rest in a pattern gathers remaining properties into a new outer object. It can be handy when separating one field from a larger record.

Spread uses the same three dots in an expression but moves in the other direction. [...items] creates a new array filled from an iterable; { ...article } creates a new object with the source object's own enumerable properties. Neither recursively copies nested objects. Read the position of the three dots: in a binding pattern they gather remaining data, and in a new array or object they expand data into it.

02 / THE IDEA

Build the model.

Destructuring is a pattern for assigning parts of an array or object to bindings. Defaults apply when a selected value is undefined. Spread expands an iterable into elements or an object's own enumerable properties into another object.

These forms shorten code when the shape is already clear. Deeply nested destructuring can hide missing data and make a reader decode punctuation. Prefer a small, explicit step when the data may be absent or the transformation deserves a name.

03 / UNDER THE SURFACE

How it works.

Object destructuring reads properties and assigns them to local bindings. In { title = 'Untitled', ...rest }, title receives article.title unless it is undefined; rest receives the remaining own enumerable properties in a new object. Array destructuring works by position, such as const [first, second] = items. A default does not replace null, false, or an empty string.

Spread moves values in the other direction: it expands an iterable into an array or call, or copies object properties into a new object. The syntax is concise, but it is not a deep clone. A well-named intermediate binding is often clearer than an expression with several nested destructurings and spreads. Read the data shape aloud before choosing the shortcut.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const article = { title: 'North', pages: 80 };
const items = [1, 2];
const { title = 'Untitled', ...rest } = article;
const copy = [...items];

First notice. rest is a new outer object; copy is a new array with the same element references.

The first line reads title from article. If the property is missing or undefined, title becomes 'Untitled'; other properties enter rest. The second line consumes the items iterable and creates a new array. If an element of items is an object, the new array still holds a reference to that object.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const book = { title: 'North', pages: 80 };
const { title, ...details } = book;
const numbers = [1, 2];
console.log(title, details.pages, [...numbers, 3]);

The output is 'North', 80, and [1, 2, 3]. Destructuring named title while rest gathered the other property. Array spread inserted the two existing numbers before 3 in a new array; numbers itself remains [1, 2].

06 / KEEP THIS

One thing to remember.

Syntax that saves lines should still reveal the data shape.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.