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.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay