swap two numbers without using third variable in javascript interview question

Umar Farooque Khan
3 min readJul 7, 2023

--

Swapping numbers

There are various methods to interchange variable values, and the specific technique employed may vary depending on the particular situation. During an interview, it is common for interviewers to inquire about these different approaches.

Method 1:

One commonly used approach is by using a temporary variable as an intermediary step. This involves assigning the value of one variable to the temporary variable, then assigning the value of the second variable to the first variable, and finally assigning the value of the temporary variable to the second variable. This effectively swaps the values of the two variables.

let a = 5;
let b = 10;

console.log("Before swapping:");
console.log("a =", a);
console.log("b =", b);

// Switching the values
let temp = a;
a = b;
b = temp;

console.log("After swapping:");
console.log("a =", a);
console.log("b =", b);

Output:

Before swapping:
a = 5
b = 10
After swapping:
a = 10
b = 5

In this example, we use a temporary variable temp to store the value of a before swapping. Then we assign the value of b to a and finally assign the value of temp (which holds the original value of a) to b. This way, the values of a and b are effectively swapped.

Note: that this method works for variables of any type, not just numbers.

Method 2:

Another approach to swap variable values without using a third variable-

let a = 5;
let b = 10;

console.log("Before swapping:");
console.log("a =", a);
console.log("b =", b);

// Switching the values without a third variable
a = a + b;
b = a - b;
a = a - b;

console.log("After swapping:");
console.log("a =", a);
console.log("b =", b);

Output:

Before swapping:
a = 5
b = 10
After swapping:
a = 10
b = 5

In this approach, the values of a and b are swapped by performing addition and subtraction operations. Here's how it works:

  1. a = a + b: The value of a is updated by adding the value of b. The result is stored in a, which now holds the sum of the original values of a and b.
  2. b = a - b: The value of b is updated by subtracting the original value of b from the updated value of a. This effectively cancels out the original value of b and assigns the original value of a to b.
  3. a = a - b: Finally, the value of a is updated by subtracting the new value of b from the updated value of a. This cancels out the original value of a and assigns the original value of b to a.

By performing these arithmetic operations, the values of a and b are swapped without the need for a third variable.

Method 3:

In JavaScript, you can swap variable values using destructuring assignment. Destructuring assignment allows you to extract values from arrays or objects into distinct variables. By applying this technique, you can easily swap the values of two variables. Here’s an example:

// Swapping variable values using destructuring assignment
let x = 5;
let y = 10;
[x, y] = [y, x];console.log("x =", x); // Output: x = 10
console.log("y =", y); // Output: y = 5

In this approach, we create an array [y, x] on the right-hand side of the assignment. By assigning this array to [x, y], the values are automatically swapped. The value of x becomes the previous value of y, and the value of y becomes the previous value of x.

Destructuring assignment provides a concise and readable way to swap variable values in JavaScript, enhancing code clarity and reducing the need for temporary variables.

Conclusion:

Based on my understanding, the third method utilizing destructuring assignment is a favorable approach. It offers a concise and readable solution for swapping variable values in JavaScript. By leveraging a single assignment statement, the values of two variables can be effortlessly interchanged without requiring additional variables or complex calculations. The use of destructuring assignment enhances code clarity and reduces the likelihood of introducing errors. Overall, the third method stands out as an effective and elegant technique for achieving variable swapping in JavaScript.

--

--

Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.