Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
What is the inline function in JavaScript ?
Next article icon

What is the Call Stack in JavaScript ?

Last Updated : 10 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In JavaScript, the Call Stack is an essential concept that helps the JavaScript engine keep track of function execution. It plays a vital role in managing the execution order of functions and determining how the JavaScript program handles function calls.

How Does the Call Stack Work?

JavaScript operates in a single-threaded environment, meaning that it can only execute one operation at a time. The Call Stack is the part of JavaScript that keeps track of the execution process.

  • Function Call: When a function is called, the JavaScript engine pushes the function onto the Call Stack. The function remains in the stack until it completes its execution.
  • Executing the Function: The JavaScript engine processes the function in the stack. It runs the code inside the function until it reaches the end or encounters another function call.
  • Nested Function Calls: If a function calls another function, the new function is pushed onto the Call Stack. The engine continues to execute the newly pushed function while the previous one waits for completion.
  • Returning from a Function: Once a function finishes executing, it is removed (popped) from the stack. The JavaScript engine then continues executing the function that is now at the top of the stack.
  • Completion of Program: The process continues until all functions in the stack are executed and removed. Once the stack is empty, the JavaScript engine has completed the execution of the program.

Now let’s understand this with the help of example

JavaScript
function f1() {     console.log('Hi by f1!'); }  function f2() {     f1();     console.log('Hi by f2!'); }  f2(); 

Output

Hi by f1!
Hi by f2!

In this example: The steps and illustrations below explain the call stack of the above function.

  • Step 1: When the code loads in memory, the global execution context gets pushed in the stack.

  • Step 2: The f2() function gets called, and the execution context of f2() gets pushed into the stack.

  • Step 3: The execution of f2() starts and during its execution, the f1() function gets called inside the f2() function. This causes the execution context of f1() to get pushed in the call stack.

  • Step 4: Now the f1() function starts executing. A new stack frame of the console.log() method will be pushed to the stack.

  • Step 5: When the console.log() method runs, it will print “Hi by f1” and then it will be popped from the stack. The execution context will go back to the function and now there are no lines of code that remain in the f1() function, and as a result, it will be popped from the call stack.
  • Step 6: This will similarly happen with the console.log() method that prints the line “Hi by f2” and then finally the function f2() would finish and would be pushed off the stack.

What is Stack Overflow ?

A stack overflow occurs when the call stack exceeds its memory limit, typically due to excessive function calls. This can happen when a program enters into infinite recursion or if too many functions are called and the stack runs out of space.

JavaScript
function funcA() {   funcB(); }  function funcB() {   funcA(); } 

In this example

  • funcA calls funcB, which adds funcA to the stack.
  • funcB calls funcA, which adds funcB again.
  • This cycle continues, eventually exceeding the available stack space

Why Do We Need the Call Stack?

  • Tracks Function Execution: Keeps track of which function is running at any given time.
  • Handles Nested Calls: Manages nested function calls, ensuring that functions run in the correct order.
  • Manages Recursion: Helps with recursive functions by keeping track of each function call.
  • Ensures Proper Return: Ensures the program returns to the correct point after each function finishes executing.
  • Manages Program Flow: Helps maintain the correct flow of execution, especially in complex programs with many functions.

Conclusion

The Call Stack in JavaScript manages the order of function execution, handling nested calls and recursion. It ensures functions run in the correct sequence and are properly returned to after execution. Improper use, like infinite recursion, can lead to a stack overflow. Understanding it is key to maintaining efficient program flow.



Next Article
What is the inline function in JavaScript ?

D

drajawat760
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • What is Call in JavaScript ?
    The call method is used to invoke the function with different this object. In JavaScript, this refers to an object. It depends on how we are calling a particular function. In the global scope, this refers to the global object window. The inside function also refers to the global object window. In st
    2 min read
  • What is Callback Hell in JavaScript ?
    One of the primary ways to manage asynchronous operations in JavaScript is through callback functions that execute after a certain operation completes. However, excessive use of callbacks can lead to an issue known as Callback Hell, making code difficult to read, maintain, and debug. What is Callbac
    4 min read
  • What is the inline function in JavaScript ?
    In JavaScript, an inline function is a special type of anonymous function that is assigned to a variable, or in other words, an anonymous function with a name. JavaScript does not support the traditional concept of inline functions like C or C++. Thus anonymous function and inline function is practi
    2 min read
  • What is the practical use for a closure in JavaScript?
    In JavaScript, closures are defined as inner functions that have access to variables and parameters of outer function even after the outer function has returned. The below examples show the practical use of closures: Example: In this example, a variable mult is defined that is local to the function
    2 min read
  • What is arguments in JavaScript ?
    In this article, we will learn about arguments object in JavaScript in detail. Like what is arguments object in JavaScript and then we will discuss some programs using arguments object. We will discuss the following points: What is the arguments in JavaScript?Programs related to arguments object.The
    3 min read
  • What is the (function() { } )() construct in JavaScript?
    If you've ever played around with JavaScript, you might have seen this expression. It's like a strange set of symbols, but it has a special name that is an immediately invoked function expression, or IIFE. In this article, we will understand each element of the expression as well as the functionalit
    3 min read
  • What is An Event Loop in JavaScript?
    The event loop is an important concept in JavaScript that enables asynchronous programming by handling tasks efficiently. Since JavaScript is single-threaded, it uses the event loop to manage the execution of multiple tasks without blocking the main thread. [GFGTABS] JavaScript console.log("Sta
    4 min read
  • What is the first class function in JavaScript ?
    First-Class FunctionA programming language is said to have First-class functions if functions in that language are treated like other variables. So the functions can be assigned to any other variable or passed as an argument or can be returned by another function. JavaScript treats function as a fir
    2 min read
  • Why is Currying in JavaScript Useful ?
    Currying in JavaScript is a functional programming technique that involves transforming a function that takes multiple arguments into a sequence of functions, each taking a single argument. This technique is particularly useful in JavaScript due to its support for higher-order functions and closures
    3 min read
  • What is JavaScript?
    JavaScript is a powerful and flexible programming language for the web that is widely used to make websites interactive and dynamic. JavaScript can also able to change or update HTML and CSS dynamically. JavaScript can also run on servers using tools like Node.js, allowing developers to build entire
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences