How to append an element in an array in JavaScript?
There are several methods for adding new elements to a JavaScript array.
- push(): The push() 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)
Parameter: item1, item2, …, itemX: These are required parameters. The item(s) to the end of the array.
Example:
<!DOCTYPE html><html><body><center><h1>GeeksForGeeks</h1><button onclick="GFG()">Click</button><p id="geeks"></p><script>varGeeks = ["Geeks1","Geeks2","Geeks3","Geeks4"];document.getElementById("geeks").innerHTML = Geeks;functionGFG() {Geeks.push("Geeks5","Geeks6");document.getElementById("geeks").innerHTML = Geeks;}</script></center></body></html>chevron_rightfilter_noneOutput:
- Before Click on the Button:
- After Click on the Button:
- Before Click on the Button:
- unshift(): The unshift() 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)
Parameter: item1, item2, …, itemX: These are required parameters. The item(s) to add to the beginning of the array.
Example:
<!DOCTYPE html><html><body><center><h1>GeeksForGeeks</h1><button onclick="GFG()">Click</button><p id="geeks"></p><script>varGeeks = ["Geeks1","Geeks2","Geeks3","Geeks4"];document.getElementById("geeks").innerHTML = Geeks;functionGFG() {Geeks.unshift("Geeks5","Geeks6");document.getElementById("geeks").innerHTML = Geeks;}</script></center></body></html>chevron_rightfilter_noneOutput:
- Before Click on the Button:
- After Click on the Button:
- Before Click on the Button:
- splice(): The splice() method modifies the content of an array by removing existing elements and/or adding new elements.
Syntax:
array.splice(index, howmany, item1, ....., itemX)
Parameter:
- index: This is required parameter. An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array
- howmany: This is Optional parameter. The number of items to be removed. If set to 0, no items will be removed.
- item1, item2, …, itemX: These are Optional parameters. The new item(s) to be added to the array.
Example:
<!DOCTYPE html><html><body><center><h1>GeeksForGeeks</h1><button onclick="GFG()">Click</button><p id="geeks"></p><script>varGeeks = ["Geeks1","Geeks2","Geeks3","Geeks4"];document.getElementById("geeks").innerHTML = Geeks;functionGFG() {Geeks.splice(2, 1,"Geeks5","Geeks6");document.getElementById("geeks").innerHTML = Geeks;}</script></center></body></html>chevron_rightfilter_noneOutput:
- Before Click on the Button:
- After Click on the Button:
- concat(): The concat() 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)
Parameter:
- array2, array3, …, arrayX: These are required parameters. The arrays to be joined.
Example:
<!DOCTYPE html><html><body><center><h1>GeeksForGeeks</h1><button onclick="GFG()">Click</button><p id="geeks"></p><script>functionGFG() {varg1 = ["Geeks1","Geeks2"];varg2 = ["Geeks3","Geeks4"];varg3 = ["GeeksForGeeks"];varg4 = g1.concat(g2, g3);document.getElementById("geeks").innerHTML = g4;}</script></center></body></html>chevron_rightfilter_noneOutput:
- Before Click on the Button:
- After Click on the Button:
Recommended Posts:
- How to append data to <div> element using JavaScript ?
- How to append HTML code to a div using JavaScript ?
- Implement prepend and append with regular JavaScript
- Find the min/max element of an Array using JavaScript
- How to remove an element from an array in JavaScript?
- How to get current formatted date dd/mm/yyyy in Javascript and append it to an input?
- How to select a random element from array in JavaScript ?
- PHP append one array to another
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to set the value of a select box element using JavaScript?
- How to get the type of DOM element using JavaScript?
- How to get the data attributes of an element using JavaScript ?
- How to get font properties of particular element in JavaScript ?
- How to remove an HTML element using JavaScript ?
- How to check if an element has any children in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



