Skip to main content
markp9652695
Inspiring
February 19, 2019
Answered

JS Main Function

  • February 19, 2019
  • 2 replies
  • 1508 views

The code shown below is from page 70 of Grant Gamble's InDesign JS book. The author states that main() function is called that will execute all of the code. Will someone please explain the reasoning for this? What is the purpose of the main() function?

var g = {};

main();

g = null;

function main(){

blnResult = createDialog();

//if(blnResult){addWatermark()};}

This topic has been closed for replies.
Correct answer Manan Joshi

main is the name of a function, a function can be thought of as a collection of statements that are clubbed together under a name using which we can reference these statements and execute them. A function may take an input and produces an output.

In JS the code is read from top to bottom and executed, so in the code snippet you shared the author has defined a method called main. The definition of a function starts with the keyword function followed by the function name and an argument list which is enclosed in () and separated by a comma. So your code is executed in a top bottom fashion like below

var g = {};          //Creates an object

main();              //Main method is called, i.e. the statements defined in the main function are executed

g = null;           //the object is set to null

//This is the definition of the main function,

function main(){

blnResult = createDialog();          //Method names createDialog is called here, and its output value is stored in blnResult

//if(blnResult){addWatermark()};}

Look at the comments i put in your code to explain each statement. main is not a special method you can name it anything you like, just the rule that the code is executed top to bottom should be remembered. Also if you write all the statements of the main method outside then also the code will execute the same like below

var g = {};          //Creates an object

blnResult = createDialog();        

//if(blnResult){addWatermark()};

g = null;           //the object is set to null

Hope this helps

-Manan

2 replies

willcampbell7
Legend
February 21, 2019

In JavaScript (or ExtendScript, Adobe's flavor of it) a main() function is not necessary. There is no harm in having one, but there's no need to. As for why so many scripts have a main() function, I can only guess it comes from programmers of C/C++ (and other similar languages) where main() is required, as it is the default entry point for programs in those languages.

Personally I don't use a main() function but I do like to wrap scripts completely in an immediately executing function, just to make the variable scope private. Also not necessary, but one of those things that stem from habits learned programming JavaScript in other situations, like web apps or server-side Node.

William Campbell
markp9652695
Inspiring
February 21, 2019

Very helpful comment. Thank you, williamc3112933.

Manan JoshiCommunity ExpertCorrect answer
Community Expert
February 19, 2019

main is the name of a function, a function can be thought of as a collection of statements that are clubbed together under a name using which we can reference these statements and execute them. A function may take an input and produces an output.

In JS the code is read from top to bottom and executed, so in the code snippet you shared the author has defined a method called main. The definition of a function starts with the keyword function followed by the function name and an argument list which is enclosed in () and separated by a comma. So your code is executed in a top bottom fashion like below

var g = {};          //Creates an object

main();              //Main method is called, i.e. the statements defined in the main function are executed

g = null;           //the object is set to null

//This is the definition of the main function,

function main(){

blnResult = createDialog();          //Method names createDialog is called here, and its output value is stored in blnResult

//if(blnResult){addWatermark()};}

Look at the comments i put in your code to explain each statement. main is not a special method you can name it anything you like, just the rule that the code is executed top to bottom should be remembered. Also if you write all the statements of the main method outside then also the code will execute the same like below

var g = {};          //Creates an object

blnResult = createDialog();        

//if(blnResult){addWatermark()};

g = null;           //the object is set to null

Hope this helps

-Manan

-Manan
markp9652695
Inspiring
February 20, 2019

Very helpful! Thank you, Manan, for the excellent detailed explanation.