JavaScript Document scripts Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

document.scripts returns a live HTMLCollection of every <script> element in the document (MDN). You can use it like an array — length, indexes, and loops.

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.scripts. Start with this line.

JavaScript
document.scripts
CallWhat it does
document.scriptsPrimary signature.

Built into the browser. Read or assign document.scripts — no import required.

Library declarations
JavaScript
document.scripts

Parameters

ParameterDescription
HTMLCollectionRequired. Live list of <script>
Writable?Optional. Read-only property — mutate the DOM instead

Return Value

Return ValueDescription
*Live HTMLCollection of HTMLScriptElement

JavaScript Examples

Example 1 — Does the Page Have Scripts? (MDN)

MDN pattern: check document.scripts.length.

JavaScript
const scripts = document.scripts;

if (scripts.length) {
  console.log("This page has scripts!");
  console.log("count:", scripts.length);
}
Try It Yourself

How It Works

  1. Get the collection — MDN returns every <script>.
  2. Truthiness of length — 0 is falsy; any count is truthy.
  3. Log a message when scripts exist (MDN uses alert; we use console.log).
  4. Read length for the exact count.

Example 2 — Loop src and type

Inspect each HTMLScriptElement in the collection.

JavaScript
for (const s of document.scripts) {
  console.log(
    s.getAttribute("src") || "(inline)",
    s.type || "text/javascript"
  );
}
Try It Yourself

How It Works

  1. Iterate with for…of — each value is an HTMLScriptElement.
  2. Prefer getAttribute("src") for the authored path; empty means inline.
  3. Read type — default classic scripts often omit it.
  4. Tip: s.src alone is usually an absolute URL for external scripts.

Example 3 — The Collection Is Live

Append a <script> and watch document.scripts.length update.

JavaScript
const list = document.scripts;
console.log(list.length);  // before

const s = document.createElement("script");
s.textContent = "// demo";
document.body.appendChild(s);

console.log(list.length);  // after — increased by 1
Try It Yourself

How It Works

  1. Store the collection once — you do not need to call document.scripts again.
  2. Create and append a real <script> element.
  3. Length updates live — the HTMLCollection tracks the DOM.
  4. Contrast with QSA — a saved querySelectorAll("script") result would stay frozen.

Browser Support

Document.scripts is a core HTML Document collection. MDN marks it Baseline Widely available (since June 2018).

✓ Baseline · Widely available

Document.scripts

Live HTMLCollection of every <script> element. Safe for everyday use.

99% Widely available
Google Chrome Yes
Yes
Microsoft Edge Yes
Yes
Mozilla Firefox Yes
Yes
Apple Safari Yes
Yes
Opera Yes
Yes
Internet Explorer Yes (legacy)
Yes
scripts Excellent

Bottom line: Use freely in modern browsers. See the MDN scripts reference.

Frequently Asked Questions

MDN: an HTMLCollection of every <script> element in the document. You can use it like an array to get each element.
No. MDN marks Document.scripts as Baseline Widely available (since June 2018). It is not Deprecated, Experimental, or Non-standard.
document.scripts lists every <script> in the document. document.currentScript is only the classic script currently being evaluated (or null).
Yes. HTMLCollection updates when <script> elements are added or removed from the document.
Both work. document.scripts is the dedicated Document API and returns a live HTMLCollection. querySelectorAll returns a static NodeList snapshot.
Yes. Any <script> in the document tree is included — with or without a src attribute.

Next: scrollingElement

Continue with the Document scrollingElement property.

scrollingElement →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful