What is the Deque.pop() function in collections in Node.js?
In this shot, we are going to learn about a Deque.pop() function in Node.js. The Deque collection is available in the package named as collections. Inside this collections package, there are many data structure implementations and one of them is Deque.
Installation
To install the collections node package, you can run the command below in your local system:
npm i collections
You can also execute the command in the terminal below to install the collections package.
Syntax
The syntax of the Deque.pop() function is given below:
Object pop();
Parameter
The Deque.pop() function does not accept any parameters.
Return
The Deque.pop() function returns a value of the type Object, which denotes the element that has been removed from the Deque. This element will be the last inserted element in the Deque.
Implementation
Once the collections package is installed, we can move to the implementation part and see how the code works.
var Deque = require("collections/deque");var deque_one = new Deque([1, 2, 3]);console.log("Number of elements in Deque: ", deque_one.length);console.log("Deque before calling pop(): ", deque_one.toArray());var element_popped = deque_one.pop();console.log("Number of elements in Deque: ", deque_one.length);console.log("Element popped from Deque: ", element_popped);console.log("Deque after calling pop(): ", deque_one.toArray());
Explanation
-
In line 1 we import the required package.
-
In line 3 we create an object of
Dequeand add three elements into it. -
In lines 5 and 6 we print the number of elements and the current elements present in the
Deque. -
In line 8 we call the
pop()function and store the popped element in the variableelement_popped. -
From lines 10 to 12 we print the number of elements left in the
Deque, the element that we just popped out fromDeque, and the elements that are left now in theDeque.
So, in this way, we can easily pop or remove the last inserted element from a Deque data structure in Node.js.