Clue Mediator

Push, Pop, Shift and Unshift Array Methods in JavaScript

๐Ÿ“…December 12, 2019
๐Ÿ—JavaScript

In this article we explain you Push(), Pop(), Shift() and Unshift() method one by one with example in Javascript. These methods use for add or remove items from the beginning or end of arrays.

Push Pop Shift Unshift Array Methods, JavaScript Array unshift() Method, Array.prototype.unshift(), Array.prototype.shift(), Unshift JavaScript | Unshift() Array Methods, javascript add or remove element from array, JavaScript Array Methods: Pop, Push, Shift and Unshift.

Checkout more articles on JavaScript

`push()` and `pop()` methods append elements to an array and remove an element to the end of an array respectively.

`shift()` and `unshift()` method remove an element and append elements to the beginning of an array respectively.

Array Methods in JavaScript

  1. push()
  2. pop()
  3. shift()
  4. unshift()

Method 1: push()

The `push()` method can add one or more elements to the end of an array and returns a new array length.

var colours=["Black", "Blue"];
colours.push("Yellow");
// Output: ["Black", "Blue","Yellow"]

Method 2: pop()

The `pop()` method can remove an element from the end of an array and return array.

var colours=["Black", "Yellow","Orange"];
colours.pop();
// Output: ["Black", "Yellow"]

Method 3: shift()

The `shift()` Method removes the first element of an array. This method like the pop() method, but it works at the beginning of the array.

var colours=["Black", "Yellow","Orange"];
colours.shift();
// Output: ["Yellow","Orange"]

Method 4: unshift()

The `unshift()` method can add one or more elements to the beginning of an array and returns a new array length.

var colours=["Black"];

colours.unshift("Blue");
// Output:  ["Blue","Black"]

colours.unshift("Yellow","Orange");
// Output: ["Yellow","Orange","Blue","Black"]

That's it for today.

Thanks for reading. Happy Coding!