The three ways to add JavaScript to HTML
JavaScript runs inside your HTML page and responds to what visitors do — click a button, type in a box, scroll down. To make that happen, you need to tell HTML where your JavaScript code lives. There are three ways to do this: write JavaScript directly in your HTML file using a <script> tag, link to a separate JavaScript file, or put code inside an HTML element's attributes.
Most projects use a separate JavaScript file because it keeps your code organized and lets you reuse the same script on multiple pages. But each method has a moment when it makes sense, and understanding the difference helps you choose the right one for what you are building.
Key Takeaways
- The <script src="filename.js"></script> tag in your HTML tells the browser where to find your JavaScript file and when to load it.
- Put <script> tags at the bottom of your HTML body, just before the closing </body> tag, so the page loads before JavaScript runs.
- A separate JavaScript file (with a .js extension) keeps your code organized and lets you use the same script on many pages without copying it.
- Inline JavaScript inside HTML tags (like onclick="doSomething()") works for single actions but becomes messy if you have a lot of code.
Linking a separate JavaScript file with the script tag
This is the most common method. Create a file called something like script.js and put all your JavaScript code in it. Then, in your HTML file, add a <script> tag that points to that file.
Here is what it looks like in your HTML:
<!DOCTYPE html> <html> <head> <title>My Page</title> </head> <body> <h1>Hello World</h1> <script src="script.js"></script> </body> </html>
The src attribute tells the browser where to find your JavaScript file. If script.js is in the same folder as your HTML file, you just write the filename. If it is in a subfolder called js, you would write src="js/script.js".
Notice the <script> tag is at the bottom of the body, just before </body>. This matters because the browser reads HTML from top to bottom. If you put the script tag in the <head>, the JavaScript might run before your HTML elements exist, and your code will not find them.
Writing JavaScript directly inside your HTML file
Sometimes you have a small piece of code that does not need its own file. You can write JavaScript directly in your HTML using opening and closing <script> tags.
<body> <button id="myButton">Click me</button> <script> document.getElementById("myButton").addEventListener("click", function() { alert("Button clicked!"); }); </script> </body>
This works fine for a single small script, but if you have a lot of code, your HTML file becomes hard to read. The separate file method is cleaner for anything more than a few lines.
Using onclick and other inline attributes
You can also put JavaScript directly into an HTML element using attributes like onclick, onmouseover, or onload. This runs the code when that specific thing happens.
<button onclick="alert('Hello')">Click me</button>
This is the quickest way to add a single action to a single element. However, it mixes your HTML and JavaScript together, which makes both harder to maintain. If you find yourself writing more than one or two lines of code in an attribute, move it to a separate script tag or file instead.
Where to put the script tag matters
The browser reads your HTML from top to bottom. If your JavaScript tries to find an element that has not been read yet, it will not find it and your code will break.
Put <script> tags at the bottom of your <body>, just before the closing </body> tag. This way, all your HTML elements exist before the JavaScript runs. If you must put a script in the <head>, add the word defer to the opening tag: <script src="script.js" defer></script>. This tells the browser to wait until the HTML is loaded before running the script.
Organizing multiple JavaScript files
As your project grows, you might have multiple JavaScript files. You can link to all of them by adding multiple <script> tags.
<script src="functions.js"></script> <script src="events.js"></script> <script src="animations.js"></script>
The browser loads them in the order you list them, so if one file depends on code from another, put the dependency first. For example, if events.js uses a function defined in functions.js, load functions.js first.
Many developers create a js folder inside their project and put all JavaScript files there, then link to them with src="js/filename.js". This keeps your project organized.
Frequently Asked Questions
What is the difference between putting the script tag in the head versus the body?
The browser reads HTML from top to bottom. If your script is in the head, it runs before the body elements exist, so your code cannot find them. Putting it at the bottom of the body ensures all elements are loaded first. Use the defer attribute if you must put a script in the head.
Do I need to use a separate file or can I put all my JavaScript in the HTML?
You can put it all in one HTML file using <script> tags, but a separate file is better once you have more than a few lines. Separate files are easier to read, easier to reuse on other pages, and easier to debug.
What does the src attribute do?
The src attribute tells the browser where to find your JavaScript file. It works like the href in a link tag — you give it the path to the file. If the file is in the same folder, just write the filename. If it is in a subfolder, write the path like js/script.js.
Can I link to a JavaScript file from the internet instead of my own computer?
Yes. You can use a full URL in the src attribute, like src="https://example.com/script.js". This is how you load libraries like jQuery or Bootstrap. However, your page will not work if that website is down or slow.
What does the defer attribute do?
The defer attribute tells the browser to load the JavaScript file but wait until all the HTML is read before running it. This is useful if you must put a <script> tag in the head but need the HTML elements to exist first.