javascript
Javascript program to swap two numbers without using temporary variable
// Javascript program to swap two
// numbers without using temporary
// variable
let x = 10, y = 5;
console.log("Before Swapping: x = " +
x + ", y = " + y);
// Code to swap 'x' and 'y'
// x now becomes 15
x = x + y;
// y becomes 10
y = x - y;
// x becomes 5
x = x - y;
console.log("After Swapping: x = " +
x + ", y = " + y);
Output
Before Swapping: x = 10, y = 5
After Swapping: x = 5, y = 10
After Swapping: x = 5, y = 10
Was this helpful?
Similar Posts
- add sum of numbers of given numbers
- Lodash - Get random number between two numbers
- Check if a variable is null, empty, undefined or false Javascript
- Assign items of arrays to multiple variable when creating it (Array Destructuring) Javascript(ES6)
- Lodash - Check if a number is within the Range(without if-else)
- Remove decimal points in numbers
- Swapping two values using Destructuring in Javascript