Understand the two ways to include JavaScript in a web page and learn when to use each approach for clean, scalable projects.
Internal JavaScript — code written directly inside the HTML file
using <script> tags. Everything lives in one file.
External JavaScript — code written in a separate .js
file and linked to the HTML using the src attribute. The logic lives
outside the HTML, keeping each file focused on one responsibility.
Think of it like note-taking:
<!DOCTYPE html>
<html>
<head><title>Internal JS</title></head>
<body style="font-family:sans-serif;padding:16px">
<h2 id="text"></h2>
<script>
document.getElementById("text").innerText = "Hello from Internal JS";
</script>
</body>
</html>
<script> tag within the HTML file// script.js
document.getElementById("text").innerText = "Hello from External JS";
<!DOCTYPE html>
<html>
<head><title>External JS</title></head>
<body>
<h2 id="text"></h2>
<script src="script.js"></script>
</body>
</html>
script.js — the HTML stays cleansrc attribute tells the browser where to find the filescript.js can be linked by multiple HTML pages<body>
<h2 id="text"></h2>
<!-- Script at the BOTTOM of body -->
<script>
document.getElementById("text").innerText = "Works fine";
</script>
</body>
<script> at the bottom ensures the HTML loads firstnull errors from targeting elements that don't exist yetImagine building a project:
In a real project these would be three separate files. Below they are shown individually, then combined into a live preview so you can see the result.
<!DOCTYPE html>
<html>
<head>
<title>External JS Demo</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1 id="heading">Welcome</h1>
<button onclick="changeText()">Click Me</button>
<script src="script.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
button {
padding: 10px 20px;
cursor: pointer;
font-size: 1rem;
}
function changeText() {
document.getElementById("heading").innerText = "Text Changed!";
}
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
button { padding: 10px 20px; cursor: pointer; font-size: 1rem; }
</style>
</head>
<body>
<h1 id="heading">Welcome</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerText = "Text Changed!";
}
</script>
</body>
</html>
<h2 id="title"></h2>
<script>
// your code here
</script>
document.getElementById("title") to select the headinginnerText to "My First Page" using internal JS<!-- index.html -->
<button onclick="changeBackground()">Change Color</button>
<script src="script.js"></script>
index.html with a button linked to script.jsscript.js, write a changeBackground() functiondocument.body.style.background to any color<script></script> tags within the HTML file.
src attribute points to the external file path. link and href are not valid for <script> tags.
.js file can be linked to multiple HTML pages, and keeping logic separate makes maintenance and team collaboration much easier.
Always place <script> at the end of <body> — this ensures all HTML elements exist before JS tries to access them.
Use the defer attribute for even better performance — the script downloads in parallel but executes only after the HTML is fully parsed:
<script src="script.js" defer></script>
External JS enables three major benefits in professional projects:
// Reusability — one script.js linked by 10 pages
// Maintenance — fix a bug in one place, fixed everywhere
// Collaboration — designers touch HTML, devs touch JS
Avoid inline event handlers like onclick="..." in HTML — they mix behavior with structure. Prefer event listeners in your JS file (covered later):
<!-- Avoid this -->
<button onclick="doSomething()">Click</button>
<!-- Prefer this (in your .js file) -->
<!-- button.addEventListener('click', doSomething); -->
Will this code work? Predict what happens before revealing the answer.
<head>
<script>
document.getElementById("demo").innerText = "Hello";
</script>
</head>
<body>
<h2 id="demo"></h2>
</body>
// It will NOT work.
// The script runs inside <head> — before the browser has
// created the <h2 id="demo"> element in <body>.
// Result: getElementById("demo") returns null → TypeError.
// Fix 1 — move the script to the bottom of <body>:
//
// <body>
// <h2 id="demo"></h2>
// <script>
// document.getElementById("demo").innerText = "Hello"; // works
// </script>
// </body>
// Fix 2 — keep it in <head> but use defer:
//
// <script src="script.js" defer></script>
// defer waits for the full HTML to load before executing.