While loops in js have block-level scope

does the while loop have no block-level scope like the for loop

if I define a human variable in the while loop, will the variable be cleared before I continue to operate in the previously defined variable in the next loop

Dec.14,2021

1. We can think that expressions with {} are block-level scope, provided that with let const, variables defined by var do not have block-level scope.
2. Let's take a look at the comparison: var is used in
while to declare variables
:

clipboard.png

a


let:

clipboard.png

We found that using the let declaration, the value printed for the first time is undefined, indicating that the value of b in each loop is a new value ( block-level scope takes effect ), and an error is reported when printing the value of b outside the loop, indicating that the variable b is not available outside {}. indicates that the block-level scope takes effect again.

if you have any questions, please leave a message.

Menu