To swap 2 elements of an array:
javascript[array[pos_1], array[pos_2]] = [array[pos_2], array[pos_1]];
Example, how to swap position 0 and 3 of an array:
javascriptconst array = [1, 2, 3, 4]; [array[0], array[3]] = [array[3], array[0]]; console.log(array); // [4, 2, 3, 1]
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.