Array .slice() / concat()
| Since: | ES3(ECMAScript 1999) |
|---|
Methods for extracting part of an array or combining multiple arrays into a new one. Neither method modifies the original array.
Syntax
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 members = ["item_a", "item_b", "item_c", "item_d", "item_e"]; // Extracts elements from index 1 up to (but not including) index 3. var sliced = members.slice(1, 3); console.log(sliced); // Outputs: "item_b,item_c" console.log(members); // The original array is unchanged. // A negative value counts from the end of the array. var last2 = members.slice(-2); console.log(last2); // Outputs: "item_d,item_e" var copy = members.slice(); console.log(copy); // Pattern 2: Combining arrays with concat var items1 = ["item_a", "item_b"]; var items2 = ["item_c", "item_d"]; var merged = items1.concat(items2, ["item_e"]); console.log(merged); console.log(items1); // The original array is unchanged. // Pattern 3: Practical use — pagination (splitting an array into pages) var items3 = ["item_a", "item_b", "item_c", "item_d", "item_e", "item_f", "item_g", "item_h"]; var pageSize = 3; var page1 = items3.slice(0, pageSize); var page2 = items3.slice(pageSize, pageSize * 2); console.log(page1); // Outputs: "item_a,item_b,item_c" console.log(page2); // Outputs: "item_d,item_e,item_f" var originals = ["item_a", "item_b", "item_c", "item_d"]; var backup = originals.slice(); // Save a copy first. originals.splice(1, 2); // Modify the original array. console.log(originals); // Outputs: "item_a,item_d" console.log(backup); // The backup is intact.
[ 'item_b', 'item_c' ] [ 'item_a', 'item_b', 'item_c', 'item_d', 'item_e' ] [ 'item_d', 'item_e' ] [ 'item_a', 'item_b', 'item_c', 'item_d', 'item_e' ] [ 'item_a', 'item_b', 'item_c', 'item_d', 'item_e' ] [ 'item_a', 'item_b' ] [ 'item_a', 'item_b', 'item_c' ] [ 'item_d', 'item_e', 'item_f' ] [ 'item_a', 'item_d' ] [ 'item_a', 'item_b', 'item_c', 'item_d' ]
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.