Variable declared in global scope in Javascript undefined when run in WebStorm
// Javascript
var name = "Michael";
console.log(this.name);
console.log(name);
////////////////////
Result when run in WebStorm:
undefined
Michael
Result when run in terminal in Node:
Michael
Michael
Why are the two results different?
Please sign in to leave a comment.
to me, results are identical in WebStorm and Terminal:
C:\WebstormProjects\untitled1>node app.js
undefined
Michael
see https://stackoverflow.com/questions/35432325/node-js-what-is-the-context-of-the-this-operator-when-used-in-module-scope/35486261#35486261
I did node terminal console:
> var name = "Michael"
undefined
> console.log(this.name)
Michael
undefined
> console.log(name)
Michael
undefined
by node terminal console, do you mean Node
REPL
? Node'sREPL
is global, but wher running your script in usual terminal/WebStorm by passing file to node (node script.js),this === exports
Yes. by node terminal code, I mean Node REPL.
I see the difference now. Thank you!