CKTIS / JavaScript
Array.prototype.slice: a shallow range copy
slice creates a new shallow copy from start through exclusive end while preserving sparse holes.
Version: ECMAScript 2026 draft; example target: Node.js 24.22.2
Syntax
array.slice([start[, end]])array.slice([start[, end]]) returns a new array containing indexes from normalized start inclusive to end exclusive. start defaults to 0 and end to the captured length. A negative index is relative to the end and clamped at zero; a positive one is clamped at length. If end does not follow start, the result is empty.
The copy is shallow: objects remain the same references. For a sparse array the method tests property existence, so holes remain holes. It does not mutate and is generic for array-like receivers. A null or undefined receiver throws TypeError, while failures from getters, the array species constructor, or result-property creation propagate.
Examples
const source = [{ id: 1 }, , 3, 4];
const part = source.slice(-3, -1);
console.log(part.length, 0 in part, part[1]);
const clone = source.slice(0, 1);
console.log(clone !== source, clone[0] === source[0]);
const record = { 0: "a", 2: "c", length: 3 };
const copied = Array.prototype.slice.call(record);
console.log(copied.length, Object.keys(copied).join(","), copied[0], copied[2]);Result
2 false 3 true true 3 0,2 a c