ArticlesLinear structures
Stacks
Last in, first out. Push and pop touch only the most recent item, both O(1) at the end of an array.
A stack is a list where you only touch the most recently added item. Push adds. Pop removes. Both are O(1) at the end of an array, so a TypeScript array is a perfectly good stack if you agree not to touch the middle.
The useful part is the order. The last thing you put down is the first thing you pick up. Bracket matching is the standard practice. An opener is pushed. A closer pops, and it has to match the opener that just came off. If you need to pop an empty stack, or the opener does not match, the string is unbalanced. If anything is left at the end, something was never closed.
That memory of what is still open is the reason to reach for a stack. A counter can tell you that the counts match. It cannot tell you that a square bracket closed while a parenthesis was still open. The stack remembers the order the openings arrived.