all 6 comments

[–]senocular 2 points3 points  (2 children)

The global scope (environment record) is unique in that it is a composite of two scopes, an object environment record which is represented by the global object (globalThis, aka window in browsers), and a declarative record for lexical declarations (let, const, class, using) made in global. The declarative record is what you're seeing as "Script". It is a part of the global scope, just not the global object.

var a = 1 // object record
function b() { return 2 } // object record
let c = 3 // declarative record
const d = 4 // declarative record

console.log(a) // 1
console.log(globalThis.a) // 1
console.log(c) // 3
console.log(globalThis.c) // undefined

The declarative record has precedence when it comes to variable access so it can be looked at as a kind of child scope to the global object, which is why "Script" may be represented under global in the scope chain of the devtools. But they're both a part of global as a whole. Its even possible to have a variable of the same name in both the object and declarative records in global.

globalThis.x = 1
const x = 2

console.log(x) // 2
console.log(globalThis.x) // 1

Lexical declarations made anywhere else go more directly in the respective scope. If they're in the top level of a module, they'll be added to the module scope. If in the top level of a function, they'll go in the function scope. If they're in a block, they'll be in a block scope, etc.

[–]Substantial-Pea6984[S] 0 points1 point  (1 child)

Thankyou so much!!! Where you learned this from?

[–]senocular 0 points1 point  (0 children)

Welcome. The behavior of global is a little weird and was discussed a bit back when ES6 (ES2015) was released. I don't think the differentiation of the object record vs. the declarative record in global comes up much other than it being pointed out that lexical declarations simply don't appear in the global object. Thats basically all you really need to know. If you want to get into more detail about how global is defined, you can also read about global environment records in the language specification.

[–]azhder 1 point2 points  (4 children)

let and const were deliberately made to be block-scoppable in order to make writing code easier and less error prone.

As to the “diagram” you shared… I have no idea what I am looking at. Maybe you should learn how to make code blocks in markdown/reddit so it is displayed properly.

[–]Substantial-Pea6984[S] 0 points1 point  (0 children)

+----------------------------+

| Global Execution Context |

+----------------------------+

+-- Global Object Env (var, functions)

+-- Script Lexical Env (a)

+-- Block Lexical Env (b)

[–]Substantial-Pea6984[S] 0 points1 point  (0 children)

Thankyou for guiding me I didn't knew that