Stack (Implementation)

In this lesson, we are going to look at how Stacks are implemented in JavaScript and how the main Stack functions actually work.

Introduction #

Most programming languages come with the Stack data structure built-in​. However, we will be implementing a stack from scratch, which will allow you to truly master the ins-and-outs of the data structure.

Implementation #

Stacks can be implemented using Arrays or Linked Lists. Each implementation has its own advantages and disadvantages. Here, however, we will show an implementation of stacks using arrays.

As mentioned in the previous lesson, a typical Stack must contain the following functions:

  • push(element)
  • pop()
  • isEmpty()
  • getTop()
  • size()

We will take a close look at these functions individually, but before we do, let’s construct a Stack class and create an object. This class will consist of the member functions given above and array that will hold all the elements of the stack. Additionally, we’ll also have a variable ( to specify the top of the stack.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy