How to swap two array elements in JavaScript

Published on

Take a look at this example array of five numbers, with values representative of their original starting indexes:

const numbers = [0, 1, 2, 3, 4];

How would we go about swapping the element at index 0 and the element at index 4? Well could achieve this by using a temporary variable like so:

const numbers = [0, 1, 2, 3, 4];

const temporary = numbers[0];
numbers[0] = numbers[4];
numbers[4] = temporary;

console.log(numbers);

// [4, 1, 2, 3, 0]

Although this works I find it far more readable and succinct to instead take advantage of the ES6 feature that allows for multiple assignment to switch the elements. Using that syntax our code would resemble this:

const numbers = [0, 1, 2, 3, 4];

[numbers[0], numbers[4]] = [numbers[4], numbers[0]];

console.log(numbers);

// [4, 1, 2, 3, 0]