๐คฏ Under the hood of the JavaScript series!
3๏ธโฃ Execution context and call stack?
A Thread๐งต๐
๐ค A real-world scenario of the execution context.
โ Suppose, the execution context is like the "stadium" of the game, where players can play the game.
โ So just like this, there is also a "stadium" in JavaScript where all code will be executed.
โ This "stadium" is known as the ๐๐น๐ผ๐ฏ๐ฎ๐น ๐๐ ๐ฒ๐ฐ๐๐๐ถ๐ผ๐ป ๐๐ผ๐ป๐๐ฒ๐ ๐ in JavaScript.
โ As we already know that a game can be played in one stadium at a time, similarly JavaScript has only one ๐๐น๐ผ๐ฏ๐ฎ๐น ๐๐ ๐ฒ๐ฐ๐๐๐ถ๐ผ๐ป ๐๐ผ๐ป๐๐ฒ๐ ๐ (GEC) in a JS file.
๐ What is ๐๐น๐ผ๐ฏ๐ฎ๐น ๐๐ ๐ฒ๐ฐ๐๐๐ถ๐ผ๐ป ๐๐ผ๐ป๐๐ฒ๐ ๐?
โ After the compilation of JavaScipt code, a global execution context will be created.
โ Global Execution Context is the default place where all global JavaScript code (which is not inside a function) gets executed.
โ Or, a global execution context will be created for the top-level code, which is not inside a function.
โ And when we call a function then there is a new execution context will be created inside the Global Execution Context.
โ There can be more than one execution context placed inside the Global Execution Context.
โ Simple, when we call a function then there is a new execution context that will be created inside the Global Execution Context every time.
โ A Global Execution Context can contain global let, const, var declaration, function, scope chain, etc.
โ Remember, there can be more than one execution context inside the Global Execution Context.
โ So all these execution contexts make up together a ๐ฐ๐ฎ๐น๐น ๐๐๐ฎ๐ฐ๐ธ.
๐ What is a ๐ฐ๐ฎ๐น๐น ๐๐๐ฎ๐ฐ๐ธ?
โ A call stack is the combination of a Global Execution Context as well as more than one execution context that is kept inside a function.
โ All execution contexts will be stacked on top of each other.
โ When the Global Execution Context will be created it will be stored in a call stack just like in the above picture.
โ And when we call a function then the new execution context will be created after completing the job it will remove from the call stack.
โ Just like LIFO
That's all for now, we will meet in the next thread๐
But if you like make sure to:
1. Follow me @ATechAjay
2. Retweet the first tweet.
3. Turn on the notification to never miss these amazing tweets.
Thank you so much for staying to the end of this thread.