In this example, you will learn to write a Javascript program to swap two variables.
But before going into the example, you must have knowledge of the following JavaScript topics:
To swap two numbers we can use different methods.
Swap Two Variables Using Third Variable
Example 1: Using temp Variable
// javascript program to swap two variables
// take user input
let x = prompt('Enter the first variable: ');
let y = prompt('Enter the second variable: ');
// temporary variable
let temp;
// swap variables
temp = x;
x = y;
y = temp;
console.log(`Value of x after swapping: ${x}`);
console.log(`Value of y after swapping: ${y}`);
Output
Enter the first variable: 1 Enter the second variable: 2 Value of x after swapping: 2 Value of y after swapping: 1
Here,
1. We first created a temp
variable to store the value of x
temporarily.
2. Then, we assigned the value of y
to x
.
3. After then, we assigned the value of temp
to y
.
As a result, the value of variables x
and y
are swapped.
Note: you can also swap other data types using this method.
Swap Two Variables Without Using Third Variable
Example 2: Using Destructuring Assignment
// javascript program to swap two variables
// take user input
let x = prompt('Enter the first variable: ');
let y = prompt('Enter the second variable: ');
// swap variables
// using destructuring assignment
[x, y] = [y, x]
console.log(`Value of x after swapping: ${x}`);
console.log(`Value of y after swapping: ${y}`);
Output
Enter the first variable: 1 Enter the second variable: 2 Value of x after swapping: 2 Value of y after swapping: 1
Here,
We are swapping the value of variables x
and y
using Destructuring Assignment [x, y] = [y, x]
which is an ES6 feature.
Destructuring assignment lets you extract the elements of an array into variables. For example, [x, y] = [1, 2, 3]
, 1
is assigned to variable x
, and Correspondingly 2
is assigned to variable y
.
First, we created a temporary array [y, x]
, which evaluates to [2, 1]
.
Then, destructuring of the temporary array happens : [x, y] = [2, 1]
.
As a result, the value of variables x
and y
are swapped.
Note: you can also swap other data types using this method.
Example 3: Using the Arithmetic Operators
// javascript program to swap two variables
// take user input
let x = parseInt(prompt('Enter the first variable: '));
let y = parseInt(prompt('Enter the second variable: '));
// swap variables
// using addition and subtraction operator
x = x + y;
y = x - y;
x = x - y;
console.log(`Value of x after swapping: ${x}`);
console.log(`Value of y after swapping: ${y}`);
Output
Enter the first variable: 1 Enter the second variable: 2 Value of x after swapping: 2 Value of y after swapping: 1
This approach swaps variables without using extra memory (like using an extra array or variable).
In the above program, we have used the parseInt()
method with prompt()
because prompt()
takes the input as a string so we need to convert it into a number.
Now let’s see how this approach swaps the values. Initially, the value of variable x
is 1
, and the value of y
is 2
.
1. x = x + y
assigns the value 1 + 2
to x
( Now, x = 3 ).
2. y = x - y
assigns the value 3 - 2
to y
( Now, y = 1 ).
3. x = x - y
assigns the value 3 - 1
to x
( Now, x = 2 ).
Finally, the value of variable x
is 2
and the value of y
is 1
.
Note: this approach can only be used with integer values.
Example 4: Using Bitwise XOR Operator
// javascript program to swap two variables
// take user input
let x = prompt('Enter the first variable: ');
let y = prompt('Enter the second variable: ');
// swap variables
// using Bitwise XOR operator
x = x ^ y;
y = x ^ y;
x = x ^ y;
console.log(`Value of x after swapping: ${x}`);
console.log(`Value of y after swapping: ${y}`);
Output
Enter the first variable: 1 Enter the second variable: 2 Value of x after swapping: 2 Value of y after swapping: 1
Bitwise XOR operator returns true if both the operands are different.
Let’s see how we used the XOR operator in the above program to swap the values. Initially, the value of variable x
is 1
, and the value of y
is 2
.
1. x = x ^ y
assigns the value 1 ^ 2
to x
( Now, x = 3 ).
2. y = x ^ y
assigns the value 3 ^ 2
to y
( Now, y = 1 ).
3. x = x ^ y
assigns the value 3 ^ 1
to x
( Now, x = 2 ).
Finally, the value of variable x
is 2
and the value of y
is 1
.
Note: this approach can only be used with integer values.