Lädt...


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


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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!

...

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 35.8 Punkte
🔧 Programmierung

🔧 Scope in JavaScript – Global vs Local vs Block Scope Explained


📈 34.14 Punkte
🔧 Programmierung

🪟 Nightingale is always online, but not always fun: An early access review


📈 28.59 Punkte
🪟 Windows Tipps

🔧 Understanding the Scope Chain, Scope, and Lexical Environment in JavaScript


📈 26.54 Punkte
🔧 Programmierung

🔧 Understanding Scope and Scope Chain in JavaScript.


📈 26.54 Punkte
🔧 Programmierung

📰 Qualcomm Snapdragon 8c und 7c für PCs der Reihe „Always On, Always Connected“ vorgestellt


📈 23.27 Punkte
📰 IT Nachrichten

⚠️ Kaspersky Small Office Security Always on security for always on business


📈 23.27 Punkte
⚠️ Malware / Trojaner / Viren

⚠️ Kaspersky Small Office Security Always on security for always on business


📈 23.27 Punkte
⚠️ Malware / Trojaner / Viren

🔧 Understanding JavaScript Scope: Global and Local


📈 20.87 Punkte
🔧 Programmierung

🐧 How to change IP SCOPE to global?


📈 20.87 Punkte
🐧 Linux Tipps

📰 DNSChanger and the Global Scope of Cybersecurity


📈 20.87 Punkte
📰 IT Security Nachrichten

📰 Zukunft Scope und das Wachstum der Global Phone-basierte Authentifizierungslösungen Market ...


📈 20.87 Punkte
📰 IT Security Nachrichten

📰 New WannaCryptor-like ransomware attack hits Ukraine … may be global in scope


📈 20.87 Punkte
📰 IT Security Nachrichten

🔧 Node.js vs. Browser: Understanding the Global Scope Battle


📈 20.87 Punkte
🔧 Programmierung

📰 Blackbaud penalized $3M for not disclosing the full scope of ransomware attack


📈 18.59 Punkte
📰 IT Security Nachrichten

📰 US Supreme Court limits scope of CFAA and rules bribing cops for data is not hacking


📈 18.59 Punkte
📰 IT Nachrichten

🔧 Teach Your LLM to Always Answer With Facts Not Fiction


📈 16.96 Punkte
🔧 Programmierung

📰 Amazon AWS Outage Shows Data in the Cloud is Not Always Safe


📈 16.96 Punkte
📰 IT Security Nachrichten

🐧 A reminder of why you should always have a strong password and not have your ssh port on 22.


📈 16.96 Punkte
🐧 Linux Tipps

🔧 Teach Your LLM to Always Answer With Facts Not Fiction


📈 16.96 Punkte
🔧 Programmierung

🐧 How does wine work and how is it not always consistent in seemingly random ways?


📈 16.96 Punkte
🐧 Linux Tipps

🪟 Switching from Windows 11 to Linux or Mac always gets attention, so why not when it's in reverse?


📈 16.96 Punkte
🪟 Windows Tipps

📰 Why Not Always Multi-Factor Authentication?


📈 16.96 Punkte
📰 IT Security Nachrichten

🍏 10 Best Ways To Fix Always On Display Not Working on iPhone


📈 16.96 Punkte
🍏 iOS / Mac OS

🐧 It's Tux, not Pixar and Tux always behaves and looks like that.


📈 16.96 Punkte
🐧 Linux Tipps

🔧 Fix IT: One person always speaks in meetings but does not say anything that could interest you.


📈 16.96 Punkte
🔧 Programmierung

📰 Your Password Is… wait for it… NOT Always Encrypted


📈 16.96 Punkte
📰 IT Security Nachrichten

📰 Why Mean Time to Repair Is Not Always A Useful Security Metric


📈 16.96 Punkte
📰 IT Security Nachrichten

📰 For Apple users without latest security updates, the letter 'd' is not always the letter 'd'


📈 16.96 Punkte
📰 IT Security Nachrichten

🍏 Eufy Says Its Security Camera Footage Is Local-Only, But That’s Not Always True


📈 16.96 Punkte
🍏 iOS / Mac OS

matomo