The <script> element embeds executable code or data — most often JavaScript, either inline or via src. It can also hold JSON or other data when type is a non-JavaScript MIME type.
Control loading with async, defer, and type="module". Provide a no-JS fallback with <noscript>.
Baseline · Widely available.
Not deprecated. Core script support has been stable since about July 2015 (and earlier). Some attributes have uneven support.
Deprecated attributes.
Do not use charset or language on <script>. Documents are UTF-8; use type instead of language.
Experimental extras.
attributionsrc (Attribution Reporting) is newer and limited — skip it until you need that API.
Syntax
HTML Syntax
syntax.html
<script src="file.js"></script>
Both start and end tags are required (even when empty with src). Inline form: <script>/* JS here */</script>.
Reference
Attributes
The attributes you will use most often on <script>:
Set type to a valid non-JS MIME type (e.g. application/json).
Put JSON in the element body. The browser will not execute it as script.
Give it an id so another script can find it.
Parse with JSON.parse in a classic or module script.
Edge Cases & Pitfalls
Most bugs come from blocking the parser, misusing async/defer, or trusting untrusted script sources.
loading
async / defer need src
On classic inline scripts they have no effect (MDN). Prefer defer when order and DOM readiness matter; async when scripts are independent.
order
async does not keep order
If script B needs A, do not mark both async. Use defer (or modules) so they run in document order.
security
Never inject untrusted script text
Treat user content as data. Use CSP, and consider integrity for CDN scripts.
deprecated
Skip charset and language
UTF-8 is required for HTML documents. Identify language with type when needed.
a11y
Provide a no-JS path when needed
Use <noscript> for essential content or instructions when scripting is off.
Compatibility
Browser Support
The <script> element is core HTML in every browser. Module scripts, import maps, and some newer attributes have later start versions.
✓ Baseline · Widely available
<script>
Classic scripts are universally supported. ES modules are widely available in modern browsers; pair nomodule for legacy fallbacks when you must support older clients.
98%Classic scripts (approx.)
Google ChromeAll / modules modern
Full support
Mozilla FirefoxAll / modules modern
Full support
Apple SafariAll / modules modern
Full support
Microsoft EdgeAll / modules modern
Full support
OperaAll / modules modern
Full support
Samsung InternetModern
Full support
Internet ExplorerClassic only
Partial
<script>Widely supported
Bottom line: Safe for classic and modern module scripts. Prefer defer or modules to avoid blocking; skip deprecated charset/language.
Remember
Key Takeaways
Role:<script> embeds or loads JavaScript (or non-JS data blocks via type).
Fallback:nomodule for classic scripts when shipping modules.
Legacy: skip deprecated charset / language.
One line: load JS with <script src> + defer (or modules); keep inline scripts small.
UA style
Default CSS Settings
Scripts are not shown as page content. Typical user-agent styling:
default.css
script {
display: none;
}
Do not rely on styling script for UI. Put visible messages in HTML elements your script updates.
Frequently Asked Questions
The script element embeds or loads executable code — usually JavaScript — or holds non-JS data blocks (for example JSON) when type is a non-JavaScript MIME type.
Both download without blocking HTML parsing when used with src. async runs as soon as the file is ready (order not guaranteed). defer runs in document order after parsing, before DOMContentLoaded. Neither applies to classic inline scripts.
External scripts often use defer in the head, or sit just before </body>. Modules defer by default. Avoid classic parser-blocking scripts in the middle of the page unless you need them immediately.
It treats the script as an ES module: deferred by default, can use import/export, and cross-origin loads use CORS. Pair with nomodule for a classic fallback on older browsers.
No for new work. Both are deprecated. Documents use UTF-8; omit charset. Use type instead of the old language attribute.
No. script is Baseline and widely available. Some attributes (charset, language) are deprecated; some newer ones (for example attributionsrc) are experimental.