How to Connect your HTML file to a JavaScript file

8/22/2022

JavaScript can be connected to your HTML in the head section, your code will look like the below: In the script source (src), you can use a relative or absolute path. [code] [/code] Note: The defer attributes tells the browser to only execute the JavaScript after the HTML has been fully loaded. This is especially useful because the JS (in our case) is loaded in the head (before any of the HTML's body is loaded) - without the defer attribute the JS may attempt to query HTML elements that don't yet exist (since the JS is called before the HTML body is loaded) causing our code to break. Please note that the above is one way to do it. Another way to do it would be to place the script attribute before the last HTML element in the body section.