Swap Two Variables In-Place

Account Required

This free lesson is available if you sign in.
If you don't have an account, you can sign up for free.

Swap Two Variables In-Place

Sign in to access this content

You must sign in to access this content.
If you don't have an account you can sign up for free!

Problem

let alpha = 'john';
let beta = 'jane';

// Don't use any new variables

// Test
console.log(alpha == 'jane' && beta == 'john'); // true

Solution

let alpha = 'john';
let beta = 'jane';

// Don't use any new variables
[beta, alpha] = [alpha, beta];

// Test
console.log(alpha == 'jane' && beta == 'john'); // true
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

Can you take two JavaScript variables, alpha and beta, and swap the values around so that the value stored in beta is now stored in alpha and vice versa without using a third variable. We'll answer that in this lesson. So let's go here. We have two JavaScript variables, alpha containing the string, John and Beta containing the string. Jane, our key constraint is that we will not use any new variables, and our final test will be that alpha must contain the value of beta, which was Jane and Beta must contain the value of Alpha, which was drawn. Now, most people will quite easily arrive at a temporary

00:35

variable solution where the user temporary variable to store the value of Alpha so that they can then put in the value of beta into Alpha, and then finally recover the value of Alpha from the temporary variable to store into beta, essentially swapping alpha and beta. However, this violates are constraint of not treating any temporary variables. Fortunately, we do have a solution to this problem thanks to JavaScript Destructuring. Here's a quick recap. We can create two variables, one and two by destructuring them from an existing array containing the strings, John and Jane.

01:08

So now one will be John and two will be Jane. And in addition to Destructuring, while creating variables, we can even destructure into existing variables. For example, we can have the variables one and two declared beforehand and then use these two variables to store the values provided by the array first and second members. Now, since we are creating arrays out strings over here, we can actually create an array out of existing variables as well. So we might as well just use the variables alpha and beta that we already have that are storing these two same strings. And now for the final crescendo, instead

01:43

of storing these values into these two local variables that we just created, we can use the existing variables beta and alpha in that order to essentially swap the values of alpha and beta using JavaScript destructuring. So this is not something that was possible with older versions of JavaScript, for example, ES five and Below, which did not have a already structuring. So as JavaScript continues to evolve, more interesting patterns like this continue to emerge.