Search⌘ K
AI Features

Deeper Destructuring, destructuring functions, and pitfalls

Learn how to apply deep destructuring to objects and arrays in ES6, including use with function parameters and default values. Understand common mistakes such as silent errors from typos and how to keep code readable and error-free.

Destructuring objects and arrays in any depth is possible. We can also use default values. Objects or arrays that don’t exist on the right become assigned to undefined on the left.

Node.js
let user = {
name : 'Ashley',
email : 'ashley@ilovees2015.net',
lessonsSeen : [ 2, 5, 6, 7, 9 ],
nextLesson : 10
};
let {
lessonsSeen : [
first,
second,
third,
fourth,
fifth,
sixth = null,
seventh
],
nextLesson : eighth
} = user;
console.log( "first:\t\t"+first+"\nsecond:\t\t"+second+"\nthird:\t\t"+third+
"\nfourth:\t\t"+fourth+"\nfifth:\t\t"+fifth+"\nsixth:\t\t"+sixth+
"\nseventh:\t"+seventh+"\neighth:\t\t"+eighth);

Notice that the null ...