Consider following code snippet:
<script>
let a = 1;
console.log(a); //prints 1
</script>
<script>
console.log(a); //prints 1
</script>
I want to ask if a is block scoped due to the declaration by let which means it is scoped to single <script> tag the why a in second script tag is printing value 1?
If you want to restrict the scope of that a variable to the first <script> element, then wrap the content in a block statement:
<script>
{
let a=1;
console.log(a); //prints 1
}
</script>
<script>
console.log(a); //Uncaught ReferenceError: a is not defined
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With