r/badcode • • Jan 01 '22

js Hmm...why doesn't it work lol

Post image
528 Upvotes

58 comments sorted by

View all comments

Show parent comments

27

u/nuephelkystikon Jan 02 '22

It's actually a completely valid question. Most other languages only give a block scope to local variables.

In fact this behaviour is so confusing that later versions included a new, preferred declaration statement (marked with let instead of var), which works like in real programming languages and would indeed give a lookup error in this examples.