To append items to an array in JavaScript, the most common and efficient methods are:
push() Method
Adds one or more elements to the end of an array and returns the new length. It modifies the original array.
let arr = ['first', 'second'];
arr.push('third'); // ['first', 'second', 'third']
arr.push('fourth', 'fifth'); // ['first', 'second', 'third', 'fourth', 'fifth']To append another array, use the spread operator (...) with push():
let arr1 = ['first', 'second'];
let arr2 = ['third', 'fourth'];
arr1.push(...arr2); // ['first', 'second', 'third', 'fourth']⚠️ Important: Do not assign the return value of
push()back to the array (e.g.,arr = arr.push(...)), as it will overwrite the array with the new length (a number).
Spread Operator (...)
Creates a new array without modifying the original, enabling immutable updates.
let arr = ['first', 'second'];
let newArr = [...arr, 'third']; // ['first', 'second', 'third']This is ideal when you want to preserve the original array.
concat() Method
Combines arrays and returns a new array without modifying the originals.
let arr1 = ['first', 'second'];
let arr2 = ['third', 'fourth'];
let combined = arr1.concat(arr2); // ['first', 'second', 'third', 'fourth']unshift() Method
Adds elements to the beginning of an array (modifies original).
let arr = ['second', 'third'];
arr.unshift('first'); // ['first', 'second', 'third']splice() Method
Inserts elements at a specific index (useful for inserting in the middle).
let arr = ['first', 'third'];
arr.splice(1, 0, 'second'); // ['first', 'second', 'third']Summary:
Use
push(...)for appending to the end (modifies original).Use
[...arr, item]for immutable appending.Use
concat()for merging arrays without mutation.Use
splice()for inserting at specific positions.
Use the Array.prototype.push method to append values to the end of an array:
// initialize array
var arr = [
"Hi",
"Hello",
"Bonjour"
];
// append new value to the array
arr.push("Hola");
console.log(arr);
You can use the push() function to append more than one value to an array in a single call:
// initialize array
var arr = ["Hi", "Hello", "Bonjour", "Hola"];
// append multiple values to the array
arr.push("Salut", "Hey");
// display all values
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Note that the push() method returns the updated length of the array.
Update
If you want to add the items of one array to another array, you can use firstArray.concat(secondArray):
var arr = [
"apple",
"banana",
"cherry"
];
// Do not forget to assign the result as, unlike push, concat does not change the existing array
arr = arr.concat([
"dragonfruit",
"elderberry",
"fig"
]);
console.log(arr);
Update
Just an addition to this answer if you want to prepend any value to the start of an array (i.e. first index) then you can use Array.prototype.unshift for this purpose.
var arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
It also supports appending multiple values at once just like push.
Update
Another way with ES6 syntax is to return a new array with the spread syntax. This leaves the original array unchanged, but returns a new array with new items appended or prepended, compliant with the spirit of functional programming.
const arr1 = [
"Hi",
"Hello",
"Bonjour",
];
const arr2 = [
"Ciao",
"Hej",
"Merhaba",
];
const newArr1 = [
...arr1,
"Salut",
];
const newArr2 = [
"Salut",
...arr2,
];
const newArr3 = [
...arr1,
...arr2,
];
console.log(newArr1, newArr2, newArr3);
javascript - How to append something to an array? - Stack Overflow
Three ways to append an item to an array (Mutative)
How do I append to an array inside a json file in node?
Check checkbox check state and append to array
Videos
Use the Array.prototype.push method to append values to the end of an array:
// initialize array
var arr = [
"Hi",
"Hello",
"Bonjour"
];
// append new value to the array
arr.push("Hola");
console.log(arr);
You can use the push() function to append more than one value to an array in a single call:
// initialize array
var arr = ["Hi", "Hello", "Bonjour", "Hola"];
// append multiple values to the array
arr.push("Salut", "Hey");
// display all values
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Note that the push() method returns the updated length of the array.
Update
If you want to add the items of one array to another array, you can use firstArray.concat(secondArray):
var arr = [
"apple",
"banana",
"cherry"
];
// Do not forget to assign the result as, unlike push, concat does not change the existing array
arr = arr.concat([
"dragonfruit",
"elderberry",
"fig"
]);
console.log(arr);
Update
Just an addition to this answer if you want to prepend any value to the start of an array (i.e. first index) then you can use Array.prototype.unshift for this purpose.
var arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
It also supports appending multiple values at once just like push.
Update
Another way with ES6 syntax is to return a new array with the spread syntax. This leaves the original array unchanged, but returns a new array with new items appended or prepended, compliant with the spirit of functional programming.
const arr1 = [
"Hi",
"Hello",
"Bonjour",
];
const arr2 = [
"Ciao",
"Hej",
"Merhaba",
];
const newArr1 = [
...arr1,
"Salut",
];
const newArr2 = [
"Salut",
...arr2,
];
const newArr3 = [
...arr1,
...arr2,
];
console.log(newArr1, newArr2, newArr3);
If you're only appending a single variable, then push() works just fine. If you need to append another array, use concat():
var ar1 = [1, 2, 3];
var ar2 = [4, 5, 6];
var ar3 = ar1.concat(ar2);
alert(ar1);
alert(ar2);
alert(ar3);
The concat does not affect ar1 and ar2 unless reassigned, for example:
var ar1 = [1, 2, 3];
var ar2 = [4, 5, 6];
ar1 = ar1.concat(ar2);
alert(ar1);
There is a lot of great information on JavaScript Reference.
» npm install multer