slice() in JavaScript: taking a portion without changing the original

slice hands back a portion of an array or a string, between two indexes, without touching the starting value. The end index is excluded.
3 min read
Believemy logo

Showing the last three posts, cutting a text that runs too long, splitting a list into pages: every time, the job is to take a piece without damaging the whole.

slice does exactly that, and it behaves identically on an Array and on a String.


Definition

slice(start, end) hands back a new value made of the items sitting between the two indexes. The start index is included, the end index is excluded. The original value is never changed.

JAVASCRIPT
const week = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];

console.log(week.slice(0, 5));  // [ 'mon', 'tue', 'wed', 'thu', 'fri' ]
console.log(week.slice(5));     // [ 'sat', 'sun' ]
console.log(week.length);       // 7, nothing moved

const title = "Believemy";
console.log(title.slice(0, 6));  // "Believ"

Leaving out the second argument runs to the end. Leaving out both hands back a full copy, which is the most frequent sideways use of the method.


Negative indexes

A negative index counts from the end, which saves writing length - 3 everywhere.

JAVASCRIPT
const scores = [12, 15, 9, 18, 11];

console.log(scores.slice(-2));     // [ 18, 11 ], the last two
console.log(scores.slice(1, -1));  // [ 15, 9, 18 ], without first or last
console.log("2026-08-20".slice(-2));  // "20", the day
Good to know

A quiet trap: slice(-0) is the same as slice(0) and therefore hands back everything. If the number of items to take comes from a variable that can be zero, test it first, or the full list appears where nothing was meant to.


Copying an array, and how far the copy goes

slice() with no argument produces a shallow copy: a new array, holding the very same objects.

JAVASCRIPT
const original = [1, 2, 3];
const copy = original.slice();
copy.push(4);
console.log(original.length);  // 3, the copy is independent

const objects = [{ seen: false }];
const objectsCopy = objects.slice();
objectsCopy[0].seen = true;
console.log(objects[0].seen);  // true, it is the same object

Adding or removing items in the copy leaves the original alone, but changing a property on an object shows up on both sides. That is the Reference vs value distinction, and it explains most of the state that changes without anyone understanding why.


Frequently asked questions

Question

Why is the end index excluded?

Because the subtraction then gives the item count directly: slice(0, 5) hands back five of them. It also allows chaining slices with no overlap and no gap, slice(0, 10) then slice(10, 20), which is exactly how pagination is computed.


Question

slice or Array.splice()?

The two names look alike, the behaviors are opposite. slice leaves the array intact and hands back the portion asked for, Array.splice() changes the array and hands back what was taken out of it. When in doubt, remember that slice is the one you can call with no consequence.


Question

How do you cut a text cleanly at one hundred characters?

text.slice(0, 100) cuts to the exact character, often in the middle of a word. To cut at the last space, look for it first: const short = text.slice(0, 100); short.slice(0, short.lastIndexOf(" ")) + "...". And check the length beforehand, or short texts get an ellipsis they never earned.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.