JavaScript Array Slice Elements

September 27, 2020
const items = [1, 2, 3, 4, 5]

First N elements

items.slice(0, 2) // [1, 2]

Last N elements

items.slice(-2) // [4, 5]

From Nth element onwards

items.slice(2)  // [3, 4, 5]

From Nth to Yth elements

items.slice(2, 4) // [3, 4]
items.slice(2, 3) // [3]
items.slice(2, 2) // []
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.