Search code examples
javascripthtmlbrowserv8

Lifecycle of javascript file inside an html file


I have a very basic question about how javascript engine of a browser say chrome executes a javascript file embedded inside the html file. I am assuming, first the html page is rendered and then javascript is executed. My question is that when the engine reaches last line of the js file, what next?. Does all the callback methods of events remain active even after hitting the last line or after last line is hit, everything related to the js file is washed out from memory and js terminates?. Or does the js writer have to take care to put a loop in js file so that the js is functional and the last line is never hit.


Solution

  • Does all the callback methods of events remain active even after hitting the last line?

    Yes.

    does the js writer have to take care to put a loop in js file so that the js is functional and the last line is never hit?

    No; in fact such an endless loop would provide a pretty bad user experience.

    More generally speaking: it's very easy to try out these things and see for yourself. All you need is a browser and a text editor :-)
    (Or even just an in-browser editor like codepen.io or jsfiddle.net or a bunch of others.)