How to append an element in an array in JavaScript ?
Last Updated :
29 Nov, 2023
In this article, we will see how to append an element in an array in JavaScript. There are several methods for adding new elements to a JavaScript array.
These are the following ways to solve the problem:
This method will add an element to the end of an array, while its twin function, the pop() method, will remove an element from the end of the array. If you need to add an element or multiple elements to the end of an array, the push() method will almost always be your simplest and quickest option.
Syntax:
array.push(item1, item2, ..., itemX)
Example: In this example, we will be adding new elements to the end of the array using the push() method.
javascript
let Geeks = [ "Geeks1" , "Geeks2" , "Geeks3" , "Geeks4" ];
console.log( "Original Array: " + Geeks);
Geeks.push( "Geeks5" , "Geeks6" );
console.log( "Updated Array: " + Geeks);
|
Output
Original Array: Geeks1,Geeks2,Geeks3,Geeks4
Updated Array: Geeks1,Geeks2,Geeks3,Geeks4,Geeks5,Geeks6
This method will add an element to the beginning of an array, while its twin function, shift(), will remove one element from the beginning of the array.
Syntax:
array.unshift(item1, item2, ..., itemX)
Example: In this example, we will be adding new elements to the beginning of the array using the unshift() method.
javascript
let Geeks = [ "Geeks1" , "Geeks2" , "Geeks3" , "Geeks4" ];
console.log( "Original Array: " + Geeks);
Geeks.unshift( "Geeks5" , "Geeks6" );
console.log( "Updated Array: " + Geeks);
|
Output
Original Array: Geeks1,Geeks2,Geeks3,Geeks4
Updated Array: Geeks5,Geeks6,Geeks1,Geeks2,Geeks3,Geeks4
This method modifies the content of an array by removing existing elements and/or adding new elements.
Syntax:
array.splice(index, amount, item1, ....., itemX)
Example: In this example, we will be adding new elements at the 3rd index of the array using the splice() method.
Javascript
let Geeks = [ "Geeks1" , "Geeks2" , "Geeks3" , "Geeks4" ];
console.log( "Original Array: " + Geeks);
Geeks.splice(2, 1, "Geeks5" , "Geeks6" );
console.log( "Updated Array: " + Geeks);
|
Output
Original Array: Geeks1,Geeks2,Geeks3,Geeks4
Updated Array: Geeks1,Geeks2,Geeks5,Geeks6,Geeks4
This method returns a new combined array comprised of the array on which it is called, joined with the array (or arrays) from its argument. This method is used to join two or more arrays and this method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.
Syntax:
array1.concat(array2, array3, ..., arrayX)
Example: In this example, we will merge three arrays together using the concat() method of JavaScript.
Javascript
let g1 = [ "Geeks1" , "Geeks2" ];
let g2 = [ "Geeks3" , "Geeks4" ];
let g3 = [ "GeeksForGeeks" ];
let g4 = g1.concat(g2, g3);
console.log(g4);
|
Output
[ 'Geeks1', 'Geeks2', 'Geeks3', 'Geeks4', 'GeeksForGeeks' ]
The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array.
Syntax:
let variablename1 = [...value];
Example: This example shows the implementation of above-explained appraoch.
Javascript
let Array1 = [2, 4, 6, 8];
let Array2 = [3, 5, 7]
newArray = [ ...Array1, ...Array2]
console.log(newArray);
|
Output
[
2, 4, 6, 8,
3, 5, 7
]
more than 1 value is expected. It allows us to merge values into the array.
Syntax:
_.concat(array, [values]);
Example: This example shows the implementation of above-explained appraoch.
Javascript
let lodash = require( "lodash" );
let array = [1, 2, 3];
let values = [0, 5, "a" , "b" ]
let newArray = lodash.concat(array, values);
console.log( "Before concat: " + array);
console.log( "After concat: " + newArray);
|
Output:
Before concat: 1,2,3
After concat: 1,2,3,0,5,a,b
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...