The array slice() method in JavaScript extracts a shallow copy of a specific portion of an array into a new array object, without modifying the original array. It promotes immutability in functional programming patterns.
const stocks = ['Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra']; const sliced_stocks = stocks.slice(1, 3); console.log(sliced_stocks); // Output: ['Kaynes', 'VMM'] console.log(stocks) // Output: [ 'Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra' ]
In this code, we started slicing from index 1 to 3 (excluding). So, the returned array contains only two elements. It is ideal for selecting a range without altering the source.
Syntax
slice(start, end)
Parameters
| Argument | Description |
| start (Optional) | It represents the zero-based index at which to start extraction from an array.
By default, it is 0 if you omit this argument. Negative values count backward from the end of the array (e.g., -1 refers to the last element). |
| end (Optional) | It is a number representing the zero-based index at which to end extraction.
By default, it is an array.length if omitted. Negative values count backward from the end. |
Without passing “end” index
When you pass a single argument, it starts at index 2 and goes to the end (stocks.length). Useful for truncating prefixes.
const stocks = ['Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra']; const sliced_stocks = stocks.slice(2); console.log(sliced_stocks); // Output: ['VMM', 'V2', 'Yatra']
Negative indices
If you pass a negative index, it will start counting from the end of the array. The last index is -1.
If you pass -2, it resolves to stocks.length – 2 (index 2). So, it will start slicing from negative index 2 up to the end of the list.
const stocks = ['Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra']; const negative_sliced_stocks = stocks.slice(-2); console.log(negative_sliced_stocks); // Output: [ 'V2', 'Yatra' ] (Index from -2 to end)
In this code, we start extracting elements from -2, meaning the second-to-last element to the last element. And hence, we got the array of the last two elements.
Mixed positive and negative indices
You can pass the positive index “start” as the first argument and the negative index “end” as the second argument.
const stocks = ['Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra']; const mixed_sliced_stocks = stocks.slice(1, -2); console.log(mixed_sliced_stocks); // Output: [ 'Kaynes', 'VMM' ]
In this code, it starts at index 1, ends at stocks.length – 2 (index 3, excluding ‘V2’ and ‘Yatra’). Combines forward/backward indexing for flexible ranges.
Sparse arrays
The sparse array contains holes, and the slice() method preserves these holes (undefined/empty slots).
The sliced[0] and sliced[2] are empty, maintaining sparsity for consistent iteration.
const sparse_array = ['Netweb', '', 'VMM', '', 'Yatra']; const sparse_sliced = sparse_array.slice(1, 4); console.log(sparse_sliced); // Output: [ '', 'VMM', '' ]
Empty array
If the array is empty, the sliced array will also be empty because there are no elements to slice.
const empty_array = []; const empty_slice = empty_array.slice(1, 2); console.log(empty_slice); // Output: []
Copying the whole array
To create a shallow copy of the array, use the array.slice() method.
const arr = [1, 2, 3]; const copy = arr.slice(); console.log(copy); // Output: [1, 2, 3] console.log(copy === arr); // Output: false (different arrays)
That’s all!





vero
I like your tutorials!
But as someone who is only learning js, i’d like to point out that in this case is good to explain why the result is what it is.
let namepartner = [‘Pearson’, ‘Specter’, ‘Litt’];
let suits = namepartner.slice(1, 2);
console.log(suits);
Specter
slice method with parameters 1 and 2 starts at 1 and end at 2(2 not included).
so as arrays start count as 0, Pearson is ignored, Spencer with position 1 is the result and Litt with position 2 is also ignored.
also maybe better example would be bigger array.