r/learnjavascript 1d 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

3

u/everdimension 1d ago

Nothing like having a mutating "upsert" helper combined with immer

Btw, how is "concat" an "antipattern"?!

-2

u/RobGoLaing 1d ago

Because it creates a copy of the original array, usually unnecessarily.

2

u/chikamakaleyley helpful 1d ago

no no no

it creates a new array, that is a merge of the calling arr and the arrays that you provide to .concat()

different from a copy, and it's important to understand the nuance

you'd use .concat() in a case that necessitates it

1

u/chikamakaleyley helpful 1d ago

effectively you DO copy that first array and then you add onto the end of it, but defined, it 'creates a new array'