5 Ways to Add Items to Arrays in JavaScript

preview_player
Показать описание
In today's video, we'll take a look at 5 different ways to add items to an array using JavaScript. All of these techniques serve their own unique purpose and they're definitely worth learning.

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!

#dcode #javascript
Рекомендации по теме
Комментарии
Автор

The last method, you dont need to use spread operator again to insert it

const moreDevices = [...devices, "Mobile Phone", "Tablet"];

ban_droid
Автор

Hey! Great refresher as the others say! Is one faster or preferred than another? I'm slowly trying to understand best-best practices, started learning about memory leaks etc, and it's a whole new world. WeakSets, WeakMaps, Proxies, etc (Of course, I have to thank you about Proxies, your video was the only one I was comfortable with)

dimitrisqq
Автор

00:00 - array.push()
1:25 - array.unshift()
2:16 - array.splice()
4:18 - array.concat()
5:54 - spread syntax

installgentoo
Автор

Some of these array methods mutate the original array. Every time you mutate an array, a puppy dies. Please think of the puppies! Chrome has implemented some of the new array methods that are alternatives to splice, reverse, and sort. The new methods are called toReversed, toSpliced, and toSorted.

bmehder
Автор

Note that the last one creates a NEW array, it doesn't change the old one, you still have access to the old one.

Cahnisama