You can append new values to a JS array using the function Array.push
. It adds new values to the end of the
JavaScript array.
Here’s how you can use push
to append a new value to an array.
const arr = [1, 2, 3];
arr.push(4);
console.log(arr); // [ 1, 2, 3, 4 ]
You can add more than one value with a single call to push
.
const arr = [1, 2, 3];
arr.push(4, 5, 6);
console.log(arr); // [ 1, 2, 3, 4, 5, 6]