-2

I'm trying to reproduce deceze's answer to Is using 'var' to declare variables optional? with the following (adapted) Node.js script:

var foo = "I'm global";
var bar = "So am I";

function myfunc() {
    var foo = "I'm local, the previous 'foo' didn't notice a thing";
    var baz = "I'm local, too";

    function innermyfunc() {
        var foo = "I'm even more local, all three 'foos' have different values";
        baz = "I just changed 'baz' one scope higher, but it's still not global";
        bar = "I just changed the global 'bar' variable";
        xyz = "I just created a new global variable";
    }
}

console.log(xyz)

However, this leads to a

console.log(xyz)
            ^

ReferenceError: xyz is not defined

However, as I understand from his answer this should be "I just created a new global variable", because it was defined without the var keyword and therefore 'bubbles up' until it reaches and attached to the global object.

Why is this not the case?

(I also have a second question: in the original answer the function names myfunc and innermyfunc were not there, but this leads to a SyntaxError. Is it not allowed to define anonymous functions in Node?)

Dmitry
  • 6,716
  • 14
  • 37
  • 39
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • 2
    Node.js doesn't use global scope in that way. – SLaks Nov 08 '17 at 20:49
  • https://nodejs.org/api/globals.html#globals_global – zero298 Nov 08 '17 at 20:49
  • 2
    In the code you posted, the functions aren't called. It's helpful with a simple sample like this for people to be able to copy/paste and reproduce your results. – Pointy Nov 08 '17 at 20:50
  • Implicit globals don't work if you try to use the variable before it's assigned a value. – JLRishe Nov 08 '17 at 20:54
  • You **can** have anonymous functions and use function expressions like `function(){ console.log('hello')` -- you just need to use them like `var Fn = function(){console.log("hello")` or `const Fn = () => console.log("hello")` – Mark Nov 08 '17 at 21:00

3 Answers3

2

The variable xyz won't be declared until you actually call the functions. Then xyz will be in the scope of the file (but not really global) and your console.log() should work as expected.

This, for example, will log the string to the console:

function myfunc() {

    function innermyfunc() {
        xyz = "I just created a new global variable";
    }
    innermyfunc()
}

myfunc()
console.log(xyz)

Having said that, it's a generally-accepted bad practice to declare variable like this. It's much better to use var xyz (or const or let) at the top of the file where you can see them easily and understand they should be scoped to the file. This will save you some bug-hunting time, I promise.

Mark
  • 90,562
  • 7
  • 108
  • 148
0

You need to run the functions

function func(){
    a = 5;
}
console.log(a); // a is not defined
func();
console.log(a); // 5
galchen
  • 5,252
  • 3
  • 29
  • 43
0

You have to run the functions for xyz to be declared. Also this will also throw an error if you are using strict mode.

B.K Lewis
  • 19
  • 1
  • 8