# Javascript program to Swap Two numbers using different ways

In this javascript tutorial we will cover different ways to swap two numbers in javascript. There are different ways to swap numbers. here we will cover them.

• Swap Two numbers using 3rd Temp variable
• Swap Values using Arithmetic Operators
• Swap Values using Destructuring assignment
• Swap Values using Bitwise XOR operator

Swap Two numbers using 3rd Temp variable

 ``````

Input:

Enter first value: 12

Enter second value: 24

Output:

Value of x after swapping = 24

Value of y after swapping = 12

Swap Values using Arithmetic Operators



Swap Values using Arithmetic Operators

• In the above example we taking input values into x and y values
• Then Adding the x, y and assign to variable x
• Now we are subtracting the y value from x, this will return previous x value and assigned to y
• Now subtract current y value from x, this will return x value with swap

Output:

Value of x after swapping = 32

Value of y after swapping = 24

Swap Values using Destructuring assignment



Swap Values using Destructuring assignment

Output:

Value of x after swapping = 32

Value of y after swapping = 24

Swap Values using Bitwise XOR operator



Swap Values using Bitwise XOR operator

Output

Value of x after swapping = 32

Value of y after swapping = 24