1

In D.Flanagan's 'Javascript the Definite Guide' there's an example:

var x = 1;
console.log(delete globalThis.x)

When I console log this code in node v14.15.1 then instead of false I get true. When console logging globalThis then x is not there. If I declare x without var or let then it becomes a part of global object. Funnily when I ran this code here on Stack Overflow then it behaved as Flanagan explained.

Can somebody please explain to me why global variables declared with var/let won't get a part of globalThis in my environment? Is this because the version of Node I'm using or something else?

  • try using `global` instead of `globalThis` in node.js – mrJQuery Aug 20 '21 at 08:46
  • 1
    [Using `let x` *never* creates a property on the global object](https://stackoverflow.com/q/28776079/1048572). – Bergi Aug 20 '21 at 10:06
  • Using `global` didn't work either @mrJQuery . It turns out that all the code in a node file is wrapped in a function making all variables you declare there local. – Verner Keel Aug 21 '21 at 07:28

0 Answers0