Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungRefreshed repository pull requests page generally available(22.09.2026 um 03:25 Uhr)
Sichere ProgrammierungThe Joy of Learning the Basics Again(22.09.2026 um 03:28 Uhr)
Sichere ProgrammierungZero-Code OpenTelemetry Tracing for Dagster(22.09.2026 um 03:39 Uhr)
Linux Tipps & Hardening`prime-all`(22.09.2026 um 02:28 Uhr)
IT Security Toolsopensoho v0.15.2(22.09.2026 um 03:33 Uhr)
IT Security NachrichtenUS Proposes AI Incident Alert System in Talks With China, Bessent Says(22.09.2026 um 04:01 Uhr)
Sichere ProgrammierungRefreshed repository pull requests page generally available(22.09.2026 um 03:25 Uhr)
Sichere ProgrammierungThe Joy of Learning the Basics Again(22.09.2026 um 03:28 Uhr)
Sichere ProgrammierungZero-Code OpenTelemetry Tracing for Dagster(22.09.2026 um 03:39 Uhr)
Linux Tipps & Hardening`prime-all`(22.09.2026 um 02:28 Uhr)
IT Security Toolsopensoho v0.15.2(22.09.2026 um 03:33 Uhr)
IT Security NachrichtenUS Proposes AI Incident Alert System in Talks With China, Bessent Says(22.09.2026 um 04:01 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

[JS] The top-level scope is NOT always the global scope

Understanding the scope in JavaScript is crucial for writing robust and maintainable code. While in browsers, the top-level scope has historically been synonymous with the global scope. And it is also called the global object shared by all…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Understanding the scope in JavaScript is crucial for writing robust and maintainable code. While in browsers, the top-level scope has historically been synonymous with the global scope. And it is also called the global object shared by all scripts. However, Node.js has a noteworthy distinction. The top-level scope is not the global scope. I will delve into the difference between the top-level scope in browsers and Node.js using globalThis, which can access to the global object in the both JavaScript environments.






The Global Scope in Browsers



In traditional browser environments, any code that does not specifically start up as a background task has a Window as its global object. When a variable is declared using var outside of any function or block, it becomes a global variable associated with the Window object. Let us illustrate this with a simple example:




// Browser Example

<script>
var globalVar = "I am global";

console.log(globalVar); // Outputs: "I am global"
console.log(globalThis.globalVar); // Outputs: I am global
</script>






In this case, globalThis is the Window object. globalVar is accessible globally and can be accessed from the top-level scope associated with the Window object.



However, if I add the type="module" to the script tag, the result changes.




// Browser Example

<script type="module">
var localVar = "I am local";

console.log(localVar); // Outputs: "I am local"
console.log(globalThis.localVar); // Outputs: undefined
</script>






ECMAScript evaluates the internal code as a Module rather than a Script. The top-level var creates a global variable in Script, while it does not in Module scope.






A Different Point in Node.js



On the other hands, Node.js has different behavior from browsers. Scripts running under Node.js have an object called global as their global object. Variables declared with var at the top level of a module are local to that module. Let us demonstrate this with an example.




// Node.js Example

var localVar = "I am local";

console.log(localVar); // Outputs: I am local
console.log(globalThis.localVar); // Outputs: undefined






In this case, localVar is scoped to the module, and attempting to access it from another module would result in an undefined variable.



In both Node.js CommonJS modules and native ECMAScript modules, top-level variable declarations are scoped to the module, and do not become properties of the global object.



That is about it. Happy coding!

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten [JS] The top-level scope is NOT always the global scope

Thematisch verwandte Begriffe: toplevel, scope, always, global · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-49449 | Joplin is an open source note-taking and to-do application that organise…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick