📋

slice()

🎉
💨
Time to make copies! slice() is like a photocopier for arrays!

The slice() Method

The slice() method creates a copy of a portion of an array.

The original array stays the same! It's like taking a photo of some train cars.

💡
💨

How slice() Works

1const letters = ["A", "B", "C", "D", "E"];
2
3// slice(start, end) - end is NOT included!
4letters.slice(1, 3); // ["B", "C"]
5letters.slice(2); // ["C", "D", "E"]
6letters.slice(0, 2); // ["A", "B"]
7
8// Original is unchanged!
9console.log(letters); // ["A", "B", "C", "D", "E"]
10
11// Negative numbers count from the end
12letters.slice(-2); // ["D", "E"]
🤔
💨

Remember: End is NOT Included!

slice(1, 3) gives you items at index 1 and 2, but NOT index 3.

slice(start, end) → from start up to but not including end

🎮

Try It Yourself!

Click different slice() examples to see what portion gets copied!

Original Array (unchanged)

[
A
[0]
B
[1]
C
[2]
D
[3]
E
[4]
]
length: 5
📋