kb/data/developer.mozilla.org/en-US/docs/Glossary/Call_stack-0.md

12 KiB

title chunk source category tags date_saved instance
Call stack - Glossary | MDN 1/3 https://developer.mozilla.org/en-US/docs/Glossary/Call_stack reference web, html, css, javascript, documentation 2026-05-05T05:26:49.121286+00:00 kb-cron

MDN HTML HTML: Markup language

HTML reference

HTML guides

Markup languages

CSS CSS: Styling language

CSS reference

CSS guides

Layout cookbook

JavaScriptJS JavaScript: Scripting language

JS reference

JS guides

Web APIs Web APIs: Programming interfaces

Web API reference

Web API guides

All All web technology

Technologies

Topics

Learn Learn web development

Frontend developer course

Learn HTML

Learn CSS

Learn JavaScript

Tools Discover our tools

About Get to know MDN better

Blog

  1. Glossary
  2. Call stack

Call stack

A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.

  • When a script calls a function, the interpreter adds it to the call stack and then starts carrying out the function.
  • Any functions that are called by that function are added to the call stack further up, and run where their calls are reached.
  • When the current function is finished, the interpreter takes it off the stack and resumes execution where it left off in the last code listing.
  • If the stack takes up more space than it was assigned, a "stack overflow" error is thrown.

In this article

Example

The call stack will be empty at the very beginning, and the code above would be executed like this:

  1. Ignore all functions, until it reaches the greeting() function invocation.
  2. Add the greeting() function to the call stack list, and we have:
- greeting

  1. Execute all lines of code inside the greeting() function.
  2. Get to the sayHi() function invocation.
  3. Add the sayHi() function to the call stack list, like:
- sayHi
- greeting

  1. Execute all lines of code inside the sayHi() function, until reaches its end.
  2. Return execution to the line that invoked sayHi() and continue executing the rest of the greeting() function.
  3. Delete the sayHi() function from our call stack list. Now the call stack looks like:
- greeting

  1. When everything inside the greeting() function has been executed, return to its invoking line to continue executing the rest of the JS code.
  2. Delete the greeting() function from the call stack list. Once again, the call stack become empty.

In summary, then, we start with an empty Call Stack. Whenever we invoke a function, it is automatically added to the Call Stack. Once the function has executed all of its code, it is automatically removed from the Call Stack. Ultimately, the Stack is empty again.

See also