I have an HTML page that i ship to the browser and it contains a couple of script tag.
This is my index.html file:
<body> <div id="app"></div> <script src="src/index.js"></script> <script> window.test = true; </script> </body> </html> And this is my src/index.js file:
<script> const test = () => { if (window.test) { console.log("wow!"); } }; test(); </script> In the HTML file, i define window.test after i run the src/index.js. src/index.js will only print to the console if window.test is set to true, but that's being set only after, as the second script.
What is the order in which these scripts will run? Is there a guarantee that window.test will always be set to true "just in time" for the if statement in src/index.js to print wow!?
CodeSandbox: https://codesandbox.io/s/javascript-forked-utgh8?file=/index.html