Sunday, November 28, 2021

Difference between splice() and slice() in javascript array

slice()- returns a shallow copy of a portion of given array .
ex:
var a=['j','u','r','g','e','n'];
console.log(a.slice(3,5));
output: ["g","e"]

splice() - returns the removed elements from an array . It effects the orginal array .
var a=['j','u','r','g','e','n'];
console.log(a.splice(3,5));
output:["g", "e", "n"]
orginal array:['j','u','r']

No comments:

Post a Comment

Abstract factory pattern

When single task can be done by multiple groups/family of objects and decision is taken at the runtime.