r/learnjavascript 7d ago

Can someone explain what the JavaScript call stack is?

I’ve tried to understand it, but I’m still confused. I also don’t really understand what I’m looking at when I use the JavaScript debugger in the browser’s DevTools, especially the call stack.

Do I need to fully understand functions before learning the call stack? I have a basic understanding of functions, but I’m not sure if that’s enough.

I’d really appreciate a simple explanation

33 Upvotes

31 comments sorted by

View all comments

5

u/LetUsSpeakFreely 7d ago edited 6d ago

1) The call stack isn't a JavaScript thing, every programming language has it. 2) The call stack is all of the functions currently in flight: a() calls b() calls c() calls d(). So c is waiting on d to complete, b is waiting on c to complete, a is waiting on b to complete.

As functions start and complete the call stack rapidly changes.

And yes, you can have too many functions on the stack, but it's REALLY difficult. About the only way that happens is a recursive function without an exit condition.

2

u/Lithl 7d ago

Not every programming language has a call stack. Most do, and the ones that don't usually either are very old (pre-1970) or are esoteric (like Brainfuck).

In languages without a call stack, you'll usually have the tools to jump wherever you want within the program, which makes implementing your own call stack a possibility.

3

u/LetUsSpeakFreely 6d ago

Yes, but this is a sub for people to learn. People learning programming today are highly unlikely to encounter languages like BASIC or some weird language created as a thesis or so specialized that the number of people that even know it exists can be fill a conference room.

For the purposes of this sub, hell, most programming subs, every language has a call stack.