JavaScript Program to swap two numbers without using a third variable

In this program, you will learn how to swap two numbers without using a third variable in JavaScript.


x = x + y
y = x - y
x = x - y

JavaScript Program to swap two numbers without using a third variable

Example: How to swap two numbers without using a third variable in JavaScript

let x = parseInt(prompt("Enter first number"))
let y = parseInt(prompt("Enter second number"))

x = x + y
y = x - y
x = x - y

console.log("After swap x is:"+ x)
console.log("After swap y is:"+ y)

Output:

Enter first number> 30
Enter second number> 40
After swap x is:40
After swap y is:30