👀 Live Preview
Inline script with a click handler:

The <script> tag embeds JavaScript in HTML pages. This guide covers syntax, src, async, defer, placement, inline vs external scripts, and best practices for beginners.
Add interactivity.
External files.
Load control.
head or body.
In-document code.
Best practices.
<script> Tag?The <script> tag is an essential HTML element used to embed or reference JavaScript code within an HTML document. It enables developers to enhance the functionality and interactivity of web pages.
Use script for inline JavaScript or link external .js files with the src attribute.
In HTML5, JavaScript is the default scripting language, so type="text/javascript" is optional on script elements.
Embed JavaScript directly inside a script element:
<script>
function greetUser() {
alert("Hello, User!");
}
</script><script src="app.js" defer></script>type="text/javascript" in HTML5 unless using a different script type.src, the element must be empty — no inline code inside.defer or async on external scripts in head for better loading.script blocks to hide JavaScript.| Topic | Code Snippet | Notes |
|---|---|---|
| Inline script | <script>...</script> | In-document JS |
| External file | src="app.js" | Empty element |
| defer | defer | Run after parse |
| async | async | Run when ready |
| ES module | type="module" | Modern JS |
| Browser support | Universal | All browsers |
async vs defer| Attribute | Download | Execute |
|---|---|---|
async | Parallel with HTML parsing | As soon as downloaded (unordered) |
defer | Parallel with HTML parsing | After HTML parsed (in order) |
| Neither | Blocks parsing while downloading | Immediately when encountered |
The <script> tag supports attributes such as src, async, and defer that control how scripts load and execute.
<script src="app.js" defer></script>src ExternalURL of an external JavaScript file.
src="app.js"defer LoadingDownload in parallel; execute after HTML is parsed.
deferasync LoadingDownload in parallel; execute as soon as ready.
asynctype OptionalScript language. Default is JavaScript; use module for ES modules.
type="module"The placement of the <script> tag within the HTML document can impact page loading and rendering. Consider placing scripts in the head with defer or async, or at the end of the body for inline scripts.
<head>
<script src="app.js" defer></script>
</head>
<body>
<!-- Your HTML content -->
<script>
// Inline JavaScript code
</script>
</body>Embed inline JavaScript and link external .js files with the script tag.
Inline script with a click handler:
Use <script> for inline JavaScript or external files referenced with src.
The script tag lets you include JavaScript directly within your HTML document.
<script>
function displayMessage() {
alert("This is an inline script!");
}
</script>For larger scripts or better organization, use the src attribute to link an external JavaScript file.
<script src="app.js" defer></script>onclick-only interactions with proper event handlers and focusable controls.Parser encounters script in head or body.
Inline code runs immediately, or src file is fetched.
Attributes delay execution until parse completes or file is ready.
JavaScript responds to users and updates the DOM dynamically.
The <script> tag is supported in all major browsers, including Internet Explorer.
All browsers support <script> with src, async, and defer.
Bottom line: Use <script> confidently to embed JavaScript in any browser.
Mastering the <script> tag is pivotal for web developers seeking to unlock the full potential of JavaScript. Whether it is inline scripts for small tasks or external scripts for large applications, understanding script placement and loading attributes is a crucial step toward creating dynamic web experiences.
defer or async when appropriatesrc elementstype="text/javascript" as requiredonclick for complex app logic<script>Bookmark these before you embed JavaScript.
Add behavior.
PurposeExternal files.
AttributesAfter parse.
Loadinghead or body.
PerformanceIn-document.
PatternAll browsers.
Compatibilityasync runs when downloaded. defer runs after HTML parsing, in order.defer in head for external files, or inline scripts before </body>.Practice <script> with inline code, defer, and placement in the Try It editor.
6 people found this page helpful