Stack Data Structure in JavaScript

Share this video with your friends

Send Tweet

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.

Joey Ng'ethe
Joey Ng'ethe
~ 5 years ago

@kyle Why aren't you also using a getter in the isEmpty method? What's the difference?

James Mulholland
James Mulholland
~ 4 years ago

@joey, I believe this is so you can call stack.length, not stack.length(). Getters give you property access without calling functions.

Antal Tony Tettinger
Antal Tony Tettinger
~ 3 years ago

By the way, for me in the codesandbox, if I use console.log(lowerBodyStack.length()), without the getter (removing the get from the length) I still get the correct results not zero.