The three ways to add JavaScript to an HTML page
JavaScript runs inside your HTML page, but the browser needs to know where to find it. You have three options: write JavaScript directly in your HTML file using a <script> tag, link to a separate JavaScript file, or put JavaScript directly into HTML elements as event handlers. Most real projects use a separate file, but all three work depending on what you're building.
The method you choose affects how your code is organized, how easy it is to reuse, and how fast your page loads. A single-page project might use inline JavaScript. A website with multiple pages almost always uses a separate file so you don't repeat code on every page.
Key Takeaways
- The <script> tag tells the browser where JavaScript code lives — either inside the tag itself or in a separate file you link to.
- Put <script> tags at the bottom of your HTML body, just before the closing </body> tag, so the page loads before JavaScript runs.
- Link to a separate JavaScript file using <script src="filename.js"></script> when you want to reuse the same code on multiple pages.
- Event handlers like onclick let you run JavaScript when a user clicks a button or interacts with an element, but they work best for simple actions.
- The browser reads your HTML top to bottom, so if JavaScript tries to use an element that hasn't loaded yet, it will fail silently.
Method 1: Write JavaScript inside a <script> tag in your HTML
The simplest way to add JavaScript is to write it directly in your HTML file inside a <script> tag. Open your HTML file in a text editor, find the closing </body> tag near the bottom, and add your code just before it.
Here's what it looks like:
<body> <h1>My Page</h1> <button id="myButton">Click me</button> <script> document.getElementById("myButton").addEventListener("click", function() { alert("Button was clicked!"); }); </script> </body>
When the browser reads this file, it loads the HTML first, then runs the JavaScript code inside the <script> tag. Putting the tag at the bottom ensures the button exists before JavaScript tries to find it. If you put the script at the top of your file, before the button, the code will run before the button loads and won't work.
Method 2: Link to a separate JavaScript file
When you have more than a few lines of code, or when you want to use the same JavaScript on multiple pages, create a separate file with a .js extension. This keeps your HTML clean and lets you reuse code without copying and pasting.
Create a new file called script.js in the same folder as your HTML file. Put your JavaScript code inside it — no <script> tags needed:
document.getElementById("myButton").addEventListener("click", function() { alert("Button was clicked!"); });
Then in your HTML file, link to it using a <script> tag with a src attribute, again just before the closing </body> tag:
<body> <h1>My Page</h1> <button id="myButton">Click me</button> <script src="script.js"></script> </body>
The browser will download and run script.js when it reaches that line. If you have multiple HTML pages in the same project, you can link all of them to the same script.js file, and any changes you make to the JavaScript will work on every page automatically.
Method 3: Add JavaScript directly to HTML elements
For very simple actions, you can put JavaScript directly into an HTML element using attributes like onclick, onmouseover, or onchange. The browser runs the code when that event happens.
Here's a button that runs JavaScript when clicked:
<button onclick="alert('Hello!')">Click me</button>
This works, but it's harder to read and harder to maintain as your code grows. Most developers avoid this method for anything beyond a single line. If you need to run more complex code, use Method 1 or Method 2 instead.
Why the order of your <script> tags matters
The browser reads your HTML from top to bottom. When it hits a <script> tag, it stops, runs the code, and then continues. This means if your JavaScript tries to use an element that hasn't loaded yet, it won't find it.
For example, this won't work:
<head> <script> document.getElementById("myButton").addEventListener("click", function() { alert("Clicked!"); }); </script> </head> <body> <button id="myButton">Click me</button> </body>
The script runs before the button loads, so the browser can't find an element with the id "myButton". The button will load, but clicking it won't do anything. Move the <script> tag to the bottom of the body, after the button, and it will work.
Checking that your JavaScript is connected and running
Open your HTML file in a web browser. Right-click anywhere on the page and select Inspect or Inspect Element. This opens the browser's developer tools. Click the Console tab to see if any errors appear in red text.
If you see an error like "Cannot read property 'addEventListener' of null", it usually means the JavaScript ran before the HTML element loaded. Move your <script> tag to the bottom of the body. If you see "404 Not Found" or "Failed to load resource", the browser can't find your JavaScript file — check that the filename and path in your src attribute match exactly.
You can also add a simple test line to your JavaScript to confirm it's running. Add this at the top of your code:
console.log("JavaScript is connected!");
Save the file, refresh the page in your browser, open the Console again, and you should see that message printed. If you don't see it, the script isn't loading or running.
Common mistakes and how to fix them
The file path is wrong. If you link to a JavaScript file that doesn't exist, the browser silently fails. Make sure the filename in your src attribute matches exactly — including uppercase and lowercase letters. If your JavaScript file is in a subfolder, include the folder name: <script src="js/script.js"></script>.
You're using the wrong file extension. JavaScript files must end in .js, not .txt or anything else. If you created the file in Notepad, make sure you saved it as script.js, not script.js.txt.
The script tag is in the wrong place. Put it at the bottom of your body, just before </body>. If it's in the head or before the elements it needs to use, those elements won't exist yet when the code runs.
You forgot the closing tag. <script src="script.js"></script> has both an opening and closing tag. <script src="script.js" /> (self-closing) doesn't work for external files.
Frequently Asked Questions
Can I put multiple <script> tags in one HTML file?
Yes. You can have as many <script> tags as you need. The browser runs them in order from top to bottom. If one script depends on another, make sure the first one loads before the second.
Should I put <script> tags in the head or the body?
Put them at the bottom of the body, just before </body>. This lets all your HTML load first so JavaScript can find the elements it needs. Putting scripts in the head makes the page load slower because the browser waits for the JavaScript to finish before showing the page.
What's the difference between <script> and <script src>?
<script> with code inside runs JavaScript written directly in your HTML. <script src="file.js"> loads and runs JavaScript from a separate file. Use the second method when you want to reuse code on multiple pages or keep your HTML file clean.
Why does my JavaScript code run but nothing happens?
Open the browser's developer tools (right-click, then Inspect, then Console tab) and look for error messages. Common reasons: the element your code is looking for doesn't exist yet (move the script tag to the bottom), the element's id or class name doesn't match what your code is searching for, or the file path in your src attribute is wrong.
Can I use JavaScript without HTML?
Not in a web browser. JavaScript runs inside HTML pages. You can write and run JavaScript in other environments like Node.js, but for web pages, you always need HTML to connect to.