0% found this document useful (0 votes)
2 views9 pages

Call Stack

The call stack is a mechanism in JavaScript that tracks function calls using a last in, first out (LIFO) structure. When a function is called, it is added to the top of the stack, and once it finishes executing, it is removed, allowing the program to return to the previous function. This ensures that functions are executed in the correct order, as demonstrated through an example involving two functions, f1() and f2().

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Call Stack

The call stack is a mechanism in JavaScript that tracks function calls using a last in, first out (LIFO) structure. When a function is called, it is added to the top of the stack, and once it finishes executing, it is removed, allowing the program to return to the previous function. This ensures that functions are executed in the correct order, as demonstrated through an example involving two functions, f1() and f2().

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CALL STACK

Interviewquestion-153

Follow on
@Duvvuru Kishore
The call stack is a mechanism that JavaScript
uses to keep track of function calls.
When a function is called, JavaScript adds it to the top of
the call stack.If that function calls another function, the
new function is also added to the top of the stack.This
process continues, stacking each new function on top of
the previous ones.

f2

f1
Once a function finishes executing, it is removed (or
"popped") from the top of the stack, and the program
returns to the function below it. This process continues
until the stack is empty, meaning all the functions have
finished executing.

f2

f1 f1 f1
call stack helps JavaScript keep track of where it is in
a program, ensuring that function calls are executed
in the correct order. It's a "last in, first out" (LIFO)
structure, meaning the last function added to the
stack is the first one to be executed and removed.

If you confused on how last function added is


executed first and this make the correct execution
order. Please follow me through further slides
When you run the below code:
Here's how it executes using the call stack:

Calling f2(): The program starts by calling f2(),


adding it to the call stack.

Inside f2(), Call f1(): Within f2(), f1() is called next.


f1() is added to the top of the stack because it's the
most recent call.

Execute f1(): The program executes f1(), printing


"Hi by f1!" to the screen. Once f1() finishes, it is
removed from the stack.

Resume f2(): The program returns to f2(),


continuing where it left off. It prints "Hi by f2!" to
the screen. After f2() completes, it is removed from
the stack.
In this sequence, the call stack ensures that the
functions are executed in the correct order.
Follow on
@Duvvuru Kishore

You might also like