How to Add JavaScript to an HTML Page with the script Element
The <script> element runs JavaScript in a web page. Write the code between <script> and </script>, or point its src attribute at a separate .js file. Put it at the end of body, so the elements it changes are already on the page.
Syntax
<script>
<code>
</script>
<script src="<path>"></script>
| Part | What it does |
|---|---|
<script> … </script> | Runs the JavaScript <code> written between the two tags. |
src="<path>" | Loads the JavaScript from a separate file instead, with a path relative to the HTML file. |
</script> | Closes the element. It's needed even when the element has src and nothing between its tags. |
MDN's script page lists all its attributes.
The Example
The example page has two paragraphs that say Waiting.... A script written inside the page changes the first one, and a script in a separate file, script.js, changes the second.
Step 1: Write the JavaScript File
Create a folder for the example. In it, create a file named script.js:
document.getElementById('second').textContent = 'Set by script.js.';
It finds the element whose id is second and changes its text.
Step 2: Write the HTML Page
In the same folder, create a file named index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Script element</title>
</head>
<body>
<p id="first">Waiting...</p>
<p id="second">Waiting...</p>
<script>
document.getElementById('first').textContent = 'Set by the script in the page.';
</script>
<script src="script.js"></script>
</body>
</html>
The first script element holds its code inside the page. The second loads script.js from the same folder. Both come after the paragraphs, at the end of body.
Step 3: Open It in a Browser
Double-click index.html. It opens in your browser:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- The browser ran each script when it reached it, and by then both paragraphs were already on the page.
- The first paragraph's text came from the code inside the page, and the second's from
script.js. - Neither says Waiting... any more, because the scripts changed them as the page loaded.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing <script src="script.js" /> with a slash and no closing tag | The rest of the page disappears: the browser read everything after it as part of the script, so the page showed only the paragraph above it, and script.js didn't run | Always close it: <script src="script.js"></script> |
Writing code between the tags of a script element that has src | That code never runs: in a test, only script.js ran, and the code between the tags was ignored | Use two script elements, one with src and one with the code |
Putting the script in head without defer | It runs before the elements exist: the browser's console showed Cannot set properties of null (setting 'textContent') | Put the script at the end of body, or add defer |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Everything after a script tag is missing from the page | The script element isn't closed | Add </script> |
Cannot set properties of null | The script ran before the element it changes was on the page | Move the script to the end of body, or add defer |
The code between the script tags doesn't run | The element also has src | Move the code into its own script element |
FAQ
Where should the script element go?
At the end of body, as in the example, or in head with the defer attribute: <script src="script.js" defer></script> waits until the browser has read the whole page. In a test, a script in head with defer changed the paragraph just like the one at the end of body.
Do I need type="text/javascript"?
No. JavaScript is the default, so a plain <script> runs JavaScript, as in the example.
Can one page have several script elements?
Yes. The example has two, and the browser ran both.
Conclusion
The script element runs JavaScript, either from the code between its tags or from the file named in src. Always close it with </script>, and put it after the elements it works with.
Additional Resources
- script: The Script element, on MDN.
