skip to Main Content

While executing a js file, firstly a global execution context is created to execute the entire js code. Again, a main function is also get called to execute the same code.

So my question is, do the main function is called first and then the global execution context gets created or first the global execution context gets created first and then the main function gets created?

I think, first the Global Execution context gets created and then the main function gets created.

3

Answers


  1. Again, a main function is also get called to execute the same code.

    JavaScript doesn’t have any "main" function concept like there is in some C or Java contexts. Once the global execution context is created, execution starts at the beginning of the code in the script you ran (more in the specification). (Assuming we’re talking about a non-module script.) For example:

    console.log("No main function");
    Login or Signup to reply.
  2. In js there is nothing like main function

    So the first time Js engine ( v8 engine ) recieves a code what it does ->
    — it creates GEC (GLOBAL EXECUTION CONTEXT).
    — Then it start the execution of code
    line by line ** here i am taking
    about synchronous code **
    — When it encounters Asynchronous
    code it throws it into Callback
    Queue
    — After every code executed , the GEC
    is removed by Js engine

    Login or Signup to reply.
  3. usually script languages(python,JS,…) don’t need to main function. they bootstrapping process by file and not function . also if you need async process you should define another function . name of this function does not matter . JS developers prefer main as function name

    async function main(){ // main or whatever
       const m=await loadFile();
    }
    
    main().then(console.log,console.error);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search