Is this intended JS behavior?
Came across this problem when using switch statements. Doesnt seem like each case has its own scope, however, you cannot access a variable outside of the case it was defined in (which i wasnt expecting to be able to do anyway, but if i did it would at least make sense) despite not being able to share variable names from case to case.
Take the following example
This also errors out
I understand that a let variable can just be made outside of the switch statement and then be mutated across each case but i just found this behavior very interesting
4 Replies
I’d use objects instead of switch tho
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Yup. If we're following regular JS scoping rules it makes sense to me why i would get a reference error there. That second example was just delving deeper into this
(which i wasnt expecting to be able to do anyway, but if i did it would at least make sense)I guess what my post boils down to ultimately is why i wouldnt be able to define variables under the same name across different cases
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View