Array .slice() / concat()
Methods for extracting part of an array or combining multiple arrays into a new one. Neither method modifies the original array.
Syntax
// Extracts part of an array and returns it as a new array. var newArray = array.slice(startIndex, endIndex); // Combines multiple arrays or values into a new array. var newArray = array.concat(array2, array3, ...);
Method List
| Method | Description |
|---|---|
| slice(start, end) | Returns a new array containing the elements in the specified range. The element at the end index is not included. If arguments are omitted, a copy of the entire array is returned. |
| concat(value1, value2, ...) | Returns a new array formed by merging the original array with the specified arrays or values. Both arrays and individual values can be passed as arguments. |
Sample Code
var fruits = ["apple", "orange", "grape", "banana", "peach"]; // Extracts elements from index 1 up to (but not including) index 3. var sliced = fruits.slice(1, 3); console.log(sliced); // Outputs: "orange,grape" console.log(fruits); // Outputs: "apple,orange,grape,banana,peach" — the original array is unchanged. // A negative value counts from the end of the array. var last2 = fruits.slice(-2); console.log(last2); // Outputs: "banana,peach" // Omitting arguments copies the entire array. var copy = fruits.slice(); console.log(copy); // Outputs: "apple,orange,grape,banana,peach" // Combines arrays. var arr1 = [1, 2]; var arr2 = [3, 4]; var merged = arr1.concat(arr2, [5, 6]); console.log(merged); // Outputs: "1,2,3,4,5,6" // You can also append individual values directly. var added = arr1.concat(3, 4); console.log(added); // Outputs: "1,2,3,4"
Overview
Both array.slice() and array.concat() are non-destructive methods — they return a new array without modifying the original. They are useful when you want to work with array data safely.
array.slice() takes a start index as the first argument and an end index as the second. Note that the element at the end index is not included in the result. A negative value counts from the end of the array, so slice(-2) returns the last two elements. When called with no arguments, it creates a shallow copy of the array, making it a handy way to copy an array without affecting the original.
The similarly named array.splice() is a destructive method that modifies the original array directly. Use array.slice() when you only need to read elements, and array.splice() when you need to add, remove, or replace elements.
Browser Compatibility
3 or earlier ×
3 or earlier ×
Android Browser
37+ ○
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.