JavaScript execution stack

javascript function call/invocation

In javascript we execute or call a function by a pair of parenthesis '()'. So if a function defined as function a(){} to call that function we need to use a().

single threaded synchronous

In javascript code executes in a synchronous mode line-by-line. Browser's engine-thread executes javascript along with other tasks. From our perspective we can consider only one thread allocated for all the javascript getting executed on a given browser.
You may confuse then what asynchronous ajax all about; we will see that in coming lessons.

function stack

Let's see how a block of code with function will get executed in javascript.
In javascript when execution context getting created all the function are put into memory space and they get their own execution context. Have a look at the code below. function b(){ console.log('Im in b'); } function a(){ b(); console.log('Im in a'); } a(); At this point of time when js engine will get this piece of code it will first start giving access to global object context(global execution context already there need not to create), then it will find the line a();and it will create dedicated execution context for that function and will put on top of global execution context. Next it will get b(); and it will create another execution context and will put on top of execution context crated for a(). So basically it will create a stack of execution context. Cinque Terre So from now it will first execute b();, which will print "Im in b" in console. Function b() execution is done and it would get removed from stack. Cinque Terre Now next line from function a(); in function a the next line is print in console "Im in a" and function a is done, so it would be removed from execution stack.
At last only global execution context will be there which is part of javascript engine, within browser. Cinque Terre
Back Next