Where to place your JavaScript code, in head section or body section? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Where to place your JavaScript code, in head section or body section?

According to HTML it should be placed in head section, because it interats with users and does not display in front of users. However, in the real world, where JavaScript code to be placed depends on user experience. If the interaction should let users to experience, then it should be in body section. If the interaction only runs behind, then it should be in head section.

26th Oct 2017, 2:48 PM
Agnès Lee
2 Answers
+ 2
For me, I place ALL of my JS code into its own isolated script file, and then I just link the script into the HTML. I can't assume if you're doing that or not, but I mention it because it's good practice, so just a little tip if you're not doing that. From my experience, most of the time it'll be placed into the head, but sometimes it's been better for me to place it at the bottom of the body. Ultimately, it really depends upon how you have your site set up and designed, and what you're trying to accomplish. Just use your discernment and if it's necessary to place it elsewhere, then do what you need to do.
26th Oct 2017, 3:06 PM
AgentSmith
+ 1
The most appropriate place in head as said in html. I am a web developer and I always place mine in head section. Code is placed very rarely in body section.
26th Oct 2017, 2:52 PM
Sahil Mohile