Swap Two Numbers in JavaScript
Swap two numbers in JavaScript three ways: a temp variable, array destructuring and arithmetic. Each method has an explanation and before and after output.
1// Method 1: Using a temporary variable2let a = 5;3let b = 10;4console.log("Before swap: a =", a, ", b =", b);56let temp = a;7a = b;8b = temp;9console.log("After swap: a =", a, ", b =", b);1011// Method 2: Using destructuring (ES6 - Modern approach)12let x = 5;13let y = 10;14console.log("Before swap: x =", x, ", y =", y);1516[x, y] = [y, x];17console.log("After swap: x =", x, ", y =", y);1819// Method 3: Using arithmetic (without temp variable)20let p = 5;21let q = 10;22console.log("Before swap: p =", p, ", q =", q);2324p = p + q;25q = p - q;26p = p - q;27console.log("After swap: p =", p, ", q =", q);
Sample Output
Before swap: a = 5 , b = 10
The space before the comma comes from console.log putting a space between each argument.
In this one
- Why a swap needs a place to hold one value
- How array destructuring swaps in one line
- How the arithmetic swap works and its limits
- How to check a swap with before and after prints
How it works
- 1
a = 5 and b = 10 are printed.
- 2
temp keeps 5, a becomes 10, b becomes temp (5).
- 3
x and y are swapped with [x, y] = [y, x].
- 4
p and q are swapped with addition and subtraction.
- 5
Each pair is printed again to show the new values.
Edge cases
Missing semicolon before destructuring
A line ending without ; followed by [x, y] = [y, x] can break.
JavaScript may join the two lines. Put a semicolon at the end of the line before, or start the swap line with ;.
Arithmetic swap with strings
p = "5" makes p + q become "510".
The math trick only works for numbers. Use destructuring for any type.
What's going on
Swapping means the two variables trade values. If a is 5 and b is 10, after the swap a is 10 and b is 5. Sorting programs swap values all the time, so it is worth learning early.
Method 1 uses a temporary variable. You cannot just write a = b, because then the old value of a is lost. So you first copy a into temp, then put b into a, then put temp into b. It is three lines and easy to get in the wrong order, but it works in every language.
Method 2 uses destructuring: [x, y] = [y, x]. Destructuring means unpacking values from an array into variables. The right side builds a new array [10, 5] first. Then the left side unpacks it, so x gets 10 and y gets 5. No temp variable is needed. This is the one you should keep using in JavaScript.
Method 3 uses math. p = p + q makes p 15. Then q = p - q gives 5, and p = p - q gives 10. It looks clever, but it only works with numbers, and very large numbers can lose precision. Treat it as a puzzle, not a habit.
All three methods print the values before and after so you can check the swap.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
After let t = a; a = b; b = t; with a=3, b=8, what is a?
Pick an answer to check it.
Question 2
Which line swaps x and y?
Pick an answer to check it.
Questions people ask
Which swap method should I use?
Use [a, b] = [b, a]. It is short, clear and works for any value type.
Why is there a space before the comma in the output?
console.log adds a space between each value, including before the ", b =" string.
Does destructuring create an extra array?
Yes, a tiny one. For normal programs this cost does not matter.