DEV Community

Shashi Bhushan Kumar
Shashi Bhushan Kumar

Posted on

What is Call Stack in JavaScript?

πŸ“š What is Call Stack in JavaScript? (Explained Simply)

The Call Stack is where JavaScript executes your code.

It is a data structure that follows one simple rule:

πŸ‘‰ Last In, First Out (LIFO)

The last function added to the stack runs first.


🧠 How Call Stack Works

When a function is called:

  1. It gets pushed into the Call Stack.
  2. JavaScript executes it.
  3. After completion, it gets removed (popped).

JavaScript can only execute one thing at a time,

so it uses the Call Stack to manage everything.


πŸ’» Example

function first() {
  console.log("First");
}

function second() {
  first();
  console.log("Second");
}

second();
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž What Happens Internally?

  • second() is pushed into stack.
  • Inside it, first() is pushed.
  • first() runs and is removed.
  • Then second() continues.

🍽 Simple Real-Life Example

Imagine a stack of books.

You always place a book on top.

You always remove the top book first.

That’s exactly how Call Stack behaves.


🎯 Important Interview Point

The Call Stack works on:

Last In, First Out (LIFO)

Understanding this helps you understand:

  • Event Loop
  • setTimeout behavior
  • Stack overflow errors

Top comments (0)