JavaScript Program to Swap Two Variables
In JavaScript, it is often necessary to swap the values of two variables. For example, if you have two variables x
and y
that hold the values 1
and 2
, respectively, you may want to swap their values so that x
holds the value 2
and y
holds the value 1
. In JavaScript, there are several ways to swap the values of two variables. In this tutorial, we will learn how to write a JavaScript program that swaps the values of two variables using a variety of different methods.
1. Using a Temporary Variable
The idea is to create a temporary variable to store the value of variable a temporarily, we assign the value of variable b to variable a. And then the value of temp is assigned to b.
//JavaScript program to swap two variables
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
// temporary variable is to store the value temporarily.
let temp;
// swap the variables
temp = a;
a = b;
b = temp;
console.log("The value of a after swapping: " + a);
console.log("The value of b after swapping: " + b);
Enter the first variable: 24
Enter the second variable: 42
The value of a after swapping: 42
The value of b after swapping: 24
2. Swapping using Arithmetic Operators
The idea is to get a sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from the sum.
Note: The same can be applied using Multiplication and Division. Change Sum to Multiplication and Subtraction to Division.
// Write a code to swap two numbers in JavaScript
var x = 10, y = 5;
console.log("Before Swapping: x = " + x + ", y = " + y);
// swap 'x' and 'y'
x = x + y;
// y becomes 10
y = x - y;
// x becomes 5
x = x - y;
console.log("After Swapping: x = " + x + ", y = " + y);
Before Swapping: x = 10, y = 5
After Swapping: x = 5, y = 10
3. Using es6 Destructuring Assignment
The Destructuring Assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. The destructuring assignment uses a similar syntax, but on the left-hand side of the assignment, to define what values to unpack from the sourced variable.
// Write a code to swap two numbers in JavaScript
var a = 10, b = 5;
//using destructuring assignment
[a, b] = [b, a];
console.log("The value of a after swapping: ", a);
console.log("The value of b after swapping: ", b);
console.log("After Swapping: a = " + a + ", b = " + b);
The value of a after swapping: 5
The value of b after swapping: 10
After Swapping: a = 5, b = 10
4. Using Bitwise XOR operator
Bitwise XOR operator evaluates to true
if both operands are different.
a = a ^ b
assigns the value 4 ^ 2
to a (now 6).
b = a ^ b
assigns the value 6 ^ 2
to b (now 4).
a = a ^ b
assign the value 6 ^ 4
to a (now 2).
//JavaScript program to swap two variables
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
// XOR operator to swap the variables
a = a ^ b
b = a ^ b
a = a ^ b
console.log("The value of a after swapping: ", a);
console.log("The value of b after swapping: ", b);
Enter the first variable: 24
Enter the second variable: 42
The value of a after swapping: 42
The value of b after swapping: 24
Conclusion
In conclusion, swapping the values of two variables is a common operation in JavaScript that can be useful in a variety of different scenarios. In this tutorial, we learned how to write a JavaScript program that swaps the values of two variables using a variety of different methods. By following the examples and tips in this tutorial, you should be well on your way to becoming proficient at swapping the values of variables in your own JavaScript programs.