Open In App

What is Call Stack in JavaScript ?

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The call stack is a fundamental concept in JavaScript’s runtime environment. It is a mechanism that keeps track of the execution context of functions in a program. When a function is called, a new frame is pushed onto the top of the call stack, representing the context of that function’s execution. When the function completes, its frame is popped off the stack, allowing the program to return to the context of the calling function.

Here are key points about the call stack:

  1. Function Calls:
    • When a function is called, a new frame is created and pushed onto the top of the call stack.
    • This frame contains information about the function, including its arguments and local variables.
  2. Execution Context:
    • The call stack maintains the execution context of the currently executing function.
    • It keeps track of which line of code is being executed and the values of local variables.
  3. Last In, First Out (LIFO):
    • The call stack operates on a Last In, First Out (LIFO) basis. The last function called is the first to complete and be removed from the stack.
  4. Recursion:
    • Recursion involves a function calling itself. Each recursive call adds a new frame to the call stack.
    • If not managed properly, recursion can lead to a stack overflow, where the call stack becomes too large.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads