Q:

Why does alert show up before document.write?

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!");

A:

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.

