Answers for "slicer notation javascript"

0

slicer notation javascript

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

console.log(animals.slice(2)); //start from index 2
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4)); //start from index 2 until index 4
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5)); //start from index 1 until index 5 (max index + 1 is acceptable)
// expected output: Array ["bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2)); //start at the second last value
// expected output: Array ["duck", "elephant"]

console.log(animals.slice(2, -1)); //go from index to until the last value
// expected output: Array ["camel", "duck"]
Posted by: Guest on August-13-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language