Hoisting and Execution Context
What is the difference between the global execution context and a function execution context?
The key difference between the global execution context and a function execution context is that the global context exists for the entire program duration, while function contexts are created and destroyed during function calls. Here are the main distinctions: 1. Creation and Lifecycle: - The global execution context is created when the script first loads and remains until the program ends. - Function execution contexts are created whenever a function is called and destroyed when the function completes execution. 2. Variable Environment: - The global context contains globally defined variables and functions. - Function contexts have their own local variables, parameters, and any variables and functions defined inside them. 3. 'this' Binding: - In the global context, 'this' typically refers to the global object (window in browsers, global in Node.js). - In function contexts, 'this' is determined by how the function is called (the call site). 4. Call Stack Position: - The global context forms the base of the call stack. - Function contexts are pushed onto the stack when functions are called and popped off when they complete. Understanding these differences is crucial for reasoning about variable scope, the behavior of 'this', and how JavaScript manages memory during program execution.