JavaScript can be added to a web page in three main ways: inline (directly on an element), internal (inside a <script> tag in the HTML file), or external (in a separate .js file linked from your HTML). Choosing the right place affects readability, reusability, and performance.
For small demos, inline or internal JavaScript is fine. For real projects, external JavaScript is preferred so that code is organized and reusable across multiple pages.
onclick.<script>...</script> block within the same HTML file..js file and linked using the src attribute of the <script> tag.<body> or loaded with defer so that HTML content loads first.The basic way to include JavaScript is with the <script> tag:
<script> tag syntax
<!-- Internal script -->
<script>
// JavaScript code here
</script>
<!-- External script -->
<script src="app.js"></script>
To improve performance, scripts are often loaded at the end of <body> or with the defer or async attributes:
<!-- Recommended: load after HTML is parsed -->
<script src="app.js" defer></script>
defer loads the script in the background and executes it after the HTML is fully parsed, which is ideal for most normal page scripts.
Inline JavaScript is written directly in the HTML element using event attributes like onclick. This is simple but not recommended for large projects because it mixes structure (HTML) and behavior (JavaScript).
<button onclick="alert('Hello!')">Click Me</button>
Internal JavaScript is written inside a <script> tag in the same HTML file. It is usually placed in the <head> or at the end of the <body>.
<!-- HTML file -->
<script>
function showMessage() {
alert("Internal JavaScript Example");
}
</script>
<button onclick="showMessage()">Click Me</button>
External JavaScript is stored in a separate .js file and linked using the src attribute. This is the preferred way in real-world applications.
HTML file (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>External JS Example</title>
</head>
<body>
<button onclick="showMessage()">Click Me</button>
<!-- Load JavaScript from an external file -->
<script src="script.js" defer></script>
</body>
</html>
script.js:
// script.js
function showMessage() {
alert("Hello from external file!");
}
alert('Hello!') because the JavaScript is written directly in the onclick attribute.showMessage() function defined inside the <script> tag in the same HTML file.script.js. When the button is clicked, showMessage() from the external file runs and shows an alert.Although all three give similar output (an alert box), the organization of your code becomes much cleaner and easier to manage when you move logic into an external file.
Mini interactive check: Where would you place JavaScript for a larger real-world project?
Click one of the buttons to see a suggestion.
<body> or use defer so that HTML content loads first.onclick — keep HTML and JavaScript separate for cleaner code.main.js, app.js, or form-validation.js to describe the purpose of the script.main.js and link it using <script src="main.js" defer></script>.<script> tag in the <head> vs just before </body>. Notice when errors appear if the script runs before elements exist.