JavaScript Program to Swap Two Variables (5 Ways)
Swapping two variables is a fundamental concept in programming that is useful in many situations. JavaScript offers several methods to achieve this, ranging from simple approaches using a temporary variable to modern techniques like array destructuring. In this program, we will discuss various ways to create a JavaScript program to swap two variables, complete with clear examples, explanations, and outputs.
JavaScript Program to Swap Two Variables Using a Temporary Variable
This is the most traditional and straightforward method to swap two variables.
Code
let a = 5;
let b = 10;
let temp;
temp = a;
a = b;
b = temp;
console.log("After swapping: a = " + a + ", b = " + b);
Output
After swapping: a = 10, b = 5
Explanation
A third variable, temp, temporarily holds the value of a. The value of b is assigned to a, and the value stored in temp (original value of a) is assigned to b.
JavaScript Program to Swap Two Variables Using Arithmetic Operators
You can swap two variables without using a temporary variable by leveraging arithmetic operations like addition and subtraction.
Code
let a = 5;
let b = 10;
a = a + b;
b = a - b;
a = a - b;
console.log("After swapping: a = " + a + ", b = " + b);
Output
After swapping: a = 10, b = 5
Explanation
-
a = a + b adds the values of a and b.
-
b = a - b subtracts the original value of b from a (now holding the sum) to get the original value of a.
-
a = a - b subtracts the new value of b (original a) from a (sum) to get the original value of b.
JavaScript Program to Swap Two Variables Using Bitwise XOR Operator
The XOR operator can also be used to swap two variables without a temporary variable.
Code
let a = 5;
let b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log("After swapping: a = " + a + ", b = " + b);
Output
After swapping: a = 10, b = 5
Explanation
-
a = a ^ b calculates the XOR of a and b and stores it in a.
-
b = a ^ b calculates the XOR of a (now a ^ b) and b (original b) to get the original value of a.
-
a = a ^ b calculates the XOR of a (now a ^ b) and b (original a) to get the original value of b.
JavaScript Program to Swap Two Variables Using Multiplication and Division
This method is similar to the addition and subtraction approach but uses multiplication and division.
Code
let a = 5;
let b = 10;
a = a * b;
b = a / b;
a = a / b;
console.log("After swapping: a = " + a + ", b = " + b);
Output
After swapping: a = 10, b = 5
Explanation
-
a = a * b multiplies the values of a and b.
-
b = a / b divides the product by the original value of b to get the original value of a.
-
a = a / b divides the product by the new value of b (original a) to get the original value of b.
Note: Avoid this method if either variable can be 0, as division by zero is undefined.
JavaScript Program to Swap Two Variables Using Array Destructuring
Modern JavaScript makes swapping two variables easier with array destructuring.
Code
let a = 5;
let b = 10;
[a, b] = [b, a];
console.log("After swapping: a = " + a + ", b = " + b);
Output
After swapping: a = 10, b = 5
Explanation
The array destructuring syntax [a, b] = [b, a] directly swaps the values of a and b without requiring additional variables or operations.