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...
For further actions, you may consider blocking this person and/or reporting abuse
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? ๐