Javascript Array.Splice vs Array.slice
Bidhubhushan Gahan
1 min read
Array.slice()
slice() is a method of javascript array datatype that helps to get a subarray from an array. It doesn't change the original array.
Syntax
const arr = [1,2,3,4,5,6]
const slicedArr = arr.slice(1,4) //Here the first parameter represents the starting index and the 2nd parameter
//shows upto which the slice action should be taken
console.log(slicedArr)
if the end parameter is not specified then the total length of the array is considered.
Array.splice()
splice() changes, deletes, and can update the original array. It replaces the original array with a new array and returns it.
let arr = [
1,3,2,4,5,6,7,8,9
];
let returnedArr = arr.splice(1,4);
console.log(arr);
console.log(returnedArr);
if we want to delete and update the array then we should use arr.splice(start,0,delete,update) accordingly.
0
Subscribe to my newsletter
Read articles from Bidhubhushan Gahan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by