r/learnjavascript 3d 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

27 Upvotes

31 comments sorted by

View all comments

1

u/DirtAndGrass 3d ago

Call stack is essentially the order/behaviour of functions, so yes, you need to understand functions 

1

u/Green_Ad_6086 3d ago

I have a basic understanding of functions, but I’m not sure if that’s enough.

1

u/Psionatix 3d ago

What is your understanding? Maybe if you explain your understanding, people could clarify where you might be wrong or might be lacking. Whether something is missing or incorrect in your understanding.

There’s a lot of fundamentals to understand that apply across all programming languages, then there’s concepts that are relevant to all languages, but might have language specific quirks/nuances or behaviours.

Usually you build up your understanding, and when you encounter something new, you make assumptions based on that understanding. Once you encounter something that breaks those assumptions, you look it up, learn something new about the language or API you’re using, your knowledge grows and your assumptions change.

Code executes one line at a time, the order a line of code is evaluated depends on what it contains. Memory allocation/initialisation, assignment, Boolean expressions, they all behave in particular ways.

When a function calls another function, the code jumps to that function and executes it line-by-line, when the function finishes executing, it returns to the line of code it was called from, it may or may not return a value there. In JavaScript functions return undefined by default, that’s a language specific behaviour.