JavaScript Array slice()
- Previous Page shift()
- Next Page some()
- Go to the Previous Level JavaScript Array Reference Manual
Definition and usage
slice()
The method returns a new array object with the selected elements.
slice()
The method selects from the given start Starts at the element at the given end Ends at the parameter but does not include.
Note:slice()
The method does not change the original array.
Instance
Example 1
Select elements from the array:
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(1, 3);
Example 2
Select elements using negative values:
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var myBest = fruits.slice(-3, -1);
Syntax
array.slice(start, end)
Parameter value
Parameter | Description |
---|---|
start |
Optional. An integer specifying where to start the selection (the index of the first element is 0). Use negative numbers to select from the end of the array. If omitted, it is similar to "0". |
end |
Optional. An integer specifying the position to end the selection. If omitted, it will select all elements from the beginning to the end of the array. Use negative numbers to select from the end of the array. |
Technical details
Return value: | A new array containing the selected elements. |
---|---|
JavaScript version: | ECMAScript 1 |
Browser support
All browsers fully support slice()
Method:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
Related Pages
Tutorial:JavaScript Array
Tutorial:JavaScript Array Const
Tutorial:JavaScript Array Methods
Tutorial:JavaScript Sort Array
Tutorial:JavaScript Array Iteration
- Previous Page shift()
- Next Page some()
- Go to the Previous Level JavaScript Array Reference Manual