When inspecting scopes of a function in the DevTools console I noticed a \"script\" scope. After a bit of research it seems to be created for let
and const
When you declare a variable using var
on the top level (i.e. not inside a function), it automatically becomes a global variable (so in browser you can access it as a property of window
). It's different with variables declared using let
and const
—they don't become global variables. You can access them in another script tag, but you can't access them as properties of window
.
See this example:
<script>
var test1 = 42;
let test2 = 43;
</script>
<script>
console.log(test1); // 42
console.log(window.test1); // 42
console.log(test2); // 43
console.log(window.test2); // undefined
</script>