JavaScript Program to Swap Two Variables

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Using a Temporary Variable

//JavaScript program to swap two variables

//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

//create a temporary variable
let temp;

//swap 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}`);

Output

Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4

Here,

  1. We created a temp variable to store the value of a temporarily.
  2. We assigned the value of b to a.
  3. The value of temp is assigned to b

As a result, the value of the variables are swapped.

Note: You can also swap strings or other data types using this method.


Example 2: Using es6(ES2015) Destructuring assignment

//JavaScript program to swap two variables

//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

//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}`);

Output

Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4

Here, a new es6 feature, called destructuring assignment [a, b] = [b, a], is used to swap the value of two variables. If [a, b] = [1, 2, 3], the value of a will be 1 and value of b will be 2.

  • First a temporary array [b, a] is created. Here the value of [b, a] will be [2, 4].
  • The destructuring of the array is done, i.e [a, b] = [2, 4].

As a result, the value of the variables are swapped.

You can learn more about destructuring in JavaScript Destructing Assignment.

Note: You can also swap strings or other data types using this method.


You can also swap the variable's values using the arithmetic operators.

Example 3: Using Arithmetic Operators

//JavaScript program to swap two variables

//take input from the users
let a = parseInt(prompt('Enter the first variable: '));
let b = parseInt(prompt('Enter the second variable: '));

// addition and subtraction operator
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}`);

Output

Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4

This method only uses the two variables and swaps the value of the variables using arithmetic operators + and -.

Here, parseInt() is used because prompt() takes input from the user as a string. And when numeric strings are added, it behaves as a string. For example, '2' + '3' = '23'. So parseInt() converts a numeric string to number.

To learn more about the type conversion, go to JavaScript Type Conversions.

Let's see how the above program swaps values. Initially, a is 4 and b is 2.

  • 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).

Finally, a is 2 and b is 4.

Note: You can use arithmetic operators (+, -) if both variables are of number type.


Example 4: Using Bitwise XOR operator

//JavaScript program to swap two variables

//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

// XOR operator
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}`);

Output

Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4

Bitwise XOR operator evaluates to true if both operands are different. To learn more about bitwise operators, visit JavaScript Bitwise Operators.

Let's see how the above program swaps values. Initially, a is 4 and b is 2.

  • 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).

Finally, a is 2 and b is 4.

Note: You can use this method for only integer (whole number) values.

Before we wrap up, let's put your understanding of this example to the test! Can you solve the following challenge?

Challenge:

Write a function to find the nth Fibonacci number.

  • The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it.
  • Starting with 0 and 1, the sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, and so on.
  • Return the nth Fibonacci number for the given n.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges