0

On the browser I can use the window object. How to do this on node.js?

var name="test"; 
var <name> = 3; 
print(test); 
//output: 3

1 Answer 1

1

I can't do this in node in any local scope, but you can do this in global scope (like in browser window object):

var name = 'test';
global[name] = 3;
console.log(test); // 3
console.log(global['test']); // 3;

So, global object exactly the same as browser window object.

Difference is: in browser when you declaring var test = 2 in top-level scope you actually creating window['test'], but in node you don't, because every module scoped with function call by default.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.