Skip to content

Latest commit

 

History

History
25 lines (17 loc) · 1.58 KB

43-Call-Stack.md

File metadata and controls

25 lines (17 loc) · 1.58 KB

Call Stack in JavaScript Engine

In JavaScript, the call stack is a data structure that keeps track of where in the program we currently are. Whenever a function is called, a new frame is added to the top of the call stack representing that function call. When that function completes, its frame is removed from the top of the call stack and control returns to the previous frame.

A call stack is important because it allows JavaScript to keep track of function calls and their parameters. It also makes sure that functions are executed in the order they were called, with each function completing before the next one starts.

Here's an example of how the call stack works:

function foo() {
  console.log('foo');
}

function bar() {
  console.log('bar');
  foo();
}

bar();

When the bar function is called, a new frame is added to the top of the call stack representing the bar function call. The console.log('bar') statement is then executed, followed by the call to the foo function. This adds another frame to the top of the call stack representing the foo function call.

When the foo function completes, its frame is removed from the top of the call stack and control returns to the bar function. The bar function then completes and its frame is removed from the top of the call stack as well.

In summary, the call stack is a crucial part of how JavaScript engines execute code. It keeps track of function calls and their parameters, ensuring that functions are executed in the correct order and allowing for the orderly execution of complex programs.