Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

main() function in JavaScript?

I have seen a main() function in some JavaScript files I have come across. Is it the same main function as you use in other languages such as C#, C++?? If you put a main function in your JS file, is that where the code starts executing? Or is it just another name used for a function?

I have searched the web but didn't find anything useful regarding this matter.

like image 785
Isuru Avatar asked Dec 25 '11 13:12

Isuru


People also ask

What is the main function in JavaScript?

JavaScript Functions. JavaScript provides functions similar to most of the scripting and programming languages. In JavaScript, a function allows you to define a block of code, give it a name and then execute it as many times as you want.

Do we have a main function in JavaScript?

Many programming languages require a special user-written function that marks the begin of the execution. For example, in C this function must always have the name main() . In JavaScript, however, such a function is not required.

Why is main () important?

The main() function is required so that the startup code knows where execution of your code should start.

What is main function in node JS?

The main() is just a wrapper being used by Node. js and stays in the call stack as long as the main (non-callback) part of the program is running. The function gets added to the stack, then the statements inside the function get added as they are called and removed as they are executed.


2 Answers

No, main is not the same in JavaScript as in C languages. It's just another function, but the original programmer is probably using the name as a convention to indicate where the code should start running.

like image 117
FishBasketGordo Avatar answered Oct 05 '22 21:10

FishBasketGordo


"Main" function has nothing different then any other function (Its just a name). It will run when called and not automatically.

like image 45
Alon Adler Avatar answered Oct 05 '22 21:10

Alon Adler