Array.prototype.slice
Returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included)
Syntax
array.slice(start?, end?)Parameters
| Parameter | Type | Description |
|---|---|---|
| start | number | Zero-based index at which to start extraction |
| end | number | Zero-based index before which to end extraction |
Return Value
A new array containing the extracted elements
Examples
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2)); // ['camel', 'duck', 'elephant']
console.log(animals.slice(2, 4)); // ['camel', 'duck']const arr = [1, 2, 3, 4, 5];
const last3 = arr.slice(-3);
console.log(last3); // [3, 4, 5]const original = [{ id: 1 }, { id: 2 }];
const copy = original.slice();
console.log(copy); // [{ id: 1 }, { id: 2 }]Understanding Array.prototype.slice
The Array.prototype.slice method in JavaScript returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included). It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.
The method signature is array.slice(start?, end?). It accepts 2 parameters: start, end. When called, it returns a new array containing the extracted elements. Understanding when and how to use slice() helps you write more expressive, readable code.
Common use cases for Array.prototype.slice include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-splice, array-concat, string-slice, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Array.prototype.slice is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Array.prototype.spliceChanges the contents of an array by removing or replacing existing elements and/or adding new elements in place
Array.prototype.concatMerges two or more arrays into a new array without changing the existing arrays
String.prototype.sliceExtracts a section of a string and returns it as a new string, without modifying the original string
Array.prototype.atTakes an integer value and returns the item at that index, allowing for positive and negative integers where negative integers count back from the last item
More Array Methods
Other methods in the Array object
Related Tools
More Array Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.