Search code examples
javascripthtmlw3cweb-standardsstandards-compliance

Why are JS scripts usually place in the header of a document?


Why are JS scripts usually place in the header of a document? Is it required by standards, or is it just a convention with no particular reason?


Solution

  • It's just a convention. It's usually recommended to put scripts at the end of the body so the page can display before loading them, which is always a plus. Also, document.body can't be used until the document is loaded or if you put the script in the body.