There are 3 popular methods which can be used to insert or add an object to an array.
- push()
- splice()
- unshift()
Method 1: push() method of Array
The push() method is used to add one or multiple elements to the end of an array. It returns the new length of the array formed. An object can be inserted by passing the object as a parameter to this method. The object is hence added to the end of the array.
Syntax:
array.push(objectName)
Example:
<!DOCTYPE html> <html> <head> <title>Adding object in array</title> <style> body { text-align: center; } </style> </head> <body> <h1 style="color: green">Geeksforgeeks</h1> <p>Click the button to add new elements to the array.</p> <button onclick="pushFunction()">Add elements</button> <p id="geeks"></p> <script> var list = ["One", "Two", "Three"]; document.getElementById("geeks").innerHTML = list; function pushFunction() { list.push("Four", "Five", ); document.getElementById("geeks").innerHTML = list; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Method 2: splice() method
The splice method is used to both remove and add elements from a specific index. It takes 3 parameters, the starting index, the number of elements to delete and then the items to be added to the array. An object can only be added without deleting any other element by specifying the second parameter to 0.
The object to be inserted is passed to the method and the index where it is to be inserted is specified. This inserts the object at the specified index.
Syntax:
arr.splice(index, 0, objectName)
Example:
<!DOCTYPE html> <html> <head> <title>Adding object in array</title> <style> body { text-align: center; } </style> </head> <body> <h1 style="color: green">Geeksforgeeks</h1> <p>Click the button to add new elements to the array.</p> <button onclick="spliceFunction()">Add elements</button> <p id="geeks"></p> <script> var list = ["HTML", "CSS", "JavaScript"]; document.getElementById("geeks").innerHTML = list; function spliceFunction() { list.splice(2,0,"Angular", "SQL", ); document.getElementById("geeks").innerHTML = list; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Method 3: unshift() method
The unshift() method is used to add one or multiple elements to the beginning of an array. It returns the length of the new array formed. An object can be inserted by passing the object as a parameter to this method. The object is hence added to the beginning of the array.
Syntax:
arr.unshift(object);
Example:
<!DOCTYPE html> <html> <head> <title>Adding object in array</title> <style> body { text-align: center; } </style> </head> <body> <h1 style="color: green">Geeksforgeeks</h1> <p>Click the button to add new elements to the array.</p> <button onclick="unshiftFunction()">Add elements</button> <p id="geeks"></p> <script> var list = ["Geeks", "Contribute", "Explore"]; document.getElementById("geeks").innerHTML = list; function unshiftFunction() { list.unshift("for", "Geeks", ); document.getElementById("geeks").innerHTML = list; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
- Supported Browser
- Google Chrome 1.1 or above
- Mozila Firefox 1.1 or above
- Safari
- Internet Explore 5.5 or above
- Opera
Recommended Posts:
- How to add key/value pair to a JavaScript object?
- JavaScript | Add new attribute to JSON object
- JavaScript | How to add an element to a JSON object?
- How to conditionally add a member to an object using JavaScript ?
- How to add a property to a JavaScript object using a variable as the name?
- How to check a JavaScript Object is a DOM Object ?
- How to access an object having spaces in the object's key using JavaScript ?
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- How to convert Object's array to an array using JavaScript ?
- HTML | DOM Object Object
- How to select first object in object in AngularJS?
- JavaScript | Add new elements at the beginning of an array
- How to add elements to an existing array dynamically in JavaScript ?
- How to check if an array includes an object in JavaScript?
- How to check object is an array in JavaScript?
- Sort an Object Array by Date in JavaScript
- indexOf method in an object array in JavaScript
- Convert an Array to an Object in JavaScript
- How to push an array into the object in JavaScript ?
- How to convert arguments object into an array 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.


