-3
// At global scope

window.a = 42; and var a = 42; are same

Then why this runs perfectly

// At global scope
window.a = 42;    // Creates binding in the outer global environment
let a = "answer"; // Creates binding in the inner global environment
console.log(a);   // Shows "answer"

But it shows error

// At global scope
var a = 42;    
let a = "answer"; 

Also for second case var a would be assigned in global memory while let a would be assigned in script memory.

srrsre
  • 1
  • 1
  • 1
    https://stackoverflow.com/questions/28776079/do-let-statements-create-properties-on-the-global-object – epascarello Oct 07 '21 at 17:47
  • There’s a difference between variables and properties. See [var vs this in Javascript object](/q/4946625/4642212). Note that `var` is much more lax in general. – Sebastian Simon Oct 07 '21 at 17:48
  • because the second is a **redeclaration**, – Mister Jojo Oct 07 '21 at 17:49
  • @MisterJojo for second case `var a` would be assigned in global memory while `let a` would be assigned in script memory. – srrsre Oct 07 '21 at 17:51
  • @srrsre There is no “script memory” and “global memory”. But these terms may be helpful: [global Environment Record](//tc39.es/ecma262/#sec-global-environment-records) and [declarative Environment Record](//tc39.es/ecma262/#sec-declarative-environment-records), and the different semantics between `var` and `let` during [ScriptEvaluation](//tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation) and [function calls](//tc39.es/ecma262/#sec-function-calls-runtime-semantics-evaluation). – Sebastian Simon Oct 07 '21 at 17:56
  • so what ? this remains a redeclaration on the same level of scope – Mister Jojo Oct 07 '21 at 17:56
  • @MisterJojo yes at same level but at different memory. – srrsre Oct 07 '21 at 17:58
  • 1
    no there are no different memories, with the 'let' and 'const' syntax, the javascript language has become more strict against those who juggle with variable names and usages. – Mister Jojo Oct 07 '21 at 18:07
  • Then why in chrome it show two different scoped @MisterJojo – srrsre Oct 07 '21 at 18:09
  • Chrome use V8 (JS engine) and and mozilla another (I forgot the name JägerMonkey?) what they do in their kitchen is their own business, the only thing that matters is that they respect the specifications set out in ECMAscript – Mister Jojo Oct 07 '21 at 18:18

1 Answers1

1

window.a = 42; and var a = 42; are same

Not exactly, no. See What is the purpose of the var keyword and when should I use it (or omit it)?.

window.a is just a property access and doesn't follow any scoping rules.

var a and let a are both variable declarations, and let complains when you mistakenly declare the same variable twice. This is not different in the global scope than it is in a local scope. See why we can't re-declare variable with let in global situation, What's the difference between using "let" and "var"? and Redeclaring a variable using `let` results in “Uncaught SyntaxError: redeclaration of let …”.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375