Back To Tutorials

Implement a Stack in Javascript

Intermediatechallenge

In this challenge, you will implement a Stack data structure in JavaScript. A stack is a collection of elements that follows the Last In, First Out (LIFO) principle. The last element added to the stack is the first one to be removed.

You will implement the essential methods of a stack such as push(), pop(), peek(), and isEmpty().

Objective:

  • Implement a class-based Stack data structure in JavaScript with standard stack operations.

Requirements:

  1. Create a Stack class that implements the following methods:
    • push(element) — Adds an element to the top of the stack.
    • pop() — Removes and returns the top element from the stack. Returns null if the stack is empty.
    • peek() — Returns the top element of the stack without removing it. Returns null if the stack is empty.
    • isEmpty() — Returns true if the stack is empty, otherwise returns false.
    • size() — Returns the number of elements in the stack.
  2. The stack should use an internal array to store elements and follow LIFO order.

Steps:

  1. Define the Stack Class:

    • Use a class in JavaScript to define the stack.
    • Store the stack elements in an internal array.
  2. Implement the Methods:

    • push(element) — Add the element to the top (end) of the array.
    • pop() — Remove and return the last element from the array (top of the stack). Handle the case where the stack is empty.
    • peek() — Return the last element from the array without removing it.
    • isEmpty() — Check if the length of the array is zero.
    • size() — Return the length of the internal array.
  3. Test the Stack:

    • Create a stack instance and use the methods to ensure they behave as expected.
    • Test edge cases such as popping from an empty stack or peeking into an empty stack.

Resources:


Solution:

Here’s the solution for the stack implementation in JavaScript:

// Stack class implementation
class Stack {
  constructor() {
    this.stack = [];  // Internal array to store stack elements
  }

  // Pushes an element onto the top of the stack
  push(element) {
    this.stack.push(element);
  }

  // Pops the top element from the stack and returns it
  pop() {
    if (this.isEmpty()) {
      return null; // Return null if the stack is empty
    }
    return this.stack.pop(); // Remove and return the top element
  }

  // Returns the top element of the stack without removing it
  peek() {
    if (this.isEmpty()) {
      return null; // Return null if the stack is empty
    }
    return this.stack[this.stack.length - 1]; // Return the last element
  }

  // Checks if the stack is empty
  isEmpty() {
    return this.stack.length === 0; // True if no elements in the stack
  }

  // Returns the size of the stack
  size() {
    return this.stack.length;
  }
}

// Example Usage
const myStack = new Stack();

myStack.push(10);
myStack.push(20);
myStack.push(30);

console.log(myStack.peek());  // Output: 30
console.log(myStack.pop());   // Output: 30
console.log(myStack.size());  // Output: 2
console.log(myStack.isEmpty()); // Output: false
myStack.pop();
myStack.pop();
console.log(myStack.isEmpty()); // Output: true