Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js variable declaration and scope

When I type this in node.js, I get undefined.

var testContext = 15; function testFunction() {   console.log(this.testContext); } testFunction(); =>undefined 

Without var keyword, it passes (=>15). It's working in the Chrome console (with and without var keyword).

like image 528
IGRACH Avatar asked Nov 08 '13 01:11

IGRACH


People also ask

How do you declare a variable in node JS?

To define a global variable in NodeJS we need to use the global namespace object, global . It's important to be aware that if you do not declare a variable using one of the keywords var , let or const in your codebase then the variable is given a global scope.

What is scope in node JS?

In a browser, global scope is the window object. In Node. js, global object represents the global scope. To add something in global scope, you need to export it using export or module.

What is the scope of a variable declaration?

Scope is the context in which a variable, procedure, class, or type is declared. Scope affects the accessibility of an item's value outside that context. For example, variables declared within a procedure are typically not available outside of the scope of that procedure.

How do you assign a value to a variable in node JS?

var getValue = function getValue(){ global. value = 5; }; console. log(value);


2 Answers

It doesn't work in Node when using var because testContext is a local of the current module. You should reference it directly: console.log(testContext);.

When you don't type var, what happens is that testContext is now a global var in the entire Node process.

In Chrome (or any other browser - well, I'm unsure about oldIE...), it doesn't matter if you use var or not in your example, testContext will go to the global context, which is window.

By the way, the "global context" is the default this of function calls in JS.

like image 146
gustavohenke Avatar answered Sep 24 '22 05:09

gustavohenke


The key difference is that all modules (script files) in Node.js are executed in their own closure while Chrome and other browsers execute all script files directly within the global scope.

This is mentioned in the Globals documentation:

Some of these objects aren't actually in the global scope but in the module scope - this will be noted.

The vars you declare in a Node module will be isolated to one of these closures, which is why you have to export members for other modules to reach them.

Though, when calling a function without a specific context, it will normally be defaulted to the global object -- which is conveniently called global in Node.

function testFunction() {     return this; }  console.log(testFunction() === global); // true 

And, without the var to declare it, testContext will default to being defined as a global.

testContext = 15; console.log(global.testContext); // 15 
like image 21
Jonathan Lonowski Avatar answered Sep 23 '22 05:09

Jonathan Lonowski