Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

How to swap variables in Javascript

Swapping variables is a common need in any programming language and thus all languages provide support for this basic necessity in many ways. Here are five ways to swap variables in Javascript!

Swap two variables in Javascript

Method 1: Use a temporary variable

This is the most common approach taught in all computer science classes. We create a temporary variable to store the value of one of the variables temporarily. We assign the value of the second variable to the first variable, and then assign the value of the temporary variable to the second variable. As a result, the values of the variables are swapped.

Here is some example code:

let a = 1;
let b = 2;
let temp;
temp = a;
a = b;
b = temp;
console.log(a); // Output: 2
console.log(b); // Output: 1

Method 2: Use a destructuring assignment

This method uses array destructuring to swap the values of the variables. We create an array with the values of the variables in reverse order, and then use destructuring assignment to assign the values of the array to the variables. As a result, the values of the variables are swapped.

Here is example code for this approach:

let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a); // Output: 2
console.log(b); // Output: 1

Notice how succinct the code is? This is the favorite of many veteran Javascript programmers.

Method 3: Use addition/subtraction

This method involves using arithmetic operators like +/- to swap the values of the variables. We add the values of the variables and store the result in the first variable. We then subtract the value of the second variable from the first variable and store the result in the second variable. Finally, we subtract the value of the second variable from the first variable again and store the result in the first variable. As a result, the values of the variables are swapped.

let a = 1;
let b = 2;
a = a + b;
b = a - b;
a = a - b;
console.log(a); // Output: 2
console.log(b); // Output: 1

Of course this method only works if the variables to be swapped are numbers in the first place.

Method 4: Use multiplication/division

This method is simply an adaptation of the above in that it uses multiplication and division operators rather than addition or subtraction.

let a = 1;
let b = 2;
a = a * b;
b = a / b;
a = a / b;
console.log(a); // => 2
console.log(b); // => 1

In the above code, we first assign the product of a and b to a, then assign the quotient of a and b to b, and finally assign the quotient of a and b to a. This way, the values of a and b are swapped.

Method 5: Use bitwise XOR

This method involves using the bitwise XOR operator to swap the values of the variables. We perform the XOR operation between the values of the variables and store the result in the first variable. We then perform the XOR operation between the value of the first variable and the value of the second variable and store the result in the second variable. Finally, we perform the XOR operation between the value of the first variable and the value of the second variable again and store the result in the first variable. As a result, the values of the variables are swapped.

Consider the below code:

let a = 1;
let b = 2;
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log(a); // Output: 2
console.log(b); // Output: 1

Thus we have seen five different ways to swap two variables in Javascript. Which one is your favorite?

If you liked this blogpost, learn about Python’s XOR and how it can be used to swap variables (among other uses). Also learn how to swap positions of values in a Python dictionary.

Want to learn Javascript with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.