Extracting values without the boilerplate

Grabbing some or all of the values out of an array or object literal and stuffing them into individual variables is a routine chore, and historically it produced fairly wordy code:

const theArray = [ false, true, false ];
const firstElement = theArray[0];
const secondElement = theArray[1];
const thirdElement = theArray[2];

Since ES6 in 2015, destructuring has offered a more compact route: individual values are lifted out of the structure and bound to a set of identifiers without touching each key or index separately. Neither flavour of the syntax mutates the source — despite the alternative name unpacking, the original array or object is left intact.

Binding pattern destructuring

The simplest form declares every identifier with a single let or const (or var), using brackets on the left of the assignment:

const theArray = [ false, true, false ];
const [ firstElement, secondElement, thirdElement ] = theArray;

console.log( firstElement );
// Result: false

console.log( secondElement );
// Result: true

console.log( thirdElement );
// Result: false

For arrays, each comma-separated identifier takes the value of the element at the matching position. Skipping an element is a matter of leaving an identifier out between commas, much as you would when writing a sparse array:

const theArray = [ true, false, true ];
const [ firstElement, , thirdElement ] = theArray;

console.log( firstElement );
// Result: true

console.log( thirdElement );
// Result: true

Objects use curly braces instead. In the basic version, the identifier names must match the property keys:

const theObject = {
  "theProperty" : true,
  "theOtherProperty" : false
};
const { theProperty, theOtherProperty } = theObject;

console.log( theProperty );
// result: true

console.log( theOtherProperty );
// result: false

The reason is structural: arrays are indexed collections whose iteration order matters, so positional matching is natural. Objects are keyed collections — a pile of properties meant to be defined and accessed in any order — so there is no sequence to rely on, and key-name matching is the straightforward default. The trade-off is that this form assumes a given shape for the object.

The alternative syntax is object-literal-shaped but works nothing like object literal notation. Inside the braces come zero or more comma-separated pairs: the property key, a colon, then the identifier that should receive that property's value. The closing brace is followed by = and the object to unpack:

const theObject = {
  "theProperty" : true,
  "theOtherProperty" : false
};
const { theProperty : theIdentifier, theOtherProperty : theOtherIdentifier } = theObject;

console.log( theIdentifier );
// result: true

console.log( theOtherIdentifier );
// result: false

It looks unusual in print but becomes familiar with a few uses.

Assignment pattern destructuring

Assignment patterns target something more specific than a fresh declaration: a variable declared with let (or, technically, var), a property of another object, or an element of an array. With arrays and let variables, you simply declare the variables first and destructure afterwards:

const theArray = [ true, false ];
let theFirstIdentifier;
let theSecondIdentifier

[ theFirstIdentifier, theSecondIdentifier ] = theArray;

console.log( theFirstIdentifier );
// true

console.log( theSecondIdentifier );
// false

The result matches binding pattern destructuring, which can use const from the start:

const theArray = [ true, false ];

let [ theFirstIdentifier, theSecondIdentifier ] = theArray;

console.log( theFirstIdentifier );
// true

console.log( theSecondIdentifier );
// false
const theArray = [ true, false ];

const [ theFirstIdentifier, theSecondIdentifier ] = theArray;

console.log( theFirstIdentifier );
// true

console.log( theSecondIdentifier );
// false

The difference matters when the destructured values are headed into an existing array or an object's properties. Binding patterns would declare the identifiers a second time — let, const and var can only create variables, so the line would be read as let theResultArray and fail because theResultArray already exists:

// Error
const theArray = [ true, false ];
let theResultArray = [];

let [ theResultArray[1], theResultArray[0] ] = theArray;
// Uncaught SyntaxError: redeclaration of let theResultArray

Assignment patterns sidestep that entirely:

const theArray = [ true, false ];
let theResultArray = [];

[ theResultArray[1], theResultArray[0] ] = theArray;

console.log( theResultArray );
// result: Array [ false, true ]

Objects work too, with a catch. Note the disambiguating parentheses around the destructuring line:

