JavaScript Program to Swap Two Numbers Easily
You can swap two numbers in JavaScript using a temporary variable like
let temp = a; a = b; b = temp; or using array destructuring like [a, b] = [b, a];.Examples
Inputa = 5, b = 10
Outputa = 10, b = 5
Inputa = -3, b = 7
Outputa = 7, b = -3
Inputa = 0, b = 0
Outputa = 0, b = 0
How to Think About It
To swap two numbers, think of holding one number temporarily so you don't lose it when you overwrite its variable. You store the first number in a temporary place, assign the second number to the first variable, then put the stored number into the second variable. Alternatively, JavaScript lets you swap directly using array destructuring without a temporary variable.
Algorithm
1
Get the two numbers to swap, call them a and b.2
Store the value of a in a temporary variable temp.3
Assign the value of b to a.4
Assign the value of temp to b.5
Return or print the swapped values.Code
javascript
let a = 5; let b = 10; // Using a temporary variable let temp = a; a = b; b = temp; console.log('After swapping: a =', a, ', b =', b);
Output
After swapping: a = 10 , b = 5
Dry Run
Let's trace swapping a=5 and b=10 through the code
1
Initial values
a = 5, b = 10
2
Store a in temp
temp = 5
3
Assign b to a
a = 10
4
Assign temp to b
b = 5
5
Final swapped values
a = 10, b = 5
| Step | a | b | temp |
|---|---|---|---|
| Initial | 5 | 10 | undefined |
| Store a | 5 | 10 | 5 |
| Assign b to a | 10 | 10 | 5 |
| Assign temp to b | 10 | 5 | 5 |
Why This Works
Step 1: Temporary storage
We use a temporary variable temp to hold the value of a so it is not lost when we overwrite a.
Step 2: Overwrite first variable
We assign the value of b to a, so now a holds the second number.
Step 3: Restore second variable
We assign the stored value in temp back to b, completing the swap.
Alternative Approaches
Array destructuring
javascript
let a = 5; let b = 10; [a, b] = [b, a]; console.log('After swapping: a =', a, ', b =', b);
This method is shorter and more modern but requires understanding of array destructuring.
Arithmetic operations
javascript
let a = 5; let b = 10; a = a + b; b = a - b; a = a - b; console.log('After swapping: a =', a, ', b =', b);
This method swaps without extra memory but can cause overflow with very large numbers.
Complexity: O(1) time, O(1) space
Time Complexity
Swapping two numbers involves a fixed number of steps, so it runs in constant time O(1).
Space Complexity
Only a few variables are used, so space complexity is constant O(1).
Which Approach is Fastest?
All methods run in O(1) time; array destructuring is concise and readable, arithmetic avoids extra variables but risks overflow.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Temporary variable | O(1) | O(1) | Simple and clear for beginners |
| Array destructuring | O(1) | O(1) | Modern, concise syntax |
| Arithmetic operations | O(1) | O(1) | No extra variables, but risky with large numbers |
Use array destructuring
[a, b] = [b, a] for a clean and modern swap in JavaScript.Forgetting to use a temporary variable or destructuring causes one value to overwrite the other, losing data.