Problem Statement
write a JavaScript program to swap the values in two variables
Solution
to swap the values in a two variables, we can use a temporary variable, or destructuring assignment. we can also use arithmetic operators if the values are numbers
Programs
1. swap two variables using temporary variable
var a = 5;
var b = 7;
var temp;
temp = a;
a = b;
b = temp;
console.log('a : '+a);
console.log('b : '+b);
2. swap two variables using arithmetic operators
var a = 5;
var b = 7;
a = a + b;
b = a - b;
a = a - b
console.log('a : '+a);
console.log('b : '+b);
3. swap two variables using destructuring assignment
var a = 5;
var b = 7;
[a, b] = [b, a]
console.log('a : '+a);
console.log('b : '+b);