Where do I put my JavaScript code in my file for better performance?

Where do I put my JavaScript code in my program file? Can I put it at the beginning or at the end?

Last updated:7/19/2021 5:07:24 AM

1 Answers

Ethan Karla
Ethan Karla

You can put your JavaScript code anywhere in the webpage. As we know that the loading of the website starts from the top of the webpage and excites and presents the user one by one. So, you can put it anywhere but let me clear one thing with a scenario.

Suppose, you have created a web page and you have added a large library on the <head> tag at the top of the webpage. So, how will the webpage load. First, it will start loading from above and it will switch to the first script tag. If user's internet speed will be slow then library will take some time and without fully loading that library nothing will render on user machine and one more thing without website content there is no need of javascript for user . So, through this what we have understood is to load the UI of the website first and then after switching to JavaScript. This is the best way to make your website good for your user.

Hope this information has cleared your confusion.

Happy Coding!