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 not and will instead return a new array. Which is the best depends on your use case ๐
Mutative
This will change the original array
const array = ['๐ฆ'];
array.push('๐ด');
array.splice(array.length, 0, '๐ด');
array[array.length] = '๐ด';
// Result
// ['๐ฆ', '๐ด']
Non Mutative
This will create a new array and the original array remains unchanged.
const original = ['๐ฆ'];
let newArray;
newArray = original.concat('๐ฆ');
newArray = [...original, '๐ฆ'];
// Result
newArray; // ['๐ฆ', '๐ฆ']
original; // ['๐ฆ']
3 Ways to Append Item to Array (Mutative)
Let's look at the 3 ways we can push an item to an array. This will be the mutative way, meaning it will change the original array.
push
The simplest way to add items to the end of an array is to use push
.
const zoo = ['๐ฆ', '๐ฎ'];
zoo.push('๐ง');
console.log(zoo); // ['๐ฆ', '๐ฎ', '๐ง']
Notice I said item*s* and not just item ๐ Yup, you can push multiple items.
const zoo = ['๐ฆ', '๐ฎ'];
zoo.push('๐ง', '๐ฆ', '๐ค');
console.log(zoo); // ['๐ฆ', '๐ฎ', '๐ง', '๐ฆ', '๐ค']
But passing individual item is such a drag, luckily we can use the ES6's spread feature. This allows us to pass an array and then use the ...
syntax to spread the item into individual arguments ๐
const zoo = ['๐ฆ', '๐ฎ'];
const birds = ['๐ง', '๐ฆ', '๐ค'];
zoo.push(...birds);
console.log(zoo); // ['๐ฆ', '๐ฎ', '๐ง', '๐ฆ', '๐ค']
splice
At first glance, this method can seem super needy ๐ because we're passing a bunch of arguments. That's because this method can add OR remove array items. Hence it requires us to give it a bit more info for it to do it's job. Let's look at the parameters it require
Parameters | Parameter Name | Definition |
---|---|---|
1 | startIndex | The index where you want to add/remove item |
2 | deleteCount | The number of items you want to remove |
3 | items | The number you want to add (If you're removing, you can just leave this blank) |
const zoo = ['๐ฆ', '๐ฎ'];
zoo.splice(
zoo.length, // We want add at the END of our array
0, // We do NOT want to remove any item
'๐ง', '๐ฆ', '๐ค', // These are the items we want to add
);
console.log(zoo); // ['๐ฆ', '๐ฎ', '๐ง', '๐ฆ', '๐ค']
length
I think this is the most clever way of all the methods. It's one of the ways that never crossed my mind. So let's understand why this works.
Arrays in JavaScript are zero-index. So the first item has an index of 0
.
const zoo = ['๐ฆ', '๐ฎ'];
// '๐ฆ' | Index = 0
// '๐ฎ' | Index = 1
The bracket notation in arrays allows us to retrieve the item BUT it can also let us override that item.
const zoo = ['๐ฆ', '๐ฎ'];
// Retrieve
zoo[0]; // Returns '๐ฆ'
// Override
zoo[1] = '๐ฅฉ';
console.log(zoo); // ['๐ฆ', '๐ฅฉ']; **
// ** JUST JOKING ๐
// ...I promise no animals were hurt in this blog post ๐ฌ
The interesting thing is array.length
returns us the total count of items in the array. That means the length is always one number higher than the last item of our index. So by assigning a value at the length index, it's essentially adding an item to the end of the array.
const zoo = ['๐ฆ', '๐ฎ'];
const length = zoo.length; // 2
zoo[length] = '๐ฏ';
console.log(zoo); // ['๐ฆ', '๐ฎ', '๐ฏ']
2 Ways to Append Item to Array (Non Mutative)
Alright, let's move on to appending an item to an array in a non-mutative way. Where the original array will remain un-touched and a new array will contain the addition.
concat
This method is meant to merge arrays. So we can use it to add multiple items by passing in an array.
const ocean = ['๐', '๐ฆ'];
const fish = ['๐ ', '๐']; // Array of multiple items
const aquarium = ocean.concat(fish);
aquarium; // ['๐', '๐ฆ', '๐ ', '๐']
// Original Array Not Affected
ocean; // ['๐', '๐ฆ']
But it doesn't just accept arrays as its parameter, it also accept value(s).
const ocean = ['๐', '๐ฆ'];
const aquarium = ocean.concat('๐ก'); // Add a single value
const sushi = ocean.concat('๐ก', '๐'); // Add multiple values
aquarium; // ['๐', '๐ฆ', '๐ก']
sushi; // ['๐', '๐ฆ', '๐ก', '๐']
// Original Array Not Affected
ocean; // ['๐', '๐ฆ']
spread
We can use the spread syntax to expand each array element into individual elements. A very popular application is to use spread to create a copy or merge two separate arrays. This is similar to the effects of concat
.
const ocean = ['๐', '๐ฆ'];
const fish = ['๐ ', '๐'];
const aquarium = [...ocean, ...fish];
aquarium; // ['๐', '๐ฆ', '๐ ', '๐']
// Original Array Not Affected
ocean; // ['๐', '๐ฆ']
However, if we didn't use spread, we will instead get a nested array, which is not what we want.
const ocean = ['๐', '๐ฆ'];
const fish = ['๐ ', '๐'];
const aquarium = [ocean, fish];
// [ ['๐', '๐ฆ'], ['๐ ', '๐'] ]
So I can use it to merge arrays, but we can also pass the individual value(s), just like we do in creating a regular array.
const ocean = ['๐', '๐ฆ'];
const aquarium = [...ocean, '๐ก']; // Add a single value
const sushi = [...ocean, '๐ก', '๐']; // Add multiple values
aquarium; // ['๐', '๐ฆ', '๐ก']
sushi; // ['๐', '๐ฆ', '๐ก', '๐']
// Original Array Not Affected
ocean; // ['๐', '๐ฆ']
To Mutate Or Not to Mutate?
So the question is, to mutate or not to mutate ๐ญ Well that really depends on your use case. When you're working in Redux or any state management architecture, then it's all about the immutability. So the non-mutative methods will be your choices. Also, the idea of immutability is often preferred as it's considered a good practice to avoid side effects -- which is the foundation of functional programming and producing pure functions.
But does that mean we should never use mutative methods? Not at all. Because there are times when immutability just doesn't matter. Take this simple example.
function menu(isFriday) {
const food = ['๐', '๐ณ'];
isFriday ? food.push('๐ท') : food;
return food;
}
In these cases, why not use the mutative methods. My go-to for appending a value is push
. Why? Because it's less typing (yes, I'm very lazy ๐) and super readable. Yes, you can argue that concat
is also very short to type. But check out this performance test. Push is lot faster! โก๏ธ
Community Input
@DigianPaul: To Mutate or Not to Mutate? In general it is a very deep question. But, to simplify, suppose the original array is still needed somewhere else? Then you don't want to mutate it. If it is not needed, you can mutate it directly, which is usually faster than creating a copy.
Said there are data structures where creating a copied of the array is as cheap as mutate it (or comparable cheap) and those are very cool but not so widespread in the JavaScript community.
Those are called "Persistent data structures" and are extremely useful in a lot of cases. But they are quite complex to design.
They make simple to implement functionality like undo-redo for instance. But they really shine in functional programming and also in multithread applications.
@KClarkADSTech: Also you can prepend by using [ newEl, ...array]
or array.unshift(newEl)
.
@stojakovic99: 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: (However you should never ever do this.)
const array = [1, 2];
array.length = 3;
console.log(array); // [1, 2, <1 empty item>]
@devhammed: You can also use it to shrink array
const array = [1, 2];
array.length = 1;
console.log(array); // [1]
@johnkazer: 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!
Resources
- MDN Web Docs: push
- MDN Web Docs: concat
- MDN Web Docs: splice
- MDN Web Docs: spread
- MDN Web Docs: array.length
- Stack Overflow: How to append something to an array?
- How to append an item to an array in JavaScript
- JavaScript Array Methods: Mutating vs. Non-Mutating
- To mutate, or not to mutate, in JavaScript
- Array Add
- JavaScript "Add to Array" Functions (push vs unshift vs others)
- push is 945x than concat
- Originally published atย www.samanthaming.com
Thanks for reading โค
Say Hello! Instagram | Twitter | SamanthaMing.com
Top comments (18)
What's the point of these two lines?
Pretty sure
array.push('๐ด');
already gets the job done.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 arrayarray.splice(array.length, 0, '๐ด')
removes 0 items from the end of the array, and adds one itemarray[array.length] = '๐ด'
adds an item to a specific position, which in this case is at the end of the arrayyes, 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 ๐ค
Ah, yep, I thought it was all being executed sequentially
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:
However you should never ever do this.
Yes! Good one, let me add it to my notes ๐ช Thanks for sharing ๐
You can also use it to shrink array
WOOO! never thought of that! Adding to my notes!!! thanks for sharing ๐
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!
Yes, great point John! Let me add that to my notes! Thanks for sharing ๐
great post, keep going
You bet! Thanks for the encouragement! ๐
Nice article!
Thanks! Glad you found it helpful ๐
Great stuff, thanks for sharing!
You're welcome! Thanks for reading my article ๐
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.
Do say more, what's Golang like? ๐