Ads by ProfitSence

Slice vs Splice JavaScript Code Example

Last Updated on Wednesday 5th Oct 2022
  • splice insert Or RemoveItems inAnywhere Of Array
  • slice returns slicedItems from Anywhere Of Array

Splice Javascript

  • Add or Remove items in anywhere of an array.


					let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

  • Modifies the original array can 'add' or 'remove' items from anywhere in the Array
					const myArr = ['a', 'b', 'c', 'd', 'e', 'f'];
let deletedItems = myArr.splice(2, 3);
console.log(myArr); // ["a","b","f"]
console.log(deletedItems); // ["a","b","f"]


Slice Javascript


					const newSlicedArr = arr.slice([start[, end]])

  • returns a new sliced array.
  • It does NOT modify the original Array
					const myArr = ['a', 'b', 'c', 'd', 'e', 'f'];
const newSlicedArr = myArr.slice(2, 4);
console.log(newSlicedArr); // ["c","d"]