Check out a free preview of the full TypeScript Fundamentals course:
The "Destructured Assignment" Lesson is part of the full, TypeScript Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Mike shows a way to pick one or more properties off an object into distinct variables. Mike takes questions from students.

Get Unlimited Access Now

Transcript from the "Destructured Assignment" Lesson

[00:00:00]
>> Mike: We also got something called destructured assignment, and this is a rather complex example just to show, like, the power of this idea. But say we have a value here, person, which is a pretty deeply nested object and sorry for the unconventional formatting here, but hopefully you get the idea of the outlining.

[00:00:17] And then on the bottom, we are basically trying to capture values that exist deep within this structure. And, we can capture one or more values at the same time in one statement, right? So, in this case, just reading the second let here, we're saying first, well sorry, we're saying to begin I wish to go into this name object and go and grab me whatever value is there under the property first.

[00:00:51] And then go into the languages object and into the backend object, and get me that backend object. And I want to assign that to a variable called serverSkills. So we do that all in one statement here. So effectively, we'd be able to log out something like this. So we're logging out the thing we got under the value of name first.

[00:01:19] And then, the keys available on language's backend, and that's where we get elixir from. But that's the idea of destructured assignment, and if you've used a language that had something like pattern matching in it. You could think of it just as, during assignment, we basically try to make, try to look at the structure on the right and the left and figure out a way that assignments can be made such that, we can have the stuff on the left captures values from stuff on the right.

[00:01:55] Yes?
>> Speaker 2: So name and languages would not be accessible as variables?
>> Mike: Correct. Name and languages would not be accessible as variables. They are simply there to define structure of where I'm looking to go and grab variables. And the reason you know that is because it's a key, right?

[00:02:16] There's a colon to the right of it, as opposed to places where I'm capturing things, or there isn't. The exception being backend where I'm basically aliasing it. If I didn't have serverSkills and I deleted the column to the right of backend, I'd have a variable that I could use as backend.

[00:02:34] So that's just the optional renaming at work.