DEV Community

Rajika Imal
Rajika Imal

Posted on

1

JavaScript quick tips: Array.copyWithin()

Array.prototype.copyWithin() does a copy (shallow) of the selected elements within the same array, to a specified index.

To copy value in index 3 to index 1

const myArray = [1, 2, 3, 4, 5];
myArray.copyWithin(1, 3, 4); // [1, 4, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

To copy all values from specified index to the end to a given index

const myArray = [1, 2, 3, 4, 5];
myArray.copyWithin(1, 3); // [1, 4, 5, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay