Data Structures with JavaScript: Stacks
Tags: javascript, beginners, programming, computerscience
Published: 2019-09-30 16:25:40

---
title: Data Structures with JavaScript: Stacks
published: true
description: How to create and use stacks with JavaScript. A comprehensive guide of what a stack is, how to implement them, and its use cases.
tags: javascript, beginners, programming, computerscience
cover_image: https://thepracticaldev.s3.amazonaws.com/i/v533oepte6z5r75gk6uu.jpg
---

_Cover image by [Clem Onojeghuo](https://unsplash.com/@clemono2?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on Unsplash_

_¿Español? Puedes encontrar la versión traducida de este artículo aquí: [Estructuras de datos con JavaScript — Parte 1: Pilas (Stacks)](https://medium.com/noders/estructuras-de-datos-con-javascript-parte-1-pilas-stacks-5a2092cff16a)_

Data Structures! - Now, now, before we start to panic, let's just take a deep breath and jump into this _(not so)_ magical adventure to unveil the mysteries of the commonly feared data structures in JavaScript: What are they, what are they good for and most important of all, how to implement them.

In this article, we'll start with what's probably one of the most common ones: **Stacks**. So where do we start? At the beginning!

### What is a stack?

A stack is a structure used to store data that functions in a **linear and unidirectional** way. That last part is really important because it sets the rules that we'll need to follow when working with stacks: Every element or piece of data added to a stack will be included in the same order and direction _(from start to end)_.

Stacks manage their data under a principle called **LIFO** (**L**ast **I**n **F**irst **O**ut). This means that the last element we add will always be the first one that will be extracted from it.

A commonly used analogy to describe stacks is to think of the way that plates are stored in a kitchen cabinet. Every time we go to grab a new plate, we'll always take the one that sits at the top, which coincidentally was the last one we put there. _(Sometimes we'll try to be clever and take the one in the middle, but then the plates fall, break, and we'll get in trouble_ 🤕*)*

Let's look at a graphical representation of how a stack would work: _(we'll go through what every part of this is later)_:

![A diagram of a Stack](https://thepracticaldev.s3.amazonaws.com/i/p7bhlf196zad0m4e65ac.png)

<figcaption>Looks like a stack</figcaption>

#### When to use a stack

There are a lot of practical examples that we can find nowadays where stacks are being used. There is also a good chance that we've been using them on a daily basis without even knowing. In fact, to get to this article, we did it with one of the most used stacks that there is: **the navigation history of our browser**. Every new page that we visit is stored on top of the previous one, and it creates a stack of values that allows us to go back, one by one _(with the back button)_.

Additionally, stacks are useful when we need a data structure to store data that will be displayed in chronological order _(such as a list of latest tweets or articles)_. For this example, the most recent piece of data added will be the first one shown, and so on, and so on.

So every time that we need to store data in order, and then remove that data from the last one to the first one added, a stack will be your best friend.

### Complexity of a stack

Depending on the type of implementation of a stack _(by using an array or an object)_, there are different levels of complexity, both for space _(amount of memory that it will use)_ and time _(how long it will take to perform operations on that stack, such as: adding, reading, searching and deleting elements)_.

_(Note: Let's consider n = depending on the number of elements in the structure, 1 = direct access)._

#### Space complexity

- **Array:** O(n).
- **Object:** O(n).

For both cases, the space complexity will be O(n), meaning that it will increase or decrease proportionally to the number of elements that are stored.

#### Time complexity

For an implementation using arrays:

- **Read:** O(1)
- **Search:** O(n)
- **Insert:** O(n)
- **Delete:** O(n)

An using objects:

- **Read:** O(n)
- **Search:** O(n)
- **Insert:** O(1)
- **Delete:** O(1)

### Methods and/or functionality of a stack

Traditionally, a stack needs to have functionality that allows to add new elements, extract them, and review them. Even though we can choose whatever name we want for these methods, there is a convention of using the following names to define them:

- **push:** Adds a new value **at the end of the stack**.
- **pop:** Returns the last value, **removing it from the stack.**
- **peek:** Returns the last value inserted, **without removing it from the stack.**
- **size:** Returns the number of elements that the stack has.
- **print:** Displays the contents of the stack.

### How to implement a stack

#### Option 1: Using an array

Implementing a stack using arrays in JavaScript is relatively straightforward since most of the methods from the previous list are already included in the [Array prototype implementation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), so we only need to write a small wrapper that interacts with these methods and return the corresponding values.

The only method that we'll need to implement manually is **peek**, which will return the last value of the array, equal to the [length of the array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) minus one _(since arrays are zero index-based, but length shows the total amount of elements that the array contains, starting from 1)_.

The implementation would look something like this.

```javascript
class Stack {
  constructor() {
    // we create and initialize the stack as an empty array.
    this.stack = [];
  }

  push(element) {
    // pushing an element uses the native push method.
    this.stack.push(element);
    return this.stack;
  }

  pop() {
    // pop will return the last element by using the native pop method.
    return this.stack.pop();
  }

  peek() {
    // peek checks the last element of the array by using the length
    // (total number of elements) minus 1 to find the right index.
    return this.stack[this.stack.length - 1];
  }

  size() {
    // size just returns the length of the array.
    return this.stack.length;
  }

  print() {
    // print will do a console log of the array
    console.log(this.stack);
  }
}

const stack = new Stack();
console.log(stack.size()); // 0
console.log(stack.push("Stone Cold Steve Austin")); // ["Stone Cold Steve Austin"]
console.log(stack.push("The Rock")); // ["Stone Cold Steve Austin", "The Rock"];
console.log(stack.size()); // 2
stack.print(); // ["Stone Cold Steve Austin", "The Rock"];
console.log(stack.peek()); // The Rock
console.log(stack.pop()); // The Rock
console.log(stack.peek()); // Stone Cold Steve Austin
```

#### Option 2: Using an object

Implementing a stack with an object requires a bit of additional work since the native methods of the arrays will not be available here, so we'll have to implement them manually.

One of the ways to achieve this is, when creating the stack, initialize a variable that will act as a cursor and keep the current position of the last element added, as well as the total number of elements inserted. Since the default behavior of a stack just requires us to add/remove the last element added, as long as we keep track of the current position, we should be able to achieve this.

```javascript
constructor() {
  this.stack = {};
  this.count = 0;
}
```

To add elements, we'll use _**this.count**_ as a reference of the current position and we'll use JavaScript's bracket notation to do a direct insertion in the object.

```javascript
push(element) {
  this.stack[this.count] = element;
  this.count++;
  return this.stack;
}
```

For **peek, print** y **size**, the implementation is basically the same as with arrays. The main difference is that we'll use _**this.count**_ instead of _**Array.length**_ to identify the index of the element that we'll need to show or to return the total number of elements added.

```javascript
peek() {
  return this.stack[this.count - 1];
}

size() {
  return this.count;
}

print() {
  console.log(this.stack);
}
```

Finally, for **pop** it'll be necessary to do some extra work. The difference with the last case is that after returning the element, we'll need to delete it from the object and move the cursor back to track the _**new last element**_.

```javascript
pop() {
  this.count--;
  const element = this.stack[this.count];
  delete this.stack[this.count];
  return element;
}
```

The full implementation would be as follows:

```javascript
class Stack {
  constructor() {
    this.stack = {};
    this.count = 0;
  }

  push(element) {
    this.stack[this.count] = element;
    this.count++;
    return this.stack;
  }

  pop() {
    this.count--;
    const element = this.stack[this.count];
    delete this.stack[this.count];
    return element;
  }

  peek() {
    return this.stack[this.count - 1];
  }

  size() {
    return this.count;
  }

  print() {
    console.log(this.stack);
  }
}

const stack = new Stack();
console.log(stack.size()); // 0
console.log(stack.push("Stone Cold Steve Austin")); // { "0": "Stone Cold Steve Austin" }
console.log(stack.size()); // 1
console.log(stack.peek()); // Stone Cold Steve Austin
console.log(stack.push("The Rock")); // { "0": "Stone Cold Steve Austin", "1": "The Rock" }
console.log(stack.size()); // 2
stack.print(); // { "0": "Stone Cold Steve Austin", "1": "The Rock" }
console.log(stack.peek()); // The Rock
console.log(stack.pop()); // The Rock
stack.print(); // { "0": "Stone Cold Steve Austin" }
console.log(stack.size()); // 1
console.log(stack.peek()); // Stone Cold Steve Austin
```

### Source Code

You can find the source code of this example here:[https://github.com/Xabadu/js-data-structures](https://github.com/Xabadu/js-data-structures)

Originally published on my blog at [xabadu.dev](https://xabadu.dev/js-data-structures-stacks/)