GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Last In, First Out (LIFO) principle
  • Infix to Postfix

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Stack in JavaScript - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

Stack in JavaScript

Last Updated : 1 Jul, 2026

A stack is a linear data structure that allows operations to be performed at one end, called the top. The two primary operations are:

  1. Push: Adds an element to the top of the stack.
  2. Pop: Removes and returns the top element from the stack.


JavaScript
let stack = [];

// 2. The "Push" Operation // Adding elements to the top of the stack
stack.push('A'); // Stack is now: ['A']
stack.push('B'); // Stack is now: ['A', 'B']

console.log("Pushing 'C' onto the stack...");
stack.push('C'); // Stack is now: ['A', 'B', 'C']

console.log("Current Stack:", stack); 
console.log("Top of the stack is:", stack[stack.length - 1]); // Outputs 'C'

console.log("-----------------------------------");

// 3. The "Pop" Operation // Removing the top element from the stack
console.log("Popping the top element...");
let poppedElement = stack.pop(); // Removes 'C' and stores it

console.log("Popped element:", poppedElement); // Outputs 'C'
console.log("Stack after pop:", stack);        // Stack is now back to: ['A', 'B']
console.log("New Top of the stack is:", stack[stack.length - 1]); // Outputs 'B'


  • It fundamental data structure in computer science that follows the Last In, First Out (LIFO) principle.
  • This means that the last element added to the stack will be the first one to be removed.
  • Stacks are widely used in various applications, such as function call management, undo mechanisms, and parsing expressions.

Extreme Conditions in a Stack

1. Stack Underflow:

  • Occurs when you try to perform a pop or peek operation on an empty stack.
  • Handling: Check if the stack is empty before performing these operations.

2. Stack Overflow:

  • Occurs when you try to push an element into a stack that has reached its maximum capacity (in languages or implementations where the stack size is fixed).
  • Handling: Check if the stack is full before performing a push operation.

Ways to Implement Stack in JavaScript

In JavaScript, stacks can be implemented using arrays or linked lists. Both approaches provide efficient insertion and removal of elements from the top of the stack.

1. Array Implementation of a Stack In JavaScript

  • In a stack implementation, we need to do push and pop operations at the same end.
  • In an array, we can do both operations at the end of the array (or last element) in O(1) time.
JavaScript
class Stack {
    constructor() { this.items = []; }

    push(element) { this.items.push(element); }

    pop() {
        if (this.isEmpty()) {
            return null;
        }
        return this.items.pop();
    }

    peek() {
        if (this.isEmpty()) {
            return null;
        }
        return this.items[this.items.length - 1];
    }

    isEmpty() { return this.items.length === 0; }

    size() { return this.items.length; }

    print() { console.log(this.items); }
}
// Example Usage
const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack.peek()); 
console.log(stack.pop());    
console.log(stack.size());   
console.log(stack.isEmpty());
stack.print();                


Time Complexity: All operations in the Stack Class ( Push , Pop, Peek, isEmpty, Size,) have O(1) time complexity.  print Stack(), which is O(n).
Auxiliary Space: O(1) for all operations, except print(), which is O(n)

2. Linked List Implementation of Stack in JavaScript

  • In a stack implementation, we need to do push and pop operations at the same end.
  • In a linked list, we can do both operations at the beginning of the list (or first element) in O(1) time.
JavaScript
// Node class representing each element in the stack
class Node {
    constructor(value)
    {
        this.value = value;
        this.next = null;
    }
}

// Stack class using a Linked List
class Stack {
    constructor()
    {
        this.top = null;
        this.size = 0;
    }

    // Push operation
    push(value)
    {
        const newNode = new Node(value);
        newNode.next = this.top;
        this.top = newNode;
        this.size++;
    }

    // Pop operation
    pop()
    {
        if (this.isEmpty()) {
            console.log("Stack is empty!");
            return null;
        }
        const poppedValue = this.top.value;
        this.top = this.top.next;
        this.size--;
        return poppedValue;
    }

    // Peek operation
    peek()
    {
        return this.isEmpty() ? null : this.top.value;
    }

    // Check if the stack is empty
    isEmpty() { return this.size === 0; }

    // Returns the size of the stack
    getSize() { return this.size; }

    // Print stack elements
    printStack()
    {
        let current = this.top;
        let stackValues = [];
        while (current) {
            stackValues.push(current.value);
            current = current.next;
        }
        console.log("Stack:", stackValues.join(" -> "));
    }
}

// Example Usage
const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.printStack();
console.log("Top Element:", stack.peek());
console.log("Popped Element:", stack.pop());
stack.printStack();


Time Complexity: All operations in the Stack Class ( Push , Pop, Peek, isEmpty, Size,) have O(1) time complexity.
Auxiliary Space : O(1) for all operations except print(), which is O(n).

Also Check

Easy Problems

  • Infix to Postfix
  • Prefix to Infix
  • Prefix to Postfix
  • Postfix to Prefix
  • Check for balanced parentheses
  • Reverse a string using stack

Medium Problems

  • Mergable Stack
  • Previous Smaller Element
  • Next Greater Element
  • Stock Span Problem
  • Next Greater Frequency Element
  • Delete middle of a stack
  • Check if a queue can be sorted into another queue

Hard Problems

  • Largest Rectangular Area in a Histogram
  • Sum of Max of all Subarrays
  • Stack Permutations
  • Length of the longest valid substring
  • Find if an expression has duplicate parenthesis
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.