xxxxxxxxxx
function swap(array,el1, el2) {
let temp = array[el1]
array[el1] = el2
array[el2] = temp
}
xxxxxxxxxx
var first = 5;
var second = 7;
[first, second] = [second, first];
console.log(first, second);
//answer - 7 5
xxxxxxxxxx
let a = 1;
let b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1
const arr = [1,2,3];
[arr[2], arr[1]] = [arr[1], arr[2]];
console.log(arr); // [1,3,2]
xxxxxxxxxx
// program to swap variables
let x = 4;
let y = 7;
// swapping variables
[x, y] = [y, x];
console.log(x); // 7
console.log(y); // 4
xxxxxxxxxx
var first = 5;
var second = 7;
[first, second] = [second, first]
console.log(first, second)
//Output: 7,5
xxxxxxxxxx
function swap(x, y) {
var t = x;
x = y;
y = t;
return [x, y];
}
console.log(swap(2, 3));
xxxxxxxxxx
let a = 1;
let b = 2;
let temp;
temp = a;a = b;b = temp;
a; // => 2
b; // => 1
xxxxxxxxxx
let a = "red";
let b = "blue";
let c = a; // red
a = b; //over-rides to blue
b = c;
console.log(a);
console.log(b);
xxxxxxxxxx
var a = [1,2,3,4,5], b=a.length;
for (var i=0; i<b; i++) {
a.unshift(a.splice(1+i,1).shift());
}
a.shift();
//a = [5,4,3,2,1];