Write a JavaScript Program to Implement a Stack

A stack is a data structure that follows the principle of Last In First Out (LIFO).

It means that the last element added to the stack is the first element to be removed from the stack.

In this article, we will discuss how to implement a stack using JavaScript.

Creating a Stack Class

To implement a stack in JavaScript, we need to create a class that defines the structure of the stack. Here is an example of how to create a Stack class:

class Stack {
  constructor() {
    this.items = [];
  }
}

In the constructor function, we create an empty array called items. This array will hold the elements of the stack.

Pushing Elements into the Stack

The push() method is used to add elements to the top of the stack. Here’s how we can implement the push() method:

class Stack {
  constructor() {
    this.items = [];
  }

  push(element) {
    this.items.push(element);
  }
}

In the push() method, we use the push() function of the array to add an element to the top of the stack.

Popping Elements from the Stack

The pop() method is used to remove elements from the top of the stack.

Here’s how we can implement the pop() method:

class Stack {
  constructor() {
    this.items = [];
  }

  push(element) {
    this.items.push(element);
  }

  pop() {
    if (this.items.length === 0) {
      return "Underflow";
    }
    return this.items.pop();
  }
}

In the pop() method, we first check if the stack is empty. If the stack is empty, we return “Underflow”. Otherwise, we use the pop() function of the array to remove the top element from the stack.

Peeking at the Top Element of the Stack

The peek() method is used to retrieve the top element of the stack without removing it. Here’s how we can implement the peek() method:

class Stack {
  constructor() {
    this.items = [];
  }

  push(element) {
    this.items.push(element);
  }

  pop() {
    if (this.items.length === 0) {
      return "Underflow";
    }
    return this.items.pop();
  }

  peek() {
    return this.items[this.items.length - 1];
  }
}

In the peek() method, we use the length property of the array to retrieve the index of the top element. We then use this index to retrieve the top element from the array.

Checking if the Stack is Empty

The isEmpty() method is used to check if the stack is empty. Here’s how we can implement the isEmpty() method:

class Stack {
  constructor() {
    this.items = [];
  }

  push(element) {
    this.items.push(element);
  }

  pop() {
    if (this.items.length === 0) {
      return "Underflow";
    }
    return this.items.pop();
  }

  peek() {
    return this.items[this.items.length - 1];
  }

  isEmpty() {
    return this.items.length === 0;
  }
}

In the isEmpty() method, we use the length property of the array to check if the stack is empty.


Conclusion

In this article, we discussed how to implement a stack using JavaScript. We created a Stack class and implemented methods such as push(), pop(), peek(), and isEmpty().