0

This query is related to storage structure but not to understand the scope rule for let keyword.


Using var keyword, a is a property of window object(like a dictionary)

var a = 10;
console.log(window.a); // 10
console.log(window['a']) // 10

fis a property of window object

function f(){}

console.log(window['f']) // function object

Using let keyword, b is not a property of window dictionary

let b = 20;
console.log(window.b); // undefined

My understanding is, any name(function/var/..) introduced in a JavaScript code will be a property(member) of window dictionary(nested) object.

Edit:

enter image description here


Whose property is b?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • *My understanding is, any name(function/var/..) introduced in a JavaScript code will be a property(member) of window dictionary(nested) object.* And, that is the problem. That statement is not true. See my answer below. – Scott Marcus Sep 08 '17 at 21:55
  • @ScottMarcus Whose property is `b` in that screen shot? Query edited – overexchange Sep 08 '17 at 22:28
  • As answered below. `b` belongs to the Global scope. But, because of the use of `let` to declare `b`, it is scoped to a special hidden object that has Global scope. Your expectation that all Globals must become properties of `window` is incorrect and that is the source of your confusion. – Scott Marcus Sep 09 '17 at 20:40

1 Answers1

1

None of the code you've supplied relates to a "dictionary".

var vs. let determines the scope that the variable has. With var, the variable is scoped to its containing function (or the Global scope, if outside of all functions).

let gives the variable block level scope which can be more granular than function level scope (i.e. branches of an if/else statement).

If the declaration is in the Global scope, then let vs. var won't make any difference because the scope is Global.

Both declarations will create global variables, but let doesn't explicitly create a named property on the window object as will happen with var.

See this for details.

Also from MDN:

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

And, from the ECMAScript spec. itself:

let and const declarations define variables that are scoped to the running execution context’s LexicalEnvironment.

A var statement declares variables that are scoped to the running execution context’s VariableEnvironment.

This is why globally declared let variables are not accessible via window like globally declared var variables are.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • `window` is a dictionary datastructure – overexchange Sep 08 '17 at 21:17
  • @overexchange No, it's not. JavaScript has objects and they can be seen as similar to dictionaries, but a globally declared variable just becomes a property of the global object. `window` is an object. `var` or `let` in the global scope doesn't change this. – Scott Marcus Sep 08 '17 at 21:18
  • It is. `window` is an object, which stores its key-value pairs in dictionary data structure. `'a'` is one key of `window` object, in above code. – overexchange Sep 08 '17 at 21:19
  • @overexchange I'm not going to argue this with you. JavaScript does not explicitly have dictionaries. It has objects, with keys and values. But, the larger point is that this structure has no bearing on the use of `var` vs. `let`. You are equating a storage structure with scope, which is a false equivalency. Variables declared in the global scope are global and become properties of the global object, regardless of how you declared them. – Scott Marcus Sep 08 '17 at 21:21
  • As per above code, `f` & `a` are keys of `window` object, which is a dictionary data structure. Scoping rule does not map to storage structure. It is up to V8 engine. – overexchange Sep 08 '17 at 21:23
  • @overexchange For the sake of argument, let's say we agree. You are still missing my point and the answer to your question. `var` vs. `let` on a globally scoped variable makes no difference. `let` is for declaring "block" scope (more granular at a lower scope), not global. Also, it is not up to V8 on how to map properties or set scope. That is all laid out in the ECMAScript standard. – Scott Marcus Sep 08 '17 at 21:24
  • Before raising this query, I understand and agree that, `var` vs. `let` on a globally scoped variable makes no difference, which is scope rule aspect unlike my query – overexchange Sep 08 '17 at 21:27
  • @overexchange Then what, exactly is your question?! – Scott Marcus Sep 08 '17 at 21:27
  • Copy-paste -> Whose property is b? Did you read this question – overexchange Sep 08 '17 at 21:28
  • `b` is still a property of `window`. It's just not explicitly attached to it as it is with `var`. This boils down to implicit globals vs. explicit globals. – Scott Marcus Sep 08 '17 at 21:30
  • @overexchange See my updated answer and provided link for details. – Scott Marcus Sep 08 '17 at 21:36
  • It does not boil down to explicit global(`var g = 2;`) outside functions or implicit global(`g=2;`), inside any function. First line of your answer is wrong. – overexchange Sep 08 '17 at 21:36
  • 1
    @overexchange No, that's not what I said. Please don't be so argumentative and you just may learn something. – Scott Marcus Sep 08 '17 at 21:37
  • My question has nothing to do with scope rules. You talk about scope rules using `let` keyword, in your answer. – overexchange Sep 08 '17 at 21:38
  • @overexchange I have answered your question and supplied a link for further reading/understanding. You can argue all you want or you can read what I've written and possibly learn something. Shouting at the moon won't change it. – Scott Marcus Sep 08 '17 at 21:40
  • @overexchange Note that the word "scoped" is used in the definitions of each. This is absolutely an issue of scope. – Scott Marcus Sep 08 '17 at 21:53
  • 1
    @overexchange you are coming across as pretty rude. Even if you disagree with Scott or think he is wrong, it would still be good manners to interact nicely with someone that has taken the time to reply to your question – Dave Pile Sep 08 '17 at 21:59
  • @DavePile I used phrases like *No, it's not.* or *answer is wrong* unlike the other user using phrases like *Shouting at the moon*. Can you point any word in my conversation, that looks rude? – overexchange Sep 08 '17 at 22:30
  • 1
    @overexchange: A global environment consists of two parts: 1) A *declarative* environment record, which is basically an internal data structure that is not accessible from user land code. That's where `const`, `let`, `class` and others live. It's the same kind of environment used in functions. 2) An *object* environment record which, as the name implies, stores all bindings as properties of an object. That object is accessible in user land code (it's the global object). You can learn more about this in the spec: https://ecma-international.org/ecma-262/8.0/#sec-global-environment-records – Felix Kling Sep 08 '17 at 22:43
  • @overexchange What DavePile is trying to let you know is that from the first exchange with me, you've been argumentative and insisting that your understand of JavaScript is right and that my explanations were wrong. That is not the attitude to take when posting a question seeking help and that is why I responded (after many futile attempts to correct your understanding) that you were, in essence "shouting at the moon" because no matter how much you object, it won't change the facts. Your question makes several false assumptions that you continue to assert as facts. – Scott Marcus Sep 09 '17 at 20:44
  • @overexchange In fact, this question has everything to do with scope. – Scott Marcus Sep 09 '17 at 20:45
  • @overexchange Still defiant to the end. What Felix said was exactly what I quoted from the spec. The *"declarative"* environment he speaks of is the *"LexicalEnvironment"* that I quoted from the spec. and the *"object"* environment he speaks of is the *"VariableEnvironment"* I quoted from the spec. Oh, and "You're welcome. Good luck." – Scott Marcus Sep 09 '17 at 21:02
  • Yes I know that, but his explanation is much better, to get completely clarified – overexchange Sep 09 '17 at 21:06