Stack Data Structure in Javascript
Tags: javascript, webdev, beginners, computerscience
Published: 2020-03-01 14:00:14

Stacks are one of the most common data structures in computer science that one can consider. A stack is an ordered collection of homogeneous data elements,where the insertion and deletion takes place at one end, known as `top`,which means the element which is inserted last must be deleted first.

The stack is also called as `LAST IN FIST OUT(LIFO)`.

Javascript does not have Stack data structure built into it, but that should not limit you from building one, using the already built-in data types like arrays.

**Real-Life Applications of Stack:-**


+ Books, Clothes piled on top of each other
+ Floors in a building.

# Use cases of stacks in programming - 

+ Browser back-forth button.
+ Reversing a string
+ Converting expressions(prefix-infix-postfix)
+ Recursive functions.
+ Maintaining any sort of LIFO data.

# Basic Operations 

The basic operation that can performed are `Insertion`, `deletion` and `display`.

**PUSH(terminology for Insertion)** - Inserting or adding the elments into the stack.It places the object on top of the stack.

**POP(terminology for Deletion)** - Deleting or removing an element from the stack.It removes an object from the top of the stack.

**IsEmpty** - Reports whether the stack is empty or not.

**IsFull** - Reports whether the stack exceeds limit or not.

**Peek** - returns the `top` record without popping it.

# Direction for creating a Stack data Structure in Javascript

Create a stack data structure.The stack should be a class with methods `push`,`pop`,and `peek`.Adding an element to the stack should store it until it is removed.

#Examples Usage

```javascript

const s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.pop(); //returns 3
s.pop(); // returns 2
```
```javascript

//Implementing Stack using arrays
class Stack {
  constructor() {
    // data is a normal array data type built into javascript
    this.data = [];
  }
  //It places the item on top of the stack
  push(item) {
    this.data.push(item);
  }
  // It removes an item from the top of the stack
  pop() {
    return this.data.pop();
  }

  // peek is to return the last record inside of our array without actually removing it.
  peek() {
    return this.data[this.data.length - 1];
  }
}

module.exports = Stack;
```
In the next article of this series, we will be implementing Stack using Queues in Javascript.

If you are a visual learner,be sure to check this tool out. Its called Quokkajs.It is a  developer productivity tool for rapid JavaScript / TypeScript prototyping. Runtime values are updated and displayed in your IDE next to your code, as you type.[QuokkaJS](https://quokkajs.com/)

[![Rapid Prototyping Tool](https://camo.githubusercontent.com/d3ab68016d3ca28361f9f11106cb87cd51feb3c2/68747470733a2f2f71756f6b6b616a732e636f6d2f6173736574732f696d672f6d61696e2d766964656f2e676966)](https://quokkajs.com/ "Rapid Prototyping Tool")
 
If you found this article helpful, please tap the <img src="https://practicaldev-herokuapp-com.freetls.fastly.net/assets/emoji/emoji-one-unicorn-695106da1194513bfa0092f1a75943f59089be7d6addbc7459bdfab80f5709e6.png" alt="drawing" width="30" height="30"/> Follow this channel for more articles on Data Structures using Javascript.