DEV Community

Cover image for 5 Way to Append Item to Array in JavaScript

5 Way to Append Item to Array in JavaScript

Samantha Ming on April 29, 2020

Here are 5 ways to add an item to the end of an array. push, splice, and length will mutate the original array. Whereas concat and spread will no...
Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan
const array = ['๐ŸฆŠ'];

array.push('๐Ÿด');
array.splice(array.length, 0, '๐Ÿด');
array[array.length] = '๐Ÿด';

// Result
// ['๐ŸฆŠ', '๐Ÿด']

What's the point of these two lines?

array.splice(array.length, 0, '๐Ÿด');
array[array.length] = '๐Ÿด';

Pretty sure array.push('๐Ÿด'); already gets the job done.

Collapse
 
luisaugusto profile image
Luis Augusto • Edited

I think it is just showing that there are three ways to make a mutative change to an array, each line does the same thing.

array.push('๐Ÿด') simply adds to the end of the array
array.splice(array.length, 0, '๐Ÿด') removes 0 items from the end of the array, and adds one item
array[array.length] = '๐Ÿด' adds an item to a specific position, which in this case is at the end of the array

Collapse
 
samanthaming profile image
Samantha Ming

yes, that's what I'm trying to do, thanks for clarifying Luis!

Aleksandr, I see how's that can be confusing...maybe i should fix it up ๐Ÿค”

Thread Thread
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

Ah, yep, I thought it was all being executed sequentially

Collapse
 
stojakovic99 profile image
Nikola Stojakoviฤ‡

Great article!

Something for beginners (I'm pretty sure you already knew about this):

One hacky way to add an empty item to an array (if we can even call it appending an item to an array, I think more appropriate term would be resizing) would be:

const array = [1, 2];

array.length = 3;

console.log(array); // [1, 2, <1 empty item>]

However you should never ever do this.

Collapse
 
samanthaming profile image
Samantha Ming

Yes! Good one, let me add it to my notes ๐Ÿ’ช Thanks for sharing ๐Ÿ˜€

Collapse
 
devhammed profile image
Hammed Oyedele

You can also use it to shrink array

const array = [1, 2]

array.length = 1

console.log(array) // [1]
Thread Thread
 
samanthaming profile image
Samantha Ming

WOOO! never thought of that! Adding to my notes!!! thanks for sharing ๐Ÿ˜

Collapse
 
johnkazer profile image
John Kazer

The point about not mutating if you need the array elsewhere is a good one, especially for parallel or concurrent programming. Even in single threaded JavaScript you might have passed an array by reference and so mutate things unexpectedly. Generally I find non-mutation to be a more relaxing method!

Collapse
 
samanthaming profile image
Samantha Ming

Yes, great point John! Let me add that to my notes! Thanks for sharing ๐Ÿ‘

Collapse
 
zuchers profile image
zuchers

great post, keep going

Collapse
 
samanthaming profile image
Samantha Ming

You bet! Thanks for the encouragement! ๐Ÿ™Œ

Collapse
 
delta456 profile image
Swastik Baranwal

Nice article!

Collapse
 
samanthaming profile image
Samantha Ming

Thanks! Glad you found it helpful ๐Ÿ™‚

Collapse
 
maniekm profile image
Mariusz

Great stuff, thanks for sharing!

Collapse
 
samanthaming profile image
Samantha Ming

You're welcome! Thanks for reading my article ๐Ÿ™‚

Collapse
 
rafaacioly profile image
Rafael Acioly

Great post :)

Every time i read a post with "X ways to do something" i remember when i started to learn Golang then i start laughing.

Collapse
 
samanthaming profile image
Samantha Ming

Do say more, what's Golang like? ๐Ÿ˜ƒ