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?


4 comments

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.