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

Clean code chapter 3(Robert C.martin)

  Summary: --------  1. Functions should hardly ever be 20 lines long.  2.Keep blocks (inside if, else, while, for, etc.) short.  Ideally, j...