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?


0
4 comments

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

0

I did node terminal console:

> var name = "Michael"
undefined
> console.log(this.name)
Michael
undefined
> console.log(name)
Michael
undefined

0

by node terminal console, do you mean Node REPL? Node's REPL is global, but wher running your script in usual terminal/WebStorm by passing file to node (node script.js), this === exports

0

Yes. by node terminal code, I mean Node REPL.

I see the difference now. Thank you!

 

0

Please sign in to leave a comment.