Javascript Useful Tips

How to use .slice() method

array slice() method take 2 parameters start and end it return shallow copy of array 

const arr = ["react", "nodejs", "angular", "typescript", "php", "java"];

//To get all elements from array index 2
console.log(arr.slice(2));

//To get all elements from array index 2 to 4

console.log(arr.slice(2, 4));

//To get last element of array

console.log(arr.slice(-1));

//To get last 2 elements of array

console.log(arr.slice(-2));