Implementing a Stack in JavaScript
Published: 2022-10-22 09:24:50

Stack is one on the most common and widely used data-structure. stack follows the **LIFO(Last In First Out)** policy.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xjdft1w9kpxwn71sw9h.jpg)

In the above image, we can see the stack of books. If we notice carefully, we can add a new book only from the top. Likewise, we can only remove the book from the top. which is pretty much a similar analogy to how the stack data-structure works.


**Stack Implementation:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vepi51mg42ibhvck2iuv.png)

Now let's take a look at each individual operation that can be performed on the stack.

**PushElement():**It is used to insert an element on top of the stack.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eueevxsacfpoy93yqmf4.png)

**PopElement():**It deletes the topmost element of the stack and returns it. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fucvqpomuek9x6pt2unn.png)

**isEmpty():**This method returns true if the stack is empty; otherwise, it returns false.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gscpyhsak8o07962b4s5.png)

**Peek():**This method returns the topmost element of the stack without actually deleting it.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ry0bqv0y3oryupiem44x.png)

**Print():**This method is used to print all the elements present in the stack.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kj3svol2du3ncwkbrir.png)

_Adding elements to the stack and printing them:_ We can add new elements to the stack using PushElement() and we can print them using Print()


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6nw84897i16eef5dw4sa.png)

_Removing the topmost element and printing the remaining elements in the stack:_ For removing the topmost element of the stack we can use the PopElement() method and for printing we can use the Print() method


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bvkdhqyphp7j9tghfd0h.png)

If we take a closer look, we removed the top most element from the stack, which is 60.

_Checking whether the given stack is empty or not:_ For checking whether the stack is empty or not we can use isEmpty() method


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1wqqw2qo69xvpu5ux87.png)

In our case the stack is not empty so the isEmpty() method returns false

_Getting the topmost element of the stack without deleting it:_ To get the top most element of the stack without actually deleting it we can use Peek() method.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0wpr6a50iegqckz1569w.png)

Here we can see that Peek() is returning the top most element which is 50 without actually deleting it from the stack 

_Stack Underflow:_ This occurs when you try to remove or get the top most element of the stack when the stack is empty.

_Stack Overflow_: This occurs when you try to add a new element to the stack which is already full