jonathanjuliani
Implementing Stacks with NodeJS and Javascript
Stack (LIFO) in JavaScript: implementation, real use cases, and commented Node.js code. Straightforward hands-on tutorial.

Undo, expression parsing, DFS — all stack (LIFO): last in, first out. We implement it with the native JS array.
What you'll learn:
push/popas a stack- A small
Stackclass - 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.

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:
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
| Operation | Typical cost |
|---|---|
| push | O(1) |
| pop | O(1) |
| peek | items[items.length - 1] |
Next steps
- Implementing Queues
- Graph algorithms (ep. 14) — DFS uses a stack
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.
Related articles

Data Structures and Algorithms - A Summary of The Most Used Ones
Practical overview of the data structures and algorithms you use most in JavaScript/Node.js — with examples and links to go deeper.
Read story
Implementing Arrays with NodeJS and Javascript
JavaScript arrays in Node.js: essential methods, immutability patterns, and when an array beats other structures. Hands-on guide.
Read story