liveBook · Manning

Destructuring assignment

As explained in Secrets of the JavaScript Ninja, Third Edition

Destructuring syntax assigns values extracted from an iterable or other structured value to program bindings.

Destructuring assigns sequential values from an iterable to bindings. Though often called array destructuring, it uses the iterable’s iterator, so it works with collections beyond arrays. Rest syntax can collect remaining values, but spreading or collecting an infinite iterable will not finish. section 8.3.1

Split map entries into bindings

for (const [key, value] of directory) { console.log(key, value); } section 10.2.2

const [a, b, c] = ["a", "b", "c"];

See a minimal assignment that binds three iterable values to three variables. section 8.3.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains iterator-driven assignment and rest collection, then warns that collecting an infinite iterable will not finish.
Examples
Demonstrates assigning three array values and binding each key-value pair while iterating over a Map.
What it assumes
Builds on iterable and iterator definitions, which are explained alongside destructuring.
What you can do after
Apply destructuring to bind iterable values or separate map entries into keys and values.
How it connects
Connects destructuring to iterables, iterators, rest syntax, and Map entries.

Also explained in

Compare how 7 books explain destructuring assignment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.