Open In App

How to swap two variables in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

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;//10=10+20 now a would be 30
b = a - b;//20=30-10 now b would be 10
a = a - b;//30=30-10 so a would be now 20
 
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

Approach 3: Using destructing Assignment

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}`);
 
// a would be swapped to b and b would be swapped to a
[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

Approach 4: Using XOR Bitwise Operator

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?

  1. If the operation is related to a numeric value you can swap values with the arithmetic operation(2nd method).
  2. 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
Previous
Next
Share your thoughts in the comments
Similar Reads