How to use Array push() Method In Javascript
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.
function pushFunction() {
list.push("Four", "Five"
);
console.log(list);
}
let list = ["One", "Two", "Three"];
pushFunction();
Output
[ 'One', 'Two', 'Three', 'Four', 'Five' ]
Add an Object to an Array in JavaScript
Adding an object to an array in JavaScript entails incorporating the object as an element within the array. This process is fundamental for dynamically managing data structures, enabling flexible storage and manipulation of complex data collections within the application.
Table of Content
- Using JavaScript Array push() Method
- Using JavaScript Array splice() Method
- Using JavaScript Array unshift() Method
- Using array concatenation (concat)
- Using JavaScript Array spread operator