Skip to content
Related Articles
Open in App
Not now

Related Articles

How to add an object to an array in JavaScript ?

Improve Article
Save Article
  • Last Updated : 21 Dec, 2022
Improve Article
Save Article

In this article, we will learn about how to add an object to an array in Javascript. There are 3 popular methods that can be used to insert or add an object to an array.

Method 1: JavaScript Array push() Method.

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: In this example, we will use the push() method to add an object to the array in Javascript.

html




<body style="text-align:center">
    <h1 style="color: green">
        Geeksforgeeks
    </h1>
    <h3>
        Adding object in array
    </h3>
    <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>


Output:

 

Method 2: JavaScript Array 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: In this example, we will add new objects to the array using the splice() method in Javascript.

html




<body style="text-align: center">
    <h1 style="color: green">
        Geeksforgeeks
    </h1>
    <h3>
        Adding object in array
    </h3>
    <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>


Output:

 

Method 3: JavaScript Array 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: In this example, we will be adding new objects in the array using the unshift() method in Javascript.

html




<body style="text-align:center">
    <h1 style="color: green">
        Geeksforgeeks
    </h1>
    <h3>
        Adding object in array
    </h3>
    <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>


Output:

 

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!