Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Stack Data Structure in JavaScript

    Kyle ShevlinKyle Shevlin
    javascriptJavaScript

    In this lesson, you'll learn how to create a stack data structure. A stack is a collection of items that obeys the principle of "last in, first out". Like a stack of plates, we can only access the topmost plate at any time. We must remove that plate before we get down to other plates. This is useful for function calls, hence why it's called a "call stack" in JavaScript.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: To create our stack data structure, we're going to use a factory function that returns our stack as a plain JavaScript object. A stack is a collection of items that obeys the principle of last in, first out.

    When we place a new item onto the stack, we have to remove it first before we get to any other item in the stack that's there before. This data structure is used for handling things like nested function calls in JavaScript. Hence, why it's called a call stack.

    Our stack will have several methods and properties, push, pop, peek, to look at what's next to be removed, length, and isEmpty. We'll start by creating an array held in closure to store our items.

    We want to keep our collection in the correct order, so we always want to add and remove items from the same side of the array. We'll use the end of our array to make this happen. Thus, we'll use the array methods that match our stack methods, push and pop, to do this.

    Using push, we place new items at the end of the array. With pop, we remove the final item from the array. This ensures we maintain order in our stack. Now we'll create our peek method by returning the last item in our array.

    We can create a length property. In order to do this, we need to use a getter function. If we simply call .length on our stack, we'll get the value every time, because that's the length of the array when this object is created.

    Using a getter always ensures we get the current array's length. Lastly, we'll add the isEmpty method to quickly tell if our stack has any items left in it.

    Now that we have a stack, we can try it out. A strange but everyday example of a stack is the act of getting dressed. We have to put clothing on in a particular order. In order to change what we wear, we typically have to remove them in the reverse order.

    Let's create a lower body stack for the clothing we put on our lower body. We could create another stack for our upper body, but we'll save that for another time.

    I don't know about you, but the first thing I put on when getting dressed is some underwear, followed by socks. Then I throw on my pants, followed lastly by my shoes.

    If we take a peek at a lower body stack, we should see that shoes is the first thing we need to remove. Great. Let's say it's time to get ready for bed. I need to pop my shoes off. Let's do that. Now that we've taken our shoes off, if we take a peek at our stack, it should say pants are what comes next. It does. Great.

    Let's pop those off as well and check the length of our stack. Looks like we have two items left to remove. To keep this PG, I'll save the disrobing for a time when I'm not doing an Egghead lesson. At least we know our stack works the way it should.