JS Where To
In this page:
<script>
// inline JavaScript
</script>
<script src="file.js"></script>
Inline JavaScript
Inline JavaScript is written directly inside an HTML element's attribute, such as onclick, running only when that specific event happens on that specific element.
Example: Inline JavaScript
<button onclick="console.log('Button clicked')">Click Me</button>
Internal Script Tag
An internal script tag places JavaScript inside the HTML document itself, wrapped in <script></script>, keeping the code separate from individual elements while still living in the same file.
Example: Internal Script Tag
<script>
// Print "Running from an internal script tag." to the console
console.log("Running from an internal script tag.");
</script>
External JavaScript Files
An external file keeps all JavaScript in its own .js file, linked into the HTML page with a script tag's src attribute, which keeps HTML clean and lets multiple pages share the exact same code.
Example: External JavaScript Files
<script src="main.js"></script>
Head vs Body Placement
A script placed in the head runs before the browser has parsed the body, which can cause errors if it tries to access elements that don't exist yet, while a script placed just before the closing body tag runs after all HTML above it has already loaded.
Example: Head vs Body Placement
<head>
<script>
console.log(document.body); // null - body not parsed yet
</script>
</head>
<body>
<p>Content</p>
</body>
The defer and async Attributes
defer tells the browser to download the script in the background and run it only after the whole page has been parsed, in order, while async downloads in the background and runs the script as soon as it's ready, regardless of parsing order.
Example: The defer and async Attributes
<script src="main.js" defer></script>
<script src="analytics.js" async></script>
- Writing large amounts of JavaScript inline on HTML elements, which becomes very hard to read and maintain as a page grows.
- Placing a script tag before the elements it needs to access without deferring it, causing errors because those elements don't exist yet.
- Forgetting the src attribute points to the correct relative or absolute path when linking an external JavaScript file.
- JavaScript can live inline on an element, inside an internal <script> tag, or in an external linked file.
- External files are the most maintainable approach for real projects, since the same file can be reused across many pages.
- The defer and async attributes control when an external script actually executes relative to page parsing.
All three placement methods, inline, internal, and external scripts, along with defer and async attributes, are supported by every modern browser.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: