Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 20 - Fixed some typos and wording #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions 20 - Swapping Variables with Destructuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Let's look at a couple more examples of when you would actually use destructuring in the real world. Destrucuring is not just when you want to grab variables out of an object or out of an array - it has a few other uses too!

I'm building an application for a wrestling application. In wrestling, they are tag teaming. I don't really know that much about wrestling, but I know that there's one guy in the ring and one guy on the side!
I'm building an application for a wrestling team. In wrestling, they are tag teaming. I don't really know that much about wrestling, but I know that there's one guy in the ring and one guy on the side!

Let's create two new variables. It will become obvious why I choose `let` over `const` here in a second.

Expand All @@ -15,19 +15,19 @@ I'm not sure if these guys ever wrestled together, but it seems feasible. They n

What happens when you need to switch? The Rock is going in the ring and the Hulkster is going on the side.

How did you swap two variables before destructuring?
How do you swap two variables before destructuring?

If you use `inRing = onSide`, now `inRing` is The Rock, but then also `onSide` is also The Rock and we've lost the Hulkster altogether! Shit!

How do we get around that? The way we used to be able to do it is we would make a `temp` variable.
How do we get around that? The way we used to do it is we would make a `temp` variable.

```js
var temp = inRing;
inRing = onSide;
onSide = temp;
```

They're totally switched now, and then you would delete your actual temp variable. That's really hard to follow. That's hard to look at, especially someone coming after six months of not working on this code, trying to understand what's going on.
They're totally switched now, and then you would delete your actual temp variable. That's really hard to follow. It's hard to look at, especially for someone coming back after six months of not working on this code, trying to understand what's going on.

With **JavaScript destructuring**, we can just swap them. We make use of an array here:

Expand All @@ -41,4 +41,4 @@ What!? The above code creates an array of `[onSide, inRing]` and immediately des

Now you see why I use let instead of const here, because we're actually updating the values of these variables.

Handy! Put that in your JavaScript tool belt because you'll need to sooner than later!
Handy! Put that in your JavaScript tool belt because you'll need it sooner than later!