When the following code, in the order written, is executed, why does the alert still come first even though it's on the 2nd line? Thanks!
document.write ("Hello World");
alert("You wrote to the document!");
When the following code, in the order written, is executed, why does the alert still come first even though it's on the 2nd line? Thanks!
document.write ("Hello World");
alert("You wrote to the document!");
Javascript is interpreted so it goes line by line so the document.write
is running first and alert
is running after it.Moreover, in single threaded javascript model the further execution is stopped till the response of popups are received.
The reason why you don't see the result is that the rendering of HTML
is stopped due to alert popup so all the popups will be seen before most of the rendering of your page.
This link will help you understand better.
Modern browsers are set up such that once they encounter a JavaScript code they will essentially pause the rendering of HTML and run though the entire JavaScript before they resume the HTML rendering.