I'm a developer having difficulties in differentiating between the terms Call Stack and Task Queue. Is there anyone who can help me explain the difference?
Thanks in advance.
A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.
Often people use these somewhat interchangeably but a call stack is solely a data structure. It describes a stack of function calls along with various associated state like the values of local variables, return states, etc. The stack is also a data structure, but it's ultimately a memory allocator.
At the most basic level, a call stack is a data structure that uses the Last In, First Out (LIFO) principle to temporarily store and manage function invocation (call).
A stack is, by definition, last in first out (LIFO). A first in first out (FIFO) data structure would be a queue, not a stack. Show activity on this post. The callstack gets filled from bottom to top: first f1() gets added (this function is being executed), when a subfunction ( f1.
eg.
in JavaScript there is function called timeout. when you call the function timeout on the "call stack" it would register in "job queue". It won't fire immediately, but will be triggered once the time is reached.
timeout(function(){
console.log("one");
}, 100);
console.log("two");
in call stack, console.log("one")
is triggered first, but in job queue, the result will display after two
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With