Recommended Free Tools
JavaScript’s slice() method returns a selected portion of an array or string without changing the original. It includes the item at the start index but stops before the end index; negative indexes count backward from the end.
How does JavaScript slice() work?
slice() takes an optional start index and an optional end index: value.slice(start, end). The start position is included, while the end position is excluded. If you omit end, the method selects through the end of the value.
For example, slicing from index 1 to index 4 selects the elements or characters at indexes 1, 2, and 3—not index 4. This end-exclusive rule makes the length of a selected range easy to calculate: subtract the start index from the end index.
Using slice() with arrays
For an array, slice() returns a new array containing the selected elements. It does not add, remove, or replace elements in the original array.
#1 Best Overall
const colors = ["red", "green", "blue", "purple"];
const middleColors = colors.slice(1, 3);
console.log(middleColors); // ["green", "blue"]
console.log(colors); // ["red", "green", "blue", "purple"]
Because the end index is excluded, slice(1, 3) includes indexes 1 and 2. Leave off the second argument to copy from the chosen start position to the array’s end:
const lastTwo = colors.slice(2);
console.log(lastTwo); // ["blue", "purple"]
Array slices are shallow copies
The returned array is new, but slicing does not recursively copy objects inside it. If an element is an object or another array, the original and sliced arrays refer to the same nested value. Changing a nested object through one array is therefore visible through the other; changing which elements the outer array contains does not change the other array.
Rank #2
Using slice() with strings
For a string, slice() returns a new string containing the selected characters. The source string remains unchanged.
const message = "JavaScript";
const part = message.slice(0, 4);
console.log(part); // "Java"
console.log(message); // "JavaScript"
As with arrays, omitting the end index takes the remaining characters from the start position onward:
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11const ending = message.slice(4);
console.log(ending); // "Script"
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.What do negative indexes mean?
A negative index counts backward from the end of the array or string: -1 refers to the last position, -2 to the one before it, and so on.
const numbers = [10, 20, 30, 40, 50];
console.log(numbers.slice(-2)); // [40, 50]
console.log(numbers.slice(1, -1)); // [20, 30, 40]
In the second example, the start index is 1 and the end index is the last position. Since the end is excluded, the result stops before 50.
Quick Recap
Best Value
Rank #4
When should you use slice()?
- Use array
slice()when you need a selected range or a separate outer array while preserving the original array. - Use string
slice()when you need a substring by position and want to keep the source string unchanged. - Use negative indexes when selecting relative to the end, such as the last few array elements or characters.
- Remember that an array slice is shallow: it does not create independent copies of nested objects.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

