4

On the browser the global object is window object, and in nodejs the global object is global object.

when I run this code using nodejs on the terminal i had this output

console.log(this === global) ===> this return false

And then using the interactive mode of nodejs

>this === global

true

But on the browser both console.log and this === window returns true

What is the difference?

Lundu Harianja
  • 443
  • 1
  • 9
  • 17

1 Answers1

2

I can give you a part of the answer :

In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node this is different. The top-level scope is not the global scope; var something inside a Node module will be local to that module.

https://nodejs.org/api/globals.html#globals_global

But i don't know why the top level scope is the global scope in interactive mode.

antoinestv
  • 3,286
  • 2
  • 23
  • 39