How to Append an Element in JavaScript Array
Javascript push() method appends elements in the Array. The push() method adds new elements to the end of an array and returns the new length. The push() method appends new item(s) at the end of the Array.
Javascript append an element to the array
To append any element in the Javascript array, use one of the following methods.
- Using the array.push() function.
- Use the Array.concat() method to add the elements of one array to another array.
- To insert at the first index, use the array.unshift() method.
Javascript array push()
Javascript array push() is an inbuilt method that appends an element at the end of an array. You can append single or multiple items in the Array using the push() method.
Syntax
array.push(element1, element2,..., elementn)
Parameter Values
The push() function takes item1, item2, …, itemX which is required. The element(s) to add to the Array.
Example
dark = ['jonas', 'martha', 'claudia'] dark.push('ulrich') console.log(dark)
Output
[ 'jonas', 'martha', 'claudia', 'ulrich' ]
You can see that the new element is appended at the end of the Array.
Append multiple elements in Javascript array
To append multiple elements in Javascript array, use the Array push() method.
dark = ['jonas', 'martha', 'claudia'] dark.push('ulrich', 'mikkel') console.log(dark)
Output
[ 'jonas', 'martha', 'claudia', 'ulrich', 'mikkel' ]
Append to Array using Javascript array concat()
Javascript array concat() is an inbuilt method that is used to join two or more arrays. The concat() method does not modify the existing arrays but returns a new array containing the values of the joined arrays.
Syntax
array1.concat(array2, array3, ..., arrayX)
Parameter Values
The concat() function takes single or multiple arrays as an argument that is required.
Example
dark1 = ['jonas', 'martha', 'claudia'] dark2 = ['ulrich', 'mikkel'] dark3 = ['helge', 'adam', 'noah'] cast = dark1.concat(dark2, dark3) console.log(cast)
Output
[ 'jonas', 'martha', 'claudia', 'ulrich', 'mikkel', 'helge', 'adam', 'noah' ]
You can see that the concat() function returns a new array containing the values of the joined arrays.
If you’re only appending the single variable, then array push() works just fine if you need to append another array, use array concat().
You can use push() and apply() the function to append two arrays as well.
dark1 = ['jonas', 'martha', 'claudia'] dark2 = ['ulrich', 'mikkel'] Array.prototype.push.apply(dark1, dark2) console.log(dark1)
Output
[ 'jonas', 'martha', 'claudia', 'ulrich', 'mikkel' ]
If you use more than two arrays, then it will operate on the first two arrays. The other arrays will be ignored. So if you only want to join two arrays, then use this method. Otherwise, go with the Array.concat() method.
With the new ES6 spread operator, merging two arrays using push becomes even more comfortable.
dark1 = ['jonas', 'martha', 'claudia'] dark2 = ['ulrich', 'mikkel'] dark1.push(...dark2) console.log(dark1)
Output
[ 'jonas', 'martha', 'claudia', 'ulrich', 'mikkel' ]
Append item at the start of an array in Javascript
To append an item at the start of the Array, use the javascript array unshift() method. Javascript unshift() method appends new items to the beginning of an array and returns the new length. The unshift() method changes the length of an array.
Syntax
array.unshift(item1, item2, ..., itemX)
Parameter values
The unshift() method accepts item1, item2, …, itemX, and it is required. The item(s) to add to the beginning of the Array.
Example
dark = ['jonas', 'martha', 'claudia'] dark.unshift('ulrich', 'mikkel') console.log(dark)
Output
[ 'ulrich', 'mikkel', 'jonas', 'martha', 'claudia' ]
You can see that two new items added at the start of the Array.