const theObject = {
  "theProperty" : true,
  "theOtherProperty" : false
};
let theProperty;
let theOtherProperty;

({ theProperty, theOtherProperty } = theObject );

console.log( theProperty );
// true

console.log( theOtherProperty );
// false

Without the grouping operator, a pair of curly braces where a statement is expected is parsed as a block statement, which produces a syntax error:

// Error
const theObject = {
  "theProperty" : true,
  "theOtherProperty" : false
};
let theProperty;
let theOtherProperty;

{ theProperty, theOtherProperty } = theObject;
// Uncaught SyntaxError: expected expression, got '='

As with binding patterns, the identifiers can match the property keys, or the alternate object syntax lets any identifier be used:

const theObject = {
  "theProperty" : true,
  "theOtherProperty" : false
};
let theFirstIdentifier;
let theSecondIdentifier;

({ theProperty: theFirstIdentifier, theOtherProperty: theSecondIdentifier } = theObject );

console.log( theFirstIdentifier );
// true

console.log( theSecondIdentifier );
// false

What assignment patterns add is that any kind of assignment target is acceptable:

const theObject = {
  "theProperty" : true,
  "theOtherProperty" : false
};
let resultObject = {};

({ theProperty : resultObject.resultProp, theOtherProperty : resultObject.otherResultProp } = theObject );

console.log( resultObject );
// result: Object { resultProp: true, otherResultProp: false }

Both syntaxes support default values, applied when an element or property is missing or explicitly undefined:

const theArray = [ true, undefined ];
const [ firstElement, secondElement = "A string.", thirdElement = 100 ] = theArray;

console.log( firstElement );
// Result: true

console.log( secondElement );
// Result: A string.

console.log( thirdElement );
// Result: 100
const theObject = {
  "theProperty" : true,
  "theOtherProperty" : undefined
};
const { theProperty, theOtherProperty = "A string.", aThirdProperty = 100 } = theObject;

console.log( theProperty );
// Result: true

console.log( theOtherProperty );
// Result: A string.

console.log( aThirdProperty );
// Result: 100

Nested structures

The syntax earns its keep on nested data. You can always unpack an object containing an object as a property and then unpack the inner object separately:

const theObject = {
  "theProperty" : true,
  "theNestedObject" : {
    "anotherProperty" : true,
    "stillOneMoreProp" : "A string."
  }
};

const { theProperty, theNestedObject } = theObject;
const { anotherProperty, stillOneMoreProp = "Default string." } = theNestedObject;

console.log( stillOneMoreProp );
// Result: A string.

More concisely, the nested object can come apart inside the same binding:

const theObject = {
  "theProperty" : true,
  "theNestedObject" : {
    "anotherProperty" : true,
    "stillOneMoreProp" : "A string."
  }
};
const { theProperty, theNestedObject : { anotherProperty, stillOneMoreProp } } = theObject;

console.log( stillOneMoreProp );
// Result: A string.

Mixed structures are just as compact:

const theObject = [{
  "aProperty" : true,
},{
  "anotherProperty" : "A string."
}];
const [{ aProperty }, { anotherProperty }] = theObject;

console.log( anotherProperty );
// Result: A string.

The density borders on opacity, and some experimentation is usually needed before it clicks — but once it does, complex data structures break down quickly, without a trail of intermediate values.

Rest properties

Everything above deals in known quantities. Real data tends to be a tangled object or a jam-packed array, or both. An ellipsis — ..., not a Unicode … — followed by an identifier inside a destructuring assignment is a rest property, holding every element or property beyond those explicitly unpacked, bundled in the same kind of structure as the source:

const theArray = [ false, true, false, true, true, false ];
const [ firstElement, secondElement, ...remainingElements ] = theArray;

console.log( remainingElements );
// Result: Array(4) [ false, true, true, false ]

Take an object shaped like an API payload — data the code doesn't control:

