Back
Node.jsJavaScriptArticle · Apr 1, 2024 · 8 min

jonathanjuliani

Implementing Stacks with NodeJS and Javascript

Stack (LIFO) in JavaScript: implementation, real use cases, and commented Node.js code. Straightforward hands-on tutorial.

Illustration for stacks (LIFO) with Node.js and JavaScript

Undo, expression parsing, DFS — all stack (LIFO): last in, first out. We implement it with the native JS array.

What you'll learn:

  • push / pop as a stack
  • A small Stack class
  • Call stack and parsing as real examples

Prerequisites: arrays (ep. 2) and linked lists (ep. 3).


Stack (LIFO): push and pop in practice

In this post we are going to implement stacks, so, how they work? what are stacks?

Stacks are one of may data structures that we can use to better handle data in our applications. So basically in simple words stacks are a way to order and process data (collections). Stacks work with a principle of LIFO (last in, first out) by that we mean that the last element included on a stack will be the first one to be removed/processed.

implementing-stacks-with-nodejs-and-javascript

You can easily understand and implement Stacks in NodeJS/Javascript, they can help you to optimize some data collection that you need to reverse, our when you need a specific sequence to operate a collection. You know, when you did that interview that you have been asked to revert an array? Yeah, that's right you could have used stacks to do that.

Well personally I don't like those kind of interviews, but that's a subject for another post...let's dive in on implementing Stacks.

Stacks: How to Implement

One thing you should know before dive in the code, Stacks are an ordered type of list (collection, array - call it what you want) so every operation is based on the top of the list/stack. That's why we call it LIFO.

Implementing Stacks in JavaScript

When we are talking about vanilla javascript or node without specifics libraries, they don't have a built in stack data structure for us to use. And that's awesome, because we can implement or own, this help us understand how stack works and we can implement it on our own way to suit our needs.

Of course there is a bunch of libraries that have Stacks and other datastructures for you to use, these libs are good when you already have a understanding on how Stacks works and need to use them within your apps without having to implement and do maintanence, but for the sake of our knowlowegde here, let's implement our own, it's not a big deal.

Here is a sample:

code
class Stack {
  constructor() {
    this.items = []
  }
 
  // Adds an element on top of the Stack
  push(element) {
    this.items.push(element)
  }
 
  // Removes the element on top of the Stack
  pop() {
    if (this.items.length == 0) {
      return 'Empty' // stack is empty
    }
    return this.items.pop()
  }
 
  // Returns the element on top of the Stack (without removing it)
  peek() {
    return this.items[this.items.length - 1]
  }
 
  // Checks if the Stack is empty
  isEmpty() {
    return this.items.length == 0
  }
 
  // Logs all the stack elements
  printStack() {
    for (let i = this.items.length - 1; i >= 0; i--) {
      console.log(this.items[i])
    }
  }
}

Edge cases: pop on an empty stack

pop() on an empty array returns undefined — no throw. In production, guard with isEmpty() or fail explicitly.

Is stack overflow the same stack? Yes — the JS call stack blew up from recursion without a base case. Same LIFO idea.


TL;DR — Stack

OperationTypical cost
pushO(1)
popO(1)
peekitems[items.length - 1]

Next steps


Before you go

Favorite one-liner to reverse an array with a stack? Comment below.

Subscribe to the Engineering Ledger

Get architecture and performance notes in your inbox. Same list as the timed prompt—subscribe here anytime.

No spam. No third-party APIs. Just me sending updates.

The Engineering Ledger

Bi-weekly transmissions on architecture, performance, and practical engineering. Subscribe from any article—no spam.