There is multiple ways to set the variable as a global in JavaScript, you can use var at global scope so that it will become a global variable.
<script>
var yourGlobalVariable;
function foo() {
// ...
}
</script>
Also, you can use globalThis, if you are using any modern environments:
<script>
function foo() {
globalThis.yourGlobalVariable = ...;
}
</script>
I hope this will help.
Want to know more about Java? Prefer this tutorial on Learn Java.
Want to become a Java expert? Join Java Course now!!