It was my understanding that variables created with let
in Javascript cannot be global. I thought that meant that the variable only lived in that particular file.
However, when I make a simple/contrived example:
A.js:
let a = 5;
B.js:
console.log(a);
index.html:
<script type="text/javascript" src="A.js"></script>
<script type="text/javascript" src="B.js"></script>
it logs 5! Strangely though if I log window.a
, that logs as undefined
, so a global variable is not being created.
My question is, how is the variable getting shared between files without being a global variable?