Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. HTML Tag Dictionary
  3. <script>

<script>

The script element is used to embed JavaScript code inline or to load an external JS file.

Syntax

<!-- Inline JavaScript -->
<script>
  console.log("Hello!");
</script>

<!-- Load an external file -->
<script src="app.js"></script>

<!-- Asynchronous loading (defer recommended) -->
<script src="app.js" defer></script>

Common Attributes

AttributeDescription
srcSpecifies the URL of an external JavaScript file. If set, any inline code inside the tag is ignored.
typeSpecifies the MIME type of the script. Defaults to text/javascript if omitted. Use module to load the script as an ES module.
deferDefers script execution until HTML parsing is complete, just before the DOMContentLoaded event. Only applies to external files.
asyncDownloads the script in parallel with HTML parsing and executes it as soon as the download is complete. Execution order is not guaranteed.
moduleSetting type="module" treats the script as an ES module. Behaves like defer by default.

Sample Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Script Sample</title>

  <!-- defer: runs after the DOM is built (recommended in <head>) -->
  <script src="main.js" defer></script>

  <!-- async: runs immediately after download (use for independent scripts) -->
  <script src="analytics.js" async></script>

  <!-- ES module -->
  <script type="module" src="app.mjs"></script>
</head>
<body>

  <p id="message">Loading...</p>

  <!-- Inline script (traditionally placed at the end of <body>) -->
  <script>
    // The DOM exists here, so you can access it directly
    document.getElementById("message").textContent = "Loaded!";
  </script>

</body>
</html>

Output

When the page loads, the inline script replaces the paragraph text.

<!-- Content displayed on the page -->
Loaded!

Notes

Where you place the script element and how you load it has a significant impact on page load performance. A plain script tag inside <head> blocks HTML parsing, so it is strongly recommended to add the defer attribute when loading external scripts. With defer, the script downloads in the background without blocking parsing and executes after the DOM is fully built.

async is well suited for scripts that operate independently, such as analytics tools or ad tags. Because the execution order of multiple async scripts is not guaranteed, avoid using it for scripts that depend on each other.

Using ES modules (type="module") lets you split your code across files with import/export syntax. Modules behave like defer by default and are only loaded once even if referenced multiple times. On pages where you want to disable script execution entirely, you can use the noscript element to display fallback content.

Browser Support

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
Supported in all versions
Safari Safari
18+
2 or earlier ×
Edge Edge
80+
11 or earlier ×
IE IE
11 or earlier ×
Opera Opera
48 or earlier ×
iOS Safari iOS Safari
18+
1 or earlier ×
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .