-3

Studying global vars in JS here, I set out to try it and to my surprise this:

var thisVar = "global var";

function showVarLet() {
  var thisVar = "local var";
  console.log("%s   %s", thisVar, window.thisVar);
}
showVarLet();

gives me:

local var
undefined

but the same in the browser console, gives me:

local var
global var

So, what´s with this window object?

EDIT:

I tried to check in the console what would happen if instead of window.thisVar I referenced this.thisVar, my assumption was that I would access the local variable but I keep accessing the global one, why so?

Adriano_Pinaffo
  • 1,429
  • 4
  • 23
  • 46
  • *"gives me: `local var undefined`"* Where? – adiga Feb 11 '19 at 19:55
  • 1
    In the snippet getting the correct output – ellipsis Feb 11 '19 at 19:55
  • well, in which runtime you execute above where you said `gives me` – john Smith Feb 11 '19 at 19:55
  • You have probably run it on a site like jsfiddle or plunker. It will give you `undefined` because they usually wrap the code in the `javascript` section in a function https://stackoverflow.com/questions/33080320/in-jsfiddle-why-is-this-global-variable-not-defined-on-window – adiga Feb 11 '19 at 20:01
  • I just wrote an html file, put the code inside ` – Adriano_Pinaffo Feb 11 '19 at 20:07
  • `var` is scoped to the nearest function. If you use it inside a function called `global`, it won't become global. A variable is a global variable if it's declared outside of any function. Which is what happens when you run it in the console – adiga Feb 11 '19 at 20:13
  • Possible duplicate of [window.variableName](https://stackoverflow.com/questions/11148997/window-variablename) and [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431) – adiga Feb 11 '19 at 20:17
  • @adiga, I´m posting a (apparent) issue I´m having, the one you posted is more general and wants to know the concept. – Adriano_Pinaffo Feb 11 '19 at 20:20
  • This https://stackoverflow.com/questions/11148997/window-variablename is not a duplicate of mine. Moderators don´t read and downvote :/ – Adriano_Pinaffo Feb 11 '19 at 20:25
  • What do you mean "*the one you posted is more general and wants to know the concept*"? The concepts are exactly what you need to learn here. `window.variableName ` duplicate has many useful explainers on *when* a variable gets attached to the window object. The other has multiple examples on scope of the `var` and global variables. Thoroughly go through them and then explain why it's not a duplicate. – adiga Feb 11 '19 at 20:30
  • @adina, none of the answers given there apply to my example. Jonas Wilms below probably figured out the issue I was having. The "possible duplicates" you posted are questions around the concept of global variables. You just found random stuff regarding global variables. – Adriano_Pinaffo Feb 11 '19 at 20:35

1 Answers1

0

and the code I showed is in a function called global()

then none of the two thisVars is global, one is a local variable of the global() function, the other one is a local variable of showVarLet(). You cannot access local variables via window..

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151