loading...

Understanding Call Stacks In Java Script

jerryenebeli profile image jerry enebeli ・1 min read

What is a call stack?

A call stack is a region in memory that keeps track of the running/active subroutine/function in a program. it follows a first in last out approach(FILO). When a function is called it is pushed on top of the call stack and when it returns it is popped out of the call stack.

Global execution context?

A global execution context is always the first function on the stack. it is an anonymous function the creates the environment in which the javascript code runs. For an environment like the browser the global execution context creates a global object called windows and assigns it to "this".

Stack limits and overflow

As interesting as stacks sounds they also have a memory limit just like any storage. the limit of the stack determines the total number of functions that can be on the stack at once. Stack limits differ in various browsers. when a stack limit is exceeded it causes an error known as stack overflow.

The quickest way to cause a stack overflow is by recursion.

function sayHello() {
    sayHello()
}

sayHello();

The above code will cause a stack overflow because it keeps adding to the stack and exceed the given stack memory allocation.

Javascript has only one call stack

Javascript is a single-threaded language and what this means for the call stack is javascript can only have one call stack.

Discussion

pic
Editor guide