I was today’s years old when I learn that you can actually append an array to an array using the
push method instead of the
concat is nice but it creates a new array but what I wanted is to simply modify the original array, therefore, avoid allocating more memory.
array.push actually accepts multiple parameters so we can just use the spread operator to merge two arrays.
const names = ['foo', 'bar']; const otherNames = ['alpaca', 'llama', 'dog', 'cat']; names.push(...otherNames);
Why avoid allocation memory?
Featured image by Andrea Piacquadio.