javascript swap elements code example
Example 1: swap function javascript
let a = 1;
let b = 2;
let temp;
temp = a;a = b;b = temp;
a;
b;
Example 2: javascript swap variables
var a = 5;
var b = 3;
[a, b] = [b, a];
Example 3: swap elements in array javascript
const heavy = ["Rammstein", "Motorhead", "Imsomnium", "Be´lakor"];
let aux = "";
const intercambiar = (arr, i1, i2) => {
aux = arr[i1];
arr[i1] = arr[i2];
arr[i2] = aux;
return arr;
};
let resulIntercambiar = intercambiar(heavy, 1, 2);
console.log(resulIntercambiar);
Example 4: js swap
dmitripavlutin.com › swap-variables-javascript
Example 5: javascript swap array elements
var b = list[y];
list[y] = list[x];
list[x] = b;