The easiest way is to enclose your whole code in a function, like this:
<script>
function stuff(){
// your code here ...
}
stuff()
// or with an anonymous function:
(()=>{
// your code here ...
})()
</script>
You can also avoid the error by using var
instead of const
or let
.