...

/

Destructuring Examples

Destructuring Examples

destructuring, associativity and ES6 notation

We'll cover the following...

Consider the following examples:

Press + to interact
let user = {
name : 'Ashley',
email : 'ashley@ilovees2015.net',
lessonsSeen : [ 2, 5, 6, 7, 9 ],
nextLesson : 10
};
let { email, nextLesson } = user;
console.log(user);
// email becomes 'ashley@ilovees2015.net'
// nextLesson becomes 10

In a destructuring expression L = R, we take the right value R, and break it down so that the new variables in L can be assigned a value. In the above code, we used the object property shorthand notation.

Press + to interact
let { email, nextLesson } = user;

Without this shorthand notation, our code will look like this:

Press + to interact
let {
email: email,
nextLesson: nextLesson
} = user;
console.log(user)

In this case, the above code is equivalent with the ...