This title could have been βhow not to get confused between JavaScriptβs splice and slice,β because I can never remember the difference between the two.
So I am hoping this trick will help both me and you in the future:
S (p) lice = Slice + (p) => Slice + in (p) lace
Array.prototype.slice() is used to slice an array from the start
point to the end
point, excluding the end
.
As the name suggests, it is used to slice elements out of an array. But unlike slicing a cake, slicing an array does not cut the actual array, but keeps it unmodified (infinite cake!).
arr.slice(start, [end])
Rules
end
is omitted, end becomes the end (last element) of the array.start
is -ve, the elements are counted from the end.const infiniteCake = ['π°','π°','π°','π°','π°','π°']
let myPieceOfCake = infiniteCake.slice(0) // ['π°']
let yourDoublePieceOfCake = infiniteCake.slice(0,2) // (2) ["π°", "π°"]
console.log(infiniteCake) //['π°','π°','π°','π°','π°','π°']
#javascript #developer