Example 1 — Log the Running Script
At top level, currentScript is the <script> element executing now.
const el = document.currentScript;
console.log(el.tagName); // "SCRIPT"
console.log(el.id); // e.g. "demo"
console.log(!el.src); // true for inline scripts How It Works
- Run at top level inside a classic
<script>. - Read the element — MDN returns an
HTMLScriptElement. - Inspect attributes like
idand whethersrcis empty (inline). - Not a module — module scripts do not set
currentScript.

