There are two best working method to use script file

1 - if you have external javascript file then you link JS file  in the head by script  tag

Or Before the end of body tag by script tag  it work properly in both case

Example 01

<!---*********** This is index.html file ********---->

<DOCTYPE html>

<head>

<title> how use script file </title>

<!--************** attached external js file( index.js ) in head section  ****************/

<script src="index.js"> </script>

</head>

<body>

<p> Hey script tag working in both case </p>

<!--- ******you can also include external js file here*******--->

</body>

</html>



2 - If you have not external js file then you should write script before the end of body

Example 02

<body>

<p> use script in same file </p>

<script>

alert(" you can script code in here its working fine ");

</script>

</body>