Yes, it is kinda a quality of life thing. Basically, you can assign things to objects using shorthand like
const myObj = { foo: 1, bar: 2 }
Instead of
const myObj = {}
myObj.foo = 1
myObj.foo = 2
There wasn’t the same thing for extracting things, but destructuring now provides that:
const { foo, bar } = myObj
rather than
myObj.foo
myObj.bar
It is, like a lot of the small ES6 syntax adjustments, a way to be more specific about what exactly is happening at a particular stage of your program. You describe the exact shape of the data you want up front, and if something errors, it generally errors earlier than it would otherwise (which is what you want), and error messages tend to be more specific (again, what you want).
You don’t really see the benefit from small toy examples unless you’ve programmed in JS for a while, but I guess the most obvious example is pulling value from API responses. This is a basic GitHub API response:
{
"login": "DanCouper",
"id": 573694,
"node_id": "MDQ6VXNlcjU3MzY5NA==",
"avatar_url": "https://avatars3.githubusercontent.com/u/573694?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/DanCouper",
"html_url": "https://github.com/DanCouper",
"followers_url": "https://api.github.com/users/DanCouper/followers",
"following_url": "https://api.github.com/users/DanCouper/following{/other_user}",
"gists_url": "https://api.github.com/users/DanCouper/gists{/gist_id}",
"starred_url": "https://api.github.com/users/DanCouper/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/DanCouper/subscriptions",
"organizations_url": "https://api.github.com/users/DanCouper/orgs",
"repos_url": "https://api.github.com/users/DanCouper/repos",
"events_url": "https://api.github.com/users/DanCouper/events{/privacy}",
"received_events_url": "https://api.github.com/users/DanCouper/received_events",
"type": "User",
"site_admin": false,
"name": "Dan Couper",
"company": "@SoPost ",
"blog": "",
"location": "Newcastle, UK",
"email": null,
"hireable": true,
"bio": null,
"public_repos": 33,
"public_gists": 69,
"followers": 32,
"following": 93,
"created_at": "2011-01-20T00:46:50Z",
"updated_at": "2018-06-08T23:46:39Z"
}
And with destructuring, I could do something like:
async function grabUserInfo(username) {
const request = await fetch(`https://api.github.com/${username}`)
const response = await request.json();
const userInfo = printUserInfo(response);
console.log(user info);
}
function printUserInfo({ type, name, location = 'not provided'}) {
return `${name} is a GitHub ${type}. Location: ${location}`;
}
So hitting the API with my GH username will log DanCouper is a GitHub User. Location: Newcastle, UK
.
The key thing is I can destructure directly as a function parameter, so the printUserInfo
function takes the response object, grabs the field values I want, and prints them out. If I pass it an object of the wrong ‘shape’, it will blow up straightaway. It also demonstrates that I can pass defaults: I don’t have to provide a location to GitHub, so that field might not be there (I think the API might just return an empty string if it’s left blank though, so example may not be totally correct, but hopefully you can see the reasoning)
And more in-depth:
http://exploringjs.com/es6/ch_destructuring.html