by Sai gowtham

JavaScript Array methods Push, Pop, Unshift and Shift

In this tutorial, we will learn about how to add or remove items from the beginning or end of the arrays by using four different methods in JavaScript.

Push() method

The push() method is used to add the new elements to the end of an array.

Example:

const arr = ["paper", "painter", "poet"];

arr.push("pan");

console.log(arr); //  ["paper", "painter", "poet", "pan"]

Pop() method

The pop() method is used to remove the last element from an array, and returns the removed element.

const arr = ["paper", "painter", "poet"];

console.log(arr.pop()); // "poet"

console.log(arr); // ["paper", "painter"]

Unshift() method

The unshift() method is used to add the new elements at the beginning of an array.

const arr = ["paper", "painter", "poet"];

arr.unshift("pocket");

console.log(arr); // ["pocket", "paper", "painter", "poet"]

Shift() method

The shift() method is used to remove the first element from an array, and returns the removed element.

const arr = ["paper", "painter", "poet"];

console.log(arr.shift()); // "paper"

console.log(arr); // ["painter", "poet"]

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course