Languages

JavaScript - what is the fastest way to move first element to the end of array?

0 points
Asked by:
Wiktor-Sribiew
830

What is the fastest way to move an element from the beginning of an array to the end in JavaScript?

For example, if we have:

and the result I want is:

1 answer
0 points
Answered by:
Admin Dirask Community
4380

Just combine shift() and push() methods:

  1. shift() method removes the first element from an array and returns that removed element,

  2. push() method adds the element at the end of the array.

Practical example

Note:

If you want to move any element to the first/last position check out the See also section below.

 

See also

  1. JavaScript - move element of array to the last position

  2. JavaScript - move element of array to the first position

  3. JavaScript - move array element from one position to another

References

  1. Array.prototype.push() - JavaScript | MDN
  2. Array.prototype.shift() - JavaScript | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join