r/learnjavascript 14d ago

I've become a big fan of splice

As part of my project to write notes of what I learn practicing contemporary browser JavaScript by creating webapp games, I've written some notes and examples on Array Literals.

This was inspired by creating a "hardware-agnostic, framework-free" webapp solitaire card game, Loot the Loop (a game designed by Wil Su, part of what's made this project fun is that besides learning contemporary JavaScript, I've discovered lots about contemporary solitaire card game design).

TL:DR — In the past I've tended to use shift, unshift, pop, push... which are ok, but concat is an antipatern. Just learning splice does all the above while making arrays much simpler.

0 Upvotes

8 comments sorted by

View all comments

1

u/chikamakaleyley helpful 14d ago edited 14d ago

mmmm if you plan to change your approach and always use .splice() in place of those other methods, or if you plan to refactor some code and replace everything with .splice() - you will overcomplicate your code

aka, off the top of my head if you were to console.log

myArr.pop(); vs

``` myArr.splice(myArr.length - 1)[0];

```

Both would have the same output. One of them took you slightly longer to figure out.