📋 Table Of Content
In this short article, we will learn how to append an item to an array?
Method 1 : Using push() to Append Item in an Array.
push() method is use to add items to the end of an given array and return a new length of array. This method modifies the original Array.
You can pass more then one item in push() method. Example
const array = [1,2,3] array.push(4) console.log(array)
[ 1, 2, 3, 4 ]
Method 2 : Using Spread Operator to Append items in an Array
The spread operator (...) allows an iterable (like array) and expands it into individual items. This method do not modify the given array and will instead returns a new array.
var newArray = [...values];
const arr1 = [1,2,3] const arr2 = [4,5,6] const newArray = [...arr1, ...arr2] console.log(newArray)
[ 1, 2, 3, 4, 5, 6 ]