Categories
JavaScript Answers

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

Spread the love

Sometimes, we want to move item in array to last position with JavaScript.

In this article, we’ll look at how to move item in array to last position with JavaScript.

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

To move item in array to last position with JavaScript, we use the shift and push methods.

For instance, we write

const a = [5, 1, 2, 3, 4];
a.push(a.shift());

to call a.shift to return the first item from array a after removing it.

Then we call a.push with the return item to put it as the last item in a.

Conclusion

To move item in array to last position with JavaScript, we use the shift and push methods.

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 *