Onload on window and document

Please explain
What is the purpose of onload function used with document and window?
Also why is console.log(); hi2 logging before hi?
Please help

wondow.onload is used to execute certain code on page load. If you have anything that you want the page to do or behave in a certain way when it loads , that code should go there.

But here you are missing a closing brace for the onload function. That could be a problem.