const firstPost = {
  "id": "mat-update-1.md",
  "slug": "mat-update-1",
  "body": "Hey, great to meet you, everybody. I'm Mat — \\"Wilto\\" is good too — and I'm here to teach you JavaScript. Not just what JavaScript is or what JavaScript does, but the *how* and the *why* of JavaScript. The weird stuff. The *deep magic_.\\n\\nWell, okay, I'm not *currently* here to teach you JavaScript, but I will be soon. Right now I'm just getting things in order for the course — planning, outlining, polishing the fancy semicolons that I only take out when I'm having company over, writing like 5,000 words about `this` as a warm-up that completely got away from me, that kind of thing.",
  "collection": "emails",
  "data": {
    "title": "Meet your Instructor",
    "pubDate": "2025-05-08T09:55:00.630Z",
    "headingSize": "large",
    "showUnsubscribeLink": true,
    "stream": "javascript-for-everyone"
  }
};

If only the title and body are needed, the id and slug properties can simply be omitted:

const firstPost = {
  "id": "mat-update-1.md",
  "slug": "mat-update-1",
  "body": "Hey, great to meet you, everybody. I'm Mat — \\"Wilto\\" is good too — and I'm here to teach you JavaScript. Not just what JavaScript is or what JavaScript does, but the *how* and the *why* of JavaScript. The weird stuff. The *deep magic_.\\n\\nWell, okay, I'm not *currently* here to teach you JavaScript, but I will be soon. Right now I'm just getting things in order for the course — planning, outlining, polishing the fancy semicolons that I only take out when I'm having company over, writing like 5,000 words about `this` as a warm-up that completely got away from me, that kind of thing.",
  "data": {
    "title": "Meet your Instructor",
    "pubDate": "2025-05-08T09:55:00.630Z",
    "headingSize": "large",
    "showUnsubscribeLink": true,
    "stream": "javascript-for-everyone"
  }
};

const { data : { title }, body } = firstPost;

console.log( title );
// Result: Meet your Instructor

console.log( body );
/* Result:
Hey, great to meet you, everybody. I'm Mat — \\"Wilto\\" is good too — and I'm here to teach you JavaScript. Not just what JavaScript is or what JavaScript does, but the *how* and the *why* of JavaScript. The weird stuff. The *deep magic_.

Well, okay, I'm not *currently* here to teach you JavaScript, but I will be soon. Right now I'm just getting things in order for the course — planning, outlining, polishing the fancy semicolons that I only take out when I'm having company over, writing like 5,000 words about `this` as a warm-up that completely got away from me, that kind of thing.
*/

The inner data object is another matter: future properties of unknown name may arrive there. A rest property collects them all while the title is pulled out on its own:

const firstPost = {
  "id": "mat-update-1.md",
  "slug": "mat-update-1",
  "body": "Hey, great to meet you, everybody. I'm Mat — \\"Wilto\\" is good too — and I'm here to teach you JavaScript. Not just what JavaScript is or what JavaScript does, but the *how* and the *why* of JavaScript. The weird stuff. The *deep magic_.\\n\\nWell, okay, I'm not *currently* here to teach you JavaScript, but I will be soon. Right now I'm just getting things in order for the course — planning, outlining, polishing the fancy semicolons that I only take out when I'm having company over, writing like 5,000 words about `this` as a warm-up that completely got away from me, that kind of thing.",
  "data": {
    "title": "Meet your Instructor",
    "pubDate": "2025-05-08T09:55:00.630Z",
    "headingSize": "large",
    "showUnsubscribeLink": true,
    "stream": "javascript-for-everyone"
  }
};

const { data : { title, ...metaData }, body } = firstPost;

console.log( title );
// Result: Meet your Instructor

console.log( metaData );
// Result: Object { pubDate: "2025-05-08T09:55:00.630Z", headingSize: "large", showUnsubscribeLink: true, stream: "javascript-for-everyone" }

Now metaData holds everything else in the data property, whatever that turns out to be. For this job in isolation, the result is about as good as it gets — though in JavaScript generally, a better way tends to exist somewhere, and finding it is part of the work.