AppDividend
Latest Code Tutorials

Pop Push Shift and Unshift Array Methods in JavaScript

239

Get real time updates directly on you device, subscribe now.

Pop Push Shift and Unshift Array Methods in JavaScript is today’s leading topic. We will see pop, push, shift, and unshift method one by one in Javascript Node.js with an example. JavaScript gives us four methods to add or remove items from the beginning or end of arrays.

Pop Push Shift and Unshift Array Methods in JavaScript

We will test all of our examples inside Node.js because it is the easy way to see the output inside the terminal. Otherwise, we need to create an HTML file and include the JS file inside it and then check the browser console to see the output.

Now, if create a project folder and open that folder inside an editor. I am using VSCode and if possible, start using that editor for JavaScript development as well.

Okay, after that, initialize the package.json file by the following command.

npm init -y

Now install the nodemon server by the following command.

npm install nodemon --save-dev

Create a new js file called server.js in a project folder.

Start the nodemon server by the following command.

nodemon server

#JavaScript Array pop() Method

The pop() method removes a last element of an array and returns that element. It will remove an item from the end of an array and return that item. The pop() method can be called or applied to objects that resembling arrays. Objects which do not contain the length property reflecting a last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner. If you call pop() method on an empty array, it returns as undefined. Let us see this action in the example.

// server.js

let stocks = [
  { type:"Apple", year:1975 },
  { type:"Microsoft", year:1976 },
  { type:"Amazon", year:1995 }
];

poppedStock = stocks.pop();
console.log(stocks);
console.log("Popped Item", poppedStock);

See in the terminal for the output.

 

Pop Push Shift and Unshift Array Methods in JavaScript

So, here from the example, we can see that the last element is removed and returned. 

#JavaScript Array push() Method

The push() method adds the new items to the end of an array and returns a new length. The new item(s) will be added at the end of an array. The push() method changes the length of an array. To add items at the beginning of an array, use the unshift() method. The only native, array-like objects are strings, although they are not suitable in applications of this method, as strings are immutable.

// server.js

let stocks = [
  { type:"Apple", year:1975 },
  { type:"Microsoft", year:1976 },
  { type:"Amazon", year:1995 }
];

stocks.push({
  type: "Tesla", year: "2003"
});
console.log(stocks);

 

JavaScript Array push() Method

#JavaScript Array shift() Method

The shift() method removes the first item of an array. The shift() method changes the length of the array. The return value of the shift method is the removed item.

// server.js

let stocks = [
  { type:"Apple", year:1975 },
  { type:"Microsoft", year:1976 },
  { type:"Amazon", year:1995 }
];

shiftedStock = stocks.shift();
console.log(stocks);
console.log("Shifted Item", shiftedStock);

 

JavaScript Array shift() Method

#JavaScript Array unshift() Method

The unshift() method adds the new item to the beginning of an array and returns the new length. The unshift() method changes the length of an array because it adds new items to an array.

// server.js

let stocks = [
  { type:"Apple", year:1975 },
  { type:"Microsoft", year:1976 },
  { type:"Amazon", year:1995 }
];

let newStock = {
  type: "Tesla", year: 2003
};

unshiftedStock = stocks.unshift(newStock);
console.log(stocks);
console.log("Shifted Item", unshiftedStock);

 

JavaScript Array unshift() Method

Related Posts
1 of 13

Finally, Pop Push Shift and Unshift Array Methods in JavaScript tutorial example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.