How to use Array unshift() Method In Javascript
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.
function unshiftFunction() {
list.unshift("for", "Geeks",);
console.log(list);
}
let list = ["Geeks", "Contribute", "Explore"];
unshiftFunction();
Output
[ 'for', 'Geeks', 'Geeks', 'Contribute', 'Explore' ]
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