CKTIS / JavaScript
Array.prototype.splice: removing, replacing, and inserting
splice mutates an array at a selected position, shifts its tail, and returns a new array of removed elements.
Version: ECMAScript 2026 draft; example target: Node.js 24.22.2
Syntax
array.splice(start, deleteCount?, ...items)array.splice(start, deleteCount?, ...items) changes array in place and returns a new array containing removed elements. start is converted to an integer: a negative value counts from the end, while a position beyond the length means the end. When start is supplied, omitting deleteCount removes the entire tail; explicitly passing undefined means zero. Other values are clamped from zero through the remaining element count. items are inserted before the shifted tail.
The result length equals the number of removed positions and corresponding holes remain holes. The receiver can grow, shrink, or keep its length; splice() with no arguments removes nothing. Result allocation observes Symbol.species on an Array subclass.
The method is generic for mutable array-like objects: it reads length, copies or deletes indexed properties, and writes the new length. It can throw TypeError when writes, deletion, or length are forbidden, or when the new length would exceed 2^53 - 1; failures from conversion, accessors, proxy traps, and the species constructor propagate.
Examples
const queue = ["build", "test", "deploy"];
const removed = queue.splice(1, 1, "lint", "test");
console.log(removed.join(","));
console.log(queue.join(","));
const tail = queue.splice(-2);
console.log(tail.join(","));
console.log(queue.join(","));
const none = queue.splice(1, undefined, "docs");
console.log(none.length, queue.join(","));
const sparse = [, "b", "c"];
const cut = sparse.splice(0, 2);
console.log(cut.length, 0 in cut, 1 in cut, cut[1], sparse.join(","));Result
test build,lint,test,deploy test,deploy build,lint 0 build,docs,lint 2 false true b c