First of all, I call func1 and so it gets on top of the global execution context. And then it calls func2. What I wanna know is, after calling func2, does func1 immediately return or get off the execution stack? Or is it like, first func2 gets on top of func1 execution context, it returns and then func1 returns and finally we're back to global execution context?
func1();
function func1 () {
func2();
}
function func2 () {
const x = 2;
}
Function calls are implemented as a stack. When func1
is called, it immediately calls func2
. When func2
returns, it returns to the scope of func1
and continue from there. Browser optimizations might realize that there are no more instructions after func2()
and skip the chain back up, but that's implementation-dependent.