Kodeclik Blog
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.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2024. All rights reserved.
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!
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:
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.
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.
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.
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?
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.
let a = 1;
let b = 2;
let temp;
temp = a;
a = b;
b = temp;
console.log(a); // Output: 2
console.log(b); // Output: 1
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a); // Output: 2
console.log(b); // Output: 1
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
let a = 1;
let b = 2;
a = a * b;
b = a / b;
a = a / b;
console.log(a); // => 2
console.log(b); // => 1
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