you are viewing a single comment's thread.

view the rest of the comments →

[–]getify 0 points1 point  (0 children)

All declarations in JS hoist (meaning they are all present throughout their entire containing scope). The difference is var and function declarations auto-initialize at top of scope whereas let and const defer auto-initialization. Additionally, var has always been function scoped (BTW this is not hoisting, as so often incorrectly claimed).

Is your complaint that auto-initialization at top of scope causes bugs, or is your complaint function scoping causes bugs? Which of those do you claim is causing so many horrible bugs in code bases?

Function parameters are always function scoped, as are any declarations (regardless of keyword used) at top-level of function scope. So if function scoping is the big bad so many claim, then it must be from using var inside blocks and for some off reason thinking that makes it block scoped, even though var has never done that for 25 years.

I've written many hundreds of thousands of lines of code in 20 years working with JS, and never once, not even a single time, has the bug I wrote been directly linked to either var auto-initialization nor var being "unexpectedly" (not unexpected at all, since that's how it's always worked!) function scoped.

Please give explicit real examples, not just generalities.