infogexsolutions@gmail.com
Note : We help you to Grow your Business

51

Success in getting happy customer

3

Hundred of successful business

25

Total clients who love HighTech

5

Stars reviews given by satisfied clients

JavaScript execution context

  1. Global Execution Context:

    • Default: The initial, base execution context. There is only one GEC per JavaScript program.
      The global execution context is the outermost context in which JavaScript code is executed.
    • Global Scope: Code not inside any function resides in the global context, and thus has access to global variables and functions.
      In Other Word
      Any code written outside of functions in a JavaScript program operates within the global scope, allowing it to interact with and utilize global variables and functions.
    • Creation: The GEC is created before your code starts executing.
  2. 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.

  3. Function Execution Context (FEC):

    • For Each Function Call: A new FEC is created every time a function is called.
    • Local Scope: Variables and functions declared inside a function have limited visibility (scope) within the FEC and cannot be accessed from outside it.
    • Multiple FECs: There can be many FECs running at the same time (e.g., nested function calls).
    • Each function call creates its own execution context, which includes the function's local variables, parameters, and any nested function declarations.
    • The function's execution context is pushed onto the call stack, and when the function completes execution, its execution context is removed from the stack.

Two main phases in an Execution Context (EC) for JavaScript

  1. Creation Phase

    • Variable Object (VO) Creation: A special object is created to store all variables and function declarations defined within the current context (global or function).
    • Memory Creation Phase in JavaScript

      During the memory creation phase, the JavaScript engine performs the following steps:

      1. Set up memory space for variables and functions before executing the code.
      2. Hoist variable declarations (var, let, and const) and function declarations to the top of their respective scopes.
      3. For variables declared with var, allocate memory space and initialize it with the value undefined. However, the assignment of the value to the variable is not performed until the execution phase.
      4. For function declarations, hoist the entire function body, allowing functions to be called before they are defined in the code.
      5. Allocate memory space for function parameters.
      6. Set up the environment for the execution phase by creating the Variable Environment and Lexical Environment for each execution context.
  2. 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).

  3. Execution Phase

    Code Execution in JavaScript

    • Variable Assignment: Variables declared with var, let, or const are assigned their defined values (or undefined if no value is assigned).
    • Function Calls: When a function is called, a new Execution Context is created specifically for that function call. This process repeats for nested function calls, following the Creation and Execution phases again.
    • Expression Evaluation: Expressions (calculations, comparisons, etc.) are evaluated to produce results.
    • Statement Execution: Statements like if/else, for, and while control the flow of execution.

Key Points of Execution Contexts in JavaScript

code Example


                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

Global Execution Context (GEC) Execution

  1. 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.
  2. nameTwo() Called:
    • FEC for nameTwo Created: (similar process to nameOne)
    • FEC for nameTwo Destroyed
  3. nameThree() Called:
    • FEC for nameThree Created: (similar process to nameOne)
    • FEC for nameThree Destroyed
  4. GEC Destroyed: The program finishes.

Key Points

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:

Function Execution Context:

Real Development Scenario:

In a real development scenario, you might use this setup to handle user authentication on a website.

Summary

Memory Creation Phase

Execution Phase

Web Development
App Development
UI/UX Developer
Cloud
Backend
artificial
Online
computer
Online
Jobs
business
Online