Array copyWithin()
The copyWithin() method of arrays and typed arrays shifts or copies items of an array to another index of the array without changing its length.
Overview
The copyWithin() method of arrays and typed arrays shifts or copies items of an array to another index of the array without changing its length.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 45 | 12 | 32 | 9 | 45 | 9 | |
| Built-in object | ||||||
| The copyWithin() method of TypedArray instances shallow copies part of this typed array to another location in the same typed array and returns this typed array without modifying its length. This method has the same algorithm as Array.prototype.copyWithin(). | 45 | 12 | 34 | 10 | 45 | 10 |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
const arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3); // [4, 5, 3, 4, 5]
arr.copyWithin(1, 3, 4); // [4, 4, 3, 4, 5] Live demo
Use cases
Using Array copyWithin()
The copyWithin() method of arrays and typed arrays shifts or copies items of an array to another index of the array without changing its length.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.
Related links
Powered by web-features