This is the window object in web browsers, or its equivalent in other JS environments. It acts as a container for all global variables and functions.
this Keyword:In the global context, this also references the global object.
During the memory creation phase, the JavaScript engine performs the following steps:
this Value Determination: The value of the this keyword is determined based on the context. In the global context, this typically refers to the global object (like window in browsers).
function nameOne() {
console.log("one")
}
function nameTwo() {
console.log("two")
}
function nameThree() {
console.log("three")
}
nameOne()
nameTwo()
nameThree()
Overall Execution Flow in JavaScript
Global Execution Context (GEC) Creation
- Variable Object: nameOne, nameTwo, and nameThree are hoisted and placed into memory.
- Scope Chain: The GEC forms the base of the scope chain.
- this: Set to the global object (e.g., window in browsers).
Global Execution Context (GEC) Execution
- nameOne() Called:
- FEC for nameOne Created:
- Creation Phase: Variable Object (VO) for nameOne is created (no internal variables), scope chain is established, this likely points to the global object.
- Execution Phase: "one" is logged to the console.
- FEC for nameOne Destroyed: Control returns to the GEC.
- nameTwo() Called:
- FEC for nameTwo Created: (similar process to nameOne)
- FEC for nameTwo Destroyed
- nameThree() Called:
- FEC for nameThree Created: (similar process to nameOne)
- FEC for nameThree Destroyed
- GEC Destroyed: The program finishes.
Key Points
- Call Stack: The JavaScript engine uses the call stack to keep track of which function is currently executing and to manage the creation and destruction of function execution contexts.
- One at a Time: Functions are executed one after the other with each function getting its own temporary execution context.
- Hierarchy: The GEC is the base, and each function call temporarily creates a new execution context on top of it.
Understanding Execution Contexts in JavaScript: Handling User Authentication Example
Let's delve into execution contexts with a detailed example comparing global and function execution contexts in a real development scenario.
Example: Handling User Authentication
Suppose you're building a web application that requires user authentication. You have a function to check if a user is logged in and another function to display the user's profile. Let's explore how execution contexts work in this scenario.
// Global variables
var isLoggedIn = false;
var username = "guest";
// Function to check if user is logged in
function checkLogin() {
if (isLoggedIn) {
console.log("User is logged in as " + username);
} else {
console.log("User is not logged in");
}
}
// Function to display user profile
function displayProfile() {
checkLogin(); // Calling checkLogin function from displayProfile function
console.log("Displaying user profile for " + username);
}
// Simulating user login
function login(user) {
isLoggedIn = true;
username = user;
console.log("User " + username + " logged in successfully");
}
Now, let's examine how execution contexts work:
Global Execution Context:
- When the JavaScript file is loaded, the global execution context is created.
- The global variables
isLoggedIn
and username
are created in the global execution context.
- The functions
checkLogin()
, displayProfile()
, and login(user)
are also defined in the global execution context.
- These functions are available globally and can be called from anywhere in the script.
Function Execution Context:
- When a function is called, a new function execution context is created for that function.
- For example, when
displayProfile()
is called, a new function execution context is created for it.
- Within the
displayProfile()
function execution context, variables like isLoggedIn
and username
are resolved first in the function's local variable environment. If not found, JavaScript looks up the scope chain to the outer variable environment (the global execution context) to resolve them.
- Similarly, when
checkLogin()
is called from within displayProfile()
, a new function execution context is created for checkLogin()
.
Real Development Scenario:
In a real development scenario, you might use this setup to handle user authentication on a website.
- The
checkLogin()
function could be called whenever you need to verify if the user is logged in before displaying certain content or accessing protected resources.
- The
displayProfile()
function could be used to display the user's profile information after they log in.
- The
login(user)
function could be called when the user successfully logs in, updating the global variables isLoggedIn
and username
.