Categories
JavaScript Answers

How to move item in array to last position with JavaScript?

Spread the love

To move item in array to last position with JavaScript, we use the splice method.

For instance, we write

const a = [1, 2, 5, 4, 3];
a.splice(4, 0, a.splice(2, 1)[0]);
console.log(a);

to remove the 3rd element from the array and return it in an array with a.splice(2, 1).

We get the removed item with index 0.

And then we put it after index 4 by calling splice again with 4, 0, and a.splice(2, 1)[0].

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *