Select elements:
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);
Try it Yourself »
Select elements using negative values:
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const myBest = fruits.slice(-3, -1);
Try it Yourself »
The slice()
method returns selected elements in an array, as a new array.
The slice()
method selects from a given start, up to a (not inclusive) given end.
The slice()
method does not change the original array.
array.slice(
start,
end)
Parameter | Description |
start | Optional. Start position. Default is 0. Negative numbers select from the end of the array. |
end | Optional. End position. Default is last element. Negative numbers select from the end of the array. |
A new array containing the selected elements. |
slice()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!