Sitemap

Understanding a Stack using JavaScript.

4 min readNov 4, 2022

--

In data structure, a stack represents a sequence of objects or elements in a linear data structure format. There might some of you who don’t understand the concept of stack by reading the definition. So, I decided to explain it as simple as possible using a drawing. Here’s a simple example of a stack:

This is a stack. It has a linear structure with only one way to get in and get out. Therefore, it has to follow the principle of LIFO (Last In First Out). If I put something inside of the stack, it will start by filling up from the bottom. As I keep adding stuffs to the stack, it will start “stacking” up and it will end up like this:

As I mentioned earlier, a stack follows LIFO principle and the element at the bottom can’t be accessed without removing elements at the top.

Press enter or click to view image in full size

In JavaScript, there are various methods that we can use to make any changes but, I will only cover push and pop methods.

The push method is simply “pushing” down new element at the very top of a stack. It is like the image from above where I’m pushing down alphabet letters from top to bottom.

The pop method removes elements at the very top of the stack. So, if I apply a pop method once, it will remove letter “f” from the stack since it’s at the very top of the stack at the moment.

Apply to Code

Now that we understand how a stack works, let’s apply that to an actual coding problem. Let’s say I have to create a function that can check if a string containing bunch of “{“ and “}” is balanced or not.

Get Yoonsung Kim’s stories in your inbox

Join Medium for free to get updates from this writer.

First, I will create a stack using a normal array that we all are familiar to.

function checkBracesString(s){// This is our stack!
// Yes, I know it's an array but, we are using it as a stack.
let stack = [];}console.log(checkBracesString('{{{}}}')) // expecting: trueconsole.log(checkBracesString('}{{{}}}}}}')) //expecting: false

I created a function that contains a stack. Now, I want to check each letters for the curly braces using a for loop. Here, I want to check for each letters using if and else statements. The rest of the code is look like this.

function checkBracesString(s){  let stack = [];  // Looping through each letters using a for loop.  for(let i = 0; i < s.length; i++){    // If there the letter at the position s[i] is equal to '{', push it into the stack    if(s[i] === '{'){       stack.push(s[i])    // Else if the letter is equal to '}', then move onto next if statement.    }else if(s[i] === '}'){       // First, I want to check the stack itself if it's empty. If it's empty, that means there's no '{' to cancel out. So return false.       if(stack.length === 0){         return false;       // If the stack is not empty, then move onto next condition.
// Else if the letter at the very top of the stack is equal to '{', pop out from the stack.
} else if(stack[stack.length-1] === '{'){ stack.pop(); // Else, return false } else { return false; } } } // The stack must be empty at the end to return true(Balanced). Otherwise, it's a false(unbalanced). return stack.length === 0;}console.log(checkBracesString('{{{}}}')) // expecting: trueconsole.log(checkBracesString('}{{{}}}}}}')) //expecting: false

For visual learners, I drew the process that might help you. I’m not the best artist though. Also, I added an image of my code is actually working.

Press enter or click to view image in full size

I hope this helps those who are currently studying data structure and algorithm like myself. Try to solve this problem on your own and let me know what other ways that you can solve this problem.

Other than that, have a nice day!

--

--

Yoonsung Kim
Yoonsung Kim

Written by Yoonsung Kim

Software Engineer | Full-Stack