-1

I learned in one of the kyle simpson's javascript courses that if we declare a variable inside a Javascript function without any prefixing var keyword, then that variable is made available in global execution context, but when I try it in chrome developer tool or nodejs it throws ReferenceError:....

Has anything changed ?

Happens in both Firefox and Chrome on Windows and Linux.

function foo() {
  bar = "I am in global scope";
}
foo();
console.log(bar);


    

ReferenceError: bar is not defined

The other question is about undefined being appended to the output log,I do not have any mention of it in this question. Not sure why this is marked as duplicate. This question is about global execution context / scope

Thanks.

finepax007
  • 651
  • 3
  • 8
  • 16
  • 1
    I can't reproduce the problem — https://i.imgur.com/HkiUumO.png — but don't do this. Implicit globals are awful (and forbidden in strict mode, always use strict mode). – Quentin Jul 22 '19 at 09:11
  • 1
    The problem is reproducible in Chrome dev tools. But not really in the snippet. Anyway you shouldn't be doing this at all – Abana Clara Jul 22 '19 at 09:12
  • It should consoles both in client and server however it's bad practise anyways – BT101 Jul 22 '19 at 09:12
  • 1
    *"Has anything changed ?"* Not in loose mode, no, nor will it. JavaScript's steering committee, TC39, is quite rightly **extremely** conscious of backward-compatibility. In strict mode, that code fails because `bar` is undeclared as of where you assign to it. – T.J. Crowder Jul 22 '19 at 09:13
  • I understand that this is not a good practice, and we should have strict mode . But just wanted to validate what Kyle said in session. I am running this in terminal using node command and in chrome console – finepax007 Jul 22 '19 at 09:17
  • @finepax007 - That's odd. When I use `node` interactively, I don't get `undefined`. I've tried Node v12, v8, v6, and v4. I see `"I am in global scope"` from the `console.log`. (Of course, I **also** see `undefined` afterward, which is the result of calling `console.log`.) – T.J. Crowder Jul 22 '19 at 09:21
  • I was printing console.log(bar), instead I should have used console.log(window.bar); in Chrome console and console.log(global.bar) in node explicitly. I could see bar variable in these objects. – finepax007 Jul 22 '19 at 09:58
  • @finepax007 - No, there's no need to prefix `bar` with the global object. – T.J. Crowder Jul 22 '19 at 10:07

1 Answers1

3

Has anything changed ?

Not in loose mode, no, nor will it. JavaScript's steering committee, TC39, is quite rightly extremely conscious of backward-compatibility.

In strict mode, that code fails because bar is undeclared as of where you assign to it.

You've said that you're seeing this in Chrome's console and also in Node.js's REPL. I see undefined in both cases, but it's because those environments are showing the result of the call to console.log (which is undefined) after showing bar. I see "I am in global scope" first, then undefined (the result of calling console.log):

enter image description here

enter image description here

That saide, don't use the console for this sort of thing. :-) Consoles are a very special environment, particularly around scope, because of their interactive nature. If you want to know how something really works, and if it's even tangentially scope-related, it's best to replicate it in an actual file or script, not the console.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    That seems to suggest that Chrome devtools did change from sloppy mode to strict mode? It did work without an error in Chrome 75.0.3770.142. – Bergi Jul 22 '19 at 09:53
  • Correct, mine is Chrome 55.0.2883.87 – finepax007 Jul 22 '19 at 10:01
  • @Bergi - No, the console doesn't switch to strict mode as far as I'm aware. I thought the OP was seeing `undefined` and not seeing `"I am in global scope"` and my dodgy eyes made me miss it in the Chrome console as well. But I think they're just missing the `"I am in global scope"` output. – T.J. Crowder Jul 22 '19 at 10:08
  • @finepax007 - See above, and edited answer. – T.J. Crowder Jul 22 '19 at 10:08
  • @finepax007 - FWIW, I'd **strongly** recommend allowing Chrome to update itself to something more recent than v55. – T.J. Crowder Jul 22 '19 at 10:10
  • 1
    @ T.J. Crowder I wish I could :), My helpdesk ticket to upgrade chrome is still in pending state since last couple of months :) – finepax007 Jul 22 '19 at 10:28
  • @finepax007 - Yikes... :-) – T.J. Crowder Jul 22 '19 at 10:31