JavaScript Array Slice() Method - W3Schools

JavaScript Array slice() ❮ Previous JavaScript Array Reference Next

Examples

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 »

Description

The slice() method returns selected elements in 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.

See Also:

The Array splice() Method

The Array toSpliced() Method

Syntax

array.slice(start, end)

Parameters

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.

Return Value

A new array containing the selected elements.

Array Tutorials:

Array Tutorial

Array Const

Basic Array Methods

Array Search Methods

Array Sort Methods

Array Iteration Methods

Browser Support

slice() is an ECMAScript1 (JavaScript 1997) feature.

It is supported in all browsers:

Chrome Edge Firefox Safari Opera
Previous JavaScript Array Reference Next +1 Sign in to track progress

Từ khóa » Hàm Slice Trong Js