// initialize array var arr = [ "Hi", "Hello", "Bonjour" ]; // append new value to the array arr.push("Hola"); console.log(arr); [ "Hi", "Hello", "Bonjour", "Hola" ]
It must be noted that using the same Array.push() method, you can also append multiple values into an array at the same time. To append multiple items to an array, you can use push() by calling it with multiple arguments. Here is an example showing this in code.
const fruits = ['banana', 'pear', 'apple'] fruits.push('mango', 'melon', 'avocado')
If there is a case where you want to add the items of one array to another array, you can use Array.concat() method. In this way, you are appending a whole array into the original array. here is an example showing this in code.
var arr = [ "apple", "banana", "cherry" ]; arr = arr.concat([ "dragonfruit", "elderberry", "fig" ]); console.log(arr);
If you want to append an item or value into the start of an array, then you can use the Array.unshift() method. The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
var arr = [1, 2, 3]; arr.unshift(0); console.log(arr);