Array splice()
The splice() method of Array instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
To create a new array with a segment removed and/or replaced without mutating the original array, use toSpliced(). To access part of an array without modifying it, see slice().
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 1 | 12 | 1 | 1 | 18 | 1 | |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
const arr = ['a', 'b', 'c', 'd'];
// Remove
arr.splice(1, 2); // Return value: ['b', 'c'], arr: ['a', 'd']
// Insertion
arr.splice(1, 0, 'x', 'y'); // arr: ['a', 'x', 'y', 'd']
// Replacement
arr.splice(1, 1, 'z'); // arr: ['a', 'z', 'y', 'd'] Live demo
Use cases
-
Using Array splice()
The splice() method of Array instances changes the contents of an array by
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.