Should I always put my JavaScript file in the head tag of my HTML file?


You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tag or <body> tag.

It’s good for performance to add JavaScript in <body> element. Here’s an example to add <script> under <body>…</body> −

<html>
   <body>
      <script>
         <!--
            document.write("Hello World!")
         //-->
      </script>
   </body>
</html>


Here’s an example to add <script> under <head>…</head> −

<html>
   <head>
      <script>
         <!--
            document.write("Hello World!")
         //-->
       </script>
   </head>
   <body>

   </body>
</html>

Ali
Ali

Updated on: 13-Jun-2020

332 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements