0

According to MDN, the this keyword behaves like the following.

In the global execution context (outside of any function), this refers to the global object, whether in strict mode or not.

I test it by running these codes:

'use strict'; 
console.log(this === global);

The result is confusing. When I put those codes into a file and run it by doing node test.js, it returns false. But when I run it in repl.it, it returns true.

I expect that both ways should return true. Can anyone explain why?

Junbang Huang
  • 1,927
  • 19
  • 26

1 Answers1

1

Files in node are modules and are in their own context to avoid polluting the global namespace.

In the repl you're in its own context, which happens to be the global context.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302