[ACCEPTED]-In JavaScript, what code executes at runtime and what code executes at parsetime?-runtime

Accepted answer
Score: 39

A javascript file is run in a 2-pass read. The 8 first pass parses syntax and collects function 7 definitions, and the second pass actually 6 executes the code. This can be seen by noting 5 that the following code works:


function foo() {
  return 5;

but the following 4 doesn't

foo(); // ReferenceError: foo is not defined

foo = function() {
  return 5;

However, this isn't really useful 3 to know, as there isn't any execution in 2 the first pass. You can't make use of this 1 feature to change your logic at all.

Score: 4

Unlike C++, it is not possible to run logic 7 in the Javascript parser.

I suspect that 6 you're asking which code runs immediately 5 and which code runs when you create each 4 object instance.

The answer is that any code 3 in a function that you call will only run 2 when you call the function, whereas any 1 code outside of a function will run immediately.

Score: 4

Not sure what you ask exactly so I'll just 12 share what I know.

JavaScript functions are 11 "pre loaded" and stored in the browser's 10 memory which means that when you have function 9 declared in the very end of the page and 8 code calling it in the very beginning, it 7 will work.

Note that global variables, meaning 6 any variable assigned outside of a function, are 5 not preloaded, so can be used only after 4 being declared.

All commands outside of a function will be parsed in the order they appear.

JavaScript doesn't really 3 have "runtime", it can only respond to events 2 or have code executed via global timers. Any 1 other code will be parsed and "forgotten".

Score: 3

While JavaScript's direct ancestor is Scheme, JavaScript 3 didn't inherit macros, so the answer is 2 fairly simple: there is never any code run during 1 parse time.

Score: 2

Roughly speaking, Interpreter gets all variables 3 and functions first, and then they get hoisted and 2 executed.

For more detail, I hope these links 1 might be helpful:

More Related questions