How to swap two variables in JavaScript ?
The swapping process refers to changing the value from one variable to another variable. In this article, we will be learning about swapping 2 variables with Various Approaches.
We would be using various ways to swap variables:
Approach 1: Using Temporary variable
- Let’s say we create a temp variable that stores the value of a variable A.
- Then we copy the value of B to A (A would be overwritten).
- Then we copy the value of temp to B (earlier value of A).
Syntax:
temp = a;
a = b;
b = temp;
Note: Here, temp is the new 3rd variable that we are using to swap the value of variables a and b.
Example: Below is an example of the above-explained approach.
Javascript
let a = 20;
let b = 10;
let temp;
console.log(`before swapping: a= ${a}`);
console.log(`before swapping b= ${b}`);
temp = a;
a = b;
b = temp;
console.log(`after swapping a= ${a}`);
console.log(`after swapping b= ${b}`);
|
Output
before swapping: a= 20
before swapping b= 10
after swapping a= 10
after swapping b= 20
Approach 2: Using Arithmetic Operations
- Firstly, we add a + b to a (a would be greater than b as it is).
- Now we subtract b from a so the value of b is now available to b
- Now we subtract a with b again to a so a will have the value of B
Syntax:
a = a + b;
b = a - b;
a = a - b;
Example: Below is an example of the above-explained approach.
Javascript
let a = 10;
let b = 20;
console.log(`before swap a= ${a}`);
console.log(`before swap a= ${b}`);
a = a + b;
b = a - b;
a = a - b;
console.log(`after swap a= ${a}`);
console.log(`after swap a= ${b}`);
|
Output
before swap a= 10
before swap a= 20
after swap a= 20
after swap a= 10
Here we will be using destructing Assignment. The destructing assignment makes it possible to unpack values from an array or object or from other properties into distinct variables.
Syntax:
[b,a]=[a,b];
Example: Below is an example of the above-explained approach.
Javascript
let a = 40;
let b = 30;
console.log(`before swap a= ${a}`);
console.log(`before swap a= ${b}`);
[b, a] = [a, b];
console.log(`after swap a= ${a}`);
console.log(`after swap a= ${b}`);
|
Output
before swap a= 40
before swap a= 30
after swap a= 30
after swap a= 40
the XOR bitwise operation is used to swap the values without the need for a temporary variable. This method takes advantage of the property that a ^ b ^ b
is equal to a
.
Syntax:
a ^ b
Example: Below is an example of the above-explained approach.
Javascript
let a = 5;
let b = 10;
console.log( "Before swapping: a =" , a, "b =" , b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log( "After swapping: a =" , a, "b =" , b);
|
Output
Before swapping: a = 5 b = 10
After swapping: a = 10 b = 5
Note: When a large amount of data is concerned Destructing Assignment may helpful.
Where to use which method?
- If the operation is related to a numeric value you can swap values with the arithmetic operation(2nd method).
- If the operation is related to other types like string or boolean you can use the 1st method.
Last Updated :
28 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...