Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Program entry point

JavaScript does not require a main() function. The host environment initiates evaluation of a script or module, and top-level code can explicitly call a function that starts the application.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

function main() {
  // Application logic
}
main();

A program entry point is where program execution begins. ECMAScript does not require a special main() function or automatically call one because of its name. The host environment selects code and initiates script or module evaluation.

For a script, declarations are prepared before statements execute. Top-level code then executes according to control flow. A declaration such as function main() { ... } does not execute the function body: an explicit main() call does that.

A module has a separate process for loading, linking, and evaluating dependencies. Therefore, do not assume that the first statement in the entry file is the first code executed in the whole application. Dependency cycles and asynchronous evaluation further affect ordering.

In the example, main is an ordinary name chosen by the author. The statement console.log(main()); explicitly calls main() and passes its return value directly to console.log, which prints Ready to the console. Entry-file selection and the availability of environment APIs are not determined by ECMAScript alone.

Examples

function main() {
  return "Ready";
}

console.log(main());

Result

Ready

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources