![]() calculation calls addTwo, which adds it to the call stack.With the result of addOne, addTwo exits as well and is being removed from the call stack.When it exits, it is removed from the call stack. addOne doesn't call any other functions.addThree calls addTwo, which is added to the call stack.calculation() calls addThree(), which again is added to the call stack.main calls calculation(), which is why it is added to the top of the call stack.This function is added to the call stack. The file loads and the main function is being called, which stands for the execution of the entire file.The steps of the previous example are the following: The order in which the stack processes each function call follows the LIFO principle (Last In, First Out). Note how each function call is being added to the call stack and removed once it finishes.Įvery time a function calls another function, it's added to the top of the stack, on top of the calling function. I created this small visualization to make it easier to understand: The call stack is a mechanism that helps the JavaScript interpreter to keep track of the functions that a script calls.Įvery time a script or function calls a function, it's added to the top of the call stack.Įvery time the function exits, the interpreter removes it from the call stack.Ī function either exits through a return statement or by reaching the end of the scope. JavaScript can do one single thing at a time because it has only one call stack. You've probably already heard that JavaScript is single-threaded. ![]() Web APIs, the callback queue, and the event loop are all features that the browser provides.Ī representation of NodeJS would look similar, but in this article, I'll focus on how JavaScript works in the browser. Note how most of the things in the graphic aren't part of the JavaScript language itself. I will cover each of them in this section.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |