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!

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:

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:

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.

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.

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:

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

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.