Is there a main () method like java that can be run directly in js?

is there a main () method like java in js that can be run directly in writing?

Mar.05,2021

all statements of reasonable js are run directly, such as executing

with node
//main.js
console.log("It's ok");

I think what you mean is that you see a lot of defined classes and functions but don't use them, so it looks like no statements are executed.

function print(){
  console.log("It's ok");
}
// print()

(function main(){
    //main
})()

so here's the problem. We all know that JAVA's main () method runs directly. Why? Sit and wait for the boss downstairs to answer.


do you want the page to be running as soon as it is loaded?

window.onload=function(){
    mian();
}
function mian(){
  console.log("mian");
}

js is an interpreted language, which does not need to be compiled and run directly. Which file you run, which file is the entry
Java is a compiled language, and needs to be compiled and run on JVM , so there is an entry function that makes it easy to find entry


any function that executes immediately is like this.
you don't even have to write a function

Menu