An example of my question can be duplicated with the following three files...
index.htm:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
</head>
<body>
Hello, world!
</body>
</html>
script1.js:
$(function() {
const THE_TEXT = 'This is THE_TEXT';
});
script2.js:
$(function() {
function log_THE_TEXT() {
console.log(THE_TEXT);
}
log_THE_TEXT();
});
I expect to see "This is THE_TEXT" in the console log after loading the page. Instead there's this error:
ReferenceError: THE_TEXT is not defined script2.js:6:3
and:
jQuery.Deferred exception: THE_TEXT is not defined log_THE_TEXT@http://example.com/js-test/script2.js:6:3
If I define the constant in script2.js, it works as expected. So why doesn't it work, and is there a way to make it?