How to use unshift() Method In Javascript
JavaScript unshift() Method will add an element to the beginning of an array, while its twin function, shift(), will remove one element from the beginning of the array.
Syntax:
array.unshift(item1, item2, ..., itemX)
Example: In this example, we will be adding new elements to the beginning of the array using the unshift() method.
// Input array
let Geeks = ["Geeks1", "Geeks2", "Geeks3", "Geeks4"];
// Display input array
console.log("Original Array: " + Geeks);
// Adding element in begining
Geeks.unshift("Geeks5", "Geeks6");
// Display updated array
console.log("Updated Array: " + Geeks);
Output
Original Array: Geeks1,Geeks2,Geeks3,Geeks4 Updated Array: Geeks5,Geeks6,Geeks1,Geeks2,Geeks3,Geeks4
How to append an element in an array in JavaScript ?
Appending an element to an array in JavaScript means adding a new item to the end of the array. , this process extends the array’s length, accommodating new data. JavaScript offers several methods for appending elements which accepts one or more elements as arguments and adds them to the end of the array.
These are the following ways to solve the problem:
Table of Content
- Using JavaScript push() Method
- Using JavaScript unshift() Method
- Using JavaScript splice() Method
- Using JavaScript concat() Method
- Javascript spread operator
- Using Lodash _.concat() function
- Using the array length property