JavaScript | Add new elements at the beginning of an array
Adding new elements at the beginning of existing array can be done by using Array unshift() method. This method is similar to push() method but it adds element at the beginning of the array.
Syntax:
ArrayObject.unshift( arrayElement );
- ArrayObject: It is the array, where to insert the element.
- arrayElement: It is the element, which is to be inserted.
Example: This example inserts the element 6 at the beginning of array GFG_Array.
<!DOCTYPE html> <html> <head> <title> JavaScript | Insert at beginning </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id="up"></p> <button onclick="myGFG()"> Click to insert </button> <p id="down" style="color: green"></p> <!-- Script to add element at beginning of array --> <script> var GFG_Array = [1, 2, 3, 4, 5]; var up = document.getElementById("up"); up.innerHTML = GFG_Array; var down = document.getElementById("down"); down.innerHTML = "elements of GFG_Array = " + GFG_Array; function myGFG() { GFG_Array.unshift("6"); down = document.getElementById("down"); down.innerHTML = "element of GFG_Array = " + GFG_Array; } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking the button:

- After clicking the button:

Example: This example inserts the element GFG_7 at the beginning of the GFG_Array.
<!DOCTYPE html> <html> <head> <title> JavaScript | Insert at beginning </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id="up"></p> <button onclick="myGFG()"> Click to insert </button> <p id="down" style="color: green"></p> <script> var GFG_Array = ["GFG_1", "GFG_2", "GFG_3", "GFG_4"]; var up = document.getElementById("up"); up.innerHTML = GFG_Array; var down = document.getElementById("down"); down.innerHTML = "elements of GFG_Array = " + GFG_Array; function myGFG() { GFG_Array.unshift("GFG_7"); down = document.getElementById("down"); down.innerHTML = "element of GFG_Array = " + GFG_Array; } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking the button:

- After clicking the button:

Recommended Posts:
- PHP program to add item at the beginning of associative array
- How to get the elements of one array which are not present in another array using JavaScript?
- Sum and Product of Array elements using JavaScript
- JavaScript | Remove empty elements from an array
- How to unpack array elements into separate variables using JavaScript ?
- Delete the array elements in JavaScript | delete vs splice
- Beginning BootStrap (Part-2) | Grid System
- Remove all the child elements of a DOM node in JavaScript
- Hide or show HTML elements using visibility property in JavaScript
- JavaScript | Array pop()
- RMS Value Of Array in JavaScript
- JavaScript | Array some() function
- JavaScript | array.values()
- JavaScript | array.flatMap()
- JavaScript | Array map() Method
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.



