💾isCurrentlyInGlobalScope()

JSconceptsscopeglobal ⟩ isCurrentlyInGlobalScope()

check if the current context (this) is in the global scope.

// must use arrow function to get the enclosing context
const isCurrentlyInGlobalScope = () => this === globalThis;

  • in browsers

isCurrentlyInGlobalScope()    // ✅ true (top-level scope == global scope)
  • in Node.js

isCurrentlyInGlobalScope()    // ❌ false (top-level scope == module scope)❗️ 

Last updated