
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Popping elements from a Stack in Javascript
Consider a simple stack class in Javascript.
Example
class Stack { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; // Init an array that'll contain the stack values. this.container = []; } // A method just to see the contents while we develop this class display() { console.log(this.container); } // Checking if the array is empty isEmpty() { return this.container.length === 0; } // Check if array is full isFull() { return this.container.length >= maxSize; } push(element) { // Check if stack is full if (this.isFull()) { console.log("Stack Overflow!"); return; } this.container.push(element); } }
Here the isFull function just checks if the length of the container is equal to or more than maxSize and returns accordingly. The isEmpty function checks if a size of the container is 0. The Push function is used to add new elements into the stack.
In this section, we are going to add POP operation in this class. Popping elements from a Stack mean removing them from the top of the array. We are taking the end of the container array to be the top of the array as we'll perform all operations with respect to it. So we can implement the pop function as follows −
Example
pop() { // Check if empty if (this.isEmpty()) { console.log("Stack Underflow!"); return; } this.container.pop(); }
You can check if this function is working fine using −
Example
let s = new Stack(2); s.display(); s.pop(); s.push(20); s.push(30); s.pop(); s.display();
Output
This will give the output −
[] Stack Underflow! [ 20 ]
- Related Articles
- Peeking elements from a Stack in Javascript
- Pushing elements to a Stack in Javascript
- Clearing the elements of a Stack in Javascript
- Sorting elements of stack using JavaScript
- Program to check maximum sum of all stacks after popping some elements from them in Python
- Not able to push all elements of a stack into another stack using for loop in JavaScript?
- Creating a Stack in Javascript
- Peeking elements from a Queue in Javascript
- Stack Data Structure in Javascript
- The Stack Class in Javascript
- Implementation of Stack in JavaScript
- Implement a stack from a LinkedList in Java
- Create a Stack from a collection in C#
- Remove elements from a queue using Javascript
- Remove elements from a PriorityQueue using Javascript

Advertisements