When you write JavaScript, you should write them in a JavaScript file that ends with a .js
extension. These
JavaScript files can be named anything you want. Most people name their first JavaScript file either app.js
or main.js
Then, to link a JavaScript file to HTML you should use <script>
tag with the src attribute pointing to the
JavaScript file. Place this tag before </body>
tag.
Ensure the JavaScript file linked properly by adding this alert()
function to your JavaScript file. if the link is
correct, you’ll see a prompt when you open your index.html
in a browser. Afterwards, remove this alert code.
JavaScript Placement in HTML
It’s not recommended to add your JavaScript file within the <head>
tag, as it can delay HTML and CSS display. if
needed, use the async
attribute.
Relative and Absolute Paths
Use a relative path to link your JavaScript file, starting with the file name or folder. Absolute paths start
with a /
or an HTTP protocol and are used for external resources like jQuery