DEV Community

jerry enebeli
jerry enebeli

Posted on

4 1

Understanding Call Stacks In Java Script

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.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs