Closed shivamrathi99 closed 3 years ago
Hi,
Just wanted to point out that you can pass any negative value for 'start' and for 'end'. Explicitly stating An index of (-1) points to the last element of the array. seems like some magic case where -1 is bound to the end of array.
Usage of negative arguments it's in fact described in Syntax section. However I agree with that it's good idea to mention that briefly in description (this one on top of the page, not in description section) and add example - it's the first thing that is visible on the page.
I suggest something like: 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. A negative index can be used, indicating an offset from the end of the sequence. The original array will not be modified.
Issue moved to mdn/content #1441 via ZenHub
URL(s)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
Request type
Details
This code: 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. const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2)); // expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4)); // expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5)); // expected output: Array ["bison", "camel", "duck", "elephant"]
can be written as : 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. An index of (-1) points to the last element of the array. const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; console.log(animals.slice(2)); // expected output: Array ["camel", "duck", "elephant"] console.log(animals.slice(2, 4)); // expected output: Array ["camel", "duck"] console.log(animals.slice(1, 5)); // expected output: Array ["bison", "camel", "duck", "elephant"] console.log(animals.slice(2, -1)); // expected output: Array ["camel", "duck"]
to improve understanding concepts.
thanks