Interview Question

Why <script> tags should be placed at the end of body tag?

Srijan } Author Pic
SrijanJan 25, 2025 - JavaScript
Why <script> tags should be placed at the end of body tag? - Reacted Node

Photo: Jorge Jesus/Pexels

As we know that HTML is loaded and executed line by line in browser. So, when the browser encounters a <script> tag, it loads and executes the javascript code on the spot. This may slow down the page rendering speed and thus webpage will take more time to load.

1<body>
2 <!-- Other contents -->
3 <!-- .... -->
4 <script src="myscripts.js"></script>
5</body>

Javascript is often used to manipulate DOM and add new functionality to the webpage. If <script> tag is not added at end of the <body> tag, DOM may not be ready by that time, thus preventing javascript to work on it, leading to unknown behaviors.

Therefore, we should always add <script> tag at the end of the <body> tag to prevent unknown behaviors and make the webpage load faster.