Do Execution Context created first or main function called first while executing a JS file?

82 Views Asked by At

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

There are 3 best solutions below

0
T.J. Crowder On

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");

0
Aditya Raj singh On

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

0
MajidTaheri On

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);