Using Array.unshift() and Array.shift() in Javascript


Add to or remove the first element from an array.

Source Code

let fruits = ["Banana", "Orange"];
fruits.unshift("Apple"); // Add to the start
console.log(fruits); // ["Apple", "Banana", "Orange"]
let firstFruit = fruits.shift(); // Remove from the start
console.log(firstFruit); // "Apple"
console.log(fruits); // ["Banana", "Orange"]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments