DEV Community

Cover image for Splice! Slice! Shoot, I meant Shift!

Splice! Slice! Shoot, I meant Shift!

Laurie on July 30, 2019

There is a bit of a joke in the land of JavaScript that arrays have one too many functions with scarily similar names. You may have seen this tweet...
Collapse
 
jjlabajo profile image
JJ Labajo • Edited

I had this experience when I was just beginning to understand javascript, at the same time I was exploring Vue. I got into situation where I assigned a variable to a spliced array, and I was so dumb wondering why does both arrays are changing even if I just manipulated one of them.

I was googling for answers but I wasn't able to see a solution - soooo I ended up using Json Stringify and Parse. When I assign the array to a variable, I, first, Stringify it and then Parse it (just to make a "shallow" copy). Yea, funny right? Months later I accidentally found out the solution of the problem while reading some article. That "months later" is today, and the "article" is yours. Thanks a lot. .slice() FTW! \m/

Collapse
 
laurieontech profile image
Laurie

So glad it helped! Sorry it came a little late 😅

Collapse
 
jjlabajo profile image
JJ Labajo

It's perfect. Thaaaaanks.

Collapse
 
nickymeuleman profile image
Nicky Meuleman

Always have to look up which one I want too!
Which one mutated again? .splice? .slice?

In a similar vein of mild frustration:
I hate the person that made .substring and .substr happen. They are similar, but not quite. But the name is SO similar.

Collapse
 
marcellothearcane profile image
marcellothearcane

What's the difference between .shift() and .pop()?

Collapse
 
laurieontech profile image
Laurie

Pop() removes an item from the end of the array and shift() removes an item from the beginning of it.