Copy part of a JavaScript array with slice() and negative indexes
array.slice(start, end) copies a range into a new array. It includes start and stops before end. Omit the end to copy through the last element, or use a negative index to count back from the end.
Include the start index and exclude the end
Array indexes start at zero. slice(1, 4) copies indexes 1, 2, and 3. If the normalized end is at or before the start, or the start is outside the upper bound, the result is empty. The input array keeps all of its elements. See Array.prototype.slice().
{
const items = ['One', 'Two', 'Three', 'Four', 'Five', 'Six'];
const selected = items.slice(1, 4);
console.log(selected); // ['Two', 'Three', 'Four']
console.log(items);
console.log(items.slice(3, 3)); // []
}
Omit the end or count from the back
-1 points to the last element and -2 points to the previous one. A negative end is still excluded. Clip indexes to the array’s bounds: slice(-100, 100) copies all six items. An omitted or undefined start means zero.
{
const items = ['One', 'Two', 'Three', 'Four', 'Five', 'Six'];
console.log(items.slice(2)); // ['Three', 'Four', 'Five', 'Six']
console.log(items.slice(-2)); // ['Five', 'Six']
console.log(items.slice(-4, -1)); // ['Three', 'Four', 'Five']
console.log(items.slice());
}
Compute array ranges for pages
For pages numbered from 1, calculate the zero-based start as (page - 1) * pageSize and the exclusive end as start + pageSize. Use a positive integer for the page number and page size. The last page can contain fewer items.
{
const items = ['A', 'B', 'C', 'D', 'E'];
const pageSize = 2;
function getPage(page) {
const start = (page - 1) * pageSize;
return items.slice(start, start + pageSize);
}
console.log(getPage(2)); // ['C', 'D']
console.log(getPage(3)); // ['E']
console.log(getPage(4)); // []
}
Page 2 returns ['C', 'D']. Page 3 returns ['E'], and page 4 returns []. There is no need to calculate a smaller end for a partial page.
Try a complete range-selection example
Save the HTML in UTF-8 as array-slice-example.html and open it. The initial range 1 to 4 returns Two, Three, and Four. With the end field blank, start 2 returns the last four items and start -2 returns the last two. A blank start means zero. Reset restores the fields, and the original count remains 6.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Select part of an array</title>
</head>
<body>
<label for="start">Start index</label>
<input id="start" type="number" step="1" value="1">
<label for="end">End index (exclusive, blank to copy through the end)</label>
<input id="end" type="number" step="1" value="4">
<button id="select" type="button">Select</button>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite"></pre>
<script>
const items = ['One', 'Two', 'Three', 'Four', 'Five', 'Six'];
const startInput = document.querySelector('#start');
const endInput = document.querySelector('#end');
const output = document.querySelector('#output');
function show() {
const start = Number(startInput.value);
const endText = endInput.value.trim();
const end = endText === '' ? undefined : Number(endText);
if (startInput.validity.badInput || endInput.validity.badInput ||
!Number.isInteger(start) || (end !== undefined && !Number.isInteger(end))) {
output.textContent = 'Enter integer indexes. A blank start means 0 and a blank end means the array length.';
return;
}
const selected = items.slice(start, end);
output.textContent = `Original: ${JSON.stringify(items)}\n` +
`Selection: ${JSON.stringify(selected)}\n` +
`Original length: ${items.length}`;
}
document.querySelector('#select').addEventListener('click', show);
document.querySelector('#reset').addEventListener('click', () => {
startInput.value = '1';
endInput.value = '4';
show();
});
show();
</script>
</body>
</html>
Understand shallow copies
slice() creates a shallow copy. It preserves references to objects, so editing an object’s property through the copy also affects the original object. Replacing the element in the copy changes only that array slot. Use splice() when you intend to insert or remove elements in the original.
{
const people = [{ name: 'Mina' }];
const copy = people.slice();
copy[0].name = 'Yuna';
console.log(people[0].name); // 'Yuna'
copy[0] = { name: 'Hana' };
console.log(people[0].name);
}




