Interview Question
Why <script> tags should be placed at the end of body tag?
SrijanJan 25, 2025 - JavaScript
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.
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.