Kid
Kid

Reputation: 2215

Javascript array slice basic explanation needed

I learn this Array prototype slice

This is what I tried:

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(1, 2));
//outputs: ["bison"]

Why is this not outputting ["bison", "camel"] since the doc state it's a zero because operation?

enter image description here

Upvotes: 0

Views: 43

Answers (2)

Belhadjer Samir
Belhadjer Samir

Reputation: 1669

the end is not included more info

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

Upvotes: 1

AdriSolid
AdriSolid

Reputation: 2825

The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

More info here.

Upvotes: 1

Related Questions